2026-08-04 09:53:48 -03:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useDeleteVendor } from "@/domain/vendors/use-cases/use-delete-vendor";
|
|
|
|
|
import { useVendorDeactivationImpact } from "@/domain/vendors/use-cases/use-vendor-deactivation-impact";
|
|
|
|
|
import type { VendorDeactivationImpact, VendorListItem } from "@/domain/vendors/types/vendor";
|
|
|
|
|
|
|
|
|
|
export interface VendorDeactivationState {
|
|
|
|
|
target: VendorListItem | null;
|
|
|
|
|
error: string | null;
|
|
|
|
|
impact: VendorDeactivationImpact | undefined;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
impactError: Error | null;
|
|
|
|
|
isSaving: boolean;
|
|
|
|
|
open: (row: VendorListItem) => void;
|
|
|
|
|
close: () => void;
|
|
|
|
|
confirm: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 22:39:11 -03:00
|
|
|
export function useVendorDeactivation(onSuccess?: () => void): VendorDeactivationState {
|
2026-08-04 09:53:48 -03:00
|
|
|
const [target, setTarget] = useState<VendorListItem | null>(null);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const deleteVendor = useDeleteVendor();
|
|
|
|
|
const {
|
|
|
|
|
data: impact,
|
|
|
|
|
isLoading,
|
|
|
|
|
error: impactError,
|
|
|
|
|
} = useVendorDeactivationImpact(target?.id ?? undefined);
|
|
|
|
|
|
|
|
|
|
const open = (row: VendorListItem) => {
|
|
|
|
|
setTarget(row);
|
|
|
|
|
setError(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
setTarget(null);
|
|
|
|
|
setError(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const confirm = () => {
|
|
|
|
|
if (!target || target.id == null) return;
|
|
|
|
|
setError(null);
|
2026-08-19 13:36:37 -03:00
|
|
|
// The dialog has shown whatever open work orders exist, so confirming here is
|
|
|
|
|
// the explicit confirmation the API requires to deactivate past them (SH-254).
|
|
|
|
|
deleteVendor.mutate(
|
|
|
|
|
{ id: target.id, confirmOpenWorkOrders: (impact?.openWorkOrders.length ?? 0) > 0 },
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
setTarget(null);
|
|
|
|
|
onSuccess?.();
|
|
|
|
|
},
|
|
|
|
|
onError: (err: Error) => setError(err.message || "Failed to deactivate vendor"),
|
2026-08-06 22:39:11 -03:00
|
|
|
},
|
2026-08-19 13:36:37 -03:00
|
|
|
);
|
2026-08-04 09:53:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
target,
|
|
|
|
|
error,
|
|
|
|
|
impact,
|
|
|
|
|
isLoading,
|
|
|
|
|
impactError,
|
|
|
|
|
isSaving: deleteVendor.isPending,
|
|
|
|
|
open,
|
|
|
|
|
close,
|
|
|
|
|
confirm,
|
|
|
|
|
};
|
|
|
|
|
}
|