260303_01_ENH: TanStack Query 적용
This commit is contained in:
90
src/query/message.ts
Normal file
90
src/query/message.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { qk } from "@/query/keys";
|
||||
import type { ChannelType, Template, TemplateSummary } from "@/types/messageTemplate";
|
||||
import type { ProjectMainTemplateUpdateRequest } from "@/types/project";
|
||||
|
||||
export function useTemplateListQuery(chnnlTy: ChannelType, itrntPrjctCd: string, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.templateList(chnnlTy, itrntPrjctCd),
|
||||
queryFn: () => nextApi.get<TemplateSummary[]>(`/message-template/list/${chnnlTy}/${itrntPrjctCd}`),
|
||||
enabled: enabled && !!itrntPrjctCd,
|
||||
staleTime: 30_000,
|
||||
meta: { ignoreGlobalLoading: true },
|
||||
});
|
||||
}
|
||||
|
||||
export function useMainTemplateIdxQuery(prjctIdx: number, chnnlTy: ChannelType, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.mainTemplate(prjctIdx, chnnlTy),
|
||||
queryFn: () => nextApi.get<number | null>(`/project/main-template/${prjctIdx}/${chnnlTy}`),
|
||||
enabled: enabled && prjctIdx > 0,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
}
|
||||
|
||||
export function useTemplateQuery(tmplatIdx: number, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.template(tmplatIdx),
|
||||
queryFn: () => nextApi.get<Template>(`/message-template/${tmplatIdx}`),
|
||||
enabled: enabled && tmplatIdx > 0,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
// meta: { ignoreGlobalLoading: true },
|
||||
});
|
||||
}
|
||||
|
||||
export function useSaveTemplateMutation(chnnlTy: ChannelType, itrntPrjctCd: string) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (v: { method: "put" | "patch"; template: Template }) => {
|
||||
const { method, template } = v;
|
||||
return method === "put"
|
||||
? nextApi.put<Template>(`/message-template`, template)
|
||||
: nextApi.patch<Template>(`/message-template`, template);
|
||||
},
|
||||
onSuccess: (res) => {
|
||||
if (res.tmplatIdx) {
|
||||
qc.setQueryData(qk.template(res.tmplatIdx), res);
|
||||
}
|
||||
qc.invalidateQueries({ queryKey: qk.templateList(chnnlTy, itrntPrjctCd) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateMainTemplateMutation(prjctIdx: number, chnnlTy: ChannelType) {
|
||||
const qc = useQueryClient();
|
||||
const key = qk.mainTemplate(prjctIdx, chnnlTy);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (req: ProjectMainTemplateUpdateRequest) => {
|
||||
await nextApi.patch(`/project/main-template`, req);
|
||||
return req.tmplatIdx ?? null;
|
||||
},
|
||||
onMutate: async (req) => {
|
||||
const prev = qc.getQueryData<number | null>(key) ?? null;
|
||||
qc.setQueryData<number | null>(key, req.tmplatIdx ?? null);
|
||||
return { prev };
|
||||
},
|
||||
onError: (_err, _req, ctx) => {
|
||||
if (ctx?.prev !== undefined) qc.setQueryData<number | null>(key, ctx.prev);
|
||||
},
|
||||
onSettled: () => {
|
||||
qc.invalidateQueries({ queryKey: key });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUploadTemplateBodyImageMutation(itrntPrjctCd: string) {
|
||||
return useMutation({
|
||||
mutationFn: async (file: File): Promise<string> => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
return nextApi.putForm<string>(`/message-template/body-image/${itrntPrjctCd}`, formData);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user