- Implemented CatalogTemplateForm for creating and managing asset templates. - Added inventory management components including AssetInventoryPage and related forms. - Introduced internationalization support for asset and inventory components in English and Spanish. - Created tests for catalog and inventory functionalities to ensure proper rendering and behavior. - Established API interactions for inventory management and asset catalog retrieval. - Enhanced user experience with form dialogs for quick creation of sites and persons within company inventory.
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import { z } from "zod/v4";
|
|
|
|
import type { AssetInput } from "../common/inventory";
|
|
import { AssetResponseSchema, InventoryResponseSchema } from "../common/inventory";
|
|
export const inventoryKeys = { company: (id: string) => ["inventory", id] };
|
|
export class InventoryApiError extends Error {
|
|
constructor(public readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
async function request(path: string, body?: unknown, csrfToken?: string) {
|
|
const response = await fetch(`/api/assets/${path}`, {
|
|
method: body === undefined ? "GET" : "POST",
|
|
credentials: "same-origin",
|
|
headers:
|
|
body === undefined
|
|
? {}
|
|
: { "content-type": "application/json", "x-csrf-token": csrfToken ?? "" },
|
|
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
});
|
|
const data: unknown = await response.json();
|
|
if (!response.ok) {
|
|
const error = z.object({ code: z.string() }).safeParse(data);
|
|
throw new InventoryApiError(error.success ? error.data.code : "INTERNAL_ERROR");
|
|
}
|
|
return data;
|
|
}
|
|
export const inventoryApi = {
|
|
async get(companyId: string) {
|
|
return InventoryResponseSchema.parse(await request(companyId));
|
|
},
|
|
async save(companyId: string, values: AssetInput, token: string, id?: string) {
|
|
return AssetResponseSchema.parse(
|
|
await request(`${companyId}${id ? `/${id}/update` : ""}`, { values }, token)
|
|
);
|
|
},
|
|
};
|