Skip to content

Commit

Permalink
feat: document (#268)
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
  • Loading branch information
rabyeoljji authored Nov 22, 2024
1 parent 30cfe33 commit e58ad7d
Show file tree
Hide file tree
Showing 38 changed files with 336 additions and 241 deletions.
8 changes: 8 additions & 0 deletions .storybook/preview.ts → .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { Providers } from '../src/providers'
import type { Preview } from '@storybook/react'
import '../src/app/globals.css'

Expand All @@ -11,6 +12,13 @@ const preview: Preview = {
},
},
},
decorators: [
(Story) => (
<Providers>
<Story />
</Providers>
),
],
}

export default preview
2 changes: 1 addition & 1 deletion src/app/(routes)/document/(create)/@header/default.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DirectorySelectDrawer from '@/features/document/components/directory-select-drawer'
import DirectorySelectDrawer from '@/features/directory/components/directory-select-drawer'
import GoBackButton from '@/shared/components/custom/go-back-button'
import Text from '@/shared/components/ui/text'

Expand Down
3 changes: 2 additions & 1 deletion src/app/(routes)/document/(create)/file/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { FunctionComponent, PropsWithChildren } from 'react'
import type { Metadata } from 'next'
import { DirectoryProvider } from '@/features/directory/contexts/directory-context'

export const metadata: Metadata = {}

interface LayoutProps extends PropsWithChildren {}

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

export default Layout
4 changes: 2 additions & 2 deletions src/app/(routes)/document/(create)/file/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Icon from '@/shared/components/custom/icon'
import Text from '@/shared/components/ui/text'
import { EditDocumentProvider } from '@/features/editor/context/edit-document-context'
import TitleInput from '@/features/editor/components/title-input'
import { EditDocumentProvider } from '@/features/modify/context/edit-document-context'
import TitleInput from '@/features/modify/components/title-input'
import FixedBottom from '@/shared/components/custom/fixed-bottom'
import NewQuizDrawer from '@/features/quiz/components/new-quiz-drawer'
import { Button } from '@/shared/components/ui/button'
Expand Down
2 changes: 1 addition & 1 deletion src/app/(routes)/document/(create)/notion/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditDocumentProvider } from '@/features/editor/context/edit-document-context'
import { EditDocumentProvider } from '@/features/modify/context/edit-document-context'
import NewQuizDrawer from '@/features/quiz/components/new-quiz-drawer'
import FixedBottom from '@/shared/components/custom/fixed-bottom'
import Icon from '@/shared/components/custom/icon'
Expand Down
2 changes: 1 addition & 1 deletion src/app/(routes)/document/(create)/ui/header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DirectorySelectDrawer from '@/features/document/components/directory-select-drawer'
import DirectorySelectDrawer from '@/features/directory/components/directory-select-drawer'
import GoBackButton from '@/shared/components/custom/go-back-button'
import Text from '@/shared/components/ui/text'

Expand Down
31 changes: 18 additions & 13 deletions src/app/(routes)/document/(main)/@header/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import Text from '@/shared/components/ui/text'
import { useEffect, useState } from 'react'
import { cn } from '@/shared/lib/utils'
import Link from 'next/link'
import { useDirectoryContext } from '@/features/document/contexts/directory-context'
import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger } from '@/shared/components/ui/drawer'
import SortIconBtn from '@/features/document/components/sort-icon-button'
import DirectoryMenuDots from '@/features/document/components/directory-menu-dots'
import DirectoryMenuDots from '@/features/directory/components/directory-menu-dots'
import GoBackButton from '@/shared/components/custom/go-back-button'
import { useDirectories } from '@/requests/directory/hooks'
import CreateDirectoryDialog from '@/features/directory/components/create-directory-dialog'
import { useDocumentContext } from '@/features/document/contexts/document-context'
import { useDirectoryContext } from '@/features/directory/contexts/directory-context'

// Header 컴포넌트
const Header = () => {
const { data } = useDirectories()
const { isSelectMode, setIsSelectMode } = useDirectoryContext()
const { selectedDirectory } = useDirectoryContext()
const { isSelectMode, setIsSelectMode } = useDocumentContext()
const [isDrawerOpen, setIsDrawerOpen] = useState(false)

return (
Expand All @@ -33,7 +35,7 @@ const Header = () => {
<GoBackButton icon="cancel" onClick={() => setIsSelectMode(false)} />

<Text as="span" typography="subtitle2-medium" className="ml-[35px]">
전공 공부
{selectedDirectory?.name}
</Text>
<Text as="span" typography="button4" className="text-button-text-primary">
전체 선택
Expand Down Expand Up @@ -75,7 +77,8 @@ interface Props {
}

const DirectorySelectDrawer = ({ isDrawerOpen, setIsDrawerOpen, directories }: Props) => {
const { selectedDirectoryId, setButtonHidden, setSelectedDirectoryId } = useDirectoryContext()
const { selectedDirectory, selectedDirectoryId, selectDirectoryId } = useDirectoryContext()
const { setButtonHidden } = useDocumentContext()

useEffect(() => {
if (isDrawerOpen) {
Expand All @@ -85,17 +88,19 @@ const DirectorySelectDrawer = ({ isDrawerOpen, setIsDrawerOpen, directories }: P
}
}, [isDrawerOpen, setButtonHidden])

const currentDirectory = directories.find((directory) => directory.id === selectedDirectoryId)
const totalNotes = directories.reduce((acc, directory) => acc + directory.documentCount, 0)
const handleDirectorySelect = (id: number | null) => {
selectDirectoryId(id)
setIsDrawerOpen(false)
}

return (
<>
<Drawer open={isDrawerOpen} onOpenChange={setIsDrawerOpen} direction="top">
<DrawerTrigger asChild>
<button className="flex size-fit items-center">
<h2 className="mr-[8px] text-title2">
{currentDirectory
? `${currentDirectory.emoji} ${currentDirectory.name}`
{selectedDirectory
? `${selectedDirectory.emoji ?? '📁'} ${selectedDirectory.name}`
: '전체 노트'}
</h2>
<Icon name="chevron-down" className="size-[20px]"></Icon>
Expand All @@ -109,13 +114,13 @@ const DirectorySelectDrawer = ({ isDrawerOpen, setIsDrawerOpen, directories }: P
>
<div className="flex h-fit flex-col bg-background-base-01">
<div className="border-b border-border-divider">
<button className="w-full" onClick={() => setSelectedDirectoryId(null)}>
<button className="w-full" onClick={() => handleDirectorySelect(null)}>
<DrawerTitle className="mt-[24px] flex items-center justify-between px-[18px]">
<Text as="span" typography="subtitle2-medium">
전체 노트
</Text>
<Text as="span" typography="text1-medium" className="text-text-caption">
노트 {totalNotes}
노트 {selectedDirectory?.documentCount}
</Text>
</DrawerTitle>
</button>
Expand All @@ -126,7 +131,7 @@ const DirectorySelectDrawer = ({ isDrawerOpen, setIsDrawerOpen, directories }: P
<button
key={directory.id}
className="flex items-center justify-between py-[10px]"
onClick={() => setSelectedDirectoryId(directory.id)}
onClick={() => handleDirectorySelect(directory.id)}
>
<Text
as="span"
Expand All @@ -135,7 +140,7 @@ const DirectorySelectDrawer = ({ isDrawerOpen, setIsDrawerOpen, directories }: P
directory.id === selectedDirectoryId && 'text-text-accent font-bold'
)}
>
{`${directory.emoji || '📄'} ${directory.name}`}
{`${directory.emoji ?? '📁'} ${directory.name}`}
</Text>
<Text as="span" typography="text1-medium" className="text-text-caption">
노트 {directory.documentCount}
Expand Down
15 changes: 9 additions & 6 deletions src/app/(routes)/document/(main)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FunctionComponent, PropsWithChildren } from 'react'
import type { Metadata } from 'next'
import { Metadata } from 'next'
import BottomNavLayout from '@/shared/components/custom/bottom-nav-layout'
import { DirectoryProvider } from '@/features/document/contexts/directory-context'
import { DocumentProvider } from '@/features/document/contexts/document-context'
import { DirectoryProvider } from '@/features/directory/contexts/directory-context'

export const metadata: Metadata = {}

Expand All @@ -13,10 +14,12 @@ const Layout: FunctionComponent<LayoutProps> = ({ header, children }) => {
return (
<BottomNavLayout where="퀴즈 노트">
<DirectoryProvider>
<div className="flex h-[calc(100dvh-88px)] w-full flex-col overflow-hidden bg-background-base-02 text-text-primary">
{header}
{children}
</div>
<DocumentProvider>
<div className="flex h-[calc(100dvh-88px)] w-full flex-col overflow-hidden bg-background-base-02 text-text-primary">
{header}
{children}
</div>
</DocumentProvider>
</DirectoryProvider>
</BottomNavLayout>
)
Expand Down
5 changes: 3 additions & 2 deletions src/app/(routes)/document/[id]/(main)/@header/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import Link from 'next/link'
import { useParams, useRouter } from 'next/navigation'
import usePreviousPath from '@/shared/hooks/use-previous-path'
import GoBackButton from '@/shared/components/custom/go-back-button'
import { useGetDocumentDetail } from '@/requests/document/hooks'
import { getRelativeTime } from '@/shared/utils/date'
import { useQuery } from '@tanstack/react-query'
import { queries } from '@/shared/lib/tanstack-query/query-keys'

// Header 컴포넌트
const Header = () => {
const router = useRouter()
const { id } = useParams()
const { getPreviousPath } = usePreviousPath({ getCustomPath: true })
const { data } = useGetDocumentDetail(Number(id[0]))
const { data } = useQuery(queries.document.item(Number(id[0])))

const handleClickCancel = () => {
const previousPath = getPreviousPath()
Expand Down
8 changes: 5 additions & 3 deletions src/app/(routes)/document/[id]/modify/@header/default.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client'

import EditCancelDialog from '@/features/document/components/edit-cancel-dialog'
import { useEditDocumentContext } from '@/features/editor/context/edit-document-context'
import { useDirectoryContext } from '@/features/directory/contexts/directory-context'
import EditCancelDialog from '@/features/modify/components/edit-cancel-dialog'
import { useEditDocumentContext } from '@/features/modify/context/edit-document-context'
import { useUpdateDocument } from '@/requests/document/hooks'
import { toast } from '@/shared/hooks/use-toast'
import { cn } from '@/shared/lib/utils'
Expand All @@ -12,6 +13,7 @@ const Header = () => {
const router = useRouter()
const { mutate: updateDocumentMutate } = useUpdateDocument()
const { documentTitle: title, editorMarkdownContent: content } = useEditDocumentContext()
const { selectedDirectory } = useDirectoryContext()

const handleClickSave = () => {
if (title.trim().length === 0 || content.trim().length === 0) return
Expand Down Expand Up @@ -41,7 +43,7 @@ const Header = () => {
<EditCancelDialog />

<div className="rounded-full bg-background-base-02 px-[16px] py-[5px] text-text1-medium">
📊 전공 공부
{selectedDirectory?.emoji} {selectedDirectory?.name}
</div>

<button onClick={handleClickSave} className="text-button2 text-button-text-primary">
Expand Down
2 changes: 1 addition & 1 deletion src/app/(routes)/document/[id]/modify/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionComponent, PropsWithChildren } from 'react'
import type { Metadata } from 'next'
import { EditDocumentProvider } from '@/features/editor/context/edit-document-context'
import { EditDocumentProvider } from '@/features/modify/context/edit-document-context'

export const metadata: Metadata = {}

Expand Down
6 changes: 3 additions & 3 deletions src/app/(routes)/main/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Text from '@/shared/components/ui/text'
import BOMB from '@/../../public/images/bomb.png'
import RANDOM_QUIZ from '@/../../public/images/random-quiz.png'
import Image from 'next/image'
import { DirectoryProvider } from '@/features/document/contexts/directory-context'
import { DocumentProvider } from '@/features/document/contexts/document-context'
import AddDocumentMenu from '@/features/document/components/add-document-menu'
import Link from 'next/link'
import Icon from '@/shared/components/custom/icon'
Expand Down Expand Up @@ -87,9 +87,9 @@ const Home = () => {
</div>
</div>

<DirectoryProvider>
<DocumentProvider>
<AddDocumentMenu />
</DirectoryProvider>
</DocumentProvider>
</main>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryObj } from '@storybook/react'
import DirectoryMenuDots from '.'
import { DirectoryProvider } from '../../contexts/directory-context'
import { DocumentProvider } from '@/features/document/contexts/document-context'

const meta = {
title: 'document/DirectoryMenuDots',
Expand All @@ -11,13 +11,13 @@ const meta = {
},
decorators: [
(Story) => (
<DirectoryProvider>
<DocumentProvider>
<div className="relative mx-auto max-w-mobile p-4">
<div className="absolute bottom-0 right-0">
<Story />
</div>
</div>
</DirectoryProvider>
</DocumentProvider>
),
],
} satisfies Meta<typeof DirectoryMenuDots>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
DropdownMenuTrigger,
} from '@/shared/components/ui/dropdown-menu'
import Text from '@/shared/components/ui/text'
import { useDirectoryContext } from '../../contexts/directory-context'
import DirectoryDialog from '@/features/quiz/components/directory-dialog'
import SetDirectoryNameDialog from '../../../directory/components/set-directory-name-dialog'
import SetDirectoryNameDialog from '../set-directory-name-dialog'
import { useDocumentContext } from '../../../document/contexts/document-context'

const DirectoryMenuDots = () => {
const { setIsSelectMode } = useDirectoryContext()
const { setIsSelectMode } = useDocumentContext()

return (
<DropdownMenu>
Expand Down
2 changes: 1 addition & 1 deletion src/features/directory/contexts/directory-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface DirectoryContextValues {
selectedDirectory: Directory.Item | null

selectedDirectoryId: number | null
selectDirectoryId: (id: number) => void
selectDirectoryId: (id: number | null) => void
}

const DirectoryContext = createContext<DirectoryContextValues | null>(null)
Expand Down
4 changes: 2 additions & 2 deletions src/features/document/components/add-document-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Text from '@/shared/components/ui/text'
import DimmedBackground from './dimmed-background'
import AnimatedButtons from './animate-buttons'
import { cn } from '@/shared/lib/utils'
import { useDirectoryContext } from '../contexts/directory-context'
import { useDocumentContext } from '../contexts/document-context'

const AddDocumentMenu = () => {
const { isExpandedBtns } = useDirectoryContext()
const { isExpandedBtns } = useDocumentContext()

return (
<div
Expand Down
8 changes: 4 additions & 4 deletions src/features/document/components/add-first-document/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import Icon from '@/shared/components/custom/icon'
import Text from '@/shared/components/ui/text'
import usePreviousPath from '@/shared/hooks/use-previous-path'
import Link from 'next/link'

const AddFirstDocument = ({ userName }: { userName: string }) => {
usePreviousPath()

return (
<Link
href={'document'}
href={{
pathname: 'document',
query: { ref: 'add-first-document' },
}}
className="border-special mt-[12px] flex h-fit w-full items-center justify-between rounded-[20px] py-[22px] pl-[25px] pr-[22px]"
>
<div className="flex items-center">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Meta, StoryObj } from '@storybook/react'
import { useEffect } from 'react'
import { DirectoryProvider, useDirectoryContext } from '../../contexts/directory-context'
import AnimatedButtons from '.'
import { DocumentProvider, useDocumentContext } from '../../contexts/document-context'

const AnimatedButtonsWithProvider = ({ isExpandedBtns }: { isExpandedBtns: boolean }) => {
const { setIsExpandedBtns } = useDirectoryContext()
const { setIsExpandedBtns } = useDocumentContext()

useEffect(() => {
setIsExpandedBtns(isExpandedBtns)
Expand All @@ -30,13 +30,13 @@ const meta = {
},
decorators: [
(Story, context) => (
<DirectoryProvider initialValues={{ isExpandedBtns: context.args.isExpandedBtns }}>
<DocumentProvider initialValues={{ isExpandedBtns: context.args.isExpandedBtns }}>
<div className="relative mx-auto h-[500px] max-w-mobile p-4">
<div className="absolute bottom-0 right-0">
<Story {...context.args} />
</div>
</div>
</DirectoryProvider>
</DocumentProvider>
),
],
} satisfies Meta<typeof AnimatedButtonsWithProvider>
Expand Down
4 changes: 2 additions & 2 deletions src/features/document/components/animate-buttons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { Button, ButtonProps } from '@/shared/components/ui/button'
import Icon, { IconProps } from '@/shared/components/custom/icon'
import { useEffect, useState } from 'react'
import { cn } from '@/shared/lib/utils'
import { useDirectoryContext } from '../../contexts/directory-context'
import Text from '@/shared/components/ui/text'
import { useRouter } from 'next/navigation'
import { addDocumentButtons } from '../../config'
import { useDocumentContext } from '../../contexts/document-context'

type Custom = number | 'plus' | 'cancel'

// AnimatedButtons 컴포넌트
const AnimatedButtons = () => {
const router = useRouter()
const [isFirstRender, setIsFirstRender] = useState(true)
const { buttonHidden, isExpandedBtns, setIsExpandedBtns } = useDirectoryContext()
const { buttonHidden, isExpandedBtns, setIsExpandedBtns } = useDocumentContext()

useEffect(() => {
setIsFirstRender(false)
Expand Down
Loading

0 comments on commit e58ad7d

Please sign in to comment.