2023-11-28 13:18:25 +00:00
|
|
|
import type { Shout } from '../../graphql/schema/core.gen'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
import { getPagePath } from '@nanostores/router'
|
|
|
|
import { clsx } from 'clsx'
|
|
|
|
|
2023-07-31 21:24:41 +00:00
|
|
|
import { useConfirm } from '../../context/confirm'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { useLocalize } from '../../context/localize'
|
2023-07-31 21:24:41 +00:00
|
|
|
import { useSnackbar } from '../../context/snackbar'
|
2023-05-08 17:21:06 +00:00
|
|
|
import { router } from '../../stores/router'
|
2023-11-14 15:10:00 +00:00
|
|
|
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) => {
|
2023-10-18 10:56:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-31 21:24:41 +00:00
|
|
|
const handleDeleteLinkClick = async (e) => {
|
2023-05-08 18:01:11 +00:00
|
|
|
e.preventDefault()
|
2023-07-31 21:24:41 +00:00
|
|
|
|
2023-10-16 15:57:29 +00:00
|
|
|
const isConfirmed = await showConfirm({
|
|
|
|
confirmBody: t('Are you sure you want to delete this draft?'),
|
|
|
|
confirmButtonLabel: t('Delete'),
|
|
|
|
confirmButtonVariant: 'danger',
|
2023-11-14 15:10:00 +00:00
|
|
|
declineButtonVariant: 'primary',
|
2023-10-16 15:57:29 +00:00
|
|
|
})
|
2023-07-31 21:24:41 +00:00
|
|
|
if (isConfirmed) {
|
|
|
|
props.onDelete(props.shout)
|
|
|
|
|
2023-10-16 15:57:29 +00:00
|
|
|
await showSnackbar({ body: t('Draft successfully deleted') })
|
2023-07-31 21:24:41 +00:00
|
|
|
}
|
2023-05-08 17:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={clsx(props.class)}>
|
|
|
|
<div class={styles.created}>
|
2023-10-18 10:56:41 +00:00
|
|
|
<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}>
|
2023-08-04 15:59:36 +00:00
|
|
|
<a
|
|
|
|
class={styles.actionItem}
|
|
|
|
href={getPagePath(router, 'edit', { shoutId: props.shout.id.toString() })}
|
|
|
|
>
|
|
|
|
{t('Edit')}
|
2023-05-08 17:21:06 +00:00
|
|
|
</a>
|
2023-08-04 15:59:36 +00:00
|
|
|
<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')}
|
2023-08-04 15:59:36 +00:00
|
|
|
</span>
|
2023-05-08 17:21:06 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|