webapp/src/components/Views/DraftsView/DraftsView.tsx

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-05-08 17:21:06 +00:00
import { clsx } from 'clsx'
2024-06-24 17:50:27 +00:00
import { For, Show, createEffect, createMemo, createSignal, on } from 'solid-js'
2024-06-24 17:50:27 +00:00
import { useNavigate } from '@solidjs/router'
import { useGraphQL } from '~/context/graphql'
import getDraftsQuery from '~/graphql/query/core/articles-load-drafts'
import { useEditorContext } from '../../../context/editor'
2023-05-08 17:21:06 +00:00
import { useSession } from '../../../context/session'
2023-11-28 13:18:25 +00:00
import { Shout } from '../../../graphql/schema/core.gen'
import { Draft } from '../../Draft'
2024-05-01 14:33:37 +00:00
import { Loading } from '../../_shared/Loading'
import styles from './DraftsView.module.scss'
2023-05-08 17:21:06 +00:00
export const DraftsView = () => {
2024-06-24 17:50:27 +00:00
const { session } = useSession()
const authorized = createMemo<boolean>(() => Boolean(session()?.access_token))
const navigate = useNavigate()
2023-05-08 17:21:06 +00:00
const [drafts, setDrafts] = createSignal<Shout[]>([])
2024-05-24 14:59:15 +00:00
const [loading, setLoading] = createSignal(false)
2024-06-24 17:50:27 +00:00
const { query } = useGraphQL()
2023-05-08 17:21:06 +00:00
2024-05-01 14:33:37 +00:00
createEffect(
on(
2024-06-24 17:50:27 +00:00
() => Boolean(session()?.access_token),
async (s) => {
if (s) {
2024-05-24 14:59:15 +00:00
setLoading(true)
2024-06-24 17:50:27 +00:00
const resp = await query(getDraftsQuery, {}).toPromise()
const result = resp?.data?.get_shouts_drafts
if (result) {
const { error, drafts: loadedDrafts } = result
if (error) console.warn(error)
if (loadedDrafts) setDrafts(loadedDrafts)
2024-05-06 18:16:13 +00:00
}
2024-05-24 14:59:15 +00:00
setLoading(false)
2024-05-01 14:33:37 +00:00
}
},
2024-05-24 14:59:15 +00:00
{ defer: true },
2024-05-01 14:33:37 +00:00
),
)
2023-05-08 17:21:06 +00:00
2024-02-04 17:40:15 +00:00
const { publishShoutById, deleteShout } = useEditorContext()
2023-05-08 17:21:06 +00:00
2024-01-23 13:42:05 +00:00
const handleDraftDelete = async (shout: Shout) => {
2024-06-24 17:50:27 +00:00
const success = await deleteShout(shout.id)
if (success) {
2024-02-02 21:52:04 +00:00
setDrafts((ddd) => ddd.filter((d) => d.id !== shout.id))
}
2023-05-08 17:21:06 +00:00
}
const handleDraftPublish = (shout: Shout) => {
2024-06-24 17:50:27 +00:00
publishShoutById(shout.id)
setTimeout(() => navigate('/feed'), 2000)
2023-05-08 17:21:06 +00:00
}
return (
<div class={clsx(styles.DraftsView)}>
2024-06-24 17:50:27 +00:00
<Show when={!loading() && authorized()} fallback={<Loading />}>
2023-05-08 17:21:06 +00:00
<div class="wide-container">
<div class="row">
<div class="col-md-19 col-lg-18 col-xl-16 offset-md-5">
2024-05-01 14:33:37 +00:00
<For each={drafts()}>
{(draft) => (
<Draft
class={styles.draft}
shout={draft}
onDelete={handleDraftDelete}
onPublish={handleDraftPublish}
/>
)}
</For>
2023-05-08 17:21:06 +00:00
</div>
</div>
</div>
</Show>
</div>
)
}