- Updated ProductIndex component to improve category and product filtering with memoization. - Refactored Combobox components for better performance and usability. - Added PurchaseController routes for managing purchases with appropriate permissions. - Created comprehensive tests for purchase management, covering creation, updating, and deletion scenarios. - Ensured proper handling of raw materials and their variants during purchase operations. - Implemented validation for required fields in purchase creation and updates.
79 lines
1.6 KiB
TypeScript
79 lines
1.6 KiB
TypeScript
const DRAFT_PREFIX = 'purchase-draft';
|
|
|
|
export type PurchaseDraftData = {
|
|
name: string;
|
|
unit: string;
|
|
supplierId: string;
|
|
discount: number;
|
|
shippingCost: number;
|
|
notes: string;
|
|
variants: Array<{
|
|
variant: string;
|
|
price: number;
|
|
stock: number;
|
|
photo?: string;
|
|
}>;
|
|
mode?: 'new' | 'existing';
|
|
selectedMaterialName?: string;
|
|
quantities?: Record<string, number>;
|
|
photo?: string;
|
|
};
|
|
|
|
function getKey(type: 'create' | 'edit', userId?: number): string {
|
|
if (type === 'edit') {
|
|
return `${DRAFT_PREFIX}-edit-${userId ?? 'anon'}`;
|
|
}
|
|
|
|
return `${DRAFT_PREFIX}-create-${userId ?? 'anon'}`;
|
|
}
|
|
|
|
export function savePurchaseDraft(
|
|
type: 'create' | 'edit',
|
|
data: PurchaseDraftData,
|
|
userId?: number,
|
|
): boolean {
|
|
if (type !== 'create') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const key = getKey(type, userId);
|
|
localStorage.setItem(key, JSON.stringify(data));
|
|
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function loadPurchaseDraft(
|
|
type: 'create' | 'edit',
|
|
userId?: number,
|
|
): PurchaseDraftData | null {
|
|
try {
|
|
const key = getKey(type, userId);
|
|
const raw = localStorage.getItem(key);
|
|
|
|
if (!raw) {
|
|
return null;
|
|
}
|
|
|
|
return JSON.parse(raw) as PurchaseDraftData;
|
|
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function clearPurchaseDraft(
|
|
type: 'create' | 'edit',
|
|
userId?: number,
|
|
): void {
|
|
try {
|
|
const key = getKey(type, userId);
|
|
localStorage.removeItem(key);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|