Skip to content

Commit d388591

Browse files
committed
feat: 새로 도착한 편지 개수 계산 구현
1 parent aba9db6 commit d388591

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/apis/incomingLetters.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const fetchIncomingLettersApi = async (token: string) => {
77
Authorization: `Bearer ${token}`,
88
},
99
});
10+
console.log('불러온 데이터', data);
1011
return data;
1112
} catch (error) {
1213
console.error('❌오고 있는 편지 목록을 불러오던 중 에러 발생', error);

src/pages/Home/components/HomeRight.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
import { useEffect } from 'react';
2+
3+
import { useIncomingLettersStore } from '@/stores/incomingLettersStore';
4+
15
import FloatingLetters from './FloatingLetters';
26
import GoToLetterBoard from './GoToLetterBoard';
37
import GoToLetterBox from './GoToLetterBox';
48
import NewLetterModal from './NewLetterModal';
59

610
const HomeRight = () => {
7-
//TODO : hasNewLetters 전역으로 상태 관리할지
8-
const hasNewLetters = true;
11+
const { arrivedCount, fetchIncomingLetters } = useIncomingLettersStore();
12+
useEffect(() => {
13+
fetchIncomingLetters();
14+
}, []);
915

1016
return (
1117
<div className="flex h-screen w-full max-w-150 min-w-[300px] flex-shrink-0 grow snap-start flex-col items-center overflow-x-hidden pt-5">
12-
{hasNewLetters && <FloatingLetters />}
18+
{arrivedCount !== 0 && <FloatingLetters />}
1319
<GoToLetterBox />
1420
<GoToLetterBoard />
15-
{hasNewLetters && <NewLetterModal />}
21+
{arrivedCount !== 0 && <NewLetterModal />}
1622
</div>
1723
);
1824
};

src/pages/Home/components/NewLetterModal.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
// import { useState } from 'react';
1+
import { useEffect } from 'react';
22

3+
import { useIncomingLettersStore } from '@/stores/incomingLettersStore';
4+
5+
//TODO: 내 편지함 상세 조회에서 해당 편지를 조회하면 arrivedCount가 1 감소하도록
36
const NewLetterModal = () => {
4-
// const [newLetterCount, setNewLetterCount] = useState(0);
5-
const newLetterCount = 3;
7+
const { arrivedCount, fetchIncomingLetters } = useIncomingLettersStore();
8+
9+
useEffect(() => {
10+
fetchIncomingLetters();
11+
}, []);
612

713
return (
814
<p className="text-gray-60 body-b absolute top-30 mb-10 w-fit animate-pulse rounded-full bg-white px-6 py-4">
9-
{newLetterCount}통의 편지가 도착했어요!
15+
{arrivedCount}통의 편지가 도착했어요!
1016
</p>
1117
);
1218
};

src/stores/incomingLettersStore.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface IncomingLetters {
1212

1313
interface IncomingLettersStore {
1414
data: IncomingLetters[];
15+
arrivedCount: number;
1516
message: string;
1617
timestamp: string;
1718
fetchIncomingLetters: () => void;
@@ -35,22 +36,28 @@ const calculatingRemainingTime = (deliveryCompletedAt: string): string => {
3536

3637
export const useIncomingLettersStore = create<IncomingLettersStore>((set) => ({
3738
data: [],
39+
arrivedCount: 0,
3840
message: '',
3941
timestamp: '',
4042
fetchIncomingLetters: async () => {
4143
try {
4244
const token = localStorage.getItem('token') || '';
4345
const data = await fetchIncomingLettersApi(token);
4446

45-
const updatedLetters = data.data
46-
.map((letter: IncomingLetters) => ({
47-
...letter,
48-
remainingTime: calculatingRemainingTime(letter.deliveryCompletedAt),
49-
}))
50-
.filter((letter: IncomingLetters) => letter.remainingTime !== '00:00:00');
47+
let arrivedCount = 0;
48+
const updatedLetters = data.data.map((letter: IncomingLetters) => {
49+
const remainingTime = calculatingRemainingTime(letter.deliveryCompletedAt);
50+
if (remainingTime === '00:00:00') arrivedCount += 1; // 도착한 편지 카운트
5151

52+
return { ...letter, remainingTime };
53+
});
54+
55+
const inProgressLetters = updatedLetters.filter(
56+
(letter: IncomingLetters) => letter.remainingTime !== '00:00:00',
57+
);
5258
set({
53-
data: updatedLetters,
59+
data: inProgressLetters,
60+
arrivedCount,
5461
message: data.message,
5562
timestamp: data.timestamp,
5663
});

0 commit comments

Comments
 (0)