webapp/src/pages/edit.page.tsx

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-05-01 14:33:37 +00:00
import { Show, createEffect, createMemo, createSignal, lazy, on, onMount } from 'solid-js'
2024-02-04 11:25:21 +00:00
import { AuthGuard } from '../components/AuthGuard'
2023-04-11 13:57:48 +00:00
import { Loading } from '../components/_shared/Loading'
import { PageLayout } from '../components/_shared/PageLayout'
import { useLocalize } from '../context/localize'
2024-05-01 14:33:37 +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'
2024-03-05 13:01:47 +00:00
import { router } from '../stores/router'
2024-03-05 13:07:14 +00:00
import { redirectPage } from '@nanostores/router'
import { useSnackbar } from '../context/snackbar'
import { LayoutType } from './types'
2023-04-11 13:57:48 +00:00
2024-02-13 13:09:44 +00:00
const EditView = lazy(() => import('../components/Views/EditView/EditView'))
2023-04-11 13:57:48 +00:00
2024-05-01 14:33:37 +00:00
const getContentTypeTitle = (layout: LayoutType) => {
switch (layout) {
case 'audio':
return 'Publish Album'
case 'image':
return 'Create gallery'
case 'video':
return 'Create video'
case 'literature':
return 'New literary work'
default:
return 'Write an article'
}
}
2023-04-11 13:57:48 +00:00
export const EditPage = () => {
const { t } = useLocalize()
2024-05-01 14:33:37 +00:00
const { session } = useSession()
const snackbar = useSnackbar()
2023-04-11 13:57:48 +00:00
2024-05-01 14:33:37 +00:00
const fail = async (error: string) => {
console.error(error)
await snackbar?.showSnackbar({ type: 'error', body: t(error) })
redirectPage(router, 'drafts')
2024-03-07 10:58:12 +00:00
}
2024-05-01 14:33:37 +00:00
const [shoutId, setShoutId] = createSignal<number>(0)
const [shout, setShout] = createSignal<Shout>()
onMount(() => {
const shoutId = window.location.pathname.split('/').pop()
const shoutIdFromUrl = Number.parseInt(shoutId ?? '0', 10)
if (shoutIdFromUrl) setShoutId(shoutIdFromUrl)
2024-03-07 10:58:12 +00:00
})
2023-04-11 13:57:48 +00:00
2024-05-01 14:33:37 +00:00
createEffect(
on(
[session, shout, shoutId],
async ([ses, sh, shid]) => {
if (ses?.user && !sh && shid) {
const { shout: loadedShout, error } = await apiClient.getMyShout(shid)
if (error) {
fail(error)
} else {
setShout(loadedShout)
}
}
},
{ defer: true },
),
)
const title = createMemo(() => {
if (!shout()) {
return t('Create post')
}
2024-05-01 14:33:37 +00:00
return t(getContentTypeTitle(shout()?.layout as LayoutType))
})
2023-04-11 13:57:48 +00:00
return (
<PageLayout title={title()}>
<AuthGuard>
2024-05-01 14:33:37 +00:00
<Show when={shout()} fallback={<Loading />}>
<EditView shout={shout() as Shout} />
2023-04-11 13:57:48 +00:00
</Show>
</AuthGuard>
2023-04-11 13:57:48 +00:00
</PageLayout>
)
}
export const Page = EditPage