mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +00:00
feat: started whatsapp modal queue selection
This commit is contained in:
@@ -13,10 +13,6 @@ import DialogActions from "@material-ui/core/DialogActions";
|
|||||||
import DialogContent from "@material-ui/core/DialogContent";
|
import DialogContent from "@material-ui/core/DialogContent";
|
||||||
import DialogTitle from "@material-ui/core/DialogTitle";
|
import DialogTitle from "@material-ui/core/DialogTitle";
|
||||||
import CircularProgress from "@material-ui/core/CircularProgress";
|
import CircularProgress from "@material-ui/core/CircularProgress";
|
||||||
import Select from "@material-ui/core/Select";
|
|
||||||
import InputLabel from "@material-ui/core/InputLabel";
|
|
||||||
import MenuItem from "@material-ui/core/MenuItem";
|
|
||||||
import FormControl from "@material-ui/core/FormControl";
|
|
||||||
|
|
||||||
import { i18n } from "../../translate/i18n";
|
import { i18n } from "../../translate/i18n";
|
||||||
|
|
||||||
@@ -120,7 +116,13 @@ const QueueModal = ({ open, onClose, queueId }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<Dialog open={open} onClose={handleClose} maxWidth="lg" scroll="paper">
|
<Dialog
|
||||||
|
open={open}
|
||||||
|
onClose={handleClose}
|
||||||
|
maxWidth="sm"
|
||||||
|
fullWidth
|
||||||
|
scroll="paper"
|
||||||
|
>
|
||||||
<DialogTitle>
|
<DialogTitle>
|
||||||
{queueId
|
{queueId
|
||||||
? `${i18n.t("queueModal.title.edit")}`
|
? `${i18n.t("queueModal.title.edit")}`
|
||||||
|
|||||||
91
frontend/src/components/QueueSelector/index.js
Normal file
91
frontend/src/components/QueueSelector/index.js
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
|
import InputLabel from "@material-ui/core/InputLabel";
|
||||||
|
import MenuItem from "@material-ui/core/MenuItem";
|
||||||
|
import FormControl from "@material-ui/core/FormControl";
|
||||||
|
import Select from "@material-ui/core/Select";
|
||||||
|
import Chip from "@material-ui/core/Chip";
|
||||||
|
import { OutlinedInput } from "@material-ui/core";
|
||||||
|
import toastError from "../../errors/toastError";
|
||||||
|
import api from "../../services/api";
|
||||||
|
|
||||||
|
const useStyles = makeStyles(theme => ({
|
||||||
|
chips: {
|
||||||
|
display: "flex",
|
||||||
|
flexWrap: "wrap",
|
||||||
|
},
|
||||||
|
chip: {
|
||||||
|
margin: 2,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
const QueueSelector = ({ selectedQueueIds, onChange }) => {
|
||||||
|
const classes = useStyles();
|
||||||
|
// const [selectedQueues, setSelectedQueues] = useState([]);
|
||||||
|
const [queues, setQueues] = useState([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const { data } = await api.get("/queue");
|
||||||
|
setQueues(data);
|
||||||
|
} catch (err) {
|
||||||
|
toastError(err);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleChange = event => {
|
||||||
|
onChange(event.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ marginTop: 6 }}>
|
||||||
|
<FormControl fullWidth variant="outlined">
|
||||||
|
<InputLabel>Filas</InputLabel>
|
||||||
|
<Select
|
||||||
|
multiple
|
||||||
|
value={selectedQueueIds}
|
||||||
|
onChange={handleChange}
|
||||||
|
input={<OutlinedInput label="Filas" id="select-multiple-chip" />}
|
||||||
|
MenuProps={{
|
||||||
|
anchorOrigin: {
|
||||||
|
vertical: "bottom",
|
||||||
|
horizontal: "left",
|
||||||
|
},
|
||||||
|
transformOrigin: {
|
||||||
|
vertical: "top",
|
||||||
|
horizontal: "left",
|
||||||
|
},
|
||||||
|
getContentAnchorEl: null,
|
||||||
|
}}
|
||||||
|
renderValue={selected => (
|
||||||
|
<div className={classes.chips}>
|
||||||
|
{selected.length > 0 &&
|
||||||
|
selected.map(value => {
|
||||||
|
const queue = queues.find(q => q.id === value);
|
||||||
|
return queue ? (
|
||||||
|
<Chip
|
||||||
|
key={value}
|
||||||
|
style={{ backgroundColor: queue.color }}
|
||||||
|
variant="outlined"
|
||||||
|
label={queue.name}
|
||||||
|
className={classes.chip}
|
||||||
|
/>
|
||||||
|
) : null;
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{queues.map(queue => (
|
||||||
|
<MenuItem key={queue.id} value={queue.id}>
|
||||||
|
{queue.name}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default QueueSelector;
|
||||||
@@ -22,15 +22,18 @@ import { i18n } from "../../translate/i18n";
|
|||||||
|
|
||||||
import api from "../../services/api";
|
import api from "../../services/api";
|
||||||
import toastError from "../../errors/toastError";
|
import toastError from "../../errors/toastError";
|
||||||
|
import QueueSelector from "../QueueSelector";
|
||||||
|
|
||||||
const useStyles = makeStyles(theme => ({
|
const useStyles = makeStyles(theme => ({
|
||||||
root: {
|
root: {
|
||||||
display: "flex",
|
display: "flex",
|
||||||
flexWrap: "wrap",
|
flexWrap: "wrap",
|
||||||
},
|
},
|
||||||
textField: {
|
multFieldLine: {
|
||||||
marginRight: theme.spacing(1),
|
display: "flex",
|
||||||
flex: 1,
|
"& > *:not(:last-child)": {
|
||||||
|
marginRight: theme.spacing(1),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
btnWrapper: {
|
btnWrapper: {
|
||||||
@@ -71,6 +74,7 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const [user, setUser] = useState(initialState);
|
const [user, setUser] = useState(initialState);
|
||||||
|
const [selectedQueueIds, setSelectedQueueIds] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchUser = async () => {
|
const fetchUser = async () => {
|
||||||
@@ -80,6 +84,8 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
setUser(prevState => {
|
setUser(prevState => {
|
||||||
return { ...prevState, ...data };
|
return { ...prevState, ...data };
|
||||||
});
|
});
|
||||||
|
const userQueueIds = data.queues?.map(queue => queue.id);
|
||||||
|
setSelectedQueueIds(userQueueIds);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toastError(err);
|
toastError(err);
|
||||||
}
|
}
|
||||||
@@ -94,11 +100,12 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveUser = async values => {
|
const handleSaveUser = async values => {
|
||||||
|
const userData = { ...values, queueIds: selectedQueueIds };
|
||||||
try {
|
try {
|
||||||
if (userId) {
|
if (userId) {
|
||||||
await api.put(`/users/${userId}`, values);
|
await api.put(`/users/${userId}`, userData);
|
||||||
} else {
|
} else {
|
||||||
await api.post("/users", values);
|
await api.post("/users", userData);
|
||||||
}
|
}
|
||||||
toast.success(i18n.t("userModal.success"));
|
toast.success(i18n.t("userModal.success"));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -109,7 +116,13 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<Dialog open={open} onClose={handleClose} maxWidth="lg" scroll="paper">
|
<Dialog
|
||||||
|
open={open}
|
||||||
|
onClose={handleClose}
|
||||||
|
maxWidth="xs"
|
||||||
|
fullWidth
|
||||||
|
scroll="paper"
|
||||||
|
>
|
||||||
<DialogTitle id="form-dialog-title">
|
<DialogTitle id="form-dialog-title">
|
||||||
{userId
|
{userId
|
||||||
? `${i18n.t("userModal.title.edit")}`
|
? `${i18n.t("userModal.title.edit")}`
|
||||||
@@ -129,27 +142,18 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
{({ touched, errors, isSubmitting }) => (
|
{({ touched, errors, isSubmitting }) => (
|
||||||
<Form>
|
<Form>
|
||||||
<DialogContent dividers>
|
<DialogContent dividers>
|
||||||
<Field
|
<div className={classes.multFieldLine}>
|
||||||
as={TextField}
|
<Field
|
||||||
label={i18n.t("userModal.form.name")}
|
as={TextField}
|
||||||
autoFocus
|
label={i18n.t("userModal.form.name")}
|
||||||
name="name"
|
autoFocus
|
||||||
error={touched.name && Boolean(errors.name)}
|
name="name"
|
||||||
helperText={touched.name && errors.name}
|
error={touched.name && Boolean(errors.name)}
|
||||||
variant="outlined"
|
helperText={touched.name && errors.name}
|
||||||
margin="dense"
|
variant="outlined"
|
||||||
className={classes.textField}
|
margin="dense"
|
||||||
/>
|
fullWidth
|
||||||
<Field
|
/>
|
||||||
as={TextField}
|
|
||||||
label={i18n.t("userModal.form.email")}
|
|
||||||
name="email"
|
|
||||||
error={touched.email && Boolean(errors.email)}
|
|
||||||
helperText={touched.email && errors.email}
|
|
||||||
variant="outlined"
|
|
||||||
margin="dense"
|
|
||||||
/>
|
|
||||||
<div>
|
|
||||||
<Field
|
<Field
|
||||||
as={TextField}
|
as={TextField}
|
||||||
label={i18n.t("userModal.form.password")}
|
label={i18n.t("userModal.form.password")}
|
||||||
@@ -159,6 +163,19 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
helperText={touched.password && errors.password}
|
helperText={touched.password && errors.password}
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
margin="dense"
|
margin="dense"
|
||||||
|
fullWidth
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className={classes.multFieldLine}>
|
||||||
|
<Field
|
||||||
|
as={TextField}
|
||||||
|
label={i18n.t("userModal.form.email")}
|
||||||
|
name="email"
|
||||||
|
error={touched.email && Boolean(errors.email)}
|
||||||
|
helperText={touched.email && errors.email}
|
||||||
|
variant="outlined"
|
||||||
|
margin="dense"
|
||||||
|
fullWidth
|
||||||
/>
|
/>
|
||||||
<FormControl
|
<FormControl
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
@@ -181,6 +198,10 @@ const UserModal = ({ open, onClose, userId }) => {
|
|||||||
</Field>
|
</Field>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</div>
|
</div>
|
||||||
|
<QueueSelector
|
||||||
|
selectedQueueIds={selectedQueueIds}
|
||||||
|
onChange={values => setSelectedQueueIds(values)}
|
||||||
|
/>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -21,19 +21,19 @@ import {
|
|||||||
import api from "../../services/api";
|
import api from "../../services/api";
|
||||||
import { i18n } from "../../translate/i18n";
|
import { i18n } from "../../translate/i18n";
|
||||||
import toastError from "../../errors/toastError";
|
import toastError from "../../errors/toastError";
|
||||||
|
import QueueSelector from "../QueueSelector";
|
||||||
|
|
||||||
const useStyles = makeStyles(theme => ({
|
const useStyles = makeStyles(theme => ({
|
||||||
form: {
|
root: {
|
||||||
display: "flex",
|
display: "flex",
|
||||||
alignItems: "center",
|
flexWrap: "wrap",
|
||||||
justifySelf: "center",
|
|
||||||
"& > *": {
|
|
||||||
margin: theme.spacing(1),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
textField: {
|
multFieldLine: {
|
||||||
flex: 1,
|
display: "flex",
|
||||||
|
"& > *:not(:last-child)": {
|
||||||
|
marginRight: theme.spacing(1),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
btnWrapper: {
|
btnWrapper: {
|
||||||
@@ -61,9 +61,11 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
|
|||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const initialState = {
|
const initialState = {
|
||||||
name: "",
|
name: "",
|
||||||
|
greetingMessage: "",
|
||||||
isDefault: false,
|
isDefault: false,
|
||||||
};
|
};
|
||||||
const [whatsApp, setWhatsApp] = useState(initialState);
|
const [whatsApp, setWhatsApp] = useState(initialState);
|
||||||
|
const [selectedQueueIds, setSelectedQueueIds] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchSession = async () => {
|
const fetchSession = async () => {
|
||||||
@@ -72,6 +74,9 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
|
|||||||
try {
|
try {
|
||||||
const { data } = await api.get(`whatsapp/${whatsAppId}`);
|
const { data } = await api.get(`whatsapp/${whatsAppId}`);
|
||||||
setWhatsApp(data);
|
setWhatsApp(data);
|
||||||
|
|
||||||
|
const whatsQueueIds = data.whatsappQueues?.map(q => q.queue.id);
|
||||||
|
setSelectedQueueIds(whatsQueueIds);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toastError(err);
|
toastError(err);
|
||||||
}
|
}
|
||||||
@@ -80,14 +85,13 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
|
|||||||
}, [whatsAppId]);
|
}, [whatsAppId]);
|
||||||
|
|
||||||
const handleSaveWhatsApp = async values => {
|
const handleSaveWhatsApp = async values => {
|
||||||
|
const whatsappData = { ...values, queueIds: selectedQueueIds };
|
||||||
|
console.log("SELECTED", whatsappData);
|
||||||
try {
|
try {
|
||||||
if (whatsAppId) {
|
if (whatsAppId) {
|
||||||
await api.put(`/whatsapp/${whatsAppId}`, {
|
await api.put(`/whatsapp/${whatsAppId}`, whatsappData);
|
||||||
name: values.name,
|
|
||||||
isDefault: values.isDefault,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
await api.post("/whatsapp", values);
|
await api.post("/whatsapp", whatsappData);
|
||||||
}
|
}
|
||||||
toast.success(i18n.t("whatsappModal.success"));
|
toast.success(i18n.t("whatsappModal.success"));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -102,80 +106,113 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onClose={handleClose} maxWidth="lg" scroll="paper">
|
<div className={classes.root}>
|
||||||
<DialogTitle>
|
<Dialog
|
||||||
{whatsAppId
|
open={open}
|
||||||
? i18n.t("whatsappModal.title.edit")
|
onClose={handleClose}
|
||||||
: i18n.t("whatsappModal.title.add")}
|
maxWidth="sm"
|
||||||
</DialogTitle>
|
fullWidth
|
||||||
<Formik
|
scroll="paper"
|
||||||
initialValues={whatsApp}
|
|
||||||
enableReinitialize={true}
|
|
||||||
validationSchema={SessionSchema}
|
|
||||||
onSubmit={(values, actions) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
handleSaveWhatsApp(values);
|
|
||||||
actions.setSubmitting(false);
|
|
||||||
}, 400);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{({ values, touched, errors, isSubmitting }) => (
|
<DialogTitle>
|
||||||
<Form>
|
{whatsAppId
|
||||||
<DialogContent dividers className={classes.form}>
|
? i18n.t("whatsappModal.title.edit")
|
||||||
<Field
|
: i18n.t("whatsappModal.title.add")}
|
||||||
as={TextField}
|
</DialogTitle>
|
||||||
label={i18n.t("whatsappModal.form.name")}
|
<Formik
|
||||||
autoFocus
|
initialValues={whatsApp}
|
||||||
name="name"
|
enableReinitialize={true}
|
||||||
error={touched.name && Boolean(errors.name)}
|
validationSchema={SessionSchema}
|
||||||
helperText={touched.name && errors.name}
|
onSubmit={(values, actions) => {
|
||||||
variant="outlined"
|
setTimeout(() => {
|
||||||
margin="dense"
|
handleSaveWhatsApp(values);
|
||||||
className={classes.textField}
|
actions.setSubmitting(false);
|
||||||
/>
|
}, 400);
|
||||||
<FormControlLabel
|
}}
|
||||||
control={
|
>
|
||||||
|
{({ values, touched, errors, isSubmitting }) => (
|
||||||
|
<Form>
|
||||||
|
<DialogContent dividers>
|
||||||
|
<div className={classes.multFieldLine}>
|
||||||
<Field
|
<Field
|
||||||
as={Switch}
|
as={TextField}
|
||||||
color="primary"
|
label={i18n.t("whatsappModal.form.name")}
|
||||||
name="isDefault"
|
autoFocus
|
||||||
checked={values.isDefault}
|
name="name"
|
||||||
|
error={touched.name && Boolean(errors.name)}
|
||||||
|
helperText={touched.name && errors.name}
|
||||||
|
variant="outlined"
|
||||||
|
margin="dense"
|
||||||
|
className={classes.textField}
|
||||||
/>
|
/>
|
||||||
}
|
<FormControlLabel
|
||||||
label={i18n.t("whatsappModal.form.default")}
|
control={
|
||||||
/>
|
<Field
|
||||||
</DialogContent>
|
as={Switch}
|
||||||
<DialogActions>
|
color="primary"
|
||||||
<Button
|
name="isDefault"
|
||||||
onClick={handleClose}
|
checked={values.isDefault}
|
||||||
color="secondary"
|
/>
|
||||||
disabled={isSubmitting}
|
}
|
||||||
variant="outlined"
|
label={i18n.t("whatsappModal.form.default")}
|
||||||
>
|
|
||||||
{i18n.t("whatsappModal.buttons.cancel")}
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
color="primary"
|
|
||||||
disabled={isSubmitting}
|
|
||||||
variant="contained"
|
|
||||||
className={classes.btnWrapper}
|
|
||||||
>
|
|
||||||
{whatsAppId
|
|
||||||
? i18n.t("whatsappModal.buttons.okEdit")
|
|
||||||
: i18n.t("whatsappModal.buttons.okAdd")}
|
|
||||||
{isSubmitting && (
|
|
||||||
<CircularProgress
|
|
||||||
size={24}
|
|
||||||
className={classes.buttonProgress}
|
|
||||||
/>
|
/>
|
||||||
)}
|
</div>
|
||||||
</Button>
|
<div>
|
||||||
</DialogActions>
|
<Field
|
||||||
</Form>
|
as={TextField}
|
||||||
)}
|
label={i18n.t("queueModal.form.greetingMessage")}
|
||||||
</Formik>
|
type="greetingMessage"
|
||||||
</Dialog>
|
multiline
|
||||||
|
rows={5}
|
||||||
|
fullWidth
|
||||||
|
name="greetingMessage"
|
||||||
|
error={
|
||||||
|
touched.greetingMessage && Boolean(errors.greetingMessage)
|
||||||
|
}
|
||||||
|
helperText={
|
||||||
|
touched.greetingMessage && errors.greetingMessage
|
||||||
|
}
|
||||||
|
variant="outlined"
|
||||||
|
margin="dense"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<QueueSelector
|
||||||
|
selectedQueueIds={selectedQueueIds}
|
||||||
|
onChange={values => setSelectedQueueIds(values)}
|
||||||
|
/>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button
|
||||||
|
onClick={handleClose}
|
||||||
|
color="secondary"
|
||||||
|
disabled={isSubmitting}
|
||||||
|
variant="outlined"
|
||||||
|
>
|
||||||
|
{i18n.t("whatsappModal.buttons.cancel")}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
color="primary"
|
||||||
|
disabled={isSubmitting}
|
||||||
|
variant="contained"
|
||||||
|
className={classes.btnWrapper}
|
||||||
|
>
|
||||||
|
{whatsAppId
|
||||||
|
? i18n.t("whatsappModal.buttons.okEdit")
|
||||||
|
: i18n.t("whatsappModal.buttons.okAdd")}
|
||||||
|
{isSubmitting && (
|
||||||
|
<CircularProgress
|
||||||
|
size={24}
|
||||||
|
className={classes.buttonProgress}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Form>
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user