Browse Source

fix(dashboard): Fix setting custom fields on draft order

Michael Bromley 3 months ago
parent
commit
321a6fb865

+ 27 - 23
packages/dashboard/src/app/routes/_authenticated/_orders/components/edit-order-table.tsx

@@ -44,7 +44,11 @@ export interface OrderTableProps {
         price?: any;
         priceWithTax?: any;
     }) => void;
-    onAdjustLine: (event: { lineId: string; quantity: number; customFields: Record<string, any> }) => void;
+    onAdjustLine: (event: {
+        lineId: string;
+        quantity: number;
+        customFields: Record<string, any> | undefined;
+    }) => void;
     onRemoveLine: (event: { lineId: string }) => void;
     onSetShippingMethod: (event: { shippingMethodId: string }) => void;
     onApplyCouponCode: (event: { couponCode: string }) => void;
@@ -53,16 +57,16 @@ export interface OrderTableProps {
 }
 
 export function EditOrderTable({
-                                   order,
-                                   eligibleShippingMethods,
-                                   onAddItem,
-                                   onAdjustLine,
-                                   onRemoveLine,
-                                   onSetShippingMethod,
-                                   onApplyCouponCode,
-                                   onRemoveCouponCode,
-                                   displayTotals = true,
-                               }: Readonly<OrderTableProps>) {
+    order,
+    eligibleShippingMethods,
+    onAddItem,
+    onAdjustLine,
+    onRemoveLine,
+    onSetShippingMethod,
+    onApplyCouponCode,
+    onRemoveCouponCode,
+    displayTotals = true,
+}: Readonly<OrderTableProps>) {
     const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
     const currencyCode = order.currencyCode;
     const columns: ColumnDef<OrderLineFragment & { customFields?: Record<string, any> }>[] = [
@@ -104,7 +108,7 @@ export function EditOrderTable({
                                 onAdjustLine({
                                     lineId: row.original.id,
                                     quantity: e.target.valueAsNumber,
-                                    customFields: row.original.customFields ?? {},
+                                    customFields: row.original.customFields,
                                 })
                             }
                         />
@@ -169,9 +173,9 @@ export function EditOrderTable({
                                             {header.isPlaceholder
                                                 ? null
                                                 : flexRender(
-                                                    header.column.columnDef.header,
-                                                    header.getContext(),
-                                                )}
+                                                      header.column.columnDef.header,
+                                                      header.getContext(),
+                                                  )}
                                         </TableHead>
                                     );
                                 })}
@@ -181,14 +185,14 @@ export function EditOrderTable({
                     <TableBody>
                         {table.getRowModel().rows?.length
                             ? table.getRowModel().rows.map(row => (
-                                <TableRow key={row.id} data-state={row.getIsSelected() && 'selected'}>
-                                    {row.getVisibleCells().map(cell => (
-                                        <TableCell key={cell.id}>
-                                            {flexRender(cell.column.columnDef.cell, cell.getContext())}
-                                        </TableCell>
-                                    ))}
-                                </TableRow>
-                            ))
+                                  <TableRow key={row.id} data-state={row.getIsSelected() && 'selected'}>
+                                      {row.getVisibleCells().map(cell => (
+                                          <TableCell key={cell.id}>
+                                              {flexRender(cell.column.columnDef.cell, cell.getContext())}
+                                          </TableCell>
+                                      ))}
+                                  </TableRow>
+                              ))
                             : null}
                         <TableRow>
                             <TableCell colSpan={columns.length} className="h-12">

+ 7 - 1
packages/dashboard/src/app/routes/_authenticated/_orders/orders_.draft.$id.tsx

@@ -86,6 +86,7 @@ function DraftOrderPage() {
     const { mutate: setDraftOrderCustomFields } = useMutation({
         mutationFn: api.mutate(setDraftOrderCustomFieldsDocument),
         onSuccess: (result: ResultOf<typeof setDraftOrderCustomFieldsDocument>) => {
+            toast.success(t`Order custom fields updated`);
             refreshEntity();
         },
     });
@@ -110,6 +111,11 @@ function DraftOrderPage() {
                     break;
             }
         },
+        onError: error => {
+            if ((error as any).extensions?.code === 'ENTITY_NOT_FOUND') {
+                toast.error(t`The variant could not be added. Ensure the parent product is enabled.`);
+            }
+        },
     });
 
     const { mutate: adjustDraftOrderLine } = useMutation({
@@ -377,7 +383,7 @@ function DraftOrderPage() {
                                 onClick={e => {
                                     e.preventDefault();
                                     e.stopPropagation();
-                                    orderCustomFieldsForm.handleSubmit(onSaveCustomFields)();
+                                    onSaveCustomFields(orderCustomFieldsForm.getValues());
                                 }}
                             >
                                 <Trans>Set custom fields</Trans>