webapp/src/components/Draft/Draft.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Shout } from '../../graphql/schema/core.gen'
import { getPagePath } from '@nanostores/router'
import { clsx } from 'clsx'
import { useConfirm } from '../../context/confirm'
import { useLocalize } from '../../context/localize'
import { useSnackbar } from '../../context/snackbar'
2023-05-08 17:21:06 +00:00
import { router } from '../../stores/router'
import { Icon } from '../_shared/Icon'
import styles from './Draft.module.scss'
2023-05-08 17:21:06 +00:00
type Props = {
class?: string
shout: Shout
onPublish: (shout: Shout) => void
onDelete: (shout: Shout) => void
}
export const Draft = (props: Props) => {
const { t, formatDate } = useLocalize()
2024-02-04 17:40:15 +00:00
const { showConfirm } = useConfirm()
const { showSnackbar } = useSnackbar()
2023-05-08 17:21:06 +00:00
2023-05-08 18:01:11 +00:00
const handlePublishLinkClick = (e) => {
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
}
const handleDeleteLinkClick = async (e) => {
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',
declineButtonVariant: 'primary',
})
if (isConfirmed) {
props.onDelete(props.shout)
await showSnackbar({ body: t('Draft successfully deleted') })
}
2023-05-08 17:21:06 +00:00
}
return (
<div class={clsx(props.class)}>
<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}>
<a
class={styles.actionItem}
href={getPagePath(router, 'edit', { shoutId: props.shout.id.toString() })}
>
{t('Edit')}
2023-05-08 17:21:06 +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>
)
}