update requete ok : un user peut update ses informations

This commit is contained in:
Lou 2021-07-20 17:40:19 +02:00
commit 720599832e

View file

@ -9,6 +9,8 @@ import { setCurrentUser } from "../../../redux/action";
const UserUpdateForm = () => {
const [userData, setUserData] = useState(null);
const currentUser = useSelector((state) => state.currentUser);
const dispatch = useDispatch();
const history = useHistory();
const handleSubmit = (e) => {
e.preventDefault();
@ -51,6 +53,22 @@ const UserUpdateForm = () => {
const body = { user: { username: username, email: email } };
const response = await update(`/users/${currentUser.id}`, body, token);
console.log(response);
if (response.status === 200) {
handleChange(response);
}
};
const handleChange = (res) => {
console.log("handle change : " + res.data.attributes);
const payload = {
type: "user",
id: res.data.data.id,
email: res.data.data.attributes.email,
username: res.data.data.attributes.username
};
console.log("Payload : " + payload);
dispatch(setCurrentUser(payload));
history.push("/me");
};
useEffect(() => {