Skip to content

Commit

Permalink
feat: 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
urimeee committed Feb 19, 2025
1 parent d697d07 commit 90bca1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
5 changes: 2 additions & 3 deletions src/components/ScheduleCard/ScheduleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { useEffect, useState } from 'react';
import ScheduleDetail from '../ScheduleDetail/ScheduleDetail'; // ScheduleDetail 컴포넌트 경로에 맞게 수정
import BottomSheet from '../BottomSheet/BottomSheet'; // BottomSheet 컴포넌트 불러오기
import * as S from './ScheduleCard.style';

import { useSchedules } from '../../hooks/useSchedule';
import useSchedules from '../../hooks/useSchedule';

const ScheduleCard = () => {
const [isSheetOpen, setSheetOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -31,7 +30,7 @@ const ScheduleCard = () => {
}, []);

useEffect(() => {
console.log('scheduleList 변경');
console.log('scheduleList 변경', scheduleList);
}, [scheduleList]);

return (
Expand Down
66 changes: 26 additions & 40 deletions src/hooks/useSchedule.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { api } from '../utils/axios';

interface ScheduleProviderProps {
children: React.ReactNode;
}
// const ScheduleContext = createContext(null);

const ScheduleContext = createContext<any>(null);

export const ScheduleProvider = ({ children }: ScheduleProviderProps) => {
const useSchedules = () => {
const [scheduleList, setScheduleList] = useState([]);
const [schedule, setSchedule] = useState<string>('');

Expand All @@ -16,21 +12,24 @@ export const ScheduleProvider = ({ children }: ScheduleProviderProps) => {
}, []);

useEffect(() => {
console.log('scheduleList 변경됨 useSchedule');
console.log('scheduleList 변경됨 useSchedule' + scheduleList);
}, [scheduleList]);

const getSchedules = async (): Promise<any[]> => {
const getSchedules = async () => {
try {
await api.get('/schedules').then((response) => {
if (response.data.length < 0) {
throw new Error(response.statusText);
}
setScheduleList(response.data);
return response.data;
const response = await api.get('/schedules');

if (response.data.length < 0) {
throw new Error(response.statusText);
}

setScheduleList((prev) => {
return JSON.stringify(prev) === JSON.stringify(response.data)
? [...response.data]
: response.data;
});
} catch (e) {
console.error(e);
return [];
}
};

Expand All @@ -41,11 +40,9 @@ export const ScheduleProvider = ({ children }: ScheduleProviderProps) => {
setSchedule('');
e.preventDefault();

await api.post('/schedules', { text: schedule }).then(async () => {
const updatedSchedules = await getSchedules();
if (updatedSchedules) {
setScheduleList(updatedSchedules);
}
await api.post('/schedules', { text: schedule }).then(() => {
getSchedules();
// console.log(updatedSchedules);
});
} catch (e) {
console.error(e);
Expand All @@ -62,25 +59,14 @@ export const ScheduleProvider = ({ children }: ScheduleProviderProps) => {
}
};

return (
// scheduleList,
// schedule,
// setSchedule,
// getSchedules,
// postSchedule,
// deleteSchedule,
<ScheduleContext.Provider
value={{
scheduleList,
getSchedules,
postSchedule,
deleteSchedule,
setSchedule,
}}
>
{children}
</ScheduleContext.Provider>
);
return {
scheduleList,
schedule,
setSchedule,
getSchedules,
postSchedule,
deleteSchedule,
};
};

export const useSchedules = () => useContext(ScheduleContext);
export default useSchedules;

0 comments on commit 90bca1d

Please sign in to comment.