Browse Source

En cours : update des user details

Lou 3 years ago
parent
commit
4c398fc138

+ 0
- 2
src/components/forms/SignupForm/index.jsx View File

@@ -1,5 +1,4 @@
1 1
 import React, { useState, useEffect } from "react";
2
-import { useDispatch } from "react-redux";
3 2
 import { useHistory } from "react-router-dom";
4 3
 
5 4
 import post from "../../../services/request/Post";
@@ -9,7 +8,6 @@ const SignupForm = () => {
9 8
 	const [userData, setUserData] = useState(null);
10 9
 	const [passwordCheck, setPasswordCheck] = useState(null);
11 10
 
12
-	const dispatch = useDispatch();
13 11
 	const history = useHistory();
14 12
 
15 13
 	const handleSubmit = (event) => {

+ 64
- 6
src/components/forms/UserUpdateForm/index.jsx View File

@@ -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
 	);

+ 1
- 0
src/pages/Profile/index.jsx View File

@@ -34,6 +34,7 @@ const ProfilePage = () => {
34 34
 	};
35 35
 
36 36
 	useEffect(() => {
37
+		console.log(currentUser);
37 38
 		if (deleteSuccess && deleteSuccess === true) {
38 39
 			Cookies.remove("token");
39 40
 			dispatch(removeCurrentUser());

Loading…
Cancel
Save