Skip to content

Commit b74a89f

Browse files
committed
feat: 입력받은 데이터를 DB에 저장 & DB에서 일정불러오기
1 parent 294f170 commit b74a89f

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

src/apis/personalScheduleApi.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import supabase from '@/supabase';
2+
import { Events } from '@/utils/index';
3+
4+
export const getPersonalSchedule = async () => {
5+
const { data, error } = await supabase.from('personal_schedules').select();
6+
if (error) {
7+
throw error;
8+
}
9+
return data;
10+
};
11+
12+
// 가장 최근 데이터 하나 가져오기
13+
export const getLastPersonalSchedule = async () => {
14+
const { data, error } = await supabase
15+
.from('personal_schedules')
16+
.select()
17+
.order('created_at', { ascending: false })
18+
.limit(1)
19+
.maybeSingle();
20+
if (error) {
21+
throw error;
22+
}
23+
return data;
24+
};
25+
26+
export const addPersonalSchedule = async ({ start, end, ...props }: Events) => {
27+
const { error } = await supabase.from('personal_schedules').insert({ ...props, start_date: start, end_date: end });
28+
if (error) {
29+
throw error;
30+
}
31+
};
32+
33+
export const updatePersonalSchedule = async (id: number, { start, end, ...props }: Events) => {
34+
const { error } = await supabase
35+
.from('personal_schedules')
36+
.update({ ...props, start_date: start, end_date: end })
37+
.eq('id', id);
38+
if (error) {
39+
throw error;
40+
}
41+
};
42+
43+
export const deletePersonalSchedule = async (id: number) => {
44+
const { error } = await supabase.from('personal_schedules').delete().eq('id', id);
45+
if (error) {
46+
throw error;
47+
}
48+
};

src/components/MyCalendar/CreateEventButton.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useEventState } from '@/stores/myEventsStore.ts';
21
import DialogButton from '@/components/common/DialogButton';
32
import { InputRef } from '../common/InputForm.tsx';
43
import { useState, useRef } from 'react';
4+
import { Events } from '@/utils/index.ts';
5+
import { addPersonalSchedule } from '@/apis/personalScheduleApi.ts';
56

67
const CreateEventButton = () => {
7-
const { addEvents } = useEventState();
88
const [eventTitle, setTitle] = useState('');
99
const [startDate, setStartDate] = useState('');
1010
const [endDate, setEndDate] = useState('');
@@ -26,8 +26,14 @@ const CreateEventButton = () => {
2626

2727
const onCreateClicked = () => {
2828
if (eventTitle !== '' && startDate !== '') {
29-
addEvents({ title: eventTitle, start: startDate, end: endDate === '' ? startDate : endDate });
29+
const newEvent: Events = {
30+
title: eventTitle,
31+
start: startDate,
32+
end: endDate === '' ? startDate : endDate,
33+
};
34+
addPersonalSchedule(newEvent);
3035
}
36+
3137
setTitle('');
3238
setStartDate('');
3339
setEndDate('');

src/components/common/Calendar.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import dayGridPlugin from '@fullcalendar/daygrid';
33
import interactionPlugin from '@fullcalendar/interaction';
44
import { useRef, useState, useEffect } from 'react';
55
import { useEventState } from '@/stores/myEventsStore';
6+
import { getPersonalSchedule } from '@/apis/personalScheduleApi';
67

78
const Calendar: React.FC = () => {
89
const [calendarHeight, setCalendarHeight] = useState<string | number>('auto');
@@ -49,6 +50,8 @@ const Calendar: React.FC = () => {
4950
}
5051
};
5152

53+
const { events, addEvents } = useEventState();
54+
5255
useEffect(() => {
5356
const calendarApi = calendarRef?.current?.getApi();
5457

@@ -62,15 +65,19 @@ const Calendar: React.FC = () => {
6265
window.addEventListener('resize', updateSize);
6366
updateSize(); // 컴포넌트 마운트 시 화면 크기에 따른 업데이트
6467

68+
const data = getPersonalSchedule();
69+
data.then((schedule) => {
70+
schedule.map((x) => addEvents({ ...x, start: x.start_date, end: x.end_date }));
71+
});
72+
6573
return () => {
6674
window.removeEventListener('resize', updateSize);
6775
if (calendarApi) {
6876
calendarApi.off('datesSet', updateTitle);
6977
}
7078
};
71-
}, [updateSize]);
79+
}, [updateSize, addEvents]);
7280

73-
const events = useEventState();
7481
return (
7582
<div>
7683
<FullCalendar

src/stores/myEventsStore.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { create } from 'zustand';
22
import { Events } from '../utils/index.ts';
33

4-
const initialEventsState: Events[] = [
5-
{ title: 'Meeting', start: new Date(), end: new Date() },
6-
{ title: 'Meeting2', start: '2024-05-08', end: '2024-05-12', backgroundColor: 'red', borderColor: 'red' },
7-
{ title: 'Meeting3', start: '2024-05-08', end: '2024-05-10', backgroundColor: 'green', borderColor: 'green' },
8-
{ title: 'Meeting4', start: '2024-05-08', end: '2024-05-11' },
9-
];
10-
114
interface EventsState {
125
events: Events[];
136
addEvents: (event: Events) => void;
147
initEvents: () => void;
158
}
169

1710
export const useEventState = create<EventsState>()((set) => ({
18-
events: [...initialEventsState],
11+
events: [],
1912

2013
addEvents: (newEvent: Events) => set((state) => ({ events: [...state.events, newEvent] })),
2114
initEvents: () => set(() => ({ events: [] })),

src/utils/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export interface Events {
2-
id?: string;
32
title: string;
4-
start: Date | string;
5-
end: Date | string;
3+
start: string;
4+
end: string;
65
backgroundColor?: string;
76
borderColor?: string;
87
textColor?: string;

0 commit comments

Comments
 (0)