From e60e2c054ec76d0a617de60f3d50a96474a5647b Mon Sep 17 00:00:00 2001 From: "MSI\\derek" Date: Fri, 14 Mar 2025 17:44:23 +0800 Subject: [PATCH] update --- .../(main)/settings/byProduct/create/page.tsx | 4 +- .../(main)/settings/byProduct/edit/page.tsx | 2 +- src/app/(main)/settings/byProduct/page.tsx | 2 +- .../(main)/settings/material/create/page.tsx | 2 +- .../(main)/settings/material/edit/page.tsx | 4 +- src/app/(main)/settings/material/page.tsx | 4 +- .../(main)/settings/product/create/page.tsx | 2 +- src/app/(main)/settings/product/edit/page.tsx | 2 +- src/app/(main)/settings/product/page.tsx | 2 +- src/app/api/settings/byProduct/actions.ts | 39 ----------- src/app/api/settings/byProduct/index.ts | 40 ----------- .../settings/{material => item}/actions.ts | 22 +++--- .../api/settings/{material => item}/index.ts | 21 +++--- src/app/api/settings/product/actions.ts | 37 ---------- src/app/api/settings/product/index.ts | 40 ----------- src/app/utils/typeEnum.ts | 9 ++- .../ByProductDetails.tsx | 56 +++++++--------- .../CreateItem.tsx} | 66 ++++++++---------- .../CreateItemLoading.tsx} | 4 +- .../CreateItem/CreateItemWrapper.tsx | 53 +++++++++++++++ .../MaterialDetails.tsx | 45 +++++-------- .../NumberInputProps.ts | 0 .../ProductDetails.tsx | 21 +++--- src/components/CreateItem/index.ts | 1 + .../CreateProductMaterialWrapper.tsx | 67 ------------------- src/components/CreateProductMaterial/index.ts | 1 - .../InputDataGrid/InputDataGrid.tsx | 4 +- .../ItemsSearch.tsx} | 34 +++++----- .../ItemsSearchLoading.tsx} | 4 +- .../ItemsSearch/ItemsSearchWrapper.tsx | 26 +++++++ src/components/ItemsSearch/index.ts | 1 + .../ProductMaterialSearchWrapper.tsx | 35 ---------- src/components/ProductMaterialSearch/index.ts | 1 - 33 files changed, 218 insertions(+), 433 deletions(-) delete mode 100644 src/app/api/settings/byProduct/actions.ts delete mode 100644 src/app/api/settings/byProduct/index.ts rename src/app/api/settings/{material => item}/actions.ts (66%) rename src/app/api/settings/{material => item}/index.ts (58%) delete mode 100644 src/app/api/settings/product/actions.ts delete mode 100644 src/app/api/settings/product/index.ts rename src/components/{CreateProductMaterial => CreateItem}/ByProductDetails.tsx (86%) rename src/components/{CreateProductMaterial/CreateProductMaterial.tsx => CreateItem/CreateItem.tsx} (78%) rename src/components/{CreateProductMaterial/CreateProductMaterialLoading.tsx => CreateItem/CreateItemLoading.tsx} (90%) create mode 100644 src/components/CreateItem/CreateItemWrapper.tsx rename src/components/{CreateProductMaterial => CreateItem}/MaterialDetails.tsx (87%) rename src/components/{CreateProductMaterial => CreateItem}/NumberInputProps.ts (100%) rename src/components/{CreateProductMaterial => CreateItem}/ProductDetails.tsx (93%) create mode 100644 src/components/CreateItem/index.ts delete mode 100644 src/components/CreateProductMaterial/CreateProductMaterialWrapper.tsx delete mode 100644 src/components/CreateProductMaterial/index.ts rename src/components/{ProductMaterialSearch/ProductMaterialSearch.tsx => ItemsSearch/ItemsSearch.tsx} (74%) rename src/components/{ProductMaterialSearch/ProductMaterialSearchLoading.tsx => ItemsSearch/ItemsSearchLoading.tsx} (90%) create mode 100644 src/components/ItemsSearch/ItemsSearchWrapper.tsx create mode 100644 src/components/ItemsSearch/index.ts delete mode 100644 src/components/ProductMaterialSearch/ProductMaterialSearchWrapper.tsx delete mode 100644 src/components/ProductMaterialSearch/index.ts diff --git a/src/app/(main)/settings/byProduct/create/page.tsx b/src/app/(main)/settings/byProduct/create/page.tsx index 13106bd..37072c0 100644 --- a/src/app/(main)/settings/byProduct/create/page.tsx +++ b/src/app/(main)/settings/byProduct/create/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import { isString } from "lodash"; @@ -15,7 +15,7 @@ const byProductSetting: React.FC = async ({ searchParams }) => { return ( <> {/* {t("Create Material")} */} - + diff --git a/src/app/(main)/settings/byProduct/edit/page.tsx b/src/app/(main)/settings/byProduct/edit/page.tsx index 13106bd..2768e3d 100644 --- a/src/app/(main)/settings/byProduct/edit/page.tsx +++ b/src/app/(main)/settings/byProduct/edit/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import { isString } from "lodash"; diff --git a/src/app/(main)/settings/byProduct/page.tsx b/src/app/(main)/settings/byProduct/page.tsx index 3dd4601..ab2c01a 100644 --- a/src/app/(main)/settings/byProduct/page.tsx +++ b/src/app/(main)/settings/byProduct/page.tsx @@ -1,5 +1,5 @@ import { TypeEnum } from "@/app/utils/typeEnum"; -import MaterialSearch from "@/components/ProductMaterialSearch"; +import MaterialSearch from "@/components/ItemsSearch"; import { getServerI18n } from "@/i18n"; import Add from "@mui/icons-material/Add"; import Button from "@mui/material/Button"; diff --git a/src/app/(main)/settings/material/create/page.tsx b/src/app/(main)/settings/material/create/page.tsx index 6f9c8fc..94ada52 100644 --- a/src/app/(main)/settings/material/create/page.tsx +++ b/src/app/(main)/settings/material/create/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import { isString } from "lodash"; diff --git a/src/app/(main)/settings/material/edit/page.tsx b/src/app/(main)/settings/material/edit/page.tsx index 41b9bfa..8dc83a2 100644 --- a/src/app/(main)/settings/material/edit/page.tsx +++ b/src/app/(main)/settings/material/edit/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import { isString } from "lodash"; @@ -22,7 +22,7 @@ const materialSetting: React.FC = async ({ searchParams }) => { return ( <> {/* {t("Create Material")} */} - + diff --git a/src/app/(main)/settings/material/page.tsx b/src/app/(main)/settings/material/page.tsx index a74ea18..2f1e8bb 100644 --- a/src/app/(main)/settings/material/page.tsx +++ b/src/app/(main)/settings/material/page.tsx @@ -1,5 +1,5 @@ import { TypeEnum } from "@/app/utils/typeEnum"; -import MaterialSearch from "@/components/ProductMaterialSearch"; +import MaterialSearch from "@/components/ItemsSearch"; import { getServerI18n } from "@/i18n"; import Add from "@mui/icons-material/Add"; import Button from "@mui/material/Button"; @@ -15,7 +15,7 @@ export const metadata: Metadata = { const materialSetting: React.FC = async () => { const material = TypeEnum.MATERIAL - const { t } = await getServerI18n(material); + const { t } = await getServerI18n("material"); // preloadClaims(); return ( diff --git a/src/app/(main)/settings/product/create/page.tsx b/src/app/(main)/settings/product/create/page.tsx index 5081efe..dd6248f 100644 --- a/src/app/(main)/settings/product/create/page.tsx +++ b/src/app/(main)/settings/product/create/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import isString from "lodash/isString"; diff --git a/src/app/(main)/settings/product/edit/page.tsx b/src/app/(main)/settings/product/edit/page.tsx index e112a41..04cd5c1 100644 --- a/src/app/(main)/settings/product/edit/page.tsx +++ b/src/app/(main)/settings/product/edit/page.tsx @@ -1,6 +1,6 @@ import { SearchParams } from "@/app/utils/fetchUtil"; import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "@/components/CreateProductMaterial"; +import CreateProductMaterial from "@/components/CreateItem"; import { I18nProvider, getServerI18n } from "@/i18n"; import { Typography } from "@mui/material"; import isString from "lodash/isString"; diff --git a/src/app/(main)/settings/product/page.tsx b/src/app/(main)/settings/product/page.tsx index 6290413..0fa4c0b 100644 --- a/src/app/(main)/settings/product/page.tsx +++ b/src/app/(main)/settings/product/page.tsx @@ -1,5 +1,5 @@ import { TypeEnum } from "@/app/utils/typeEnum"; -import MaterialSearch from "@/components/ProductMaterialSearch"; +import MaterialSearch from "@/components/ItemsSearch"; import { getServerI18n } from "@/i18n"; import Add from "@mui/icons-material/Add"; import Button from "@mui/material/Button"; diff --git a/src/app/api/settings/byProduct/actions.ts b/src/app/api/settings/byProduct/actions.ts deleted file mode 100644 index a658b84..0000000 --- a/src/app/api/settings/byProduct/actions.ts +++ /dev/null @@ -1,39 +0,0 @@ -"use server"; -import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil"; -import { revalidateTag } from "next/cache"; -import { BASE_API_URL } from "@/config/api"; -import { TypeInputs, UomInputs, WeightUnitInputs } from "../material/actions"; -import { CreateItemResponse } from "../../utils"; - -export type CreateByProductInputs = { - id?: string | number - code: string; - name: string; - // same goes for other props - // change backend for null or not null - description?: string | undefined; - remarks?: string | undefined; - shelfLife?: Number | undefined; - countryOfOrigin?: string | undefined; - minHumid?: number | undefined; - maxHumid?: number | undefined; - minTemp?: number | undefined; - maxTemp?: number | undefined; - sampleRate?: number | undefined; - passingRate?: number | undefined; - netWeight?: number | undefined; - type?: TypeInputs[]; - uom?: UomInputs[]; - weightUnit?: WeightUnitInputs[]; -} - -export const saveByProduct = async (data: CreateByProductInputs) => { - // try { - const byProduct = await serverFetchJson>(`${BASE_API_URL}/byProduct/new`, { - method: "POST", - body: JSON.stringify(data), - headers: { "Content-Type": "application/json" }, - }); - revalidateTag("byProduct"); - return byProduct - }; \ No newline at end of file diff --git a/src/app/api/settings/byProduct/index.ts b/src/app/api/settings/byProduct/index.ts deleted file mode 100644 index fe90bb3..0000000 --- a/src/app/api/settings/byProduct/index.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { cache } from "react"; -import "server-only"; -import { serverFetchJson } from "@/app/utils/fetchUtil"; -import { BASE_API_URL } from "@/config/api"; -import { TypeInputs, UomInputs, WeightUnitInputs } from "../material/actions"; - -export type ByProductResult = { - id: string | number - code: string; - name: string; - description: string | undefined; - remarks: string | undefined; - shelfLife: Number | undefined; - countryOfOrigin: string | undefined; - minHumid: number | undefined; - maxHumid: number | undefined; - minTemp: number | undefined; - maxTemp: number | undefined; - sampleRate: number | undefined; - passingRate: number | undefined; - netWeight: number | undefined; - uom: UomInputs[]; - weightUnit: WeightUnitInputs[]; - type: TypeInputs[]; - action?: any; -} - - -export const fetchAllByProduct = cache(async () => { - return serverFetchJson(`${BASE_API_URL}/byProduct`, { - next: { tags: ["byProduct"] }, - }); - }); - - -export const fetchByProduct = cache(async (id: number) => { - return serverFetchJson(`${BASE_API_URL}/byProduct/details/${id}`, { - next: { tags: ["byProduct"] }, - }); -}); \ No newline at end of file diff --git a/src/app/api/settings/material/actions.ts b/src/app/api/settings/item/actions.ts similarity index 66% rename from src/app/api/settings/material/actions.ts rename to src/app/api/settings/item/actions.ts index a38ec62..467f516 100644 --- a/src/app/api/settings/material/actions.ts +++ b/src/app/api/settings/item/actions.ts @@ -6,7 +6,8 @@ import { HTMLInputTypeAttribute } from "react"; import { CreateItemResponse } from "../../utils"; export type TypeInputs = { - type: string + id: number; + name: string } export type UomInputs = { uom: string @@ -15,13 +16,10 @@ export type WeightUnitInputs = { weightUnit: string conversion: number } -export type CreateMaterialInputs = { +export type CreateItemInputs = { id?: string | number code: string; name: string; - isConsumables: boolean; - // same goes for other props - // change backend for null or not null description?: string | undefined; remarks?: string | undefined; shelfLife?: Number | undefined; @@ -32,19 +30,17 @@ export type CreateMaterialInputs = { maxTemp?: number | undefined; sampleRate?: number | undefined; passingRate?: number | undefined; - netWeight?: number | undefined; - type?: TypeInputs[]; - uom?: UomInputs[]; - weightUnit?: WeightUnitInputs[]; + netWeight: number; + typeId: number; } -export const saveMaterial = async (data: CreateMaterialInputs) => { +export const saveItem = async (data: CreateItemInputs) => { // try { - const material = await serverFetchJson>(`${BASE_API_URL}/material/new`, { + const item = await serverFetchJson>(`${BASE_API_URL}/items/new`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }); - revalidateTag("material"); - return material + revalidateTag("items"); + return item }; diff --git a/src/app/api/settings/material/index.ts b/src/app/api/settings/item/index.ts similarity index 58% rename from src/app/api/settings/material/index.ts rename to src/app/api/settings/item/index.ts index 5f452d1..a346ec5 100644 --- a/src/app/api/settings/material/index.ts +++ b/src/app/api/settings/item/index.ts @@ -4,11 +4,10 @@ import { serverFetchJson } from "@/app/utils/fetchUtil"; import { BASE_API_URL } from "@/config/api"; import { TypeInputs, UomInputs, WeightUnitInputs } from "./actions"; -export type MaterialResult = { +export type ItemsResult = { id: string | number code: string; name: string; - isConsumables: boolean; description: string | undefined; remarks: string | undefined; shelfLife: Number | undefined; @@ -20,22 +19,22 @@ export type MaterialResult = { sampleRate: number | undefined; passingRate: number | undefined; netWeight: number | undefined; - uom: UomInputs[]; - weightUnit: WeightUnitInputs[]; - type: TypeInputs[]; + // uom: UomInputs[]; + // weightUnit: WeightUnitInputs[]; + type: TypeInputs; action?: any } -export const fetchAllMaterials = cache(async () => { - return serverFetchJson(`${BASE_API_URL}/material`, { - next: { tags: ["material"] }, +export const fetchAllItems = cache(async () => { + return serverFetchJson(`${BASE_API_URL}/items`, { + next: { tags: ["items"] }, }); }); -export const fetchMaterial = cache(async (id: number) => { - return serverFetchJson(`${BASE_API_URL}/material/details/${id}`, { - next: { tags: ["material"] }, +export const fetchItem = cache(async (id: number) => { + return serverFetchJson(`${BASE_API_URL}/items/details/${id}`, { + next: { tags: ["items"] }, }); }); \ No newline at end of file diff --git a/src/app/api/settings/product/actions.ts b/src/app/api/settings/product/actions.ts deleted file mode 100644 index b73c04d..0000000 --- a/src/app/api/settings/product/actions.ts +++ /dev/null @@ -1,37 +0,0 @@ -"use server"; -import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil"; -import { revalidateTag } from "next/cache"; -import { BASE_API_URL } from "@/config/api"; -import { TypeInputs, UomInputs, WeightUnitInputs } from "../material/actions"; -import { CreateItemResponse } from "../../utils"; - -export type CreateProductInputs = { - id?: string | number - code: string; - name: string; - description?: string | undefined; - remarks?: string | undefined; - shelfLife?: number | undefined; - countryOfOrigin?: string | undefined; - minHumid?: number | undefined; - maxHumid?: number | undefined; - minTemp?: number | undefined; - maxTemp?: number | undefined; - sampleRate?: number | undefined; - passingRate?: number | undefined; - netWeight?: number | undefined; - type?: TypeInputs[]; - uom?: UomInputs[]; - weightUnit?: WeightUnitInputs[]; -} - -export const saveProduct = async (data: CreateProductInputs) => { - // try { - const product = await serverFetchJson>(`${BASE_API_URL}/product/new`, { - method: "POST", - body: JSON.stringify(data), - headers: { "Content-Type": "application/json" }, - }); - revalidateTag("product"); - return product - }; \ No newline at end of file diff --git a/src/app/api/settings/product/index.ts b/src/app/api/settings/product/index.ts deleted file mode 100644 index 67675b8..0000000 --- a/src/app/api/settings/product/index.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { cache } from "react"; -import "server-only"; -import { serverFetchJson } from "@/app/utils/fetchUtil"; -import { BASE_API_URL } from "@/config/api"; -import { TypeInputs, UomInputs, WeightUnitInputs } from "../material/actions"; - -export type ProductResult = { - id: string | number - code: string; - name: string; - description: string | undefined; - remarks: string | undefined; - shelfLife: Number | undefined; - countryOfOrigin: string | undefined; - minHumid: number | undefined; - maxHumid: number | undefined; - minTemp: number | undefined; - maxTemp: number | undefined; - sampleRate: number | undefined; - passingRate: number | undefined; - netWeight: number | undefined; - uom: UomInputs[]; - weightUnit: WeightUnitInputs[]; - type: TypeInputs[]; - action?: any; -} - - -export const fetchAllProduct = cache(async () => { - return serverFetchJson(`${BASE_API_URL}/product`, { - next: { tags: ["product"] }, - }); - }); - - -export const fetchProduct = cache(async (id: number) => { - return serverFetchJson(`${BASE_API_URL}/product/details/${id}`, { - next: { tags: ["product"] }, - }); -}); \ No newline at end of file diff --git a/src/app/utils/typeEnum.ts b/src/app/utils/typeEnum.ts index de84b22..75e9bf4 100644 --- a/src/app/utils/typeEnum.ts +++ b/src/app/utils/typeEnum.ts @@ -1,5 +1,10 @@ export enum TypeEnum { - PRODUCT = "product", MATERIAL = "material", + MATERIAL_ID = 1, + PRODUCT = "product", + PRODUCT_ID = 2, BYPRODUCT = "byProduct", - } \ No newline at end of file + BYPRODUCT_ID = 3, + CONSUMABLE = "consumables", + CONSUMABLE_ID = 4, +} \ No newline at end of file diff --git a/src/components/CreateProductMaterial/ByProductDetails.tsx b/src/components/CreateItem/ByProductDetails.tsx similarity index 86% rename from src/components/CreateProductMaterial/ByProductDetails.tsx rename to src/components/CreateItem/ByProductDetails.tsx index 95d3fbb..be48d5b 100644 --- a/src/components/CreateProductMaterial/ByProductDetails.tsx +++ b/src/components/CreateItem/ByProductDetails.tsx @@ -1,5 +1,5 @@ "use client"; -import { CreateMaterialInputs } from "@/app/api/settings/material/actions"; +import { CreateItemInputs } from "@/app/api/settings/item/actions"; import { Box, Card, @@ -12,27 +12,24 @@ import { import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import InputDataGrid from "../InputDataGrid"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { GridColDef, GridRowModel } from "@mui/x-data-grid"; import { InputDataGridProps, TableRow } from "../InputDataGrid/InputDataGrid"; import { TypeEnum } from "@/app/utils/typeEnum"; -import { CreateByProductInputs } from "@/app/api/settings/byProduct/actions"; import { NumberInputProps } from "./NumberInputProps"; type Props = { -// isEditMode: boolean; -// type: TypeEnum; + // isEditMode: boolean; + // type: TypeEnum; }; export type EntryError = | { - [field in keyof CreateByProductInputs]?: string; + [field in keyof CreateItemInputs]?: string; } | undefined; -const ByProductDetails: React.FC = ({ - - }) => { +const ByProductDetails: React.FC = ({}) => { const { t, i18n: { language }, @@ -49,7 +46,7 @@ const ByProductDetails: React.FC = ({ resetField, setError, clearErrors, - } = useFormContext(); + } = useFormContext(); const typeColumns = useMemo( () => [ { @@ -93,7 +90,7 @@ const ByProductDetails: React.FC = ({ const validationTest = useCallback( ( - newRow: GridRowModel, EntryError>> + newRow: GridRowModel, EntryError>> ): EntryError => { const error: EntryError = {}; console.log(newRow); @@ -102,6 +99,22 @@ const ByProductDetails: React.FC = ({ [] ); + const validateUom = useCallback( + ( + newRow: GridRowModel, EntryError>> + ): EntryError => { + const error: EntryError = {}; + console.log(newRow); + // if (!newRow.uom) { + // error.uom = "Uom cannot be empty"; + // } + return Object.keys(error).length > 0 ? error : undefined; + }, + [] + ); + useEffect(() => { + console.log(errors) + }, [errors]) return ( @@ -272,27 +285,6 @@ const ByProductDetails: React.FC = ({ helperText={errors.netWeight?.message} /> - - - _formKey={"type"} - columns={typeColumns} - validateRow={validationTest} - /> - - - - _formKey={"uom"} - columns={uomColumns} - validateRow={validationTest} - /> - - - - _formKey={"weightUnit"} - columns={weightUnitColumns} - validateRow={validationTest} - /> - diff --git a/src/components/CreateProductMaterial/CreateProductMaterial.tsx b/src/components/CreateItem/CreateItem.tsx similarity index 78% rename from src/components/CreateProductMaterial/CreateProductMaterial.tsx rename to src/components/CreateItem/CreateItem.tsx index 8e6315e..bc7689e 100644 --- a/src/components/CreateProductMaterial/CreateProductMaterial.tsx +++ b/src/components/CreateItem/CreateItem.tsx @@ -4,9 +4,9 @@ import { useCallback, useEffect, useMemo, useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { useTranslation } from "react-i18next"; import { - CreateMaterialInputs, - saveMaterial, -} from "@/app/api/settings/material/actions"; + CreateItemInputs, + saveItem, +} from "@/app/api/settings/item/actions"; import { FormProvider, SubmitErrorHandler, @@ -17,67 +17,69 @@ import { deleteDialog } from "../Swal/CustomAlerts"; import { Box, Button, Grid, Stack, Typography } from "@mui/material"; import { Check, Close, EditNote } from "@mui/icons-material"; import { TypeEnum } from "@/app/utils/typeEnum"; -import { - CreateProductInputs, - saveProduct, -} from "@/app/api/settings/product/actions"; -import { CreateInputsFields } from "./CreateProductMaterialWrapper"; import MaterialDetails from "./MaterialDetails"; import ProductDetails from "./ProductDetails"; import { CreateItemResponse } from "@/app/api/utils"; -import { CreateByProductInputs, saveByProduct } from "@/app/api/settings/byProduct/actions"; import ByProductDetails from "./ByProductDetails"; type Props = { isEditMode: boolean; type: TypeEnum; - defaultValues: Partial | undefined; + defaultValues: Partial | undefined; }; -const CreateProductMaterial: React.FC = ({ +const CreateItem: React.FC = ({ isEditMode, type, defaultValues, }) => { + console.log(type) const [serverError, setServerError] = useState(""); const [tabIndex, setTabIndex] = useState(0); const { t } = useTranslation(); const router = useRouter(); - const [title, mode, redirPath] = useMemo(() => { + const [typeId, title, mode, redirPath] = useMemo(() => { + var typeId = TypeEnum.CONSUMABLE_ID var title = ""; var mode = ""; var redirPath = ""; if (type === TypeEnum.MATERIAL) { + typeId = TypeEnum.MATERIAL_ID title = "Material"; - redirPath = "/material"; + redirPath = "/settings/material"; } if (type === TypeEnum.PRODUCT) { + typeId = TypeEnum.PRODUCT_ID title = "Product"; - redirPath = "/product"; + redirPath = "/settings/product"; } if (type === TypeEnum.BYPRODUCT) { + typeId = TypeEnum.BYPRODUCT_ID title = "By-Product"; - redirPath = "/byProduct"; + redirPath = "/settings/byProduct"; } if (isEditMode) { mode = "Edit"; } else { mode = "Create"; } - return [title, mode, redirPath]; + return [typeId, title, mode, redirPath]; }, [type, isEditMode]); - - const formProps = useForm({ - defaultValues: defaultValues ? defaultValues : {}, + console.log(typeId) + const formProps = useForm({ + defaultValues: defaultValues ? defaultValues : { + typeId: typeId + }, }); const errors = formProps.formState.errors; const handleCancel = () => { router.replace(`/settings/${type}`); }; - const onSubmit = useCallback>( + const onSubmit = useCallback>( async (data, event) => { let hasErrors = false; + console.log("dasasdasd") try { // checking humid input if (data.maxHumid && data.minHumid!! > data.maxHumid!!) { @@ -135,31 +137,19 @@ const CreateProductMaterial: React.FC = ({ } console.log("data posted"); console.log(data); - // do api - var response; - if (type === TypeEnum.MATERIAL) { - response = await saveMaterial(data as CreateMaterialInputs); - } else if (type === TypeEnum.PRODUCT) { - response = await saveProduct(data as CreateProductInputs); - } else if (type === TypeEnum.BYPRODUCT) { - // response = await saveByProduct(data as CreateByProductInputs); - } else { - // Handle unexpected type - throw new Error("Invalid type provided"); - } + // return + // do api + var response = await saveItem(data); if (response) { if (!Boolean(response.id)) { - formProps.setError(response.errorPosition!! as keyof CreateInputsFields, { + formProps.setError(response.errorPosition!! as keyof CreateItemInputs, { message: response.message!!, type: "required", }); - throw response; } else if (Boolean(response.id)) { router.replace(redirPath); } - } else { - throw "no response"; } } catch (e) { // backend error @@ -171,7 +161,7 @@ const CreateProductMaterial: React.FC = ({ ); // multiple tabs - const onSubmitError = useCallback>( + const onSubmitError = useCallback>( (errors) => {}, [] ); @@ -220,4 +210,4 @@ const CreateProductMaterial: React.FC = ({ ); }; -export default CreateProductMaterial; +export default CreateItem; diff --git a/src/components/CreateProductMaterial/CreateProductMaterialLoading.tsx b/src/components/CreateItem/CreateItemLoading.tsx similarity index 90% rename from src/components/CreateProductMaterial/CreateProductMaterialLoading.tsx rename to src/components/CreateItem/CreateItemLoading.tsx index e954b54..15896e9 100644 --- a/src/components/CreateProductMaterial/CreateProductMaterialLoading.tsx +++ b/src/components/CreateItem/CreateItemLoading.tsx @@ -5,7 +5,7 @@ import Stack from "@mui/material/Stack"; import React from "react"; // Can make this nicer -export const CreateProductMaterialLoading: React.FC = () => { +export const CreateItemLoading: React.FC = () => { return ( <> @@ -37,4 +37,4 @@ export const CreateProductMaterialLoading: React.FC = () => { ); }; -export default CreateProductMaterialLoading; +export default CreateItemLoading; diff --git a/src/components/CreateItem/CreateItemWrapper.tsx b/src/components/CreateItem/CreateItemWrapper.tsx new file mode 100644 index 0000000..34fd41c --- /dev/null +++ b/src/components/CreateItem/CreateItemWrapper.tsx @@ -0,0 +1,53 @@ +import { TypeEnum } from "@/app/utils/typeEnum"; +import CreateItem from "./CreateItem"; +import CreateItemLoading from "./CreateItemLoading"; +import { CreateItemInputs } from "@/app/api/settings/item/actions"; +import { notFound } from "next/navigation"; +import { fetchItem } from "@/app/api/settings/item"; +interface SubComponents { + Loading: typeof CreateItemLoading; +} + +type Props = { + id?: number + type: TypeEnum; +}; + +const CreateItemWrapper: React.FC & + SubComponents = async ({ type, id }) => { + var result + var defaultValues: Partial | undefined + console.log(type) + if (id) { + result = await fetchItem(id); + console.log(result) + defaultValues = { + typeId: result?.type.id, + id: result?.id, + code: result?.code, + name: result?.name, + description: result?.description, + remarks: result?.remarks, + shelfLife: result?.shelfLife, + countryOfOrigin: result?.countryOfOrigin, + minHumid: result?.minHumid, + maxHumid: result?.maxHumid, + minTemp: result?.minTemp, + maxTemp: result?.maxTemp, + sampleRate: result?.sampleRate, + passingRate: result?.passingRate, + netWeight: result?.netWeight + }; + } + + return ( + + ); +}; +CreateItemWrapper.Loading = CreateItemLoading; + +export default CreateItemWrapper; diff --git a/src/components/CreateProductMaterial/MaterialDetails.tsx b/src/components/CreateItem/MaterialDetails.tsx similarity index 87% rename from src/components/CreateProductMaterial/MaterialDetails.tsx rename to src/components/CreateItem/MaterialDetails.tsx index 1478cc0..1ae26ce 100644 --- a/src/components/CreateProductMaterial/MaterialDetails.tsx +++ b/src/components/CreateItem/MaterialDetails.tsx @@ -1,5 +1,5 @@ "use client"; -import { CreateMaterialInputs } from "@/app/api/settings/material/actions"; +import { CreateItemInputs } from "@/app/api/settings/item/actions"; import { Box, Card, @@ -31,7 +31,7 @@ type Props = { export type EntryError = | { - [field in keyof CreateMaterialInputs]?: string; + [field in keyof CreateItemInputs]?: string; } | undefined; @@ -52,8 +52,9 @@ const MaterialDetails: React.FC = ({}) => { resetField, setError, clearErrors, - } = useFormContext(); + } = useFormContext(); // textfield error msg locale problem + console.log(defaultValues) const typeColumns = useMemo( () => [ { @@ -97,7 +98,7 @@ const MaterialDetails: React.FC = ({}) => { const validationTest = useCallback( ( - newRow: GridRowModel, EntryError>> + newRow: GridRowModel, EntryError>> ): EntryError => { const error: EntryError = {}; console.log(newRow); @@ -117,15 +118,20 @@ const MaterialDetails: React.FC = ({}) => { ( + render={({ field: props }) => { + console.log(props) + return ( props.onChange(e.target.checked)} + checked={props.value === TypeEnum.CONSUMABLE_ID} + onChange={(e) => { + const newValue = e.target.checked ? TypeEnum.CONSUMABLE_ID : TypeEnum.MATERIAL_ID; + props.onChange(newValue); + }} /> - )} + )}} /> } label={ @@ -310,27 +316,6 @@ const MaterialDetails: React.FC = ({}) => { helperText={errors.netWeight?.message} /> - - - _formKey={"type"} - columns={typeColumns} - validateRow={validationTest} - /> - - - - _formKey={"uom"} - columns={uomColumns} - validateRow={validationTest} - /> - - - - _formKey={"weightUnit"} - columns={weightUnitColumns} - validateRow={validationTest} - /> - diff --git a/src/components/CreateProductMaterial/NumberInputProps.ts b/src/components/CreateItem/NumberInputProps.ts similarity index 100% rename from src/components/CreateProductMaterial/NumberInputProps.ts rename to src/components/CreateItem/NumberInputProps.ts diff --git a/src/components/CreateProductMaterial/ProductDetails.tsx b/src/components/CreateItem/ProductDetails.tsx similarity index 93% rename from src/components/CreateProductMaterial/ProductDetails.tsx rename to src/components/CreateItem/ProductDetails.tsx index 7a65667..dd9aafd 100644 --- a/src/components/CreateProductMaterial/ProductDetails.tsx +++ b/src/components/CreateItem/ProductDetails.tsx @@ -1,5 +1,4 @@ "use client"; -import { CreateMaterialInputs } from "@/app/api/settings/material/actions"; import { Box, Card, @@ -16,8 +15,8 @@ import { useCallback, useMemo, useState } from "react"; import { GridColDef, GridRowModel } from "@mui/x-data-grid"; import { InputDataGridProps, TableRow } from "../InputDataGrid/InputDataGrid"; import { TypeEnum } from "@/app/utils/typeEnum"; -import { CreateProductInputs } from "@/app/api/settings/product/actions"; import { NumberInputProps } from "./NumberInputProps"; +import { CreateItemInputs } from "@/app/api/settings/item/actions"; type Props = { // isEditMode: boolean; @@ -26,7 +25,7 @@ type Props = { export type EntryError = | { - [field in keyof CreateProductInputs]?: string; + [field in keyof CreateItemInputs]?: string; } | undefined; @@ -49,7 +48,7 @@ const ProductDetails: React.FC = ({ resetField, setError, clearErrors, - } = useFormContext(); + } = useFormContext(); const typeColumns = useMemo( () => [ { @@ -93,7 +92,7 @@ const ProductDetails: React.FC = ({ const validationTest = useCallback( ( - newRow: GridRowModel, EntryError>> + newRow: GridRowModel, EntryError>> ): EntryError => { const error: EntryError = {}; console.log(newRow); @@ -276,28 +275,28 @@ const ProductDetails: React.FC = ({ helperText={errors.netWeight?.message} /> - - + {/* + _formKey={"type"} columns={typeColumns} validateRow={validationTest} /> - + _formKey={"uom"} columns={uomColumns} validateRow={validationTest} /> - + _formKey={"weightUnit"} columns={weightUnitColumns} validateRow={validationTest} /> - - + */} + diff --git a/src/components/CreateItem/index.ts b/src/components/CreateItem/index.ts new file mode 100644 index 0000000..2fb406d --- /dev/null +++ b/src/components/CreateItem/index.ts @@ -0,0 +1 @@ +export { default } from "./CreateItemWrapper"; \ No newline at end of file diff --git a/src/components/CreateProductMaterial/CreateProductMaterialWrapper.tsx b/src/components/CreateProductMaterial/CreateProductMaterialWrapper.tsx deleted file mode 100644 index 5da4346..0000000 --- a/src/components/CreateProductMaterial/CreateProductMaterialWrapper.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { TypeEnum } from "@/app/utils/typeEnum"; -import CreateProductMaterial from "./CreateProductMaterial"; -import CreateMaterialLoading from "./CreateProductMaterialLoading"; -import { CreateMaterialInputs } from "@/app/api/settings/material/actions"; -import { CreateProductInputs } from "@/app/api/settings/product/actions"; -import { fetchMaterial } from "@/app/api/settings/material"; -import { fetchProduct } from "@/app/api/settings/product"; -import { fetchByProduct } from "@/app/api/settings/byProduct"; -import { notFound } from "next/navigation"; -import { CreateByProductInputs } from "@/app/api/settings/byProduct/actions"; -interface SubComponents { - Loading: typeof CreateMaterialLoading; -} - -type Props = { - id?: number - type: TypeEnum; -}; - -export type CreateInputsFields = CreateMaterialInputs | CreateProductInputs | CreateByProductInputs; - -const CreateProductMaterialWrapper: React.FC & - SubComponents = async ({ type, id }) => { - var defaultValues: Partial = {}; - var result - if (id && type === TypeEnum.MATERIAL) { - result = await fetchMaterial(id); - defaultValues = { - isConsumables: result.isConsumables - } - } else if (id && type === TypeEnum.PRODUCT) { - result = await fetchProduct(id); - } else if (id && type === TypeEnum.BYPRODUCT) { - result = await fetchByProduct(id); - } - - defaultValues = { - ...defaultValues, - id: result?.id, - code: result?.code, - name: result?.name, - description: result?.description, - type: result?.type, - remarks: result?.remarks, - shelfLife: result?.shelfLife, - countryOfOrigin: result?.countryOfOrigin, - minHumid: result?.minHumid, - maxHumid: result?.maxHumid, - minTemp: result?.minTemp, - maxTemp: result?.maxTemp, - sampleRate: result?.sampleRate, - passingRate: result?.passingRate, - netWeight: result?.netWeight, - uom: result?.uom, - weightUnit: result?.weightUnit, - }; - return ( - - ); -}; -CreateProductMaterialWrapper.Loading = CreateMaterialLoading; - -export default CreateProductMaterialWrapper; diff --git a/src/components/CreateProductMaterial/index.ts b/src/components/CreateProductMaterial/index.ts deleted file mode 100644 index 7d77b3c..0000000 --- a/src/components/CreateProductMaterial/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { default } from "./CreateProductMaterialWrapper"; diff --git a/src/components/InputDataGrid/InputDataGrid.tsx b/src/components/InputDataGrid/InputDataGrid.tsx index 5e1c783..cce272c 100644 --- a/src/components/InputDataGrid/InputDataGrid.tsx +++ b/src/components/InputDataGrid/InputDataGrid.tsx @@ -151,7 +151,7 @@ function InputDataGrid({ const reset = useCallback(() => { setRowModesModel({}) - setRows([]) + setRows(originalRows) }, []); const handleCancel = useCallback( @@ -239,7 +239,7 @@ function InputDataGrid({