import { useQueryClient } from "@tanstack/react-query"; import { Form, useDataProvider, useGetIdentity, useListContext, useRedirect, type GetListResult, } from "ra-core"; import { Create } from "@/components/ds/admin/create"; import { SaveButton } from "@/components/ds/admin/form"; import { FormToolbar } from "@/components/ds/admin/simple-form"; import { Dialog, DialogContent, DialogDescription, DialogTitle, } from "@/components/ds/ui/dialog"; import { VisuallyHidden } from "@/components/ds/ui/visually-hidden"; import type { Deal } from "../types"; import { DealInputs } from "./DealInputs"; export const DealCreate = ({ open }: { open: boolean }) => { const redirect = useRedirect(); const dataProvider = useDataProvider(); const { data: allDeals } = useListContext(); const handleClose = () => { redirect("/deals"); }; const queryClient = useQueryClient(); const onSuccess = async (deal: Deal) => { if (!allDeals) { redirect("/deals"); return; } // increase the index of all deals in the same stage as the new deal // first, get the list of deals in the same stage const deals = allDeals.filter( (d: Deal) => d.stage === deal.stage && d.id !== deal.id, ); // update the actual deals in the database await Promise.all( deals.map(async (oldDeal) => dataProvider.update("deals", { id: oldDeal.id, data: { index: oldDeal.index + 1 }, previousData: oldDeal, }), ), ); // refresh the list of deals in the cache as we used dataProvider.update(), // which does not update the cache const dealsById = deals.reduce( (acc, d) => ({ ...acc, [d.id]: { ...d, index: d.index + 1 }, }), {} as { [key: string]: Deal }, ); const now = Date.now(); queryClient.setQueriesData( { queryKey: ["deals", "getList"] }, (res) => { if (!res) return res; return { ...res, data: res.data.map((d: Deal) => dealsById[d.id] || d), }; }, { updatedAt: now }, ); redirect("/deals"); }; const { identity } = useGetIdentity(); return ( handleClose()}> Create Deal Create a new deal with company, contacts, and deal information
); };