Skip to content

Commit

Permalink
chore(service): 캘린더 api 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubinquitous committed Oct 3, 2023
1 parent 7f1a9bd commit 292e750
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/page/calender/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import httpClient from "@/apis/httpClient";
import { ICalender } from "@/interfaces";

export const getCalenderList = async (month: number) => {
const { data } = await httpClient.calender.get({
params: {
year: new Date().getFullYear(),
month,
},
});
return data;
};

export const createCalenderItem = async (calender: ICalender) => {
const { data } = await httpClient.calender.post(calender);
return data;
};

export const deleteCalenderItem = async (id: number) => {
const { data } = await httpClient.calender.deleteById({ params: { id } });
return data;
};
30 changes: 30 additions & 0 deletions src/page/calender/services/mutation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { toast } from "react-toastify";
import useModal from "@/hooks/useModal";
import { KEY } from "@/constants";
import { ICalender } from "@/interfaces";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { createCalenderItem, deleteCalenderItem } from "./api.service";

export const useAddCalenderPlanMutation = () => {
const { closeModal } = useModal();
const queryClient = useQueryClient();

return useMutation((calender: ICalender) => createCalenderItem(calender), {
onSuccess: () => {
toast.success("일정이 추가되었어요!");
queryClient.invalidateQueries([KEY.CALENDER]);
closeModal();
},
});
};

export const useDeleteCalenderPlanMutation = () => {
const queryClient = useQueryClient();

return useMutation((id: number) => deleteCalenderItem(id), {
onSuccess: () => {
toast.success("일정이 삭제되었어요!");
queryClient.invalidateQueries([KEY.CALENDER]);
},
});
};
16 changes: 16 additions & 0 deletions src/page/calender/services/query.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { KEY } from "@/constants";
import { useQuery } from "@tanstack/react-query";
import { getCalenderList } from "./api.service";

interface IUseCalenderListQueryProps {
currentMonth: number;
}

export const useCalenderListQuery = ({
currentMonth,
}: IUseCalenderListQueryProps) => {
const { data, ...queryRest } = useQuery([KEY.CALENDER], async () =>
getCalenderList(currentMonth),
);
return { data, ...queryRest };
};

0 comments on commit 292e750

Please sign in to comment.