Feat: add and edit contact in frontend

This commit is contained in:
canove
2020-07-23 18:09:45 -03:00
parent b20ed4c8db
commit 6d4e2a60a9
6 changed files with 104 additions and 33 deletions

View File

@@ -5,7 +5,7 @@ const ContactCustomField = require("../models/ContactCustomField");
// const Message = require("../models/Message"); // const Message = require("../models/Message");
// const Sequelize = require("sequelize"); // const Sequelize = require("sequelize");
// const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
// const { getWbot } = require("../libs/wbot"); // const { getWbot } = require("../libs/wbot");
exports.index = async (req, res) => { exports.index = async (req, res) => {
@@ -36,7 +36,7 @@ exports.index = async (req, res) => {
exports.store = async (req, res) => { exports.store = async (req, res) => {
// const wbot = getWbot(); // const wbot = getWbot();
// const io = getIO(); const io = getIO();
// const { number, name } = req.body; // const { number, name } = req.body;
// const result = await wbot.isRegisteredUser(`55${number}@c.us`); // const result = await wbot.isRegisteredUser(`55${number}@c.us`);
@@ -48,9 +48,14 @@ exports.store = async (req, res) => {
// } // }
// const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`); // const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
const { number, name } = await Contact.create(req.body); const contact = await Contact.create(req.body);
res.status(200).json({ number, name }); io.emit("contact", {
action: "create",
contact: contact,
});
res.status(200).json(contact);
}; };
exports.show = async (req, res) => { exports.show = async (req, res) => {
@@ -67,3 +72,24 @@ exports.show = async (req, res) => {
extraInfo, extraInfo,
}); });
}; };
exports.update = async (req, res) => {
const io = getIO();
const { contactId } = req.params;
const contact = await Contact.findByPk(contactId);
if (!contact) {
return res.status(400).json({ error: "No contact found with this ID" });
}
await contact.update(req.body);
io.emit("contact", {
action: "update",
contact: contact,
});
res.status(200).json(contact);
};

View File

@@ -0,0 +1,15 @@
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn("Contacts", "email", {
type: Sequelize.STRING,
allowNull: false,
defaultValue: "",
});
},
down: queryInterface => {
return queryInterface.removeColumn("Contacts", "email");
},
};

View File

@@ -6,6 +6,7 @@ class Contact extends Sequelize.Model {
{ {
name: { type: Sequelize.STRING }, name: { type: Sequelize.STRING },
number: { type: Sequelize.STRING }, number: { type: Sequelize.STRING },
email: { type: Sequelize.STRING, allowNull: false, defaultValue: "" },
profilePicUrl: { type: Sequelize.STRING }, profilePicUrl: { type: Sequelize.STRING },
}, },
{ {

View File

@@ -11,6 +11,6 @@ routes.get("/contacts/:contactId", isAuth, ContactController.show);
routes.post("/contacts", isAuth, ContactController.store); routes.post("/contacts", isAuth, ContactController.store);
// routes.put("/contacts/contactId", isAuth, ContactController.index); routes.put("/contacts/:contactId", isAuth, ContactController.update);
module.exports = routes; module.exports = routes;

View File

@@ -36,27 +36,27 @@ const useStyles = makeStyles(theme => ({
}, },
})); }));
const AddContactModal = ({ const ContactModal = ({
modalOpen, modalOpen,
setModalOpen, setModalOpen,
handleAddContact, handleAddContact,
onClose,
contactId, contactId,
}) => { }) => {
const classes = useStyles(); const classes = useStyles();
const initialState = {
const [contact, setContact] = useState({
id: "",
name: "", name: "",
number: "", number: "",
email: "", email: "",
extraInfo: [ extraInfo: [
{ {
id: "",
name: "", name: "",
value: "", value: "",
}, },
], ],
}); };
const [contact, setContact] = useState(initialState);
useEffect(() => { useEffect(() => {
const fetchContact = async () => { const fetchContact = async () => {
@@ -69,22 +69,22 @@ const AddContactModal = ({
}, [contactId]); }, [contactId]);
const handleClose = () => { const handleClose = () => {
setModalOpen(false); onClose();
setContact({ setContact(initialState);
id: "",
name: "",
number: "",
email: "",
extraInfo: [
{
id: "",
name: "",
value: "",
},
],
});
}; };
const handleSaveContact = async values => {
if (contactId) {
await api.put(`/contacts/${contactId}`, values);
} else {
await api.post("/contacts", values);
}
handleClose();
};
console.log(contact);
console.log("id", contactId);
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Dialog <Dialog
@@ -101,7 +101,7 @@ const AddContactModal = ({
enableReinitialize={true} enableReinitialize={true}
onSubmit={(values, { setSubmitting }) => { onSubmit={(values, { setSubmitting }) => {
setTimeout(() => { setTimeout(() => {
alert(JSON.stringify(values, null, 2)); handleSaveContact(values);
setSubmitting(false); setSubmitting(false);
}, 400); }, 400);
}} }}
@@ -126,7 +126,7 @@ const AddContactModal = ({
<TextField <TextField
label="Nome" label="Nome"
name="name" name="name"
value={values.name} value={values.name || ""}
onChange={handleChange} onChange={handleChange}
variant="outlined" variant="outlined"
margin="dense" margin="dense"
@@ -136,7 +136,7 @@ const AddContactModal = ({
<TextField <TextField
label="Número do Whatsapp" label="Número do Whatsapp"
name="number" name="number"
value={values.number} value={values.number || ""}
onChange={handleChange} onChange={handleChange}
placeholder="Ex: 13912344321" placeholder="Ex: 13912344321"
variant="outlined" variant="outlined"
@@ -147,7 +147,7 @@ const AddContactModal = ({
<TextField <TextField
label="Email" label="Email"
name="email" name="email"
value={values.email} value={values.email || ""}
onChange={handleChange} onChange={handleChange}
placeholder="Endereço de Email" placeholder="Endereço de Email"
fullWidth fullWidth
@@ -159,7 +159,7 @@ const AddContactModal = ({
style={{ marginBottom: 8, marginTop: 12 }} style={{ marginBottom: 8, marginTop: 12 }}
variant="subtitle1" variant="subtitle1"
> >
Informações extras Informações adicionais
</Typography> </Typography>
<FieldArray name="extraInfo"> <FieldArray name="extraInfo">
@@ -175,7 +175,7 @@ const AddContactModal = ({
<TextField <TextField
label="Nome do campo" label="Nome do campo"
name={`extraInfo[${index}].name`} name={`extraInfo[${index}].name`}
value={info.name} value={info.name || ""}
onChange={handleChange} onChange={handleChange}
variant="outlined" variant="outlined"
margin="dense" margin="dense"
@@ -185,7 +185,7 @@ const AddContactModal = ({
<TextField <TextField
label="Valor" label="Valor"
name={`extraInfo[${index}].value`} name={`extraInfo[${index}].value`}
value={info.value} value={info.value || ""}
onChange={handleChange} onChange={handleChange}
variant="outlined" variant="outlined"
margin="dense" margin="dense"
@@ -230,4 +230,4 @@ const AddContactModal = ({
); );
}; };
export default AddContactModal; export default ContactModal;

View File

@@ -1,4 +1,5 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import openSocket from "socket.io-client";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import Table from "@material-ui/core/Table"; import Table from "@material-ui/core/Table";
@@ -100,6 +101,33 @@ const Contacts = () => {
return () => clearTimeout(delayDebounceFn); return () => clearTimeout(delayDebounceFn);
}, [searchParam, page, token, rowsPerPage]); }, [searchParam, page, token, rowsPerPage]);
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.on("contact", data => {
if (data.action === "update" || data.action === "create") {
updateContacts(data.contact);
}
});
return () => {
socket.disconnect();
};
}, []);
const updateContacts = contact => {
setContacts(prevState => {
const contactIndex = prevState.findIndex(c => c.id === contact.id);
if (contactIndex === -1) {
return [contact, ...prevState];
}
const aux = [...prevState];
aux[contactIndex] = contact;
return aux;
});
};
const handleChangePage = (event, newPage) => { const handleChangePage = (event, newPage) => {
setPage(newPage); setPage(newPage);
}; };
@@ -119,6 +147,7 @@ const Contacts = () => {
}; };
const handleClose = () => { const handleClose = () => {
setSelectedContactId(null);
setModalOpen(false); setModalOpen(false);
}; };