add create formularium func
This commit is contained in:
@@ -8,19 +8,156 @@ import useSettings from '../../../../hooks/useSettings';
|
|||||||
import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
|
import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
|
||||||
// components
|
// components
|
||||||
import axios from '../../../../utils/axios';
|
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 Label from '@/components/Label';
|
||||||
import TableMoreMenu from '@/components/table/TableMoreMenu';
|
import TableMoreMenu from '@/components/table/TableMoreMenu';
|
||||||
import InfoDetail from "./InfoDetail";
|
import InfoDetail from "./InfoDetail";
|
||||||
import { DetailCorpFormularium } from "./Types";
|
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) {
|
export default function CategoryDetail(props: DetailCorpFormularium) {
|
||||||
|
type FormValuesProps = {
|
||||||
|
value: string;
|
||||||
|
active: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
const [isDialogOpen, setDialogOpen] = useState(false)
|
const [isDialogOpen, setDialogOpen] = useState(false)
|
||||||
|
let titles = {
|
||||||
|
name: 'Update Status',
|
||||||
|
icon: '-'
|
||||||
|
}
|
||||||
|
const [dataValue, setDataValue] = useState<CorporateFormulariumList | null>(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<FormValuesProps>({
|
||||||
|
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 = () => (
|
||||||
|
<>
|
||||||
|
<Stack paddingX={2} paddingY={2}>
|
||||||
|
<Typography variant='subtitle1'>Are you sure to {dataValue?.active === "1" ? 'Inactive' : 'Active'} this Formularium ?</Typography>
|
||||||
|
<Stack>
|
||||||
|
<Card>
|
||||||
|
<Grid container paddingX={2} paddingY={2}>
|
||||||
|
<Grid item xs={5} md={5}>
|
||||||
|
<Typography variant='inherit'>Formularium Name</Typography>
|
||||||
|
</Grid>
|
||||||
|
<Grid item xs={7}>
|
||||||
|
<Typography variant='subtitle1'>{dataValue?.category}</Typography>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Typography marginTop={5} marginBottom={3} variant='subtitle1'>Reason for Update</Typography>
|
||||||
|
<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
|
||||||
|
<RHFSelect
|
||||||
|
name='reason'
|
||||||
|
label="Reason for update"
|
||||||
|
>
|
||||||
|
<option value=""></option>
|
||||||
|
<option value="Agreement changed">Agreement changed</option>
|
||||||
|
<option value="Endorsement">Endorsement</option>
|
||||||
|
<option value="Renewal">Renewal</option>
|
||||||
|
<option value="Wrong Setting">Wrong Setting</option>
|
||||||
|
</RHFSelect>
|
||||||
|
<Stack
|
||||||
|
alignItems="center"
|
||||||
|
justifyContent="flex-end"
|
||||||
|
direction={{xs: 'column', md: 'row'}}
|
||||||
|
spacing={2}
|
||||||
|
marginTop={5}
|
||||||
|
>
|
||||||
|
<Stack direction="row" spacing={1}>
|
||||||
|
<Button
|
||||||
|
sx={{ boxShadow: 'none'}}
|
||||||
|
variant='outlined'
|
||||||
|
size='medium'
|
||||||
|
fullWidth={true}
|
||||||
|
onClick={() => setDialogOpen(false)}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
{dataValue?.active == "1" ?
|
||||||
|
<LoadingButton
|
||||||
|
sx={{boxShadow: '0px 2px 4px rgba(0,0,0,0.1)'}}
|
||||||
|
type='submit'
|
||||||
|
variant='contained'
|
||||||
|
size='medium'
|
||||||
|
fullWidth={true}
|
||||||
|
loading={isSubmitting}
|
||||||
|
color='error'
|
||||||
|
>
|
||||||
|
Inactive
|
||||||
|
</LoadingButton> :
|
||||||
|
<LoadingButton
|
||||||
|
sx={{boxShadow: '0px 2px 4px rgba(0,0,0,0.1)'}}
|
||||||
|
type='submit'
|
||||||
|
variant='contained'
|
||||||
|
size='medium'
|
||||||
|
fullWidth={true}
|
||||||
|
loading={isSubmitting}
|
||||||
|
color='success'
|
||||||
|
>
|
||||||
|
Active
|
||||||
|
</LoadingButton>
|
||||||
|
}
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</FormProvider>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
@@ -41,7 +178,14 @@ export default function CategoryDetail(props: DetailCorpFormularium) {
|
|||||||
<FindInPageOutlined />
|
<FindInPageOutlined />
|
||||||
Detail
|
Detail
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={() => setDialogOpen(!isDialogOpen)}>
|
<MenuItem onClick={() => handleActivate(true, {
|
||||||
|
id: 0,
|
||||||
|
formulaurium_category_id: 0,
|
||||||
|
category: '-',
|
||||||
|
description: '-',
|
||||||
|
active: '1'
|
||||||
|
})
|
||||||
|
}>
|
||||||
<CachedOutlined />
|
<CachedOutlined />
|
||||||
Update Status
|
Update Status
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
@@ -59,11 +203,14 @@ export default function CategoryDetail(props: DetailCorpFormularium) {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
|
|
||||||
{/* TODO: dialog update status */}
|
{/* TODO: dialog update status */}
|
||||||
{/* <DialogUpdateStatus
|
<DialogUpdateStatus
|
||||||
openDialog={isDialogOpen}
|
openDialog={isDialogOpen}
|
||||||
setOpenDialog={setDialogOpen}
|
setOpenDialog={setDialogOpen}
|
||||||
id={0}
|
description={dataDescription}
|
||||||
/> */}
|
title={titles}
|
||||||
|
data={dataValue}
|
||||||
|
content={getContent()}
|
||||||
|
/>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,56 @@
|
|||||||
|
import * as Yup from 'yup';
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { FormProvider, RHFSelect } from "../../../../components/hook-form";
|
import { FormProvider, RHFSelect } from "../../../../components/hook-form";
|
||||||
import { Button, Card, Grid, Typography } from "@mui/material";
|
import { Button, Card, Grid, Typography } from "@mui/material";
|
||||||
import { Stack, fontWeight } from "@mui/system";
|
import { Stack, fontWeight } from "@mui/system";
|
||||||
import { useNavigate, useParams } from "react-router";
|
import { useNavigate, useParams } from "react-router";
|
||||||
import React, { useEffect } from "react";
|
import React, { useEffect, useMemo } from "react";
|
||||||
import { DetailCorpFormularium } from "./Types";
|
import { DetailCorpFormularium } from "./Types";
|
||||||
import axios from "@/utils/axios";
|
import axios from "@/utils/axios";
|
||||||
|
import { LoadingButton } from "@mui/lab";
|
||||||
|
import { enqueueSnackbar } from "notistack";
|
||||||
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
|
||||||
|
|
||||||
export default function CorporateFormulariumCreateForm() {
|
export default function CorporateFormulariumCreateForm() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { corporate_id } = useParams();
|
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) => {
|
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 {
|
const {
|
||||||
|
setError,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
formState: { isSubmitting }
|
||||||
} = methods;
|
} = methods;
|
||||||
|
|
||||||
// Data Dummy
|
// Data Dummy
|
||||||
@@ -47,15 +77,10 @@ export default function CorporateFormulariumCreateForm() {
|
|||||||
<Card sx={{p:2}}>
|
<Card sx={{p:2}}>
|
||||||
<Stack spacing={3}>
|
<Stack spacing={3}>
|
||||||
<Typography variant="h6">Formularium Name*</Typography>
|
<Typography variant="h6">Formularium Name*</Typography>
|
||||||
<RHFSelect name="category" label="Category" placeholder="Category" fullWidth={true}>
|
<RHFSelect name="id" label="Category" placeholder="Category" fullWidth={true}>
|
||||||
{/* {options.map((option, index) => (
|
|
||||||
<option key={index} value={option}>
|
|
||||||
{option}
|
|
||||||
</option>
|
|
||||||
))} */}
|
|
||||||
<option value=""/>
|
<option value=""/>
|
||||||
{dataDropdownData?.map((item, index) => (
|
{dataDropdownData?.map((item, index) => (
|
||||||
<option key={index} value={item.name}>
|
<option key={index} value={item.id}>
|
||||||
{item.name}
|
{item.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
@@ -79,7 +104,9 @@ export default function CorporateFormulariumCreateForm() {
|
|||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
<LoadingButton
|
||||||
|
loading={isSubmitting}
|
||||||
|
type='submit'
|
||||||
variant="contained"
|
variant="contained"
|
||||||
sx={{
|
sx={{
|
||||||
p: 1,
|
p: 1,
|
||||||
@@ -93,7 +120,7 @@ export default function CorporateFormulariumCreateForm() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Create
|
Create
|
||||||
</Button>
|
</LoadingButton>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
Reference in New Issue
Block a user