|
@@ -1,24 +1,82 @@
|
1
|
|
-import React from "react";
|
2
|
|
-import { useSelector } from "react-redux";
|
|
1
|
+import React, { useState, useEffect } from "react";
|
|
2
|
+import { useSelector, useDispatch } from "react-redux";
|
|
3
|
+import { useHistory } from "react-router-dom";
|
|
4
|
+import Cookies from "js-cookie";
|
|
5
|
+
|
|
6
|
+import update from "../../../services/request/Update";
|
|
7
|
+import { setCurrentUser } from "../../../redux/action";
|
3
|
8
|
|
4
|
9
|
const UserUpdateForm = () => {
|
|
10
|
+ const [userData, setUserData] = useState(null);
|
|
11
|
+ const [isUpdate, setIsUpdate] = useState(false);
|
|
12
|
+
|
5
|
13
|
const currentUser = useSelector((state) => state.currentUser);
|
|
14
|
+ const dispatch = useDispatch();
|
|
15
|
+ const history = useHistory();
|
|
16
|
+ const token = Cookies.get("token");
|
6
|
17
|
|
7
|
18
|
const handleSubmit = (event) => {
|
8
|
19
|
event.preventDefault();
|
9
|
20
|
const formdata = new FormData(event.currentTarget);
|
|
21
|
+
|
|
22
|
+ let id = currentUser.id;
|
|
23
|
+ let email = currentUser.email;
|
|
24
|
+ let username = currentUser.username;
|
|
25
|
+ if (formdata.get("username") && formdata.get("email")) {
|
|
26
|
+ console.log("1");
|
|
27
|
+ let username = formdata.get("username");
|
|
28
|
+ let email = formdata.get("email");
|
|
29
|
+ setIsUpdate(true);
|
|
30
|
+ setUserData({ ...userData, username, email, id });
|
|
31
|
+ } else if (formdata.get("username") && formdata.get("email") === "") {
|
|
32
|
+ console.log("2");
|
|
33
|
+ let username = formdata.get("username");
|
|
34
|
+ setIsUpdate(true);
|
|
35
|
+ setUserData({ ...userData, username, email, id });
|
|
36
|
+ } else if (formdata.get("email") && formdata.get("username") === "") {
|
|
37
|
+ console.log("3");
|
|
38
|
+ let email = formdata.get("email");
|
|
39
|
+ setIsUpdate(true);
|
|
40
|
+ setUserData({ ...userData, username, email, id });
|
|
41
|
+ }
|
|
42
|
+ console.log("handleSubmit : " + userData);
|
10
|
43
|
};
|
11
|
44
|
|
|
45
|
+ const handleUpdate = async ({ email, username }) => {
|
|
46
|
+ let body = {
|
|
47
|
+ user: {
|
|
48
|
+ email: email,
|
|
49
|
+ username: username
|
|
50
|
+ }
|
|
51
|
+ };
|
|
52
|
+ return await update(`/users/${currentUser.id}`, body, token);
|
|
53
|
+ };
|
|
54
|
+
|
|
55
|
+ useEffect(async () => {
|
|
56
|
+ if (isUpdate === true && userData) {
|
|
57
|
+ const response = await handleUpdate(userData);
|
|
58
|
+ if (response.status === 200) {
|
|
59
|
+ console.log(response.data.attributes);
|
|
60
|
+ // const payload = {
|
|
61
|
+ // type: "user",
|
|
62
|
+ // id: response.data.id,
|
|
63
|
+ // email: response.data.attributes.email,
|
|
64
|
+ // username: response.data.attributes.username
|
|
65
|
+ // };
|
|
66
|
+ // console.log(response.data);
|
|
67
|
+ // console.log(`Payload : ${payload.type}`);
|
|
68
|
+ // dispatch(setCurrentUser(payload));
|
|
69
|
+ // history.push("/me");
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ }, [isUpdate, userData]);
|
|
73
|
+
|
12
|
74
|
return (
|
13
|
75
|
<form action="" onSubmit={handleSubmit}>
|
14
|
76
|
<label htmlFor="username">Nom d'utilisateur</label>
|
15
|
77
|
<input type="text" id="username" name="username" />
|
16
|
78
|
<label htmlFor="email">Adresse email</label>
|
17
|
79
|
<input type="email" id="email" name="email" />
|
18
|
|
- <label htmlFor="password">Mot de passe</label>
|
19
|
|
- <input type="password" id="password" name="password" />
|
20
|
|
- <label htmlFor="confirm_password">Confirmation du mot de passe</label>
|
21
|
|
- <input type="password" id="confirm_password" name="confirm_password" />
|
22
|
80
|
<button type="submit">Valider</button>
|
23
|
81
|
</form>
|
24
|
82
|
);
|