Spaces:
Sleeping
Sleeping
File size: 2,318 Bytes
f305a41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import { ApplicationStatus, WorkflowStage } from "@/data/dummyData";
const LOCAL_KEY = "fo_workflow_overrides";
export interface FoWorkflowOverride {
currentStage?: WorkflowStage;
status?: ApplicationStatus;
assignedFOI?: string;
assignedFO?: string;
foVisitNotes?: string;
}
export type EntityType = "hatchery" | "farm";
interface OverridesMap {
[compositeId: string]: FoWorkflowOverride;
}
function getKey(entityType: EntityType, id: string) {
return `${entityType}:${id}`;
}
function readOverrides(): OverridesMap {
if (typeof window === "undefined") return {};
try {
const raw = localStorage.getItem(LOCAL_KEY);
if (!raw) return {};
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object") return {};
return parsed as OverridesMap;
} catch {
return {};
}
}
function writeOverrides(map: OverridesMap) {
if (typeof window === "undefined") return;
try {
localStorage.setItem(LOCAL_KEY, JSON.stringify(map));
} catch {
// ignore
}
}
export function updateFoWorkflowOverride(
entityType: EntityType,
id: string,
patch: FoWorkflowOverride,
): FoWorkflowOverride {
const map = readOverrides();
const key = getKey(entityType, id);
const existing = map[key] || {};
const merged: FoWorkflowOverride = { ...existing, ...patch };
map[key] = merged;
writeOverrides(map);
return merged;
}
export function getFoWorkflowOverride(
entityType: EntityType,
id: string,
): FoWorkflowOverride | undefined {
const map = readOverrides();
return map[getKey(entityType, id)];
}
interface HasFoFields {
id: string;
entityType: EntityType;
currentStage: string;
status: string;
assignedFOI?: string;
assignedFO?: string;
}
export function applyFoWorkflowOverrides<T extends HasFoFields>(rows: T[]): T[] {
const map = readOverrides();
return rows.map((row) => {
const override = map[getKey(row.entityType, row.id)];
if (!override) return row;
return {
...row,
currentStage: override.currentStage ?? row.currentStage,
status: override.status ?? row.status,
assignedFOI: override.assignedFOI ?? row.assignedFOI,
assignedFO: override.assignedFO ?? row.assignedFO,
};
});
}
|