|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { |
| 4 | + Box, |
| 5 | + Button, |
| 6 | + CircularProgress, |
| 7 | + Dialog, |
| 8 | + DialogActions, |
| 9 | + DialogContent, |
| 10 | + DialogTitle, |
| 11 | + Grid, |
| 12 | + Typography |
| 13 | +} from "@mui/material"; |
| 14 | +import { Form, Formik } from "formik"; |
| 15 | +import * as Yup from "yup"; |
| 16 | +import { FormikDatePickerField } from "../../form/FormikDatePickerField"; |
| 17 | +import moment from "moment"; |
| 18 | +import { useRecoilValue } from "recoil"; |
| 19 | +import { CityService } from "../../services/api"; |
| 20 | +import { FormikSelectField } from "../../form/FormikSelectField"; |
| 21 | +import { CityType, DistrictType, PostAlertPayload, RegionType } from "../../types"; |
| 22 | +import axios, { AxiosError } from "axios"; |
| 23 | +import { globalUrls } from "../../services/api/urls"; |
| 24 | +import { useSnackbar } from "notistack"; |
| 25 | + |
| 26 | +interface CreateReportModalProps { |
| 27 | + open: boolean, |
| 28 | + onClose: any |
| 29 | +} |
| 30 | + |
| 31 | +function capitalizeFirstLetter(text: string) { |
| 32 | + return text.charAt(0).toUpperCase() + text.slice(1); |
| 33 | +} |
| 34 | + |
| 35 | +interface FormState { |
| 36 | + region_id: number | null, |
| 37 | + city_id: number | null, |
| 38 | +} |
| 39 | + |
| 40 | +const CreateReportModal = (props: CreateReportModalProps) => { |
| 41 | + const { t, i18n: { language } } = useTranslation(); |
| 42 | + moment.locale(language); |
| 43 | + const { open, onClose } = props; |
| 44 | + const submitBtnRef = useRef(null); |
| 45 | + const [loading, setLoading] = useState(false); |
| 46 | + const { enqueueSnackbar } = useSnackbar(); |
| 47 | + |
| 48 | + const [formState, setFormState] = useState<FormState>({ |
| 49 | + region_id: null, |
| 50 | + city_id: null |
| 51 | + }); |
| 52 | + |
| 53 | + const [cities, setCities] = useState<CityType[]>([]); |
| 54 | + const [districts, setDistricts] = useState<DistrictType[]>([]); |
| 55 | + |
| 56 | + const regionResponse = useRecoilValue(CityService.getRegions); |
| 57 | + const citiesResponse = useRecoilValue(CityService.getCities); |
| 58 | + const districtResponse = useRecoilValue(CityService.getDistricts); |
| 59 | + |
| 60 | + |
| 61 | + const regions: RegionType[] = regionResponse?.data ?? []; |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + setCities((citiesResponse?.data ?? []).filter((item: CityType) => item.region_id == formState.region_id)); |
| 65 | + setDistricts((districtResponse?.data ?? []).filter((item: DistrictType) => item.city_id == formState.city_id)); |
| 66 | + }, [formState]); |
| 67 | + |
| 68 | + |
| 69 | + const validations = { |
| 70 | + type: Yup.string().required(t("global_field_require")), |
| 71 | + date: Yup.string().required(t("global_field_require")), |
| 72 | + region_id: Yup.string().required(t("global_field_require")), |
| 73 | + city_id: Yup.string().required(t("global_field_require")), |
| 74 | + district_id: Yup.string().required(t("global_field_require")), |
| 75 | + } |
| 76 | + |
| 77 | + const initialValue = { |
| 78 | + type: "electricity", |
| 79 | + date: "", |
| 80 | + region_id: "", |
| 81 | + city_id: "", |
| 82 | + district_id: "", |
| 83 | + }; |
| 84 | + |
| 85 | + |
| 86 | + const handleSubmit = (payload: PostAlertPayload) => { |
| 87 | + setLoading(true) |
| 88 | + axios.post(globalUrls.POST_ALERT, payload) |
| 89 | + .then((response) => { |
| 90 | + enqueueSnackbar(t("add_alert_success_message"), { |
| 91 | + variant: "success", |
| 92 | + }); |
| 93 | + axios.get(globalUrls.GET_ALERTS) |
| 94 | + onClose(); |
| 95 | + }) |
| 96 | + .catch((error: AxiosError) => { |
| 97 | + enqueueSnackbar(error.message, { |
| 98 | + variant: "error", |
| 99 | + }); |
| 100 | + }) |
| 101 | + .finally(() => setLoading(false)) |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <Dialog |
| 106 | + open={open} |
| 107 | + onClose={() => onClose()} |
| 108 | + maxWidth={"sm"} |
| 109 | + fullWidth |
| 110 | + > |
| 111 | + <DialogTitle |
| 112 | + > |
| 113 | + <Typography |
| 114 | + variant={"h3"} |
| 115 | + > |
| 116 | + {t('create_report_modal_title')} |
| 117 | + </Typography> |
| 118 | + </DialogTitle> |
| 119 | + <DialogContent> |
| 120 | + <Box sx={{ pt: 2 }}> |
| 121 | + <Formik |
| 122 | + initialValues={initialValue} |
| 123 | + validationSchema={Yup.object().shape(validations)} |
| 124 | + onSubmit={(values: any, formikHelpers) => { |
| 125 | + const payload: PostAlertPayload = { |
| 126 | + city_id: values.city_id, |
| 127 | + district_id: values.district_id, |
| 128 | + date: values.date.split(" ")[0], |
| 129 | + begin_time: values.date.split(" ")[1], |
| 130 | + region_id: values.region_id, |
| 131 | + type: values.type |
| 132 | + } |
| 133 | + handleSubmit(payload); |
| 134 | + }}> |
| 135 | + {({ |
| 136 | + setFieldTouched, |
| 137 | + handleSubmit, |
| 138 | + setFieldValue, |
| 139 | + errors |
| 140 | + }) => |
| 141 | + <Form onSubmit={handleSubmit} className={'form'}> |
| 142 | + <Grid container spacing={2}> |
| 143 | + <FormikSelectField |
| 144 | + xs={12} |
| 145 | + variant={'outlined'} |
| 146 | + label={t('report_field_type')} |
| 147 | + name={'type'} |
| 148 | + option={ |
| 149 | + [ |
| 150 | + { |
| 151 | + label: t("label_alert_type_electricity"), |
| 152 | + value: "electricity" |
| 153 | + }, |
| 154 | + { |
| 155 | + label: t("label_alert_type_internet"), |
| 156 | + value: "internet" |
| 157 | + }, |
| 158 | + { |
| 159 | + label: t("label_alert_type_water"), |
| 160 | + value: "water" |
| 161 | + }, |
| 162 | + ] |
| 163 | + } |
| 164 | + /> |
| 165 | + <FormikDatePickerField |
| 166 | + value={moment(new Date()).format("YYYY-MM-DD HH:mm")} |
| 167 | + xs={12} |
| 168 | + variant={'outlined'} |
| 169 | + label={t('report_field_date')} |
| 170 | + name={'date'} |
| 171 | + onChange={(date) => { |
| 172 | + setFieldValue("date", date); |
| 173 | + }} |
| 174 | + /> |
| 175 | + <FormikSelectField |
| 176 | + xs={12} |
| 177 | + variant={'outlined'} |
| 178 | + label={t('report_field_region')} |
| 179 | + name={'region_id'} |
| 180 | + onChange={(value: any) => { |
| 181 | + setFormState({ ...formState, region_id: value }) |
| 182 | + localStorage.setItem("myRegionName", value.toString()); |
| 183 | + }} |
| 184 | + option={ |
| 185 | + regions.map((region, index) => { |
| 186 | + return { |
| 187 | + label: capitalizeFirstLetter(region.name ?? ""), |
| 188 | + value: region?.id ?? 0 |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | + /> |
| 193 | + <FormikSelectField |
| 194 | + xs={12} |
| 195 | + variant={'outlined'} |
| 196 | + label={t('report_field_city')} |
| 197 | + name={'city_id'} |
| 198 | + onChange={(value: any) => setFormState({ ...formState, city_id: value })} |
| 199 | + option={ |
| 200 | + cities.map((city, index) => { |
| 201 | + return { |
| 202 | + label: capitalizeFirstLetter(city.name ?? ""), |
| 203 | + value: city.id ?? 0 |
| 204 | + } |
| 205 | + }) |
| 206 | + } |
| 207 | + /> |
| 208 | + <FormikSelectField |
| 209 | + xs={12} |
| 210 | + variant={'outlined'} |
| 211 | + label={t('report_field_district')} |
| 212 | + name={'district_id'} |
| 213 | + option={ |
| 214 | + districts.map((district: DistrictType, index) => { |
| 215 | + return { |
| 216 | + label: capitalizeFirstLetter(district?.name ?? ""), |
| 217 | + value: district?.id ?? 0 |
| 218 | + } |
| 219 | + }) |
| 220 | + } |
| 221 | + /> |
| 222 | + <Grid item xs={12}> |
| 223 | + <Button |
| 224 | + fullWidth |
| 225 | + ref={submitBtnRef} |
| 226 | + type={"submit"} |
| 227 | + onClick={(e) => { |
| 228 | + Object.keys(initialValue) |
| 229 | + .forEach(field => { |
| 230 | + setFieldTouched(field, true); |
| 231 | + }); |
| 232 | + }} |
| 233 | + sx={{ display: "none" }} |
| 234 | + /> |
| 235 | + </Grid> |
| 236 | + </Grid> |
| 237 | + </Form> |
| 238 | + } |
| 239 | + </Formik> |
| 240 | + </Box> |
| 241 | + |
| 242 | + </DialogContent> |
| 243 | + <DialogActions> |
| 244 | + <Button |
| 245 | + variant={"text"} |
| 246 | + color={"primary"} |
| 247 | + onClick={() => onClose()} |
| 248 | + > |
| 249 | + {t('global_label_cancel')} |
| 250 | + </Button> |
| 251 | + <Button |
| 252 | + disabled={loading} |
| 253 | + variant={"contained"} |
| 254 | + color={"primary"} |
| 255 | + onClick={() => { |
| 256 | + //@ts-ignore |
| 257 | + submitBtnRef.current?.click() |
| 258 | + }} |
| 259 | + > |
| 260 | + { |
| 261 | + loading ? <CircularProgress /> : t('global_label_save') |
| 262 | + } |
| 263 | + </Button> |
| 264 | + </DialogActions> |
| 265 | + </Dialog> |
| 266 | + ) |
| 267 | +} |
| 268 | + |
| 269 | +export default CreateReportModal |
0 commit comments