From 36b20285ecaf47e76eeaf67e3f0b2f7c53548a70 Mon Sep 17 00:00:00 2001 From: joseCorte-exe Date: Thu, 27 Apr 2023 18:20:00 -0300 Subject: [PATCH] feat: edit user --- package.json | 1 + .../administrativeTables/ClientsTable.tsx | 68 ++++++++++--------- yarn.lock | 5 ++ 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 8531239..8177659 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "react-hook-form": "^7.32.2", "react-icons": "^4.3.1", "react-input-mask": "^2.0.4", + "react-loading": "^2.0.3", "react-pdf-thumbnail": "^0.1.0", "sharp": "^0.30.7", "styled-components": "^5.3.5", diff --git a/src/components/administrativeTables/ClientsTable.tsx b/src/components/administrativeTables/ClientsTable.tsx index 259bdb2..01453d7 100644 --- a/src/components/administrativeTables/ClientsTable.tsx +++ b/src/components/administrativeTables/ClientsTable.tsx @@ -28,6 +28,8 @@ import FaqButton1 from '../buttons/faqButton/FaqButton1'; import FaqButton2 from '../buttons/faqButton/FaqButton2'; import { StyledStatus, TableView } from './TableView'; +import ReactLoading from 'react-loading'; + const style = { position: 'absolute' as const, top: '50%', @@ -284,7 +286,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface const formData = new FormData() - const [clientEdit, setClientEdit] = useState({}) + const [clientEdit, setClientEdit] = useState() const [logo, setLogo] = useState(false) const [imageURLS, setImageURLs] = useState([]) const [images, setImages] = useState([] as any) @@ -309,31 +311,31 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface } const [openSnackSuccess, setOpenSnackSuccess] = useState(false) + const [loading, setLoading] = useState(false) - function handleUpdateClient({ - name, - email, - password, - password_confirmation, - client_id - }, id) { - formData.append('name', name) - formData.append('email', email) - formData.append('password', password) - formData.append('password_confirmation', password_confirmation) - formData.append('client_id', client_id) - formData.append('profile_picture', logo) - formData.append('role', nivelAcess) + async function handleUpdateClient(props, id) { + logo && formData.append('file', logo) + let new_profile_picture - api.put(`/user/${id}`, formData) - .then((res) => { - setOpenSnackSuccess(true) - setOpenModalInativar(false) - // window.location.reload() - }) - .catch((res) => { - setOpenSnackError(true) + try { + setLoading(true) + if (logo) { + const { data } = await api.post('/sendFile', formData) + new_profile_picture = data.url + } + await api.put(`/user/${id}`, { + ...props, + profile_picture: new_profile_picture }) + + setOpenSnackSuccess(true) + setOpenModalInativar(false) + // window.location.reload() + } catch (err) { + setOpenSnackError(true) + } finally { + setLoading(false) + } } return ( @@ -397,7 +399,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface { setOpenEditUserModal(true) setSelectedClient(row) - setClientEdit(row) + setClientEdit({ email: row.email, name: row.name, client_id: row.client_id, profile_picture: row.profile_picture }) }}>{row.name} { setOpen(true) @@ -457,12 +459,13 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface name: value.target.value }) }} + value={clientEdit?.name} variant="outlined" /> { setClientEdit({ @@ -500,6 +503,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface id="outlined-basic" label="Codigo do Cliente Smart Energia" sx={{ width: 350, ml: 5, mt: 2 }} + value={clientEdit?.client_id} onChange={(value) => { setClientEdit({ ...clientEdit, @@ -541,7 +545,6 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
- Nivel de acesso