dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

commit 2e2e48617364f07ae8d37c3e9d98023d2301b840
parent b3b2aa5e630858671c6b53abbb5957dff68fd43c
Author: Yongbin Kim <iam@yongbin.kim>
Date:   Sun, 29 Jan 2023 14:27:15 +0900

feat: 존재하지 않는 새 페이지를 편집하고 저장할 수 있도록 수정

Signed-off-by: Yongbin Kim <iam@yongbin.kim>

Diffstat:
Mcomponents/wiki/WikiBase.tsx | 2+-
Mpages/edit/[slug]/[...path].tsx | 55+++++++++++++++++++++++++++----------------------------
2 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/components/wiki/WikiBase.tsx b/components/wiki/WikiBase.tsx @@ -7,7 +7,7 @@ import { ReactNode } from 'react' export interface WikiBaseProps { pageKind: WikiToolbarProps['pageKind'] - title: string + title: ReactNode children: ReactNode } diff --git a/pages/edit/[slug]/[...path].tsx b/pages/edit/[slug]/[...path].tsx @@ -17,8 +17,6 @@ import { useRouter } from 'next/router' import { useEffect } from 'react' export interface WikiEditPageProps { - slug: string - path: string wiki: WikiInfo page?: WikiRawPage } @@ -31,40 +29,32 @@ export const getServerSideProps: GetServerSideProps<WikiEditPageProps> = async ( const token = authenticationFromCookies(context.req.cookies) - const result = await withConnection(async (conn) => { + return await withConnection(async (conn) => { const wiki = await getWikiViaSlug(conn, [slug]) - if (wiki == null) { - return null - } - - if (!resolveACL(token, wiki.acl, ACL_ACTION_WRITE)) { - return null + if (wiki == null || !resolveACL(token, wiki.acl, ACL_ACTION_WRITE)) { + return { notFound: true } } const page = await getWikiRawPage(conn, [wiki.id, path]) if (page == null) { - return null + return { + props: { + wiki: wiki, + }, + } } if (!resolveACL(token, page.acl, ACL_ACTION_WRITE)) { - return null + return { notFound: true } } - return { wiki, page } + return { + props: { + wiki: wiki, + page: page, + }, + } }) - - if (result == null) { - return { notFound: true } - } - - return { - props: { - slug: slug, - path: path, - wiki: result.wiki, - page: result.page, - }, - } } interface WikiEditFormFields { @@ -75,6 +65,8 @@ export default function WikiEditPage (props: WikiEditPageProps) { const router = useRouter() const socket = useSocket() + const [slug, path] = getSlugAndPath(router) + // 다른 사용자가 문서를 편집했을 때 // useEffect(() => { @@ -84,16 +76,23 @@ export default function WikiEditPage (props: WikiEditPageProps) { }, []) const [fields, updateFields, submit, isLoading, result, error] = useForm<WikiEditFormFields>( - { method: 'PUT', url: `/api/wiki/${props.slug}/${props.path}` }, + { method: 'PUT', url: `/api/wiki/${slug}/${path}` }, { content: props.page?.content ?? '' }, () => { - router.push(`/wiki/${props.slug}/${props.path}`) + router.push(`/wiki/${slug}/${path}`) .catch(console.error) } ) return ( - <WikiBase title={`편집: ${props.path}`} pageKind="edit"> + <WikiBase title={( + <> + <span>편집: {path}</span> + {props.page == null && ( + <small> (새 문서 만들기)</small> + )} + </> + )} pageKind="edit"> <Section> <WikiEditor value={fields.content}