dstpabuaran.com/resources/js/lib/product-draft.ts
Yoga Pangestu 384460686e feat: implement raw material management features including CRUD operations and draft handling
- Added RawMaterialController and RawMaterialVariantController for managing raw materials and their variants.
- Introduced RawMaterialRequest and RawMaterialVariantRequest for validation.
- Implemented RawMaterialService and RawMaterialVariantService for business logic.
- Created hooks for saving drafts of raw materials.
- Developed UI components for creating, editing, and listing raw materials and variants.
2026-08-02 00:54:05 +07:00

85 lines
1.8 KiB
TypeScript

const DRAFT_PREFIX = 'product-draft';
export type ProductDraftData = {
productName: string;
status: string;
description: string;
categoryIds: number[];
useSamePrice: boolean;
sharedPrices: Array<{ type: string; price: number }>;
variants: Array<{
id?: number | null;
name: string;
stock: number;
reject_stock: number;
retail_stock: number;
photos?: Array<{ key: string }>;
photo?: string;
prices: Array<{ type: string; price: number }>;
}>;
};
function getKey(
type: 'create' | 'edit',
userId?: number,
productId?: number,
): string {
if (type === 'edit' && productId) {
return `${DRAFT_PREFIX}-edit-${userId ?? 'anon'}-${productId}`;
}
return `${DRAFT_PREFIX}-create-${userId ?? 'anon'}`;
}
export function saveProductDraft(
type: 'create' | 'edit',
data: ProductDraftData,
userId?: number,
productId?: number,
): boolean {
if (type !== 'create') {
return false;
}
try {
const key = getKey(type, userId, productId);
localStorage.setItem(key, JSON.stringify(data));
return true;
} catch {
return false;
}
}
export function loadProductDraft(
type: 'create' | 'edit',
userId?: number,
productId?: number,
): ProductDraftData | null {
try {
const key = getKey(type, userId, productId);
const raw = localStorage.getItem(key);
if (!raw) {
return null;
}
return JSON.parse(raw) as ProductDraftData;
} catch {
return null;
}
}
export function clearProductDraft(
type: 'create' | 'edit',
userId?: number,
productId?: number,
): void {
try {
const key = getKey(type, userId, productId);
localStorage.removeItem(key);
} catch {
// ignore
}
}