mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-02 03:53:13 +00:00
91 lines
2.4 KiB
TypeScript
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 });
|
|
}
|