60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import {useMutation, useQueries, useQuery} from "@tanstack/react-query";
|
|
import {nextApi} from "@/lib/http-client";
|
|
import {BulkSendResponse, SaveTemplateRequest, SendSmsBulkRequest, Template} from "@/types/notification";
|
|
|
|
export const SMS_TEMPLATE_IDS = {
|
|
ACCOUNT_MONITORING: 45,
|
|
ASSIGN_CONFIRM: 46,
|
|
CUSTOM: 47,
|
|
INFORM_ASSIGN: 48,
|
|
REMIND_ASSIGN: 49,
|
|
} as const;
|
|
|
|
export const NOTIFICATION_REQUEST_SERVICE = "gwcf-review";
|
|
|
|
export function useNotificationTemplateQuery(templateIdx: number, enabled = true) {
|
|
return useQuery({
|
|
queryKey: ["notification", "template", templateIdx],
|
|
queryFn: () => nextApi.get<Template>(`/notification/template/${templateIdx}`),
|
|
enabled: enabled && templateIdx > 0,
|
|
});
|
|
}
|
|
|
|
export function useNotificationTemplatesQuery(templateIdxList: number[], enabled = true) {
|
|
return useQueries({
|
|
queries: templateIdxList.map((templateIdx) => ({
|
|
queryKey: ["notification", "template", templateIdx],
|
|
queryFn: () => nextApi.get<Template>(`/notification/template/${templateIdx}`),
|
|
enabled: enabled && templateIdx > 0,
|
|
})),
|
|
combine: (results) => ({
|
|
data: results.flatMap((result) => result.data ? [result.data] : []),
|
|
isLoading: results.some((result) => result.isLoading),
|
|
isError: results.some((result) => result.isError),
|
|
}),
|
|
});
|
|
}
|
|
|
|
export function useSaveNotificationTemplateMutation() {
|
|
return useMutation({
|
|
mutationFn: (data: SaveTemplateRequest) => {
|
|
const formData = new FormData();
|
|
formData.append(
|
|
"template",
|
|
new Blob([JSON.stringify(data)], {type: "application/json"})
|
|
);
|
|
|
|
return nextApi.patchForm<Template>("/notification/template", formData);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSendSmsBulkMutation() {
|
|
return useMutation({
|
|
mutationFn: (data: SendSmsBulkRequest) =>
|
|
nextApi.post<BulkSendResponse>("/notification/sms-bulk", data),
|
|
});
|
|
}
|