-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
related to: #169
- Loading branch information
Showing
7 changed files
with
159 additions
and
47 deletions.
There are no files selected for viewing
47 changes: 21 additions & 26 deletions
47
.../boards/[boardId]/create-post/_components/CreatePostForm/PostContentFieldEditor/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,37 @@ | ||
'use client' | ||
|
||
import { useEffect, useMemo, useState } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
|
||
import { Block, BlockNoteEditor, PartialBlock } from '@blocknote/core' | ||
import { BlockNoteView } from '@blocknote/mantine' | ||
import '@blocknote/mantine/style.css' | ||
import { useCreateBlockNote } from '@blocknote/react' | ||
|
||
import { usePostEditorStore } from '~create-post/_store/post-editor' | ||
import { FormField, FormItem, FormMessage } from '@/components/ui/form' | ||
import { CreatePost } from '@/schema/post' | ||
|
||
export const PostContentFieldEditor = () => { | ||
const { setPostContent, getPostContent } = usePostEditorStore() | ||
const [initialContent, setInitialContent] = useState< | ||
'loading' | PartialBlock[] | undefined | ||
>('loading') | ||
const { control } = useFormContext<CreatePost>() | ||
|
||
useEffect(() => { | ||
const storedBlocks = getPostContent() | ||
setInitialContent(storedBlocks) | ||
}, [getPostContent]) | ||
|
||
const editor = useMemo(() => { | ||
if (initialContent === 'loading') { | ||
return undefined | ||
} | ||
return BlockNoteEditor.create({ initialContent }) | ||
}, [initialContent]) | ||
|
||
if (editor === undefined) { | ||
return <div>글 불러오는 중...</div> | ||
} | ||
const editor = useCreateBlockNote({ initialContent: [{}] }) | ||
|
||
return ( | ||
<BlockNoteView | ||
editor={editor} | ||
onChange={() => { | ||
setPostContent(editor.document as Block[]) | ||
}} | ||
className="h-96 overflow-auto rounded-md border pt-4" | ||
<FormField | ||
control={control} | ||
name="postContent" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<BlockNoteView | ||
editor={editor} | ||
onChange={() => field.onChange(JSON.stringify(editor.document))} | ||
className="h-96 overflow-auto rounded-md border pt-4" | ||
/> | ||
<div className="flex justify-end"> | ||
<FormMessage /> | ||
</div> | ||
</FormItem> | ||
)} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/(main)/activity/_components/ActivityImageInput/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { kstFormat } from '@toss/date' | ||
import { AxiosError } from 'axios' | ||
import { z } from 'zod' | ||
|
||
import { API_ERROR_MESSAGES } from '@/constant/errorMessage' | ||
import { actionClient } from '@/lib/safe-action' | ||
import { AUTHORIZATION_API, BACKEND_API } from '@/service/config' | ||
|
||
import { generatePresignedUrl } from './index' | ||
|
||
const CreatePostServerSchema = z.object({ | ||
boardId: z.number(), | ||
postTitle: z.string(), | ||
postContent: z.string(), | ||
imageFile: z.instanceof(File), | ||
activityDate: z.object({ | ||
start: z.date().optional(), | ||
end: z.date().optional(), | ||
}), | ||
}) | ||
|
||
type CreatePostRequest = { | ||
postTitle: string | ||
postContent: string | ||
postImageUrl: string | ||
postActivityStartDate?: string | ||
postActivityEndDate?: string | ||
postType: 'ACTIVITY' | 'NOTICE' | 'EVENT' | ||
} | ||
|
||
export const createActivityPostAction = actionClient | ||
.schema(CreatePostServerSchema) | ||
.action( | ||
async ({ | ||
parsedInput: { boardId, postTitle, postContent, imageFile, activityDate }, | ||
}) => { | ||
try { | ||
const { preSignedUrl, imageUrl } = await generatePresignedUrl() | ||
|
||
await BACKEND_API.put(preSignedUrl, imageFile, { | ||
headers: { | ||
'Content-Type': imageFile.type, | ||
}, | ||
}) | ||
|
||
if (!activityDate.start) throw new Error('날짜 입력 에러') | ||
|
||
const tempDateFormat = kstFormat(activityDate.start, 'yyyy-MM-dd') // date input 수정 전까지 임시 날짜 사용 | ||
|
||
const createActivityPostRequest: CreatePostRequest = { | ||
postTitle, | ||
postContent, | ||
postImageUrl: imageUrl, | ||
postActivityStartDate: tempDateFormat, | ||
postActivityEndDate: tempDateFormat, | ||
postType: 'ACTIVITY', | ||
} | ||
|
||
const response = await AUTHORIZATION_API.post( | ||
`/boards/${boardId}/posts`, | ||
createActivityPostRequest, | ||
) | ||
|
||
return { isSuccess: true, message: response.data.message } | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const response = error.response | ||
|
||
if (response?.status === 404) { | ||
return { message: response.data.message } | ||
} | ||
} | ||
return { message: API_ERROR_MESSAGES.UNKNOWN_ERROR } | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { AUTHORIZATION_API } from '@/service/config' | ||
|
||
type generatePresignedUrlResposne = { | ||
preSignedUrl: string | ||
imageUrl: string | ||
} | ||
|
||
export const generatePresignedUrl = async () => { | ||
const response = await AUTHORIZATION_API.get<generatePresignedUrlResposne>( | ||
'/posts/generate-presigned-url', | ||
) | ||
|
||
return response.data | ||
} |