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

70 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-02-04 11:25:21 +00:00
import { For, Show, createEffect, createSignal } 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'
import styles from './DraftsView.module.scss'
2023-05-08 17:21:06 +00:00
export const DraftsView = () => {
const { isAuthenticated, isSessionLoaded } = useSession()
2023-05-08 17:21:06 +00:00
const [drafts, setDrafts] = createSignal<Shout[]>([])
const loadDrafts = async () => {
2024-01-23 13:42:05 +00:00
if (apiClient.private) {
const loadedDrafts = await apiClient.getDrafts()
2024-02-29 13:08:35 +00:00
setDrafts(loadedDrafts.reverse() || [])
2024-01-23 13:42:05 +00:00
}
2023-05-08 17:21:06 +00:00
}
2024-02-02 21:52:04 +00:00
createEffect(() => {
if (isSessionLoaded()) loadDrafts()
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)}>
<Show when={isSessionLoaded()}>
<div class="wide-container">
<div class="row">
<div class="col-md-19 col-lg-18 col-xl-16 offset-md-5">
<Show when={isAuthenticated()} fallback="Давайте авторизуемся">
<For each={drafts()}>
{(draft) => (
<Draft
class={styles.draft}
shout={draft}
onDelete={handleDraftDelete}
onPublish={handleDraftPublish}
/>
)}
</For>
</Show>
</div>
</div>
</div>
</Show>
</div>
)
}