From 6ff5205333e789dc337f29a56d465f076a790b1c Mon Sep 17 00:00:00 2001 From: Ido Melo Date: Thu, 17 Feb 2022 15:53:31 -0300 Subject: [PATCH] =?UTF-8?q?Adicionando=20funcionalidade=20edi=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20clientes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 2 + src/components/CustomersCard.js | 7 +- src/pages/customers/Edit.js | 132 ++++++++++++++++++++++++++++++++ src/pages/customers/List.js | 10 ++- 4 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/pages/customers/Edit.js diff --git a/src/App.js b/src/App.js index 34593a2..7b68f7c 100644 --- a/src/App.js +++ b/src/App.js @@ -4,6 +4,7 @@ import TemplatePage from './templates/TemplatePage' import Home from './pages/Home' import CustomersList from './pages/customers/List' import CustomersRegister from './pages/customers/Register' +import CustomersEdit from './pages/customers/Edit' import { BrowserRouter, @@ -18,6 +19,7 @@ const App = () => { } />} /> + } />} /> } />} /> }/>} /> diff --git a/src/components/CustomersCard.js b/src/components/CustomersCard.js index 5edfe7f..e657da6 100644 --- a/src/components/CustomersCard.js +++ b/src/components/CustomersCard.js @@ -23,6 +23,7 @@ const CustomersCard = ({ email, avatar, onRemoveCustomer, + onEditCustomer, }) => { const [modalOpen, setModalOpen] = useState(false) @@ -35,6 +36,10 @@ const CustomersCard = ({ handleToggleModal() } + const handleEditCustomer = id => { + onEditCustomer(id) + } + return ( <> @@ -51,7 +56,7 @@ const CustomersCard = ({ - + handleEditCustomer(id)}> handleToggleModal()}> diff --git a/src/pages/customers/Edit.js b/src/pages/customers/Edit.js new file mode 100644 index 0000000..a95b05b --- /dev/null +++ b/src/pages/customers/Edit.js @@ -0,0 +1,132 @@ +import React, { useState, useEffect } from 'react' +import { useParams } from 'react-router-dom' +import axios from 'axios' + +import { + TextField, + Stack, +} from '@mui/material/' + +import Snackbar from '../../components/Snackbar' +import ButtonLoading from '../../components/ButtonLoading' + +const Edit = () => { + const { id } = useParams() + + const [form, setForm] = useState({ + name: { + value: '', + error: false + }, + job: { + value: '', + error: false + }, + }) + + const [loading, setLoading] = React.useState(false) + const [openSnackbar, setOpenSnackbar] = useState(false) + + useEffect(() => { + axios.get(`https://reqres.in/api/users/${id}`) + .then( response => { + const { data } = response.data + console.log(data) + setForm({ + name: { + value: data.first_name, + error: false + }, + job: { + value: data.job, + error: false + }, + }) + }) + }, []) + + console.log(form) + + const handleInputChange = (e) => { + const { name, value } = e.target + setForm({ + ...form, + [name]: { + value, + }, + }) + } + + + const handleRegisterButton = () => { + let hasError = false + + let newFormState = { + ...form, + } + + if(!form.name.value) { + hasError = true + newFormState.name = { + value: form.name.value, + error: true, + helperText: 'Campo obrigatório' + } + } + + if(!form.job.value) { + hasError = true + newFormState.job = { + value: form.job.value, + error: true, + helperText: 'Campo obrigatório' + } + } + + if(hasError) { + return setForm(newFormState) + }else{ + setLoading(true) + } + + axios.patch(`https://reqres.in/api/users/${id}`, { + name: form.name.value, + job: form.job.value + }).then(() => { + setLoading(false) + setOpenSnackbar(true) + }) + } + + return ( + <> + + + + + + setOpenSnackbar(false)}/> + + + + ) +} + +export default Edit diff --git a/src/pages/customers/List.js b/src/pages/customers/List.js index ee0c4ab..79be43d 100644 --- a/src/pages/customers/List.js +++ b/src/pages/customers/List.js @@ -1,4 +1,5 @@ import React, { useState, useEffect } from 'react' +import { useNavigate } from 'react-router-dom' import axios from 'axios' import Grid from '@mui/material/Grid' @@ -6,6 +7,7 @@ import CustomersCard from '../../components/CustomersCard' const Customers = () => { const [customers, setCustomers] = useState([]) + const navigate = useNavigate() useEffect( () => { axios.get('https://reqres.in/api/users') @@ -20,8 +22,11 @@ const Customers = () => { .then( () => { const newCustomersState = customers.filter(customer => customer.id !== id) setCustomers(newCustomersState) - } - ) + }) + } + + const handleEditCustomer = id => { + navigate(`/customers/edit/${ id }`) } return ( @@ -37,6 +42,7 @@ const Customers = () => { email={item.email} avatar={item.avatar} onRemoveCustomer={handleRemoveCustomer} + onEditCustomer={handleEditCustomer} /> )