From d7e1cc0a9836428ea3467607a7506fd28c679c44 Mon Sep 17 00:00:00 2001 From: adibwp Date: Tue, 24 Oct 2023 15:17:05 +0700 Subject: [PATCH] add create formularium func --- .../Formularium/New/CategoryDetail.tsx | 167 ++++++++++++++++-- .../pages/Corporates/Formularium/New/Form.tsx | 55 ++++-- .../Formularium/New/StatusUpdateDialog.tsx | 0 3 files changed, 198 insertions(+), 24 deletions(-) create mode 100644 frontend/dashboard/src/pages/Corporates/Formularium/New/StatusUpdateDialog.tsx diff --git a/frontend/dashboard/src/pages/Corporates/Formularium/New/CategoryDetail.tsx b/frontend/dashboard/src/pages/Corporates/Formularium/New/CategoryDetail.tsx index 3062f7c0..c1194c1f 100644 --- a/frontend/dashboard/src/pages/Corporates/Formularium/New/CategoryDetail.tsx +++ b/frontend/dashboard/src/pages/Corporates/Formularium/New/CategoryDetail.tsx @@ -8,19 +8,156 @@ import useSettings from '../../../../hooks/useSettings'; import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; // components import axios from '../../../../utils/axios'; -import { LaravelPaginatedData } from '../../../../@types/paginated-data'; -import { Icd } from '../../../../@types/diagnosis'; -import BasePagination from '../../../../components/BasePagination'; import Label from '@/components/Label'; import TableMoreMenu from '@/components/table/TableMoreMenu'; import InfoDetail from "./InfoDetail"; import { DetailCorpFormularium } from "./Types"; -import DialogUpdateStatus from '../../DialogUpdateStatus'; - +import DialogUpdateStatus from '@/components/DialogUpdateStatus' +import { RHFSelect, FormProvider } from '@/components/hook-form'; +import { yupResolver } from '@hookform/resolvers/yup'; +import { useForm } from 'react-hook-form'; +import * as Yup from 'yup'; +import { CorporateFormulariumList } from "./Types"; export default function CategoryDetail(props: DetailCorpFormularium) { + type FormValuesProps = { + value: string; + active: boolean; + }; + const [open, setOpen] = React.useState(false); const [isDialogOpen, setDialogOpen] = useState(false) + let titles = { + name: 'Update Status', + icon: '-' + } + const [dataValue, setDataValue] = useState(null); + const [dataDescription, setDescriptionValue] = useState(''); + const [url, setUrl] = useState(''); + + const handleActivate = (isOpen: boolean, dataValue: CorporateFormulariumList) => { + console.log(dataValue) + setDialogOpen(isOpen) + setDataValue(dataValue) + setDescriptionValue('Are you sure to inactive this service ?') + setUrl(url) + }; + + const NewCorporateSchema = Yup.object().shape({ + reason: Yup.string().required('Reason Edit is required'), + }); + + const methods = useForm({ + resolver: yupResolver(NewCorporateSchema) + }) + + const { + reset, + handleSubmit, + formState: { isSubmitting }, + } = methods; + + const onSubmit = async (row : any) => { + try { + handleUpdate(dataValue?.active, dataValue?.id) + } catch (error: any) { + console.log('data gagal'); + } + + const ascent = document?.querySelector('ascent'); + if (ascent != null) { + ascent.innerHTML = ''; + } + }; + + const handleUpdate = (active: string, id: number) => { + console.log(active) + axios + .put(`/corporates/${id}/formulariums-update-status/${id}`, { + active: active, + reason: reason + }) + .then((res) => { + window.location.reload(); + }); + } + + const getContent = () => ( + <> + + Are you sure to {dataValue?.active === "1" ? 'Inactive' : 'Active'} this Formularium ? + + + + + Formularium Name + + + {dataValue?.category} + + + + + Reason for Update + + + + + + + + + + + + {dataValue?.active == "1" ? + + Inactive + : + + Active + + } + + + + + + + ); return ( @@ -41,7 +178,14 @@ export default function CategoryDetail(props: DetailCorpFormularium) { Detail - setDialogOpen(!isDialogOpen)}> + handleActivate(true, { + id: 0, + formulaurium_category_id: 0, + category: '-', + description: '-', + active: '1' + }) + }> Update Status @@ -59,11 +203,14 @@ export default function CategoryDetail(props: DetailCorpFormularium) { {/* TODO: dialog update status */} - {/* */} + setOpenDialog={setDialogOpen} + description={dataDescription} + title={titles} + data={dataValue} + content={getContent()} + /> ) } \ No newline at end of file diff --git a/frontend/dashboard/src/pages/Corporates/Formularium/New/Form.tsx b/frontend/dashboard/src/pages/Corporates/Formularium/New/Form.tsx index 2545ea66..95e1566c 100644 --- a/frontend/dashboard/src/pages/Corporates/Formularium/New/Form.tsx +++ b/frontend/dashboard/src/pages/Corporates/Formularium/New/Form.tsx @@ -1,26 +1,56 @@ +import * as Yup from 'yup'; import { useForm } from "react-hook-form"; import { FormProvider, RHFSelect } from "../../../../components/hook-form"; import { Button, Card, Grid, Typography } from "@mui/material"; import { Stack, fontWeight } from "@mui/system"; import { useNavigate, useParams } from "react-router"; -import React, { useEffect } from "react"; +import React, { useEffect, useMemo } from "react"; import { DetailCorpFormularium } from "./Types"; import axios from "@/utils/axios"; +import { LoadingButton } from "@mui/lab"; +import { enqueueSnackbar } from "notistack"; +import { yupResolver } from '@hookform/resolvers/yup'; export default function CorporateFormulariumCreateForm() { const navigate = useNavigate(); const { corporate_id } = useParams(); - const methods = useForm([ - // methods Logic?? - ]); + + const NewCorporaeFormulariumSchema = Yup.object().shape({ + id: Yup.string().required('required'), + }); + + const methods = useForm({ + resolver: yupResolver(NewCorporaeFormulariumSchema), + }); const onSubmit = async (data: any) => { - // logic onSubmit + await axios + .post(`/corporates/${corporate_id}/formulariums`, data) + .then((res) => { + console.log(res.data.message) + enqueueSnackbar('Formularium created successfully', { variant: 'success'}) + }) + .then((res) => { + navigate(`/corporates/${corporate_id}/formulariums/`, { replace: true }) + }) + .catch(({ response }) => { + if (response.status === 422) { + for (const [key, value] of Object.entries(response.data.errors)) { + setError(key, {message: value[0]}); + enqueueSnackbar(value[0] ?? 'Failed Processing Request', { variant: 'error' }); + } + } + else { + enqueueSnackbar('Create Failed: '+ response.data.message, { variant: "error"}) + } + }); }; const { + setError, handleSubmit, + formState: { isSubmitting } } = methods; // Data Dummy @@ -47,15 +77,10 @@ export default function CorporateFormulariumCreateForm() { Formularium Name* - - {/* {options.map((option, index) => ( - - ))} */} + ))} @@ -79,7 +104,9 @@ export default function CorporateFormulariumCreateForm() { Cancel - + diff --git a/frontend/dashboard/src/pages/Corporates/Formularium/New/StatusUpdateDialog.tsx b/frontend/dashboard/src/pages/Corporates/Formularium/New/StatusUpdateDialog.tsx new file mode 100644 index 00000000..e69de29b