Skip to content

Commit

Permalink
refactor: document type (picktoss#271)
Browse files Browse the repository at this point in the history
* feat: document card check

* feat: query client for storybook

* fix: merge and fix error

* feat: directory id에 따른 documents 가져오기 + queries 적용

* fix: 불필요한 코드 삭제

* fix: build error

* fix: import

* fix: storybook

* fix: expend buttons bug

* fix: document markdown debugging

* refactor: document type

* refactor: 타입명 수정

* fix: modify document
  • Loading branch information
rabyeoljji committed Nov 24, 2024
1 parent d5427e1 commit 9cf423c
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/app/(routes)/document/[id]/(main)/@header/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const Header = () => {
title="노트를 삭제할까요?"
content={
<Text typography="text1-medium">
{data?.name} 노트와{' '}
{data?.documentName} 노트와{' '}
<span className="text-text-wrong">{data?.totalQuizCount}개의 문제</span>{' '}
<br />
모두 삭제됩니다.
Expand Down
17 changes: 13 additions & 4 deletions src/app/(routes)/document/[id]/modify/@header/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ const Header = () => {
const { documentTitle: title, editorMarkdownContent: content } = useEditDocumentContext()
const { selectedDirectory } = useDirectoryContext()

const handleClickSave = () => {
if (title.trim().length === 0 || content.trim().length === 0) return
const handleClickSave = (id: number, title: string, content: string) => {
// eslint-disable-next-line no-console
console.log(title, content)

if (title.trim().length === 0 || content.trim().length === 0) {
alert('제목과 내용을 입력해주세요')
return
}

const blob = new Blob([content], { type: 'text/markdown' })
const file = new File([blob], `${title}.md`, { type: 'text/markdown' })

updateDocumentMutate(
{ documentId: Number(id), request: { name: title, file } },
{ documentId: id, requestBody: { name: title, file } },
{
onSuccess: () => {
toast({ description: '노트가 수정되었어요' })
Expand All @@ -46,7 +52,10 @@ const Header = () => {
{selectedDirectory?.emoji} {selectedDirectory?.name}
</div>

<button onClick={handleClickSave} className="text-button2 text-button-text-primary">
<button
onClick={() => handleClickSave(Number(id), title, content)}
className="text-button2 text-button-text-primary"
>
저장
</button>
</div>
Expand Down
11 changes: 7 additions & 4 deletions src/app/(routes)/document/[id]/modify/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FunctionComponent, PropsWithChildren } from 'react'
import type { Metadata } from 'next'
import { EditDocumentProvider } from '@/features/modify/context/edit-document-context'
import { DirectoryProvider } from '@/features/directory/contexts/directory-context'

export const metadata: Metadata = {}

Expand All @@ -10,10 +11,12 @@ interface LayoutProps extends PropsWithChildren {

const Layout: FunctionComponent<LayoutProps> = ({ header, children }) => {
return (
<EditDocumentProvider>
{header}
{children}
</EditDocumentProvider>
<DirectoryProvider>
<EditDocumentProvider>
{header}
{children}
</EditDocumentProvider>
</DirectoryProvider>
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/document/components/document-type-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Icon from '@/shared/components/custom/icon'
import { cn } from '@/shared/lib/utils'

interface Props {
type: Document.Item['documentType']
type: Document.ItemInList['documentType']
containerClassName: HTMLElement['className']
iconClassName: HTMLElement['className']
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import stripMarkdown from 'strip-markdown'

interface DocumentProps {
id: number
createType: Document.Item['documentType']
createType: Document.ItemInList['documentType']
title: string
content: string
quizCount: number
Expand Down
2 changes: 1 addition & 1 deletion src/features/modify/components/visual-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function VisualEditor({ prevContent }: VisualEditorProps) {
css`
&.ProseMirror {
padding: 16px 20px;
margin-bottom: 100px;
padding-bottom: 100px;
width: 100%;
/* min-width: 100vw; */
max-width: 430px;
Expand Down
2 changes: 1 addition & 1 deletion src/features/pages/documents-in-directory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const DocumentsInDirectory = () => {
quizCount={document.totalQuizCount}
characterCount={document.characterCount}
directory={document.directory.name}
className={cn(idx === 9 && 'mb-[30px]')}
className={cn(idx === data.documents.length - 1 && 'mb-[30px]')}
reviewCount={document.reviewNeededQuizCount}
/>
))}
Expand Down
2 changes: 1 addition & 1 deletion src/features/pages/modify-document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ModifyDocument = () => {

return (
<>
<TitleInput prevTitle={data?.name} />
<TitleInput prevTitle={data?.documentName} />

<div className="sticky top-[54px] z-10 flex items-center justify-between bg-background-base-02 px-[16px] py-[11px]">
<div className="flex items-center">
Expand Down
2 changes: 1 addition & 1 deletion src/features/search/components/search-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { cn } from '@/shared/lib/utils'
import DocumentTypeIcon from '@/features/document/components/document-type-icon'

interface Props {
createType: Document.Item['documentType']
createType: Document.ItemInList['documentType']
documentTitle: string
matchingSentence: string
resultType: 'document' | 'quiz'
Expand Down
4 changes: 2 additions & 2 deletions src/requests/document/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useUpdateDocument = () => {
const { data: session } = useSession()

return useMutation({
mutationFn: (params: { documentId: number; request: Document.Request.UpdateContent }) =>
updateDocument(params.documentId, params.request, session?.user.accessToken || ''),
mutationFn: (params: { documentId: number; requestBody: Document.Request.UpdateContent }) =>
updateDocument(params.documentId, params.requestBody, session?.user.accessToken || ''),
})
}
40 changes: 26 additions & 14 deletions src/requests/document/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export const fetchDocumentDetail = async (documentId: number) => {
try {
const session = await auth()

const { data } = await http.get<Document.Item>(API_ENDPOINTS.DOCUMENT.GET.BY_ID(documentId), {
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
})
const { data } = await http.get<Document.DetailItem>(
API_ENDPOINTS.DOCUMENT.GET.BY_ID(documentId),
{
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
}
)
return data
} catch (error: unknown) {
console.error(error)
Expand All @@ -54,18 +57,27 @@ export const fetchDocumentDetail = async (documentId: number) => {

export const updateDocument = async (
documentId: number,
request: Document.Request.UpdateContent,
requestBody: Document.Request.UpdateContent,
accessToken: string
) => {
const params = { request }

try {
await http.patch(API_ENDPOINTS.DOCUMENT.PATCH.UPDATE_CONTENT(documentId), null, {
params,
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
const formData = new FormData()
formData.append('name', requestBody.name)
formData.append('file', requestBody.file)

const response = await http.patch(
API_ENDPOINTS.DOCUMENT.PATCH.UPDATE_CONTENT(documentId),
formData,
{
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'multipart/form-data',
},
}
)

// eslint-disable-next-line no-console
console.log(response) // 디버깅용
} catch (error: unknown) {
console.error(error)
throw error
Expand Down
27 changes: 18 additions & 9 deletions src/types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ type Quiz = {
quizType: QuizType
}

type DocumentItem = {
type DocumentBase = {
id: number
documentType: CreateType
name: string
documentName?: string
status: DocumentStatus
previewContent?: string
content: string
characterCount: number
totalQuizCount: number
updatedAt: string
characterCount: number
directory: Directory
updatedAt: string
}

type DocumentDetailItem = DocumentBase & {
documentName: string
content: string
quizzes: Quiz[]
}

type DocumentListItem = DocumentBase & {
name: string
documentType: CreateType
previewContent: string
createdAt: string
reviewNeededQuizCount: number
}

Expand Down Expand Up @@ -159,8 +167,9 @@ interface SearchDocumentsResponse {
}

declare namespace Document {
type Item = DocumentItem
type List = DocumentItem[]
type DetailItem = DocumentDetailItem
type ItemInList = DocumentListItem
type List = DocumentListItem[]
type Status = DocumentStatus
type Sort = SortOption

Expand Down

0 comments on commit 9cf423c

Please sign in to comment.