feat: deal updating on the server
This commit is contained in:
@ -3,11 +3,14 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import StatusColumnsDnd from "@/app/deals/components/StatusColumnsDnd/StatusColumnsDnd";
|
||||
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
|
||||
import { updateStatusMutation } from "@/client/@tanstack/react-query.gen";
|
||||
import {
|
||||
updateDealMutation,
|
||||
updateStatusMutation,
|
||||
} from "@/client/@tanstack/react-query.gen";
|
||||
import { notifications } from "@/lib/notifications";
|
||||
|
||||
const StatusColumns = () => {
|
||||
const { refetchStatuses } = useStatusesContext();
|
||||
const { refetchStatuses, refetchDeals } = useStatusesContext();
|
||||
|
||||
const updateStatus = useMutation({
|
||||
...updateStatusMutation(),
|
||||
@ -20,30 +23,36 @@ const StatusColumns = () => {
|
||||
},
|
||||
});
|
||||
|
||||
const updateDeals = useMutation({
|
||||
...updateDealMutation(),
|
||||
onError: error => {
|
||||
console.error(error);
|
||||
notifications.error({
|
||||
message: error.response?.data?.detail as string | undefined,
|
||||
});
|
||||
refetchDeals();
|
||||
},
|
||||
});
|
||||
|
||||
const onDealDragEnd = (
|
||||
dealId: number,
|
||||
statusId: number,
|
||||
lexorank?: string
|
||||
) => {
|
||||
// Send request to server
|
||||
console.log(
|
||||
"onDealDragEnd. dealId =",
|
||||
updateDeals.mutate({
|
||||
path: {
|
||||
dealId,
|
||||
", statusId =",
|
||||
},
|
||||
body: {
|
||||
deal: {
|
||||
statusId,
|
||||
", lexorank =",
|
||||
lexorank
|
||||
);
|
||||
lexorank,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const onStatusDragEnd = (statusId: number, lexorank: string) => {
|
||||
console.log(
|
||||
"onStatusDragEnd, statusId:",
|
||||
statusId,
|
||||
", lexo:",
|
||||
lexorank
|
||||
);
|
||||
|
||||
updateStatus.mutate({
|
||||
path: {
|
||||
statusId,
|
||||
|
||||
@ -12,6 +12,7 @@ type StatusesContextState = {
|
||||
deals: DealSchema[];
|
||||
setDeals: React.Dispatch<React.SetStateAction<DealSchema[]>>;
|
||||
refetchStatuses: () => void;
|
||||
refetchDeals: () => void;
|
||||
};
|
||||
|
||||
const StatusesContext = createContext<StatusesContextState | undefined>(
|
||||
@ -20,13 +21,22 @@ const StatusesContext = createContext<StatusesContextState | undefined>(
|
||||
|
||||
const useStatusesContextState = () => {
|
||||
const { selectedBoard } = useBoardsContext();
|
||||
const { statuses, setStatuses, refetch } = useStatusesList({
|
||||
const {
|
||||
statuses,
|
||||
setStatuses,
|
||||
refetch: refetchStatuses,
|
||||
} = useStatusesList({
|
||||
boardId: selectedBoard?.id,
|
||||
});
|
||||
const { deals, setDeals } = useDealsList({ boardId: selectedBoard?.id });
|
||||
|
||||
const {
|
||||
deals,
|
||||
setDeals,
|
||||
refetch: refetchDeals,
|
||||
} = useDealsList({ boardId: selectedBoard?.id });
|
||||
|
||||
useEffect(() => {
|
||||
refetch();
|
||||
refetchStatuses();
|
||||
}, [selectedBoard]);
|
||||
|
||||
return {
|
||||
@ -34,7 +44,8 @@ const useStatusesContextState = () => {
|
||||
setStatuses,
|
||||
deals,
|
||||
setDeals,
|
||||
refetchStatuses: refetch,
|
||||
refetchStatuses,
|
||||
refetchDeals,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ import {
|
||||
getProjects,
|
||||
getStatuses,
|
||||
updateBoard,
|
||||
updateDeal,
|
||||
updateStatus,
|
||||
type Options,
|
||||
} from "../sdk.gen";
|
||||
@ -20,6 +21,9 @@ import type {
|
||||
UpdateBoardData,
|
||||
UpdateBoardError,
|
||||
UpdateBoardResponse2,
|
||||
UpdateDealData,
|
||||
UpdateDealError,
|
||||
UpdateDealResponse2,
|
||||
UpdateStatusData,
|
||||
UpdateStatusError,
|
||||
UpdateStatusResponse2,
|
||||
@ -198,3 +202,30 @@ export const getDealsOptions = (options: Options<GetDealsData>) => {
|
||||
queryKey: getDealsQueryKey(options),
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Update Deal
|
||||
*/
|
||||
export const updateDealMutation = (
|
||||
options?: Partial<Options<UpdateDealData>>
|
||||
): UseMutationOptions<
|
||||
UpdateDealResponse2,
|
||||
AxiosError<UpdateDealError>,
|
||||
Options<UpdateDealData>
|
||||
> => {
|
||||
const mutationOptions: UseMutationOptions<
|
||||
UpdateDealResponse2,
|
||||
AxiosError<UpdateDealError>,
|
||||
Options<UpdateDealData>
|
||||
> = {
|
||||
mutationFn: async localOptions => {
|
||||
const { data } = await updateDeal({
|
||||
...options,
|
||||
...localOptions,
|
||||
throwOnError: true,
|
||||
});
|
||||
return data;
|
||||
},
|
||||
};
|
||||
return mutationOptions;
|
||||
};
|
||||
|
||||
@ -17,6 +17,9 @@ import type {
|
||||
UpdateBoardData,
|
||||
UpdateBoardErrors,
|
||||
UpdateBoardResponses,
|
||||
UpdateDealData,
|
||||
UpdateDealErrors,
|
||||
UpdateDealResponses,
|
||||
UpdateStatusData,
|
||||
UpdateStatusErrors,
|
||||
UpdateStatusResponses,
|
||||
@ -148,3 +151,24 @@ export const getDeals = <ThrowOnError extends boolean = false>(
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Update Deal
|
||||
*/
|
||||
export const updateDeal = <ThrowOnError extends boolean = false>(
|
||||
options: Options<UpdateDealData, ThrowOnError>
|
||||
) => {
|
||||
return (options.client ?? _heyApiClient).patch<
|
||||
UpdateDealResponses,
|
||||
UpdateDealErrors,
|
||||
ThrowOnError
|
||||
>({
|
||||
responseType: "json",
|
||||
url: "/deal/{dealId}",
|
||||
...options,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -153,6 +153,41 @@ export type UpdateBoardSchema = {
|
||||
lexorank?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* UpdateDealRequest
|
||||
*/
|
||||
export type UpdateDealRequest = {
|
||||
deal: UpdateDealSchema;
|
||||
};
|
||||
|
||||
/**
|
||||
* UpdateDealResponse
|
||||
*/
|
||||
export type UpdateDealResponse = {
|
||||
/**
|
||||
* Message
|
||||
*/
|
||||
message: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* UpdateDealSchema
|
||||
*/
|
||||
export type UpdateDealSchema = {
|
||||
/**
|
||||
* Name
|
||||
*/
|
||||
name?: string | null;
|
||||
/**
|
||||
* Lexorank
|
||||
*/
|
||||
lexorank?: string | null;
|
||||
/**
|
||||
* Statusid
|
||||
*/
|
||||
statusId?: number | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* UpdateStatusRequest
|
||||
*/
|
||||
@ -372,6 +407,37 @@ export type GetDealsResponses = {
|
||||
|
||||
export type GetDealsResponse2 = GetDealsResponses[keyof GetDealsResponses];
|
||||
|
||||
export type UpdateDealData = {
|
||||
body: UpdateDealRequest;
|
||||
path: {
|
||||
/**
|
||||
* Dealid
|
||||
*/
|
||||
dealId: number;
|
||||
};
|
||||
query?: never;
|
||||
url: "/deal/{dealId}";
|
||||
};
|
||||
|
||||
export type UpdateDealErrors = {
|
||||
/**
|
||||
* Validation Error
|
||||
*/
|
||||
422: HttpValidationError;
|
||||
};
|
||||
|
||||
export type UpdateDealError = UpdateDealErrors[keyof UpdateDealErrors];
|
||||
|
||||
export type UpdateDealResponses = {
|
||||
/**
|
||||
* Successful Response
|
||||
*/
|
||||
200: UpdateDealResponse;
|
||||
};
|
||||
|
||||
export type UpdateDealResponse2 =
|
||||
UpdateDealResponses[keyof UpdateDealResponses];
|
||||
|
||||
export type ClientOptions = {
|
||||
baseURL: "http://localhost:8000" | (string & {});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user