feat: creating and updating groups
This commit is contained in:
@ -1,19 +1,54 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { UpdateDealGroupSchema } from "@/lib/client";
|
||||
import React from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { AxiosError } from "axios";
|
||||
import { Text } from "@mantine/core";
|
||||
import { modals } from "@mantine/modals";
|
||||
import { HttpValidationError, UpdateDealGroupSchema } from "@/lib/client";
|
||||
import {
|
||||
addDealMutation,
|
||||
createDealGroupMutation,
|
||||
removeDealMutation,
|
||||
deleteDealGroupMutation,
|
||||
updateDealGroupMutation,
|
||||
updateDealsInGroupMutation,
|
||||
} from "@/lib/client/@tanstack/react-query.gen";
|
||||
import { notifications } from "@/lib/notifications";
|
||||
|
||||
export type GroupsCrud = {
|
||||
onUpdate: (groupId: number, group: UpdateDealGroupSchema) => void;
|
||||
// onDelete: (group: DealGroupSchema, onSuccess?: () => void) => void;
|
||||
onCreate: (mainDealId: number, otherDealIds: number[]) => void;
|
||||
onUpdateDealsInGroup: (groupId: number, dealIds: number[]) => void;
|
||||
onDelete: (groupId: number) => void;
|
||||
};
|
||||
|
||||
const useDealGroupCrud = (): GroupsCrud => {
|
||||
const updateMutation = useMutation(updateDealGroupMutation());
|
||||
const queryClient = useQueryClient();
|
||||
const key = "getDeals";
|
||||
|
||||
const onError = (
|
||||
error: AxiosError<HttpValidationError>,
|
||||
_: any,
|
||||
context: any
|
||||
) => {
|
||||
console.error(error);
|
||||
notifications.error({
|
||||
message: error.response?.data?.detail as string | undefined,
|
||||
});
|
||||
if (context?.previous) {
|
||||
queryClient.setQueryData([key], context.previous);
|
||||
}
|
||||
};
|
||||
|
||||
const onSettled = () => {
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query: { queryKey: any }) =>
|
||||
query.queryKey[0]?._id === key,
|
||||
});
|
||||
};
|
||||
|
||||
const updateMutation = useMutation({
|
||||
...updateDealGroupMutation(),
|
||||
onSettled,
|
||||
onError,
|
||||
});
|
||||
|
||||
const onUpdate = (groupId: number, entity: UpdateDealGroupSchema) => {
|
||||
updateMutation.mutate({
|
||||
@ -26,43 +61,64 @@ const useDealGroupCrud = (): GroupsCrud => {
|
||||
});
|
||||
};
|
||||
|
||||
const createMutation = useMutation(createDealGroupMutation());
|
||||
const createMutation = useMutation({
|
||||
...createDealGroupMutation(),
|
||||
onSettled,
|
||||
onError,
|
||||
});
|
||||
|
||||
const onCreate = (draggingDealId: number, hoveredDealId: number) => {
|
||||
const onCreate = (mainDealId: number, otherDealIds: number[]) => {
|
||||
createMutation.mutate({
|
||||
body: {
|
||||
draggingDealId,
|
||||
hoveredDealId,
|
||||
mainDealId,
|
||||
otherDealIds,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const addDealToGroupMutation = useMutation(addDealMutation());
|
||||
const updateDealsMutation = useMutation({
|
||||
...updateDealsInGroupMutation(),
|
||||
onSettled,
|
||||
onError,
|
||||
});
|
||||
|
||||
const onAddDeal = (dealId: number, groupId: number) => {
|
||||
addDealToGroupMutation.mutate({
|
||||
const onUpdateDealsInGroup = (groupId: number, dealIds: number[]) => {
|
||||
updateDealsMutation.mutate({
|
||||
path: {
|
||||
pk: groupId,
|
||||
},
|
||||
body: {
|
||||
dealId,
|
||||
groupId,
|
||||
dealIds,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const removeDealFromGroupMutation = useMutation(removeDealMutation());
|
||||
const deleteMutation = useMutation({
|
||||
...deleteDealGroupMutation(),
|
||||
onSettled,
|
||||
onError,
|
||||
});
|
||||
|
||||
const onRemoveDeal = (dealId: number) => {
|
||||
removeDealFromGroupMutation.mutate({
|
||||
body: {
|
||||
dealId,
|
||||
const onDelete = (groupId: number) => {
|
||||
modals.openConfirmModal({
|
||||
title: "Удаление группы",
|
||||
children: <Text>Вы уверены, что хотите удалить группу?</Text>,
|
||||
confirmProps: { color: "red" },
|
||||
onConfirm: () => {
|
||||
deleteMutation.mutate({
|
||||
path: {
|
||||
pk: groupId,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
onUpdate,
|
||||
// onCreate,
|
||||
// onAddDeal,
|
||||
// onRemoveDeal,
|
||||
onCreate,
|
||||
onUpdateDealsInGroup,
|
||||
onDelete,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user