refactor: enforce frontend view model boundaries
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { apiClient, ApiErrorCode, apiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
import {
|
||||
apiClient,
|
||||
ApiErrorCode,
|
||||
apiErrorCode,
|
||||
isApiErrorCode,
|
||||
} from "@/shared/api";
|
||||
|
||||
import type {
|
||||
CreateBankCard,
|
||||
@@ -17,21 +22,27 @@ async function mutationData<T>(
|
||||
const { data, error } = await request;
|
||||
// eslint-disable-next-line @typescript-eslint/only-throw-error -- Preserve generated ProblemDetails for apiErrorCode and field-error consumers.
|
||||
if (error) throw error;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function useBankMutations() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const invalidate = () =>
|
||||
queryClient.invalidateQueries({
|
||||
predicate: ({ queryKey }) => {
|
||||
const [method, path] = queryKey;
|
||||
return method === "get"
|
||||
|
||||
return (
|
||||
method === "get"
|
||||
&& typeof path === "string"
|
||||
&& (path === "/api/v1/bank-cards"
|
||||
|| path.startsWith("/api/v1/bank-cards/"));
|
||||
|| path.startsWith("/api/v1/bank-cards/"))
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const options = {
|
||||
onError: (error: unknown) => {
|
||||
const errorCode = apiErrorCode(error);
|
||||
@@ -49,11 +60,13 @@ function useBankMutations() {
|
||||
onSuccess: invalidate,
|
||||
retry: false,
|
||||
} as const;
|
||||
|
||||
const cardCreation = useMutation({
|
||||
...options,
|
||||
mutationFn: (body: CreateBankCard) =>
|
||||
mutationData(apiClient.POST("/api/v1/bank-cards", { body })),
|
||||
});
|
||||
|
||||
const updateCard = useMutation({
|
||||
...options,
|
||||
mutationFn: ({ body, id }: { body: UpdateBankCard; id: string }) =>
|
||||
@@ -64,11 +77,13 @@ function useBankMutations() {
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const operationCreation = useMutation({
|
||||
...options,
|
||||
mutationFn: (body: CreateBankOperation) =>
|
||||
mutationData(apiClient.POST("/api/v1/bank-cards/operations", { body })),
|
||||
});
|
||||
|
||||
const updateOperation = useMutation({
|
||||
...options,
|
||||
mutationFn: ({ body, id }: { body: UpdateBankOperation; id: string }) =>
|
||||
@@ -79,6 +94,7 @@ function useBankMutations() {
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const operationDeletion = useMutation({
|
||||
...options,
|
||||
mutationFn: ({ id, version }: { id: string; version: number }) =>
|
||||
@@ -88,11 +104,13 @@ function useBankMutations() {
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const categoryCreation = useMutation({
|
||||
...options,
|
||||
mutationFn: (body: CreateExpenseCategory) =>
|
||||
mutationData(apiClient.POST("/api/v1/bank-cards/categories", { body })),
|
||||
});
|
||||
|
||||
const updateCategory = useMutation({
|
||||
...options,
|
||||
mutationFn: ({ body, id }: { body: UpdateExpenseCategory; id: string }) =>
|
||||
@@ -103,6 +121,7 @@ function useBankMutations() {
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
return {
|
||||
createCard: cardCreation,
|
||||
createCategory: categoryCreation,
|
||||
|
||||
Reference in New Issue
Block a user