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

74 lines
2.0 KiB
TypeScript
Raw Normal View History

import { openPage } from '@nanostores/router'
2023-05-08 17:21:06 +00:00
import { clsx } from 'clsx'
2024-05-01 14:33:37 +00:00
import { For, Show, createEffect, createSignal, on } from 'solid-js'
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 { apiClient } from '../../../graphql/client/core'
import { Shout } from '../../../graphql/schema/core.gen'
2023-05-08 17:21:06 +00:00
import { router } from '../../../stores/router'
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-05-06 18:16:13 +00:00
const { author, loadSession } = useSession()
2023-05-08 17:21:06 +00:00
const [drafts, setDrafts] = createSignal<Shout[]>([])
2024-05-01 14:33:37 +00:00
createEffect(
on(
2024-05-06 18:16:13 +00:00
() => author(),
async (a) => {
if (a) {
const { shouts: loadedDrafts, error } = await apiClient.getDrafts()
if (error) {
console.warn(error)
await loadSession()
}
setDrafts(loadedDrafts || [])
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) => {
2023-05-08 17:21:06 +00:00
const result = deleteShout(shout.id)
2024-02-02 21:52:04 +00:00
if (result) {
setDrafts((ddd) => ddd.filter((d) => d.id !== shout.id))
}
2023-05-08 17:21:06 +00:00
}
const handleDraftPublish = (shout: Shout) => {
const result = publishShoutById(shout.id)
if (result) {
openPage(router, 'feed')
}
}
return (
<div class={clsx(styles.DraftsView)}>
2024-05-06 18:16:13 +00:00
<Show when={author()?.id} 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>
)
}