260703_01_FST: First Commit
This commit is contained in:
41
templates/base/src/query/auth.ts
Normal file
41
templates/base/src/query/auth.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
"use client";
|
||||
|
||||
import type { LoginRequest, LoginUser } from "@kri/common";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
|
||||
export function useLoginMutation() {
|
||||
const { setFromLoginResponse } = useAuth();
|
||||
return useMutation({
|
||||
mutationFn: (request: LoginRequest) => nextApi.post<LoginUser>("/login", request, {
|
||||
skipUnauthorizedHandler: true,
|
||||
skipForbiddenHandler: true,
|
||||
skipResponseErrorHandler: true,
|
||||
}),
|
||||
onSuccess: setFromLoginResponse,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLogoutMutation() {
|
||||
const queryClient = useQueryClient();
|
||||
const { auth, logout } = useAuth();
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
const suffix = auth.user?.userId ? `/${encodeURIComponent(auth.user.userId)}` : "";
|
||||
try {
|
||||
await nextApi.post(`/logout${suffix}`, null, {
|
||||
skipUnauthorizedHandler: true,
|
||||
skipForbiddenHandler: true,
|
||||
skipResponseErrorHandler: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
logout();
|
||||
queryClient.clear();
|
||||
},
|
||||
});
|
||||
}
|
||||
56
templates/base/src/query/example.ts
Normal file
56
templates/base/src/query/example.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
"use client";
|
||||
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { queryKeys } from "@/query/key/keys";
|
||||
import type { ExampleItem, ExampleRequest } from "@/types/common";
|
||||
|
||||
export const useExampleQuery = (id: string) => useQuery({
|
||||
queryKey: queryKeys.example.detail(id),
|
||||
queryFn: () => nextApi.get<ExampleItem>(`/example/${id}`),
|
||||
enabled: Boolean(id),
|
||||
});
|
||||
|
||||
export function useCreateExampleMutation() {
|
||||
const client = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: ExampleRequest) => nextApi.post<ExampleItem>("/example", body),
|
||||
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.example.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateExampleMutation(id: string) {
|
||||
const client = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: ExampleRequest) => nextApi.put<ExampleItem>(`/example/${id}`, body),
|
||||
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.example.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function usePatchExampleMutation(id: string) {
|
||||
const client = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: Partial<ExampleRequest>) => nextApi.patch<ExampleItem>(`/example/${id}`, body),
|
||||
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.example.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteExampleMutation() {
|
||||
const client = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => nextApi.del<void>(`/example/${id}`),
|
||||
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.example.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUploadExampleMutation(id: string) {
|
||||
return useMutation({
|
||||
mutationFn: (formData: FormData) => nextApi.postForm<ExampleItem>(`/example/${id}`, formData),
|
||||
});
|
||||
}
|
||||
|
||||
export function useReplaceExampleFileMutation(id: string) {
|
||||
return useMutation({
|
||||
mutationFn: (formData: FormData) => nextApi.putForm<ExampleItem>(`/example/${id}`, formData),
|
||||
});
|
||||
}
|
||||
6
templates/base/src/query/key/keys.ts
Normal file
6
templates/base/src/query/key/keys.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export const queryKeys = {
|
||||
example: {
|
||||
all: ["example"] as const,
|
||||
detail: (id: string) => ["example", id] as const,
|
||||
},
|
||||
} as const;
|
||||
Reference in New Issue
Block a user