diff --git a/src/apis/draftLetters.ts b/src/apis/draftLetters.ts new file mode 100644 index 0000000..424853e --- /dev/null +++ b/src/apis/draftLetters.ts @@ -0,0 +1,33 @@ +import client from './client'; + +export interface DraftLetter { + letterId: number; + writerId: number; + receiverId: number; + parentLetterId: number; + zipCode: string; + title: string; + content: string; + category: string; + paperType: string; + fontType: string; + deliveryStartedAt: string; + deliveryCompletedAt: string; + matched: boolean; +} + +export const getDraftLetters = async () // token: string +: Promise => { + try { + const { data } = await client.get('/api/letters?status=draft', { + // headers: { + // Authorization: `Bearer ${token}`, + // }, + }); + console.log('임시저장된 편지 데이터', data); + return data.data; + } catch (error) { + console.error(`❌임시저장된 편지를 불러오던 중 에러가 발생했습니다`, error); + throw new Error('임시저장된 편지 불러오기 실패'); + } +}; diff --git a/src/apis/incomingLetters.ts b/src/apis/incomingLetters.ts index d4c2db7..adde539 100644 --- a/src/apis/incomingLetters.ts +++ b/src/apis/incomingLetters.ts @@ -1,6 +1,6 @@ import client from './client'; -export const fetchIncomingLettersApi = async (token: string) => { +export const getIncomingLetters = async (token: string) => { try { const { data } = await client.get('/api/letters?status=delivery', { headers: { diff --git a/src/pages/Home/components/ShowDraftModal.tsx b/src/pages/Home/components/ShowDraftModal.tsx index b067711..3cb9060 100644 --- a/src/pages/Home/components/ShowDraftModal.tsx +++ b/src/pages/Home/components/ShowDraftModal.tsx @@ -1,6 +1,8 @@ import DeleteOutlineRoundedIcon from '@mui/icons-material/DeleteOutlineRounded'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; +// import { useNavigate } from 'react-router'; +import { DraftLetter, getDraftLetters } from '@/apis/draftLetters'; import ModalBackgroundWrapper from '@/components/ModalBackgroundWrapper'; import ModalOverlay from '@/components/ModalOverlay'; @@ -9,14 +11,27 @@ interface ShowDraftModalProps { onClose: () => void; } -const DUMMY_DRAFT = [ - { id: 1, title: '취업 때문에 고민이 많아요!!' }, - { id: 2, title: '배고파서 죽을 거 같아요 😭' }, - { id: 3, title: '개발하니까 밖에 나갈 일이 없어서 너무 심심하고 피곤해요' }, - { id: 4, title: '마라샹궈 먹고 싶어요' }, -]; - const ShowDraftModal = ({ onClose }: ShowDraftModalProps) => { + const [draftLetters, setDraftLetters] = useState([]); + + // const navigate = useNavigate(); + + // const handleNavigation = (incomingId: number) => { + // navigate(`/board/letter/${incomingId}`, { + // state: { isShareLetterPreview: false }, + // }); + // }; + + useEffect(() => { + getDraftLetters() + .then((data) => { + setDraftLetters(data || []); + }) + .catch((error) => { + console.error('❌ 임시저장된 편지를 불러오는데 실패했습니다', error); + }); + }, [onClose]); + return (
@@ -29,11 +44,12 @@ const ShowDraftModal = ({ onClose }: ShowDraftModalProps) => {

임시저장 편지

로그아웃 시 임시 저장된 편지는 사라집니다

-
- {DUMMY_DRAFT.map((draft) => ( +
+ {draftLetters.map((draft) => (
handleNavigation(draft.letterId)} >

{draft.title}

diff --git a/src/stores/incomingLettersStore.ts b/src/stores/incomingLettersStore.ts index 1ea9433..ecb34e7 100644 --- a/src/stores/incomingLettersStore.ts +++ b/src/stores/incomingLettersStore.ts @@ -1,6 +1,6 @@ import { create } from 'zustand'; -import { fetchIncomingLettersApi } from '@/apis/incomingLetters'; +import { getIncomingLetters } from '@/apis/incomingLetters'; interface IncomingLetters { letterId: number; @@ -42,7 +42,7 @@ export const useIncomingLettersStore = create((set) => ({ fetchIncomingLetters: async () => { try { const token = localStorage.getItem('token') || ''; - const data = await fetchIncomingLettersApi(token); + const data = await getIncomingLetters(token); let arrivedCount = 0; const updatedLetters = data.data.map((letter: IncomingLetters) => {