import { useEffect, useState, useRef } from "react"; // material-ui import { Button, Stack, Typography, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, FormLabel, Autocomplete, TextField, Grid, } from '@mui/material'; import LoadingButton from '@mui/lab/LoadingButton'; import * as ComboData from "utils/ComboData"; import { useFormik, FormikProvider } from 'formik'; import * as yup from 'yup'; const StatusChangeDialog = (props) => { const [content, setContent] = useState(); const [dialogTitle, setDialogTitle] = useState("Confirm"); const [prositiveBtnText, setPrositiveBtnText] = useState("Confirm"); const [remarks, setRemarks] = useState(""); const [helperText, setHelperText] = useState(""); const [comboInputValue, setComboInputValue] = useState({}); const [positiveSubmitting, setPositiveSubmitting] = useState(false); const positiveOnceRef = useRef(false); const groupTitleComboList = ComboData.groupTitle; const confirmLoading = Boolean(props.confirmLoading) || positiveSubmitting; const gazetteGroupMissing = props.getStatus === "genGazetteCode" && Object.keys(props.selectedGazetteGroup ?? {}).length === 0; useEffect(() => { setComboInputValue({}); if (props.getStatus == "genGazetteCode") { setDialogTitle("Gen Gazette Code"); setPrositiveBtnText("Submit"); return getGazetteCodeContent() } else if (props.getStatus == "notAccepted") { setDialogTitle("Not Accept Reason"); setPrositiveBtnText("Mark Not Accept"); return getNotAcceptedContent(); }if (props.getStatus == "resubmit") { setDialogTitle("Re-Submit Reason"); setPrositiveBtnText("Mark Re-Submit"); return getNotAcceptedContent(); }if (props.getStatus == "paid") { setDialogTitle("Action Confirm"); setPrositiveBtnText("Confirm"); return getConfirmContent("Are you sure to mark this application as paid?"); } else { setDialogTitle("Action Confirm"); setPrositiveBtnText("Confirm"); let str = "Do you want to set the application status to '" + props.getStatus + "'?" return getConfirmContent(str); } }, [props.getStatus]); useEffect(() => { if (!props.open) { positiveOnceRef.current = false; setPositiveSubmitting(false); } }, [props.open]); const wasConfirmLoadingRef = useRef(false); useEffect(() => { if (wasConfirmLoadingRef.current && !props.confirmLoading) { positiveOnceRef.current = false; setPositiveSubmitting(false); } wasConfirmLoadingRef.current = Boolean(props.confirmLoading); }, [props.confirmLoading]); const acceptedHandle = () => () => { if (confirmLoading) return; if (positiveOnceRef.current) return; const statusKey = props.getStatus; if (statusKey === "notAccepted" || statusKey === "resubmit") { if (!remarks || remarks === "") { setHelperText("Please enter reason"); return; } setHelperText(""); props.setReason({ "reason": remarks }); } positiveOnceRef.current = true; setPositiveSubmitting(true); props.setStatusWindowAccepted(true); }; const formik = useFormik({ initialValues: ({ username: '', }), validationSchema: yup.object().shape({ }), }); const getConfirmContent = (text) => { setContent( {text} ); } const getNotAcceptedContent = () => { setContent( { setRemarks(newValues.target.value); setHelperText(""); }} variant="outlined" InputProps={ { style: { minHeight: '42.5px', maxHeight: '50vh', height: 'auto' }, } } /> ); } const getGazetteCodeContent = () => { setContent( Target Issue: {props.gazetteIssue + ", "}{props.issueNum + ", "}{props.issueDate} Gazette Group: options} getOptionLabel={(option) => { if (option == null) return ""; if (typeof option === "string") return option; return option.label != null ? String(option.label) : ""; }} inputValue={comboInputValue.label} onChange={(event, newValue) => { if (newValue != null && newValue != {}) { // console.log(comboInputValue) props.setSelectedGazetteGroupInputType(newValue.label); setComboInputValue(newValue); props.setSelectedGazetteGroup(newValue); formik.setFieldValue("", "") } else { props.setSelectedGazetteGroupInputType(""); } }} sx={{ '& .MuiInputBase-root': { alignItems: 'center' }, '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' }, '& .MuiOutlinedInput-root': { height: 40 } }} renderInput={(params) => } /> ); } return ( { if (confirmLoading) return; props.handleClose(); }} fullWidth={true} maxWidth={'md'} > {dialogTitle}
{content}
{prositiveBtnText}
); }; export default StatusChangeDialog;