|
- // material-ui
- import {
- Button,
- Grid,
- TextField,
- Autocomplete,
- Typography
- } from '@mui/material';
- import MainCard from "components/MainCard";
- import { useForm } from "react-hook-form";
- import * as React from "react";
- // import * as DateUtils from "utils/DateUtils";
- import {PNSPS_BUTTON_THEME} from "themes/buttonConst";
- import {ThemeProvider} from "@emotion/react";
- import { GeneralConfirmWindow } from "utils/CommonFunction";
- import { isGrantedAny } from "auth/utils";
- // import * as ComboData from "utils/ComboData";
- // ==============================|| DASHBOARD - DEFAULT ||============================== //
-
-
- const SearchHolidayForm = ({ applySearch, comboData, searchCriteria, onGridReady, onDeleteYear, waitDelete }) => {
- const [selectedYear, setSelectedYear] = React.useState(null);
- // const [defaultYear, setDefaultYear] = React.useState(searchCriteria.year);
- const [comboList, setComboList] = React.useState([]);
- const [isConfirmOpen, setIsConfirmOpen] = React.useState(false);
- // const [onReady, setOnReady] = React.useState(false);
-
- const {
- // register,
- handleSubmit } = useForm()
-
- const onSubmit = () => {
- if (selectedYear != null) {
- const temp = {
- year: selectedYear.label,
- };
- applySearch(temp);
- }
- };
-
- React.useEffect(() => {
- const nextCombo = Array.isArray(comboData) ? [...comboData] : [];
- setComboList(nextCombo);
-
- if (nextCombo.length === 0) {
- setSelectedYear(null);
- return;
- }
-
- const criteriaYear = searchCriteria?.year;
- const matchedByCriteria = criteriaYear != null
- ? nextCombo.find((obj) => String(obj.label) === String(criteriaYear))
- : null;
- if (matchedByCriteria) {
- setSelectedYear(matchedByCriteria);
- return;
- }
-
- const matchedBySelected = selectedYear
- ? nextCombo.find((obj) => String(obj.label) === String(selectedYear.label))
- : null;
- setSelectedYear(matchedBySelected || nextCombo[0]);
- }, [comboData, searchCriteria]);
-
-
- return (
- <MainCard xs={12} md={12} lg={12}
- border={false}
- content={false}
- >
-
- <form onSubmit={handleSubmit(onSubmit)} >
- <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1}} width="98%">
- {/*row 1*/}
- <Grid item justifyContent="space-between" alignItems="center" sx={{mt:1,ml:3,mb:1}}>
- <Typography variant="h5" >
- Year
- </Typography>
- </Grid>
- {/*row 2*/}
-
- <Grid container display="flex" alignItems={"center"}>
- <Grid item xs={9} s={6} md={4} lg={4} sx={{ ml: 3, mr: 3, mb: 3 }}>
- <Autocomplete
- disablePortal
- id="year-combo"
- value={selectedYear}
- // defaultValue={selectedYear}
- options={comboList}
- // disabled={checkCountry}
- getOptionLabel={(option) =>
- option != null && typeof option === "object" && option.label != null
- ? String(option.label)
- : ""
- }
- isOptionEqualToValue={(option, value) =>
- String(option?.label) === String(value?.label)
- }
- onChange={(event, newValue) => {
- setSelectedYear(newValue);
- }}
- sx={{
- '& .MuiInputBase-root': { alignItems: 'center' },
- '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' },
- '& .MuiOutlinedInput-root': { height: 40 }
- }}
- renderInput={(params) => <TextField {...params} placeholder={""}/>}
- />
- </Grid>
-
- {isGrantedAny(["MAINTAIN_GAZETTE_ISSUE"]) ?
- <ThemeProvider theme={PNSPS_BUTTON_THEME}>
- <Grid item sx={{ mb: 3, mr: 3 }}>
- <Button
- color="delete"
- variant="contained"
- disabled={!selectedYear || waitDelete || onGridReady}
- onClick={() => setIsConfirmOpen(true)}
- >
- Delete
- </Button>
- </Grid>
- </ThemeProvider>
- : null
- }
-
- <Grid item xs={9} s={6} md={4} lg={4} sx={{ ml: 3, mr: 3, mb: 3 }}>
- {/* <TextField
- fullWidth
- InputLabelProps={{
- shrink: true
- }}
- {...register("dateTo")}
- InputProps={{ inputProps: { min: minDate } }}
- onChange={(newValue) => {
- setMaxDate(DateUtils.dateValue(newValue));
- }}
- id="dateTo"
- type="date"
- label="To"
- defaultValue={searchCriteria.dateTo}
- /> */}
- </Grid>
-
- {/* <Grid item xs={9} s={6} md={4} lg={3}>
- </Grid> */}
- </Grid>
- <Grid container justifyContent="flex-end" direction="row" alignItems="center" spacing={3}>
- <ThemeProvider theme={PNSPS_BUTTON_THEME}>
- <Grid item sx={{ ml: 3, mb: 3, }} >
- <Button
- variant="contained"
- type="submit"
- disabled={onGridReady}
- >
- Search
- </Button>
- </Grid>
- </ThemeProvider>
- </Grid>
- </Grid>
- </form>
-
- <GeneralConfirmWindow
- isWindowOpen={isConfirmOpen}
- title="Confirm Delete"
- content={selectedYear
- ? `Confirm to delete all holidays of year ${selectedYear.label}?`
- : "Confirm to delete all holidays of the selected year?"}
- onNormalClose={() => setIsConfirmOpen(false)}
- onConfirmClose={() => {
- setIsConfirmOpen(false);
- if (selectedYear != null && onDeleteYear) {
- onDeleteYear(selectedYear.label);
- }
- }}
- />
- </MainCard>
- );
- };
-
- export default SearchHolidayForm;
|