webapp/src/components/Draft/Draft.tsx

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-10-02 18:29:32 +00:00
import { A } from '@solidjs/router'
import { clsx } from 'clsx'
import { useLocalize } from '~/context/localize'
2024-06-24 17:50:27 +00:00
import { useSnackbar, useUI } from '~/context/ui'
import type { Shout } from '~/graphql/schema/core.gen'
import { Icon } from '../_shared/Icon'
import styles from './Draft.module.scss'
2023-05-08 17:21:06 +00:00
type Props = {
shout: Shout
onPublish: (shout: Shout) => void
onDelete: (shout: Shout) => void
}
export const Draft = (props: Props) => {
const { t, formatDate } = useLocalize()
2024-06-24 17:50:27 +00:00
const { showConfirm } = useUI()
2024-02-04 17:40:15 +00:00
const { showSnackbar } = useSnackbar()
2023-05-08 17:21:06 +00:00
2024-06-24 17:50:27 +00:00
const handlePublishLinkClick = (e: MouseEvent) => {
2023-05-08 18:01:11 +00:00
e.preventDefault()
2024-02-05 19:05:49 +00:00
if (props.shout.main_topic) {
props.onPublish(props.shout)
} else {
showSnackbar({ body: t('Please, set the main topic first') })
}
2023-05-08 17:21:06 +00:00
}
2024-06-24 17:50:27 +00:00
const handleDeleteLinkClick = async (e: MouseEvent) => {
2023-05-08 18:01:11 +00:00
e.preventDefault()
const isConfirmed = await showConfirm({
confirmBody: t('Are you sure you want to delete this draft?'),
confirmButtonLabel: t('Delete'),
confirmButtonVariant: 'danger',
2024-06-26 08:22:05 +00:00
declineButtonVariant: 'primary'
})
if (isConfirmed) {
props.onDelete(props.shout)
await showSnackbar({ body: t('Draft successfully deleted') })
}
2023-05-08 17:21:06 +00:00
}
return (
2024-10-02 18:29:32 +00:00
<div class={styles.draft}>
2023-05-08 17:21:06 +00:00
<div class={styles.created}>
<Icon name="pencil-outline" class={styles.icon} />{' '}
2023-11-28 13:18:25 +00:00
{formatDate(new Date(props.shout.created_at * 1000), { hour: '2-digit', minute: '2-digit' })}
2023-05-08 17:21:06 +00:00
</div>
<div class={styles.titleContainer}>
<span class={styles.title}>{props.shout.title || t('Unnamed draft')}</span> {props.shout.subtitle}
</div>
<div class={styles.actions}>
2024-06-24 17:50:27 +00:00
<A class={styles.actionItem} href={`edit/${props.shout?.id.toString()}`}>
{t('Edit')}
2024-06-24 17:50:27 +00:00
</A>
<span onClick={handlePublishLinkClick} class={clsx(styles.actionItem, styles.publish)}>
{t('Publish')}
</span>
<span onClick={handleDeleteLinkClick} class={clsx(styles.actionItem, styles.delete)}>
2023-05-08 17:21:06 +00:00
{t('Delete')}
</span>
2023-05-08 17:21:06 +00:00
</div>
</div>
)
}