From a7829d34b8b6d53d5a87fa947c983e08b940c94e Mon Sep 17 00:00:00 2001 From: Mohammad Jalali Date: Sun, 24 Nov 2024 08:10:01 +0000 Subject: [PATCH 1/2] Feature/solve azmayesh type bugs --- .../dashboard/azmayesh/AzmayeshList.jsx | 13 + .../Forms/CandUAzmayesh/FormAndRequest.jsx | 198 +++++++++++++++ .../azmayesh/Forms/CandUAzmayesh/index.jsx | 213 ++-------------- .../RowActions/AzmayeshUpdate/index.jsx | 7 +- .../CandUSampleOfAzmayesh/FormAndRequest.jsx | 73 ++++++ .../ShowSampleOfAzmayesh/ShowSampleList.jsx | 18 +- .../azmayeshType/AzmayeshTypeList.jsx | 6 +- .../CandUAzmayeshType/FormAndRequest.jsx | 222 +++++++++++++++++ .../Forms/CandUAzmayeshType/index.jsx | 233 ++---------------- .../RowActions/UpdateAzmayeshType/index.jsx | 7 +- src/core/components/DialogLoading.jsx | 36 +++ src/core/utils/theme.js | 10 + 12 files changed, 614 insertions(+), 422 deletions(-) create mode 100644 src/components/dashboard/azmayesh/Forms/CandUAzmayesh/FormAndRequest.jsx create mode 100644 src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/Forms/CandUSampleOfAzmayesh/FormAndRequest.jsx create mode 100644 src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/FormAndRequest.jsx create mode 100644 src/core/components/DialogLoading.jsx diff --git a/src/components/dashboard/azmayesh/AzmayeshList.jsx b/src/components/dashboard/azmayesh/AzmayeshList.jsx index 89e220f..c3dcb83 100644 --- a/src/components/dashboard/azmayesh/AzmayeshList.jsx +++ b/src/components/dashboard/azmayesh/AzmayeshList.jsx @@ -119,6 +119,19 @@ const AzmayeshList = () => { ), }, + { + accessorKey: "updated_at", + header: "تاریخ بروزرسانی", + id: "updated_at", + enableColumnFilter: false, + datatype: "date", + filterFn: "equals", + Cell: ({ renderedCellValue }) => ( + + {renderedCellValue ? moment(renderedCellValue).locale("fa").format("HH:mm | YYYY/MM/DD") : "-"} + + ), + }, ], [] ); diff --git a/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/FormAndRequest.jsx b/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/FormAndRequest.jsx new file mode 100644 index 0000000..f74dd22 --- /dev/null +++ b/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/FormAndRequest.jsx @@ -0,0 +1,198 @@ +"use client"; + +import { Box, Button, DialogActions, DialogContent, Tab, Tabs, useMediaQuery } from "@mui/material"; +import React, { useState } from "react"; +import { useTheme } from "@emotion/react"; +import useRequest from "@/lib/hooks/useRequest"; +import { object, string } from "yup"; +import { useForm } from "react-hook-form"; +import { yupResolver } from "@hookform/resolvers/yup"; +import moment from "jalali-moment"; +import { CREATE_AZMAYESH, UPDATE_AZMAYESH } from "@/core/utils/routes"; +import StyledForm from "@/core/components/StyledForm"; +import TravelExploreIcon from "@mui/icons-material/TravelExplore"; +import TextSnippetIcon from "@mui/icons-material/TextSnippet"; +import ExitToAppIcon from "@mui/icons-material/ExitToApp"; +import KeyboardDoubleArrowRightIcon from "@mui/icons-material/KeyboardDoubleArrowRight"; +import KeyboardDoubleArrowLeftIcon from "@mui/icons-material/KeyboardDoubleArrowLeft"; +import BeenhereIcon from "@mui/icons-material/Beenhere"; +import CandUMapBox from "./CandUMapBox"; +import CandUCreateTimeLine from "./CandUTimeLine"; +import CandUGeneralInfo from "./CandUGeneralInfo"; + +function TabPanel(props) { + const { children, value, index } = props; + + return ( + + ); +} + +const validationSchema = object({ + azmayesh_type_id: string().required("نوع آزمایش را مشخص کنید!"), + province_id: string().required("استان را وارد کنید!"), + project_name: string().required("عنوان پروژه را وارد کنید!"), + employer: string().required("کارفرما را وارد کنید!"), + consultant: string().required("مشاور را وارد کنید!"), + contractor: string().required("پیمانکار را وارد کنید!"), + applicant: string().required("متقاضی را وارد کنید!"), + work_number: string().required("شماره کار را وارد کنید!"), + request_number: string().required("شماره درخواست را وارد کنید!"), + request_date: string().required("تاریخ درخواست را وارد کنید!"), + report_date: string().required("تاریخ گزارش را وارد کنید!"), +}); + +const CandUAzmayesh = ({ setOpen, mutate, updateInfo, rowId }) => { + const theme = useTheme(); + const requestServer = useRequest({ auth: true }); + const isMobile = useMediaQuery(theme.breakpoints.down("sm")); + const [tabState, setTabState] = useState(0); + const [mapBoxData, setMapBoxData] = useState(updateInfo ? { lat: updateInfo.lat, lng: updateInfo.lng } : null); + + const handleClose = () => { + setOpen(false); + }; + + const handleChangeTab = (event, newValue) => { + setTabState(newValue); + }; + + const handlePrev = () => { + if (tabState === 0) { + handleClose(); + } else { + setTabState(tabState - 1); + } + }; + + const defaultValues = { + azmayesh_type_id: updateInfo ? updateInfo.azmayesh_type_id : "", + azmayesh_type_name: updateInfo ? updateInfo.azmayesh_type_name : "", + province_id: updateInfo ? updateInfo.province_id : "", + province_name: updateInfo ? updateInfo.province_name : "", + project_name: updateInfo ? updateInfo.project_name : "", + employer: updateInfo ? updateInfo.employer : "", + consultant: updateInfo ? updateInfo.consultant : "", + contractor: updateInfo ? updateInfo.contractor : "", + applicant: updateInfo ? updateInfo.applicant : "", + work_number: updateInfo ? updateInfo.work_number : "", + request_number: updateInfo ? updateInfo.request_number : "", + request_date: updateInfo ? moment(updateInfo.request_date, "YYYY-MM-DD").toDate() : "", + report_date: updateInfo ? moment(updateInfo.report_date, "YYYY-MM-DD").toDate() : "", + }; + + const { + control, + register, + handleSubmit, + setValue, + formState: { isSubmitting, errors, touchedFields }, + } = useForm({ defaultValues, resolver: yupResolver(validationSchema), mode: "onBlur" }); + + const onSubmit = async (data) => { + const formData = new FormData(); + formData.append("lat", mapBoxData.lat); + formData.append("lng", mapBoxData.lng); + formData.append("azmayesh_type_id", data.azmayesh_type_id); + formData.append("azmayesh_type_name", data.azmayesh_type_name); + formData.append("province_id", data.province_id); + formData.append("province_name", data.province_name); + formData.append("project_name", data.project_name); + formData.append("employer", data.employer); + formData.append("consultant", data.consultant); + formData.append("contractor", data.contractor); + formData.append("applicant", data.applicant); + formData.append("work_number", data.work_number); + formData.append("request_number", data.request_number); + formData.append("request_date", moment(new Date(data.request_date)).format("YYYY-MM-DD")); + formData.append("report_date", moment(new Date(data.report_date)).format("YYYY-MM-DD")); + + requestServer(updateInfo ? `${UPDATE_AZMAYESH}/${rowId}` : CREATE_AZMAYESH, "post", { + data: formData, + }) + .then(() => { + mutate(); + handleClose(); + }) + .catch(() => {}); + }; + + return ( + + + } label="انتخاب محل آزمایش"> + } label="مشخصات آزمایش"> + + + + + + + + + + + {!isMobile && ( + + + + )} + + + + {tabState === 0 ? ( + + ) : ( + + )} + + + ); +}; +export default CandUAzmayesh; diff --git a/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/index.jsx b/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/index.jsx index 8f145d2..960946d 100644 --- a/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/index.jsx +++ b/src/components/dashboard/azmayesh/Forms/CandUAzmayesh/index.jsx @@ -1,204 +1,23 @@ "use client"; -import { Box, Button, Dialog, DialogActions, DialogContent, Tab, Tabs, useMediaQuery } from "@mui/material"; -import React, { useState } from "react"; -import { useTheme } from "@emotion/react"; -import useRequest from "@/lib/hooks/useRequest"; -import { object, string } from "yup"; -import { useForm } from "react-hook-form"; -import { yupResolver } from "@hookform/resolvers/yup"; -import moment from "jalali-moment"; -import { CREATE_AZMAYESH, UPDATE_AZMAYESH } from "@/core/utils/routes"; -import StyledForm from "@/core/components/StyledForm"; -import TravelExploreIcon from "@mui/icons-material/TravelExplore"; -import TextSnippetIcon from "@mui/icons-material/TextSnippet"; -import ExitToAppIcon from "@mui/icons-material/ExitToApp"; -import KeyboardDoubleArrowRightIcon from "@mui/icons-material/KeyboardDoubleArrowRight"; -import KeyboardDoubleArrowLeftIcon from "@mui/icons-material/KeyboardDoubleArrowLeft"; -import BeenhereIcon from "@mui/icons-material/Beenhere"; -import CandUMapBox from "./CandUMapBox"; -import CandUCreateTimeLine from "./CandUTimeLine"; -import CandUGeneralInfo from "./CandUGeneralInfo"; - -function TabPanel(props) { - const { children, value, index } = props; +import { Dialog } from "@mui/material"; +import FormAndRequest from "./FormAndRequest"; +import DialogLoading from "@/core/components/DialogLoading"; +const CandUAzmayesh = ({ open, setOpen, mutate, updateInfo, rowId, loadingOpen, fromUpdate = false }) => { + const isUpdateReady = fromUpdate ? updateInfo !== null : true; return ( - - ); -} - -const validationSchema = object({ - azmayesh_type_id: string().required("نوع آزمایش را مشخص کنید!"), - province_id: string().required("استان را وارد کنید!"), - project_name: string().required("عنوان پروژه را وارد کنید!"), - employer: string().required("کارفرما را وارد کنید!"), - consultant: string().required("مشاور را وارد کنید!"), - contractor: string().required("پیمانکار را وارد کنید!"), - applicant: string().required("متقاضی را وارد کنید!"), - work_number: string().required("شماره کار را وارد کنید!"), - request_number: string().required("شماره درخواست را وارد کنید!"), - request_date: string().required("تاریخ درخواست را وارد کنید!"), - report_date: string().required("تاریخ گزارش را وارد کنید!"), -}); - -const CandUAzmayesh = ({ open, setOpen, mutate, updateInfo, rowId }) => { - const theme = useTheme(); - const requestServer = useRequest({ auth: true }); - const isMobile = useMediaQuery(theme.breakpoints.down("sm")); - const [tabState, setTabState] = useState(0); - const [mapBoxData, setMapBoxData] = useState(updateInfo ? { lat: updateInfo.lat, lng: updateInfo.lng } : null); - - const handleClose = () => { - setOpen(false); - }; - - const handleChangeTab = (event, newValue) => { - setTabState(newValue); - }; - - const handlePrev = () => { - if (tabState === 0) { - handleClose(); - } else { - setTabState(tabState - 1); - } - }; - - const defaultValues = { - azmayesh_type_id: updateInfo ? updateInfo.azmayesh_type_id : "", - azmayesh_type_name: updateInfo ? updateInfo.azmayesh_type_name : "", - province_id: updateInfo ? updateInfo.province_id : "", - province_name: updateInfo ? updateInfo.province_name : "", - project_name: updateInfo ? updateInfo.project_name : "", - employer: updateInfo ? updateInfo.employer : "", - consultant: updateInfo ? updateInfo.consultant : "", - contractor: updateInfo ? updateInfo.contractor : "", - applicant: updateInfo ? updateInfo.applicant : "", - work_number: updateInfo ? updateInfo.work_number : "", - request_number: updateInfo ? updateInfo.request_number : "", - request_date: updateInfo ? moment(updateInfo.request_date, "YYYY-MM-DD").toDate() : "", - report_date: updateInfo ? moment(updateInfo.report_date, "YYYY-MM-DD").toDate() : "", - }; - - const { - control, - register, - handleSubmit, - setValue, - formState: { isSubmitting, errors, touchedFields }, - } = useForm({ defaultValues, resolver: yupResolver(validationSchema), mode: "onBlur" }); - - const onSubmit = async (data) => { - const formData = new FormData(); - formData.append("lat", mapBoxData.lat); - formData.append("lng", mapBoxData.lng); - formData.append("azmayesh_type_id", data.azmayesh_type_id); - formData.append("azmayesh_type_name", data.azmayesh_type_name); - formData.append("province_id", data.province_id); - formData.append("province_name", data.province_name); - formData.append("project_name", data.project_name); - formData.append("employer", data.employer); - formData.append("consultant", data.consultant); - formData.append("contractor", data.contractor); - formData.append("applicant", data.applicant); - formData.append("work_number", data.work_number); - formData.append("request_number", data.request_number); - formData.append("request_date", moment(new Date(data.request_date)).format("YYYY-MM-DD")); - formData.append("report_date", moment(new Date(data.report_date)).format("YYYY-MM-DD")); - - requestServer(updateInfo ? `${UPDATE_AZMAYESH}/${rowId}` : CREATE_AZMAYESH, "post", { - data: formData, - }) - .then(() => { - mutate(); - handleClose(); - }) - .catch(() => {}); - }; - - return ( - - - - } label="انتخاب محل آزمایش"> - } label="مشخصات آزمایش"> - - - - - - - - - - - {!isMobile && ( - - - - )} - - - - {tabState === 0 ? ( - - ) : ( - - )} - - + + {isUpdateReady ? ( + + ) : ( + + )} ); }; diff --git a/src/components/dashboard/azmayesh/RowActions/AzmayeshUpdate/index.jsx b/src/components/dashboard/azmayesh/RowActions/AzmayeshUpdate/index.jsx index c426685..f36bbae 100644 --- a/src/components/dashboard/azmayesh/RowActions/AzmayeshUpdate/index.jsx +++ b/src/components/dashboard/azmayesh/RowActions/AzmayeshUpdate/index.jsx @@ -9,15 +9,18 @@ const AzmayeshUpdate = ({ rowId, mutate }) => { const requestServer = useRequest({ auth: true }); const [openUpdateDialog, setOpenUpdateDialog] = useState(false); const [updateInfo, setUpdateInfo] = useState(null); + const [loadingOpen, setLoadingOpen] = useState(false); const handleGetInfo = () => { + setLoadingOpen(true); + setOpenUpdateDialog(true); requestServer(`${GET_AZMAYESH_LIST}/${rowId}`, "get") .then((response) => { setUpdateInfo(response.data.data); }) .catch(() => {}) .finally(() => { - setOpenUpdateDialog(true); + setLoadingOpen(false); }); }; @@ -39,6 +42,8 @@ const AzmayeshUpdate = ({ rowId, mutate }) => { mutate={mutate} updateInfo={updateInfo} rowId={rowId} + loadingOpen={loadingOpen} + fromUpdate={true} /> )} diff --git a/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/Forms/CandUSampleOfAzmayesh/FormAndRequest.jsx b/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/Forms/CandUSampleOfAzmayesh/FormAndRequest.jsx new file mode 100644 index 0000000..682c13c --- /dev/null +++ b/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/Forms/CandUSampleOfAzmayesh/FormAndRequest.jsx @@ -0,0 +1,73 @@ +import { Button, DialogActions, DialogContent, DialogTitle } from "@mui/material"; +import useRequest from "@/lib/hooks/useRequest"; +import React, { useState } from "react"; +import { ADD_SAMPLE_TO_AZMAYESH, UPDATE_SAMPLE_OF_AZMAYESH } from "@/core/utils/routes"; +import FormMaker from "@/core/components/FormMaker"; +import { DialogHeader } from "next/dist/client/components/react-dev-overlay/internal/components/Dialog"; + +const CandUSampleOfAzmayesh = ({ + sampleInfo, + mutate, + rowId, + setOpenSampleDialog, + defaultValues, + setDefaultValues, + isUpdate, + azmayesh_id, +}) => { + const requestServer = useRequest({ auth: true }); + const [isSubmitting, setIsSubmitting] = useState(false); + + const handleSubmit = () => { + const data = {}; + setIsSubmitting(true); + const formData = new FormData(); + formData.append("azmayesh_id", isUpdate ? azmayesh_id : rowId); + Object.entries(defaultValues).forEach(([key, value]) => { + data[key] = value; + }); + formData.append("data", JSON.stringify(data)); + requestServer(isUpdate ? `${UPDATE_SAMPLE_OF_AZMAYESH}/${rowId}` : `${ADD_SAMPLE_TO_AZMAYESH}`, "post", { + data: formData, + }) + .then(() => { + mutate(); + }) + .catch(() => {}) + .finally(() => { + setOpenSampleDialog(false); + setIsSubmitting(false); + }); + }; + + return ( + <> + + {isUpdate ? "ویرایش نمونه" : "افزودن نمونه"} + + + + + + + + + + ); +}; +export default CandUSampleOfAzmayesh; diff --git a/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/ShowSampleList.jsx b/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/ShowSampleList.jsx index 4d4bea7..2c11f67 100644 --- a/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/ShowSampleList.jsx +++ b/src/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/ShowSampleList.jsx @@ -1,16 +1,18 @@ import { useMemo } from "react"; -import { Box } from "@mui/material"; +import { Box, Typography } from "@mui/material"; import DataTableWithAuth from "@/core/components/DataTableWithAuth"; import { GET_SAMPLE_LIST } from "@/core/utils/routes"; import RowActions from "./RowActions"; import AzmayeshInfo from "@/components/dashboard/azmayesh/RowActions/ShowSampleOfAzmayesh/AzmayeshInfo"; import Toolbar from "./Toolbar"; +import moment from "jalali-moment"; const ShowSampleList = ({ sampleInfo, rowData }) => { const parsedData = (data) => { return data.data.map((item) => ({ ...JSON.parse(item.data), id: item.id, + updated_at: item.updated_at, })); }; @@ -22,7 +24,6 @@ const ShowSampleList = ({ sampleInfo, rowData }) => { id: "id", enableColumnFilter: false, datatype: "text", - filterFn: "notEquals", }, ...sampleInfo.map((item) => ({ accessorKey: item.id.toString(), @@ -30,8 +31,19 @@ const ShowSampleList = ({ sampleInfo, rowData }) => { id: item.id.toString(), enableColumnFilter: false, datatype: "text", - filterFn: "notEquals", })), + { + accessorKey: "updated_at", + header: "تاریخ بروزرسانی", + id: "updated_at", + enableColumnFilter: false, + datatype: "date", + Cell: ({ renderedCellValue }) => ( + + {renderedCellValue ? moment(renderedCellValue).locale("fa").format("HH:mm | YYYY/MM/DD") : "-"} + + ), + }, ], [sampleInfo] ); diff --git a/src/components/dashboard/azmayeshType/AzmayeshTypeList.jsx b/src/components/dashboard/azmayeshType/AzmayeshTypeList.jsx index c86cd83..b4d5fbb 100644 --- a/src/components/dashboard/azmayeshType/AzmayeshTypeList.jsx +++ b/src/components/dashboard/azmayeshType/AzmayeshTypeList.jsx @@ -28,9 +28,9 @@ const AzmayeshTypeList = () => { filterFn: "notEquals", }, { - accessorKey: "created_at", - header: "تاریخ ثبت", - id: "created_at", + accessorKey: "updated_at", + header: "تاریخ بروزرسانی", + id: "updated_at", enableColumnFilter: false, datatype: "date", filterFn: "equals", diff --git a/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/FormAndRequest.jsx b/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/FormAndRequest.jsx new file mode 100644 index 0000000..79a8d5b --- /dev/null +++ b/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/FormAndRequest.jsx @@ -0,0 +1,222 @@ +import { + Box, + Button, + Chip, + DialogActions, + DialogContent, + Divider, + FormControl, + FormHelperText, + Grid, + InputLabel, + List, + OutlinedInput, + Typography, +} from "@mui/material"; +import StyledForm from "@/core/components/StyledForm"; +import useRequest from "@/lib/hooks/useRequest"; +import { useState } from "react"; +import { useFieldArray, useForm } from "react-hook-form"; +import { array, object, string } from "yup"; +import { yupResolver } from "@hookform/resolvers/yup"; +import DataSaverOnIcon from "@mui/icons-material/DataSaverOn"; +import ListItems from "./ListItems"; +import AzmayeshFields from "./AzmayeshFields"; +import { ADD_AZMAYESH_TYPE, UPDATE_AZMAYESH_TYPE } from "@/core/utils/routes"; + +const validationSchema = object({ + name: string().required("عنوان نوع آزمایش را مشخص کنید!"), + fields: array() + .of( + object({ + name: string().required("نام فیلد الزامی است"), + type: string().required("نوع فیلد الزامی است"), + options: array().when("type", { + is: "select", + then: (schema) => schema.min(1, "حداقل باید یک آیتم برای نوع انتخابی ایجاد کنید."), + otherwise: (schema) => schema.notRequired(), + }), + }) + ) + .min(1, "حداقل باید یک فیلد ایجاد نمایید."), +}); + +const FormAndRequest = ({ setOpen, mutate, rowId, updateInfo }) => { + const requestServer = useRequest({ auth: true }); + const [selectedListItem, setSelectedListItem] = useState(null); + const [isSubmitting, setIsSubmitting] = useState(false); + + const handleClose = () => { + setOpen(false); + }; + + const defaultValues = { + name: updateInfo?.name || "", + fields: Array.isArray(updateInfo?.azmayesh_fields) + ? updateInfo.azmayesh_fields.map((field) => ({ + name: field.name || "", + type: field.type || "", + unit: field.unit || "", + options: field.option ? field.option : [], + })) + : [], + }; + + const { + control, + register, + handleSubmit, + setValue, + watch, + formState: { errors }, + } = useForm({ defaultValues, resolver: yupResolver(validationSchema), mode: "onBlur" }); + + const { fields, append, remove } = useFieldArray({ + control, + name: "fields", + }); + + const handleRemove = (index) => { + remove(index); + if (selectedListItem === index) { + setSelectedListItem(null); + } else if (selectedListItem > index) { + setSelectedListItem((prev) => prev - 1); + } + }; + + const handleAddField = () => { + append({ name: "", type: "", unit: "", options: [] }); + }; + + const onSubmit = async (data) => { + setIsSubmitting(true); + const formData = new FormData(); + formData.append("name", data.name); + data.fields.map((field, index) => { + formData.append(`fields[${index}][name]`, field.name); + formData.append(`fields[${index}][unit]`, field.unit); + formData.append(`fields[${index}][type]`, field.type); + if (field.options.length !== 0) formData.append(`fields[${index}][option]`, JSON.stringify(field.options)); + }); + + requestServer(updateInfo ? `${UPDATE_AZMAYESH_TYPE}/${rowId}` : ADD_AZMAYESH_TYPE, "post", { + data: formData, + }) + .then(() => { + mutate(); + handleClose(); + }) + .catch(() => {}) + .finally(() => setIsSubmitting(false)); + }; + + return ( + + + + + عنوان نوع آزمایش + + {errors.name ? errors.name.message : null} + + + + + + + + + + {fields.length !== 0 ? ( + fields.map((item, index) => ( + + )) + ) : ( + + فیلدی وجود ندارد + + )} + + + {errors.fields ? errors.fields.message : null} + + + + + + + + {selectedListItem !== null ? ( + + ) : ( + + + از بخش لیست فیلد ها یک آیتم را انتخاب نمایید + + + )} + + + + + + + + + ); +}; + +export default FormAndRequest; diff --git a/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/index.jsx b/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/index.jsx index 43eeb1a..ac89d5b 100644 --- a/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/index.jsx +++ b/src/components/dashboard/azmayeshType/Forms/CandUAzmayeshType/index.jsx @@ -1,222 +1,21 @@ -import { - Box, - Button, - Chip, - Dialog, - DialogActions, - DialogContent, - Divider, - FormControl, - FormHelperText, - Grid, - InputLabel, - List, - OutlinedInput, - Typography, -} from "@mui/material"; -import StyledForm from "@/core/components/StyledForm"; -import useRequest from "@/lib/hooks/useRequest"; -import { useState } from "react"; -import { useFieldArray, useForm } from "react-hook-form"; -import { array, object, string } from "yup"; -import { yupResolver } from "@hookform/resolvers/yup"; -import DataSaverOnIcon from "@mui/icons-material/DataSaverOn"; -import ListItems from "./ListItems"; -import AzmayeshFields from "./AzmayeshFields"; -import { ADD_AZMAYESH_TYPE, UPDATE_AZMAYESH_TYPE } from "@/core/utils/routes"; - -const validationSchema = object({ - name: string().required("عنوان نوع آزمایش را مشخص کنید!"), - fields: array() - .of( - object({ - name: string().required("نام فیلد الزامی است"), - type: string().required("نوع فیلد الزامی است"), - options: array().when("type", { - is: "select", - then: (schema) => schema.min(1, "حداقل باید یک آیتم برای نوع انتخابی ایجاد کنید."), - otherwise: (schema) => schema.notRequired(), - }), - }) - ) - .min(1, "حداقل باید یک فیلد ایجاد نمایید."), -}); - -const CandUAzmayeshType = ({ open, setOpen, mutate, rowId, updateInfo }) => { - const requestServer = useRequest({ auth: true }); - const [selectedListItem, setSelectedListItem] = useState(null); - const [isSubmitting, setIsSubmitting] = useState(false); - - const handleClose = () => { - setOpen(false); - }; - const defaultValues = { - name: updateInfo?.name || "", - fields: Array.isArray(updateInfo?.azmayesh_fields) - ? updateInfo.azmayesh_fields.map((field) => ({ - name: field.name || "", - type: field.type || "", - unit: field.unit || "", - options: field.option ? field.option : [], - })) - : [], - }; - - const { - control, - register, - handleSubmit, - setValue, - watch, - formState: { errors }, - } = useForm({ defaultValues, resolver: yupResolver(validationSchema), mode: "onBlur" }); - - const { fields, append, remove } = useFieldArray({ - control, - name: "fields", - }); - - const handleRemove = (index) => { - remove(index); - if (selectedListItem === index) { - setSelectedListItem(null); - } else if (selectedListItem > index) { - setSelectedListItem((prev) => prev - 1); - } - }; - - const handleAddField = () => { - append({ name: "", type: "", unit: "", options: [] }); - }; - - const onSubmit = async (data) => { - setIsSubmitting(true); - const formData = new FormData(); - formData.append("name", data.name); - data.fields.map((field, index) => { - formData.append(`fields[${index}][name]`, field.name); - formData.append(`fields[${index}][unit]`, field.unit); - formData.append(`fields[${index}][type]`, field.type); - if (field.options.length !== 0) formData.append(`fields[${index}][option]`, JSON.stringify(field.options)); - }); - - requestServer(updateInfo ? `${UPDATE_AZMAYESH_TYPE}/${rowId}` : ADD_AZMAYESH_TYPE, "post", { - data: formData, - }) - .then(() => { - mutate(); - handleClose(); - }) - .catch(() => {}) - .finally(() => setIsSubmitting(false)); - }; +import { Dialog } from "@mui/material"; +import FormAndRequest from "./FormAndRequest"; +import DialogLoading from "@/core/components/DialogLoading"; +const CandUAzmayeshType = ({ open, setOpen, mutate, rowId, updateInfo, loadingOpen, fromUpdate = false }) => { + const isUpdateReady = fromUpdate ? updateInfo !== null : true; return ( - - - - - - عنوان نوع آزمایش - - {errors.name ? errors.name.message : null} - - - - - - - - - - {fields.length !== 0 ? ( - fields.map((item, index) => ( - - )) - ) : ( - - فیلدی وجود ندارد - - )} - - - {errors.fields ? errors.fields.message : null} - - - - - - - - {selectedListItem !== null ? ( - - ) : ( - - - از بخش لیست فیلد ها یک آیتم را انتخاب نمایید - - - )} - - - - - - - - + + {isUpdateReady ? ( + + ) : ( + + )} ); }; diff --git a/src/components/dashboard/azmayeshType/RowActions/UpdateAzmayeshType/index.jsx b/src/components/dashboard/azmayeshType/RowActions/UpdateAzmayeshType/index.jsx index e9f1bde..b94dcf6 100644 --- a/src/components/dashboard/azmayeshType/RowActions/UpdateAzmayeshType/index.jsx +++ b/src/components/dashboard/azmayeshType/RowActions/UpdateAzmayeshType/index.jsx @@ -9,15 +9,18 @@ const UpdateAzmayeshType = ({ rowId, mutate }) => { const requestServer = useRequest({ auth: true }); const [openUpdateDialog, setOpenUpdateDialog] = useState(false); const [updateInfo, setUpdateInfo] = useState(null); + const [loadingOpen, setLoadingOpen] = useState(false); const handleGetInfo = () => { + setLoadingOpen(true); + setOpenUpdateDialog(true); requestServer(`${GET_AZMAYESH_TYPE_LIST_TABLE}/${rowId}`, "get") .then((response) => { setUpdateInfo(response.data.data); }) .catch(() => {}) .finally(() => { - setOpenUpdateDialog(true); + setLoadingOpen(false); }); }; @@ -39,6 +42,8 @@ const UpdateAzmayeshType = ({ rowId, mutate }) => { mutate={mutate} updateInfo={updateInfo} rowId={rowId} + loadingOpen={loadingOpen} + fromUpdate={true} /> )} diff --git a/src/core/components/DialogLoading.jsx b/src/core/components/DialogLoading.jsx new file mode 100644 index 0000000..18bcbcf --- /dev/null +++ b/src/core/components/DialogLoading.jsx @@ -0,0 +1,36 @@ +import { Box, Skeleton } from "@mui/material"; + +const DialogLoading = ({ loadingOpen }) => { + return ( + + + + + + + + + + + + + + + ); +}; + +export default DialogLoading; diff --git a/src/core/utils/theme.js b/src/core/utils/theme.js index f32031b..85c8f68 100644 --- a/src/core/utils/theme.js +++ b/src/core/utils/theme.js @@ -17,6 +17,16 @@ const theme = createTheme({ contrastText: "#fff", }, }, + components: { + MuiBackdrop: { + styleOverrides: { + root: { + backdropFilter: "blur(2px)", + backgroundColor: "rgba(0, 0, 0, 0.5)", + }, + }, + }, + }, }); export default theme; From 41051ed9b532418385060a905dcd2037ace77698 Mon Sep 17 00:00:00 2001 From: Amirhossein Mahmoodi Date: Sun, 24 Nov 2024 11:47:11 +0330 Subject: [PATCH 2/2] change version --- .env.local.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.local.example b/.env.local.example index 52ae481..78f8096 100644 --- a/.env.local.example +++ b/.env.local.example @@ -1,3 +1,3 @@ -NEXT_PUBLIC_VERSION="0.9.4" +NEXT_PUBLIC_VERSION="0.9.5" NEXT_PUBLIC_API_URL="https://rms.witel.ir" NEXT_PUBLIC_MAPTILE_ENDPOINT="https://rmsmap.rmto.ir/141map" \ No newline at end of file