2023-11-14 15:10:00 +00:00
|
|
|
import { openPage } from '@nanostores/router'
|
2023-05-08 17:21:06 +00:00
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import { createSignal, For, onMount, Show } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
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'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { Draft } from '../../Draft'
|
|
|
|
|
|
|
|
import styles from './DraftsView.module.scss'
|
2023-05-08 17:21:06 +00:00
|
|
|
|
|
|
|
export const DraftsView = () => {
|
2023-05-09 23:15:26 +00:00
|
|
|
const { isAuthenticated, isSessionLoaded } = useSession()
|
2023-05-08 17:21:06 +00:00
|
|
|
|
|
|
|
const [drafts, setDrafts] = createSignal<Shout[]>([])
|
|
|
|
|
|
|
|
const loadDrafts = async () => {
|
|
|
|
const loadedDrafts = await apiClient.getDrafts()
|
2023-05-10 01:46:39 +00:00
|
|
|
setDrafts(loadedDrafts.reverse())
|
2023-05-08 17:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
loadDrafts()
|
|
|
|
})
|
|
|
|
|
|
|
|
const {
|
2023-11-14 15:10:00 +00:00
|
|
|
actions: { publishShoutById, deleteShout },
|
2023-05-08 17:21:06 +00:00
|
|
|
} = useEditorContext()
|
|
|
|
|
|
|
|
const handleDraftDelete = (shout: Shout) => {
|
|
|
|
const result = deleteShout(shout.id)
|
|
|
|
if (result) {
|
|
|
|
loadDrafts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|