Browse Source

Une réposne valide est obtenue suite à une requete update

Lou 3 years ago
parent
commit
2e9234c1c9

+ 3
- 3
src/components/forms/LoginForm/index.jsx View File

@@ -14,9 +14,9 @@ const LoginForm = () => {
14 14
 	const dispatch = useDispatch();
15 15
 	const history = useHistory();
16 16
 
17
-	const handleSubmit = (event) => {
18
-		event.preventDefault();
19
-		const formdata = new FormData(event.currentTarget);
17
+	const handleSubmit = (e) => {
18
+		e.preventDefault();
19
+		const formdata = new FormData(e.currentTarget);
20 20
 		setUserData({
21 21
 			...userData,
22 22
 			email: formdata.get("email"),

+ 42
- 54
src/components/forms/UserUpdateForm/index.jsx View File

@@ -8,68 +8,56 @@ import { setCurrentUser } from "../../../redux/action";
8 8
 
9 9
 const UserUpdateForm = () => {
10 10
 	const [userData, setUserData] = useState(null);
11
-	const [isUpdate, setIsUpdate] = useState(false);
12
-
13 11
 	const currentUser = useSelector((state) => state.currentUser);
14
-	const dispatch = useDispatch();
15
-	const history = useHistory();
16
-	const token = Cookies.get("token");
17
-
18
-	const handleSubmit = (event) => {
19
-		event.preventDefault();
20
-		const formdata = new FormData(event.currentTarget);
21 12
 
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 });
13
+	const handleSubmit = (e) => {
14
+		e.preventDefault();
15
+		const formdata = new FormData(e.currentTarget);
16
+		if (formdata.get("email") !== "" && formdata.get("username") !== "") {
17
+			const email = formdata.get("email");
18
+			const username = formdata.get("username");
19
+			setUserData({
20
+				...userData,
21
+				username: username,
22
+				email: email
23
+			});
24
+		} else if (
25
+			formdata.get("email") !== "" &&
26
+			formdata.get("username") === ""
27
+		) {
28
+			const email = formdata.get("email");
29
+			const username = currentUser.username;
30
+			setUserData({
31
+				...userData,
32
+				username: username,
33
+				email: email
34
+			});
35
+		} else if (
36
+			formdata.get("email") === "" &&
37
+			formdata.get("username") !== ""
38
+		) {
39
+			const email = currentUser.email;
40
+			const username = formdata.get("username");
41
+			setUserData({
42
+				...userData,
43
+				username: username,
44
+				email: email
45
+			});
41 46
 		}
42
-		console.log("handleSubmit : " + userData);
43 47
 	};
44 48
 
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);
49
+	const handleUpdate = async ({ username, email }) => {
50
+		const token = Cookies.get("token");
51
+		const body = { user: { username: username, email: email } };
52
+		const response = await update(`/users/${currentUser.id}`, body, token);
53
+		console.log(response);
53 54
 	};
54 55
 
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
-			}
56
+	useEffect(() => {
57
+		if (userData !== null) {
58
+			handleUpdate(userData);
71 59
 		}
72
-	}, [isUpdate, userData]);
60
+	}, [userData]);
73 61
 
74 62
 	return (
75 63
 		<form action="" onSubmit={handleSubmit}>

+ 0
- 7
src/redux/store/index.js View File

@@ -13,13 +13,6 @@ const persistConfig = {
13 13
 
14 14
 const persistedReducer = persistReducer(persistConfig, reducer)
15 15
 
16
-// const store = createStore(
17
-//   reducer,
18
-//   composeEnhancer(applyMiddleware(thunk))
19
-// );
20
-
21
-// export default store;
22
-
23 16
 const store = createStore(
24 17
   persistedReducer,
25 18
   composeEnhancer(applyMiddleware(thunk))

+ 1
- 2
src/services/request/Post.js View File

@@ -13,8 +13,7 @@ const post = async (
13 13
   }
14 14
 
15 15
   try {
16
-    const response = await axios.post(BASE_URL + endpoint, body, { headers: opt })
17
-    return response
16
+    return await axios.post(BASE_URL + endpoint, body, { headers: opt })
18 17
   } catch (err) {
19 18
     console.error(`An error occurred while trying to fetch ${endpoint}. ${err}`);
20 19
   }

+ 1
- 2
src/services/request/Update.js View File

@@ -13,8 +13,7 @@ const update = async (
13 13
   }
14 14
 
15 15
   try {
16
-    const response = await axios.patch(BASE_URL + endpoint, body, { headers: opt })
17
-    return response
16
+    return await axios.patch(BASE_URL + endpoint, body, { headers: opt })
18 17
   } catch (err) {
19 18
     console.error(`An error occurred while trying to fetch ${endpoint}. ${err}`);
20 19
   }

Loading…
Cancel
Save