From c56dfb4ac25c969b4271654d91ab60155a235752 Mon Sep 17 00:00:00 2001 From: Nikhil Nath R Date: Tue, 16 Jun 2020 00:01:09 +0530 Subject: [PATCH] Update AddInventoryForm values with user input --- src/Components/Facility/AddInventoryForm.tsx | 87 +++++++++++--------- src/Components/Facility/models.tsx | 4 + 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/Components/Facility/AddInventoryForm.tsx b/src/Components/Facility/AddInventoryForm.tsx index 3cce8bd0c4e..0d8f5810239 100644 --- a/src/Components/Facility/AddInventoryForm.tsx +++ b/src/Components/Facility/AddInventoryForm.tsx @@ -1,12 +1,12 @@ import { Button, Card, CardContent, InputLabel } from "@material-ui/core"; import CheckCircleOutlineIcon from '@material-ui/icons/CheckCircleOutline'; import moment from 'moment'; -import React, { useCallback, useReducer, useState } from "react"; +import React, { useCallback, useReducer, useState, useEffect } from "react"; import { useDispatch } from "react-redux"; import { statusType, useAbortableEffect } from "../../Common/utils"; import { getItems } from "../../Redux/actions"; import * as Notification from "../../Utils/Notifications.js"; -import { DateInputField, TextInputField } from "../Common/HelperInputFields"; +import { SelectField, TextInputField } from "../Common/HelperInputFields"; import { Loading } from "../Common/Loading"; import PageTitle from "../Common/PageTitle"; import { InventoryItemsModel } from "./models"; @@ -28,7 +28,7 @@ const initialState = { form: { ...initForm } }; -const triageFormReducer = (state = initialState, action: any) => { +const inventoryFormReducer = (state = initialState, action: any) => { switch (action.type) { case "set_form": { return { @@ -52,18 +52,16 @@ const goBack = () => { }; export const AddInventoryForm = (props: any) => { - const [state, dispatch] = useReducer(triageFormReducer, initialState); + const [state, dispatch] = useReducer(inventoryFormReducer, initialState); const { facilityId } = props; const dispatchAction: any = useDispatch(); const [isLoading, setIsLoading] = useState(false); const [offset, setOffset] = useState(0); const [data, setData] = useState>([]); - const [totalCount, setTotalCount] = useState(0); + const [currentUnit, setCurrentUnit] = useState(); const limit = 14; - - const [form, setForm] = useState(initForm); const fetchData = useCallback( async (status: statusType) => { setIsLoading(true); @@ -71,7 +69,7 @@ export const AddInventoryForm = (props: any) => { if (!status.aborted) { if (res && res.data) { setData(res.data.results); - setTotalCount(res.data.count); + dispatch({ type: "set_form", form: { ...state.form, id: res.data.results[0]?.id } }); } setIsLoading(false); } @@ -84,18 +82,25 @@ export const AddInventoryForm = (props: any) => { }, [fetchData] ); + + useEffect(() => { + // set the default units according to the item + const item = data.find(item => item.id === state.form.id); + if (item) { + dispatch({ type: "set_form", form: { ...state.form, unit: item.default_unit?.id } }); + setCurrentUnit(item.allowed_units); + } + }, [state.form.id]) + console.log("dataaaa", data); const handleSubmit = async (e: any) => { e.preventDefault(); }; const handleChange = (e: any) => { - //handlechange - // const { value, name } = e.target; - // setForm({ ...form, [name]: value }); let form = { ...state.form }; form[e.target.name] = e.target.value; - dispatchAction({ type: "set_form", form }); + dispatch({ type: "set_form", form }); }; @@ -112,24 +117,29 @@ export const AddInventoryForm = (props: any) => {
Inventory Name - + variant="standard" + value={state.form.id} + options={data.map((e) => { return { id: e.id, name: e.name }})} + onChange={handleChange} + optionKey="id" + optionValue="name" + // errors={state.errors.isIncoming} + />
-
+
Status: - +
Quantity @@ -138,22 +148,23 @@ export const AddInventoryForm = (props: any) => { variant="outlined" margin="dense" type="number" - value={form.quantity} + value={state.form.quantity} onChange={handleChange} errors="" />
-
+
Unit - +
diff --git a/src/Components/Facility/models.tsx b/src/Components/Facility/models.tsx index 39ca8989d39..6ad736496a3 100644 --- a/src/Components/Facility/models.tsx +++ b/src/Components/Facility/models.tsx @@ -91,4 +91,8 @@ export interface InventoryItemsModel { id: number; name: string; }; + allowed_units?: [{ + id: number; + name: string; + }]; } \ No newline at end of file