ํฐ์คํ ๋ฆฌ ๋ทฐ
์ค์ ์คํฌ๋ผ ์๊ฐ์ ์บ๋ฆฐ๋ ๊ตฌํ ๊ธฐ๋ฅ์ ์ด์ผ๊ธฐ ๋๋๋ ์ค, ํํฐ๋์ ํ ๋ง๋
"์ฝ์ ๋ฐ์ดํฐ๋ ํ ๋ฒ์ ๋ค ๋ฐ์์ค๋์? ์๋ณ๋ก ๋์ด๊ฐ ๋๋ง๋ค ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฑด ์ด๋จ๊น์?"
ํ ์คํธ ๋จ๊ณ์ธ ์ง๊ธ์ ์์์ ๊ฐ ๋์ด ๊ฐ๋ง ๋ฃ์ด๋๊ณ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ ํฌ๊ฒ ๋ฌธ์ ๊ฐ ์ ๋๊ฒ ์ง๋ง,
์ค์ ๋ก ์ด ์๋น์ค๊ฐ ํ์ฑํ๋๋ค๋ฉด..
์ฌ๋ฌ ์ ์ ๊ฐ ์ฝ์ ์ผ์ ์ ์ถ๊ฐํ๊ฒ ๋๊ณ , ์๊ฐ์ด ์ง๋ ์์ฃผ ์์ ๋ฐ์ดํฐ๊น์ง ๋ค ๋ถ๋ฌ์ค๊ฒ ๋๋ค๋ฉด...
์ ๋๊ฒ ๋ค. ๋งค ๋ฌ๋ง๋ค ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์บ์ฑํด๋์ด์ผ๊ฒ ๋ค.
์๋ณ ๋ฐ์ดํฐ ์บ์ฑํ๊ธฐ
๊ฐ๋จํ ์ค๋ช ํ์๋ฉด ์บ๋ฆฐ๋ ์ปดํฌ๋ํธ - useQuery ์ปค์คํ ํ - supabase ๋ฐ์ดํฐ ํ์นญ ํจ์ ์ธ ๋ก์ง์ ๊ด๊ณ๋ฅผ ์์ ํ๋ฉด ๋๋ค.
์บ๋ฆฐ๋ ์ปดํฌ๋ํธ
์บ๋ฆฐ๋ ์ปดํฌ๋ํธ์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ํด๋น ๋ฌ์ ์ํ ๊ด๋ฆฌ - Date ๊ฐ์ฒด : Wed Apr 09 2025 23:37:00 GMT+0900 (ํ๊ตญ ํ์ค์)
- ํด๋น ๋ฌ์ ํ์ฌ ๋ ๋ - number : 2025
- useQuery ์ปค์คํ ํ ์ ๋ ๋์ ๋ฌ์ ๊ฐ ๋๊ฒจ์ฃผ์ด ๋ฐ์ดํฐ ๋ฐ์์ค๊ธฐ : ์ฝ์ ์ผ์ ๋ฐ์ดํฐ
const [month, setMonth] = useState(new Date()); //ํด๋น ๋ฌ
const calendarYear = month.getFullYear(); // ํด๋น ๋ฌ์ ๋
๋
const { data: events, isPending, isError, error } = useGetCalendarPlans(calendarYear, month);
useQuery ์ปค์คํ ํ
useQuery ์ปค์คํ ํ ์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- supabase์ ์ซ์๋ก ์ ๋ฌํด์ฃผ๊ธฐ ์ํด ํ์ ์ ํ
- ํด๋น ๋ ๋์ ๋ฌ๋ง๋ค ์บ์ฑ (staleTime์ 1์ผ)
- supabase ํ์นญ ๋ก์ง์ ๋ ๋์ ๋ฌ ๋๊ฒจ์ฃผ์ด ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
- ๋ฐ์์จ ๋ฐ์ดํฐ๋ฅผ ์บ๋ฆฐ๋์ ์ ์ฉํ๋๋ก ๊ฐ๊ณตํ๊ธฐ
export const useGetCalendarPlans = (year: number, monthDate: Date) => {
const month = monthDate.getMonth() + 1; // Date ๊ฐ์ฒด -> ์ซ์ ์ ํ
return useQuery<CalendarEventType[]>({
queryKey: [QUERY_KEY.CALENDAR_PLANS, year, month], //๋ฌ๋ง๋ค ์บ์ฑ
queryFn: async () => {
const plans: PlansType[] = await getPlans(year, month); // supabase์์ plans ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
// ์บ๋ฆฐ๋์ ์ ์ฉ๋๋๋ก ๋ฐ์ดํฐ ๊ฐ๊ณตํ๊ธฐ
const events: CalendarEventType[] = plans.map((plan) => ({
id: plan.plan_id,
title: plan.title,
start: new Date(plan.start_date),
end: new Date(plan.end_date),
}));
return events;
},
staleTime: 24 * 60 * 60 * 1000, // 1์ผ
});
};
supabase ๋ฐ์ดํฐ ํ์นญ ํจ์
supabase ๋ฐ์ดํฐ ํ์นญ ํจ์์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ๋งค ๋ฌ์ ์ฒซ ๋ ๊ณผ ๋ง์ง๋ง ๋ ๋ง๋ค๊ธฐ
- ์ฝ์ ์ถ๊ฐํ ๋ 1์๊ฐ ์ถ๊ฐํ ์ด์
- DnD ์ ๋จ์ผ ์ผ์ ๋์ด๋จ (end-start = 0 ์ผ ๋ ์๋์ฒ๋ฆฌ)
- ์ฐ์ ์ผ์ ์ ๋ง์ง๋ง ๋ ๋ฌ๋ ฅ์ ์ ์ฉ์ํค๊ธฐ
- ์๋ง~์์ด ์ด์ด์ง๋ ์ฝ์๋ ๋ํ๋ด๊ธฐ ์ํด setHours ์ ์ฉ
- lte: ์๊ฑฐ๋ ๊ฐ๋ค / gte: ํฌ๊ฑฐ๋ ๊ฐ๋ค
export const getPlans = async (year: number, month: number): Promise<PlansType[]> => {
const startOfMonth = new Date(year, month - 1, 1); //์ฒซ ๋
const endOfMonth = new Date(year, month, 0); //๋ง์ง๋ง ๋ (4/30 00:00:00)
// ํ์ฌ start_date๊ฐ 4/30 01:00 ์ด๊ธฐ ๋๋ฌธ์ ์ฐ์ ์ผ์ ์ ์ฒซ ๋ ๋ ํจ๊ป ํฌํจ์ํค๊ธฐ ์ํจ
endOfMonth.setHours(23, 59, 59, 999);
const { data: plans, error } = await supabase
.from(PLANS)
.select('plan_id, user_id, contacts_id, title, detail, priority, start_date, end_date')
//ํด๋น ๋ฌ์ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ค๊ธฐ(๋์ด๊ฐ๋ ์ฐ์ ์ผ์ ํฌํจ)
.lte('start_date', endOfMonth.toISOString()) // ์ผ์ ์ด ๋ฌ์ ๋ง์ง๋ง ๋ ๊ณผ ๊ฐ๊ฑฐ๋ ์ด์ ์ ์์
.gte('end_date', startOfMonth.toISOString()); // ์ผ์ ์ด ๋ฌ์ ์ฒซ ๋ ๋ณด๋ค ๊ฐ๊ฑฐ๋ ์ดํ์ ๋
if (error) {
throw new Error(error.message);
}
return plans;
};