feat: edit user

This commit is contained in:
joseCorte-exe 2023-04-27 18:20:00 -03:00
parent 038461fd2f
commit 36b20285ec
3 changed files with 43 additions and 31 deletions

View File

@ -58,6 +58,7 @@
"react-hook-form": "^7.32.2", "react-hook-form": "^7.32.2",
"react-icons": "^4.3.1", "react-icons": "^4.3.1",
"react-input-mask": "^2.0.4", "react-input-mask": "^2.0.4",
"react-loading": "^2.0.3",
"react-pdf-thumbnail": "^0.1.0", "react-pdf-thumbnail": "^0.1.0",
"sharp": "^0.30.7", "sharp": "^0.30.7",
"styled-components": "^5.3.5", "styled-components": "^5.3.5",

View File

@ -28,6 +28,8 @@ import FaqButton1 from '../buttons/faqButton/FaqButton1';
import FaqButton2 from '../buttons/faqButton/FaqButton2'; import FaqButton2 from '../buttons/faqButton/FaqButton2';
import { StyledStatus, TableView } from './TableView'; import { StyledStatus, TableView } from './TableView';
import ReactLoading from 'react-loading';
const style = { const style = {
position: 'absolute' as const, position: 'absolute' as const,
top: '50%', top: '50%',
@ -284,7 +286,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
const formData = new FormData() const formData = new FormData()
const [clientEdit, setClientEdit] = useState<any>({}) const [clientEdit, setClientEdit] = useState<any>()
const [logo, setLogo] = useState(false) const [logo, setLogo] = useState(false)
const [imageURLS, setImageURLs] = useState([]) const [imageURLS, setImageURLs] = useState([])
const [images, setImages] = useState([] as any) const [images, setImages] = useState([] as any)
@ -309,31 +311,31 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
} }
const [openSnackSuccess, setOpenSnackSuccess] = useState<boolean>(false) const [openSnackSuccess, setOpenSnackSuccess] = useState<boolean>(false)
const [loading, setLoading] = useState<boolean>(false)
function handleUpdateClient({ async function handleUpdateClient(props, id) {
name, logo && formData.append('file', logo)
email, let new_profile_picture
password,
password_confirmation, try {
client_id setLoading(true)
}, id) { if (logo) {
formData.append('name', name) const { data } = await api.post('/sendFile', formData)
formData.append('email', email) new_profile_picture = data.url
formData.append('password', password) }
formData.append('password_confirmation', password_confirmation) await api.put(`/user/${id}`, {
formData.append('client_id', client_id) ...props,
formData.append('profile_picture', logo) profile_picture: new_profile_picture
formData.append('role', nivelAcess) })
api.put(`/user/${id}`, formData)
.then((res) => {
setOpenSnackSuccess(true) setOpenSnackSuccess(true)
setOpenModalInativar(false) setOpenModalInativar(false)
// window.location.reload() // window.location.reload()
}) } catch (err) {
.catch((res) => {
setOpenSnackError(true) setOpenSnackError(true)
}) } finally {
setLoading(false)
}
} }
return ( return (
@ -397,7 +399,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
<TableCell align="left" style={{ cursor: 'pointer' }} onClick={() => { <TableCell align="left" style={{ cursor: 'pointer' }} onClick={() => {
setOpenEditUserModal(true) setOpenEditUserModal(true)
setSelectedClient(row) setSelectedClient(row)
setClientEdit(row) setClientEdit({ email: row.email, name: row.name, client_id: row.client_id, profile_picture: row.profile_picture })
}}>{row.name}</TableCell> }}>{row.name}</TableCell>
<TableCell align="left" style={{ cursor: 'pointer' }} onClick={() => { <TableCell align="left" style={{ cursor: 'pointer' }} onClick={() => {
setOpen(true) setOpen(true)
@ -457,12 +459,13 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
name: value.target.value name: value.target.value
}) })
}} }}
value={clientEdit?.name}
variant="outlined" variant="outlined"
/> />
<TextField <TextField
id="outlined-basic" id="outlined-basic"
label="E-mail/Usuário" label="E-mail/Usuário"
value={clientEdit.email} value={clientEdit?.email}
sx={{ width: 350, ml: 8 }} sx={{ width: 350, ml: 8 }}
onChange={(value) => { onChange={(value) => {
setClientEdit({ setClientEdit({
@ -500,6 +503,7 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
id="outlined-basic" id="outlined-basic"
label="Codigo do Cliente Smart Energia" label="Codigo do Cliente Smart Energia"
sx={{ width: 350, ml: 5, mt: 2 }} sx={{ width: 350, ml: 5, mt: 2 }}
value={clientEdit?.client_id}
onChange={(value) => { onChange={(value) => {
setClientEdit({ setClientEdit({
...clientEdit, ...clientEdit,
@ -541,7 +545,6 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
</InputUploadView> </InputUploadView>
<div className='select'> <div className='select'>
<FormControl sx={{ width: 350, ml: 5, mt: 2 }}> <FormControl sx={{ width: 350, ml: 5, mt: 2 }}>
<InputLabel id="demo-select-small">Nivel de acesso</InputLabel> <InputLabel id="demo-select-small">Nivel de acesso</InputLabel>
<Select <Select
@ -559,11 +562,14 @@ export default function ClientTable({ clients, onChange }: ClientsTableInterface
</FormControl> </FormControl>
</div> </div>
<FaqButton1 title="Cancelar" onClick={() => setOpenEditUserModal(false)} /> {!loading && <FaqButton1 title="Cancelar" onClick={() => setOpenEditUserModal(false)} />}
<FaqButton2 {!loading
? <FaqButton2
title="Salvar" title="Salvar"
onClick={() => handleUpdateClient(clientEdit, selectedClient.id)} onClick={() => handleUpdateClient(clientEdit, selectedClient.id)}
/> />
: <ReactLoading type='spin' color='#254f7f' height='5%' width='5%' />
}
</Box> </Box>
</Modal> </Modal>

View File

@ -4394,6 +4394,11 @@ react-is@^16.13.1, react-is@^16.7.0:
version "17.0.2" version "17.0.2"
resolved "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz" resolved "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz"
react-loading@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/react-loading/-/react-loading-2.0.3.tgz#e8138fb0c3e4674e481b320802ac7048ae14ffb9"
integrity sha512-Vdqy79zq+bpeWJqC+xjltUjuGApyoItPgL0vgVfcJHhqwU7bAMKzysfGW/ADu6i0z0JiOCRJjo+IkFNkRNbA3A==
react-pdf-thumbnail@^0.1.0: react-pdf-thumbnail@^0.1.0:
version "0.1.0" version "0.1.0"
resolved "https://registry.yarnpkg.com/react-pdf-thumbnail/-/react-pdf-thumbnail-0.1.0.tgz#6b8ddf861252acb2b137dfd42007f545fde52ef9" resolved "https://registry.yarnpkg.com/react-pdf-thumbnail/-/react-pdf-thumbnail-0.1.0.tgz#6b8ddf861252acb2b137dfd42007f545fde52ef9"