-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adicionando funcionalidade edição de clientes
- Loading branch information
Showing
4 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<> | ||
<Stack direction='column' maxWidth='400px' margin='0 auto' | ||
spacing={{ xs: 2, sm: 3, md: 4 }}> | ||
<TextField | ||
name="name" | ||
id="outlined-basic" | ||
label="Digite o Nome:" | ||
value={form.name.value} | ||
error={form.name.error} | ||
helperText={form.name.error? form.name.helperText : ''} | ||
onChange={handleInputChange} | ||
/> | ||
<TextField | ||
name="job" | ||
id="outlined-basic" | ||
label="Digite o Cargo:" | ||
value={form.job.value} | ||
error={form.job.error} | ||
helperText={form.job.error? form.job.helperText : ''} | ||
onChange={handleInputChange} | ||
/> | ||
<ButtonLoading text="Salvar Alterações" onClick={handleRegisterButton} loading={loading} /> | ||
</Stack> | ||
<Snackbar open={openSnackbar} severity="success" text="Alterações Salvas com sucesso!" onClose={() => setOpenSnackbar(false)}/> | ||
</> | ||
|
||
|
||
) | ||
} | ||
|
||
export default Edit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters