shoc-frontend-new/src/domain/work-orders/utils/work-order-feedback-toasts.ts
Arthur Bassi 7e1dcb7a36 fix(work-orders): toast after assigned-to save on the board
Show the save toast only after the board PATCH succeeds.
2026-09-01 10:20:38 -03:00

91 lines
2.4 KiB
TypeScript

import { toast } from "react-toastify";
import type { WorkOrderTablePatch } from "@/domain/work-orders/types/work-order-table-row";
import type { VendorAssignmentFields } from "@/domain/work-orders/utils/vendor-assignment-patch";
import { toVendorTablePatch } from "@/domain/work-orders/utils/vendor-assignment-patch";
type PersistPatchFn = (
id: string | number,
patch: WorkOrderTablePatch,
options?: {
onSuccess?: () => void;
onError?: (error: Error) => void;
},
) => void;
export function workOrderCanceledMessage(woNumber: string) {
return `Work order #${woNumber} canceled.`;
}
export function vendorAssignedMessage() {
return "Vendor assigned";
}
export function workOrderSavedMessage() {
return "Work order saved successfully";
}
export function notifyWorkOrderCanceled(woNumber: string) {
toast.warning(workOrderCanceledMessage(woNumber));
}
export function notifyVendorAssigned() {
toast.success(vendorAssignedMessage());
}
export function notifyWorkOrderSaved() {
toast.success(workOrderSavedMessage());
}
function isAssignedVendorId(vendorId: unknown): boolean {
return vendorId != null && String(vendorId).trim() !== "";
}
export function isVendorAssignmentPatch(patch: WorkOrderTablePatch): boolean {
return (
Object.prototype.hasOwnProperty.call(patch, "vendorId") && isAssignedVendorId(patch.vendorId)
);
}
export function notifySlideOverSaveSuccess(patch: WorkOrderTablePatch) {
if (isVendorAssignmentPatch(patch)) {
notifyVendorAssigned();
return;
}
notifyWorkOrderSaved();
}
export function patchWorkOrderAsCanceled(
patchField: PersistPatchFn,
row: { id: string | number; woNumber: string },
) {
patchField(
row.id,
{ status: "Canceled" },
{
onSuccess: () => notifyWorkOrderCanceled(row.woNumber),
},
);
}
export function patchWorkOrderAsCompleted(patchField: PersistPatchFn, id: string | number) {
patchField(id, { status: "Completed" });
}
export function applyVendorTableSave(
onPatch: PersistPatchFn,
id: string | number,
fields: VendorAssignmentFields,
) {
onPatch(id, toVendorTablePatch(fields), {
onSuccess: isAssignedVendorId(fields.vendorId) ? notifyVendorAssigned : notifyWorkOrderSaved,
});
}
export function applyAssignedToTableSave(
onPatch: PersistPatchFn,
id: string | number,
dispatcherId: string,
dispatcherName: string,
) {
onPatch(id, { dispatcherId, dispatcherName }, { onSuccess: notifyWorkOrderSaved });
}