Selaa lähdekoodia

En cours : update des user details

Lou 3 vuotta sitten
vanhempi
commit
4c398fc138

+ 0
- 2
src/components/forms/SignupForm/index.jsx Näytä tiedosto

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

+ 64
- 6
src/components/forms/UserUpdateForm/index.jsx Näytä tiedosto

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
 const UserUpdateForm = () => {
9
 const UserUpdateForm = () => {
10
+	const [userData, setUserData] = useState(null);
11
+	const [isUpdate, setIsUpdate] = useState(false);
12
+
5
 	const currentUser = useSelector((state) => state.currentUser);
13
 	const currentUser = useSelector((state) => state.currentUser);
14
+	const dispatch = useDispatch();
15
+	const history = useHistory();
16
+	const token = Cookies.get("token");
6
 
17
 
7
 	const handleSubmit = (event) => {
18
 	const handleSubmit = (event) => {
8
 		event.preventDefault();
19
 		event.preventDefault();
9
 		const formdata = new FormData(event.currentTarget);
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
 	return (
74
 	return (
13
 		<form action="" onSubmit={handleSubmit}>
75
 		<form action="" onSubmit={handleSubmit}>
14
 			<label htmlFor="username">Nom d'utilisateur</label>
76
 			<label htmlFor="username">Nom d'utilisateur</label>
15
 			<input type="text" id="username" name="username" />
77
 			<input type="text" id="username" name="username" />
16
 			<label htmlFor="email">Adresse email</label>
78
 			<label htmlFor="email">Adresse email</label>
17
 			<input type="email" id="email" name="email" />
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
 			<button type="submit">Valider</button>
80
 			<button type="submit">Valider</button>
23
 		</form>
81
 		</form>
24
 	);
82
 	);

+ 1
- 0
src/pages/Profile/index.jsx Näytä tiedosto

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

Loading…
Peruuta
Tallenna