import React, { useState, useEffect, useRef } from "react"; import * as Yup from "yup"; import { Formik, Form, Field } from "formik"; import { toast } from "react-toastify"; import { makeStyles } from "@material-ui/core/styles"; import { green } from "@material-ui/core/colors"; import Button from "@material-ui/core/Button"; import TextField from "@material-ui/core/TextField"; import Dialog from "@material-ui/core/Dialog"; import DialogActions from "@material-ui/core/DialogActions"; import DialogContent from "@material-ui/core/DialogContent"; import DialogTitle from "@material-ui/core/DialogTitle"; import CircularProgress from "@material-ui/core/CircularProgress"; import { FormControl, InputLabel, Select, MenuItem, IconButton, InputAdornment } from "@material-ui/core" import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import toastError from "../../errors/toastError"; import ColorPicker from "../ColorPicker"; import { Colorize } from "@material-ui/icons"; import useDialogflows from "../../hooks/useDialogflows"; const useStyles = makeStyles(theme => ({ root: { display: "flex", flexWrap: "wrap", }, textField: { marginRight: theme.spacing(1), flex: 1, }, btnWrapper: { position: "relative", }, buttonProgress: { color: green[500], position: "absolute", top: "50%", left: "50%", marginTop: -12, marginLeft: -12, }, formControl: { marginRight: theme.spacing(1), minWidth: 200, }, colorAdorment: { width: 20, height: 20, }, })); const QueueSchema = Yup.object().shape({ name: Yup.string() .min(2, "Too Short!") .max(50, "Too Long!") .required("Required"), color: Yup.string().min(3, "Too Short!").max(9, "Too Long!").required(), greetingMessage: Yup.string(), dialogflowId: Yup.number(), }); const QueueModal = ({ open, onClose, queueId }) => { const classes = useStyles(); const initialState = { name: "", color: "", greetingMessage: "", dialogflowId: "", }; const [colorPickerModalOpen, setColorPickerModalOpen] = useState(false); const [queue, setQueue] = useState(initialState); const [dialogflows, setDialogflows] = useState([]); const { findAll: findAllDialogflows } = useDialogflows(); const greetingRef = useRef(); useEffect(() => { (async () => { if (!queueId) return; try { const { data } = await api.get(`/queue/${queueId}`); if(data.dialogflowId === null) { data.dialogflowId = ""; } setQueue(prevState => { return { ...prevState, ...data }; }); } catch (err) { toastError(err); } })(); return () => { setQueue({ name: "", color: "", greetingMessage: "", dialogflowId: "", }); }; }, [queueId, open]); useEffect(() => { const loadDialogflows = async () => { const list = await findAllDialogflows(); setDialogflows(list); } loadDialogflows(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleClose = () => { onClose(); setQueue(initialState); }; const handleSaveQueue = async values => { try { if(values.dialogflowId === "") { values.dialogflowId = null; } if (queueId) { await api.put(`/queue/${queueId}`, values); } else { await api.post("/queue", values); } toast.success("Queue saved successfully"); handleClose(); } catch (err) { toastError(err); } }; return (
{queueId ? `${i18n.t("queueModal.title.edit")}` : `${i18n.t("queueModal.title.add")}`} { setTimeout(() => { handleSaveQueue(values); actions.setSubmitting(false); }, 400); }} > {({ touched, errors, isSubmitting, values }) => (
{ setColorPickerModalOpen(true); greetingRef.current.focus(); }} error={touched.color && Boolean(errors.color)} helperText={touched.color && errors.color} InputProps={{ startAdornment: (
), endAdornment: ( setColorPickerModalOpen(true)} > ), }} variant="outlined" margin="dense" /> setColorPickerModalOpen(false)} onChange={color => { values.color = color; setQueue(() => { return { ...values, color }; }); }} />
{i18n.t("queueModal.form.dialogflow")}   {dialogflows.map((dialogflow) => ( {dialogflow.name} ))}
)}
); }; export default QueueModal;