diff --git a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/ItemSelectField.jsx b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/ItemSelectField.jsx index 44fc09f..2804d4a 100644 --- a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/ItemSelectField.jsx +++ b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/ItemSelectField.jsx @@ -4,6 +4,9 @@ import SelectBox from "@/core/components/SelectBox"; const ItemSelectField = ({ control, setValue }) => { const { itemsList, loadingItemsList, errorItemsList } = useRoadItemGetItems(); + function getItemName(val) { + return itemsList.find((item) => item.id === val)?.name; + } return ( { schema={{ name: "name", value: "id" }} error={errorItemsList} helperText={error?.message} - onChange={() => { - field.onChange; + onChange={(value) => { + field.onChange(value); + setValue("explanation", getItemName(value)); setValue("sub_item_id", ""); - }} /> )} /> ); }; -export default ItemSelectField \ No newline at end of file +export default ItemSelectField; diff --git a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/SubItemSelectField.jsx b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/SubItemSelectField.jsx index 82d30b2..9d54a52 100644 --- a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/SubItemSelectField.jsx +++ b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/SubItemSelectField.jsx @@ -2,10 +2,16 @@ import useRoadItemGetSubItems from "@/lib/hooks/useRoadItemGetISubtems"; import { Controller } from "react-hook-form"; import SelectBox from "@/core/components/SelectBox"; -const SubItemSelectField = ({ control, watch }) => { +const SubItemSelectField = ({ control, watch, setValue, getValues }) => { const itemId = watch("item_id"); const { subItemsList, loadingSubItemsList, errorSubItemsList } = useRoadItemGetSubItems(itemId); + function createExplanation(val) { + const currentExplanation = getValues("explanation"); + const selectedItem = subItemsList.find((item) => item.id === val)?.name; + return `${currentExplanation}-${selectedItem}`; + } + return ( { schema={{ name: "name", value: "id" }} error={errorSubItemsList} helperText={error?.message} - onChange={field.onChange} + onChange={(val) => { + field.onChange(val); + setValue("explanation", createExplanation(val)); + }} /> )} /> ); }; -export default SubItemSelectField; \ No newline at end of file +export default SubItemSelectField; diff --git a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/index.jsx b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/index.jsx index a203436..1a804de 100644 --- a/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/index.jsx +++ b/src/components/dashboard/roadMissions/operator/Actions/Create/Form/GetItemInfo/index.jsx @@ -8,17 +8,16 @@ import { yupResolver } from "@hookform/resolvers/yup"; import { ExitToApp } from "@mui/icons-material"; import { Box, Button, DialogActions, DialogContent, Grid, Stack } from "@mui/material"; import { Controller, useForm } from "react-hook-form"; -import { array, number, object, string } from "yup"; -import ItemSelectField - from "./ItemSelectField"; -import SubItemSelectField - from "./SubItemSelectField"; +import { array, object, string } from "yup"; +import ItemSelectField from "./ItemSelectField"; +import SubItemSelectField from "./SubItemSelectField"; const validationSchema = object({ - explanation: string().required("موضوع را مشخص کنید!"), end_point: string().required("مقصد را مشخص کنید!"), region: string().required("محور ماموریت را مشخص کنید!"), category_id: string().required("نوع ماموریت را مشخص کنید!"), + item_id: string().required("موضوع را مشخص کنید!"), + sub_item_id: string().required("اقدام انجام شده را مشخص کنید!"), requested_machines: array().min(1, "نوع خودرو را مشخص کنید!").required("نوع خودرو را مشخص کنید!"), road_observed_id: string().when("category_id", { is: "3", @@ -39,7 +38,7 @@ const GetItemInfo = ({ allData, setAllData, handlePrev, setTabState }) => { road_observed_id: allData.road_observed_id, }; - const { control, handleSubmit, watch, setValue } = useForm({ + const { control, handleSubmit, watch, setValue, getValues } = useForm({ defaultValues, resolver: yupResolver(validationSchema), mode: "all", @@ -60,7 +59,12 @@ const GetItemInfo = ({ allData, setAllData, handlePrev, setTabState }) => { - + { start_date: result.start_date, end_date: result.end_date, }), + item_id: result.item_id, + sub_item_id: result.sub_item_id, }, hasSidebarUpdate: true, })