diff --git a/src/app/deals/components/StatusColumns/StatusColumns.tsx b/src/app/deals/components/StatusColumns/StatusColumns.tsx index bd3f6a3..87ccf98 100644 --- a/src/app/deals/components/StatusColumns/StatusColumns.tsx +++ b/src/app/deals/components/StatusColumns/StatusColumns.tsx @@ -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 =", - dealId, - ", statusId =", - statusId, - ", lexorank =", - lexorank - ); + updateDeals.mutate({ + path: { + dealId, + }, + body: { + deal: { + statusId, + lexorank, + }, + }, + }); }; const onStatusDragEnd = (statusId: number, lexorank: string) => { - console.log( - "onStatusDragEnd, statusId:", - statusId, - ", lexo:", - lexorank - ); - updateStatus.mutate({ path: { statusId, diff --git a/src/app/deals/contexts/StatusesContext.tsx b/src/app/deals/contexts/StatusesContext.tsx index 8ef3802..9e83488 100644 --- a/src/app/deals/contexts/StatusesContext.tsx +++ b/src/app/deals/contexts/StatusesContext.tsx @@ -12,6 +12,7 @@ type StatusesContextState = { deals: DealSchema[]; setDeals: React.Dispatch>; refetchStatuses: () => void; + refetchDeals: () => void; }; const StatusesContext = createContext( @@ -20,13 +21,22 @@ const StatusesContext = createContext( 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, }; }; diff --git a/src/client/@tanstack/react-query.gen.ts b/src/client/@tanstack/react-query.gen.ts index 99840d2..6ebf648 100644 --- a/src/client/@tanstack/react-query.gen.ts +++ b/src/client/@tanstack/react-query.gen.ts @@ -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) => { queryKey: getDealsQueryKey(options), }); }; + +/** + * Update Deal + */ +export const updateDealMutation = ( + options?: Partial> +): UseMutationOptions< + UpdateDealResponse2, + AxiosError, + Options +> => { + const mutationOptions: UseMutationOptions< + UpdateDealResponse2, + AxiosError, + Options + > = { + mutationFn: async localOptions => { + const { data } = await updateDeal({ + ...options, + ...localOptions, + throwOnError: true, + }); + return data; + }, + }; + return mutationOptions; +}; diff --git a/src/client/sdk.gen.ts b/src/client/sdk.gen.ts index 902c094..44e6b35 100644 --- a/src/client/sdk.gen.ts +++ b/src/client/sdk.gen.ts @@ -17,6 +17,9 @@ import type { UpdateBoardData, UpdateBoardErrors, UpdateBoardResponses, + UpdateDealData, + UpdateDealErrors, + UpdateDealResponses, UpdateStatusData, UpdateStatusErrors, UpdateStatusResponses, @@ -148,3 +151,24 @@ export const getDeals = ( ...options, }); }; + +/** + * Update Deal + */ +export const updateDeal = ( + options: Options +) => { + return (options.client ?? _heyApiClient).patch< + UpdateDealResponses, + UpdateDealErrors, + ThrowOnError + >({ + responseType: "json", + url: "/deal/{dealId}", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); +}; diff --git a/src/client/types.gen.ts b/src/client/types.gen.ts index 23742e6..0b980c7 100644 --- a/src/client/types.gen.ts +++ b/src/client/types.gen.ts @@ -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 & {}); };