Browse Source

inscription OK

Lou 3 years ago
parent
commit
ec15729f15
3 changed files with 12 additions and 19 deletions
  1. 8
    4
      src/components/forms/SignupForm/index.jsx
  2. 3
    14
      src/pages/Home/index.jsx
  3. 1
    1
      src/services/request/Post.js

+ 8
- 4
src/components/forms/SignupForm/index.jsx View File

@@ -43,13 +43,17 @@ const SignupForm = () => {
43 43
 		return await post("/users", body);
44 44
 	};
45 45
 
46
-	useEffect(() => {
46
+	useEffect(async () => {
47 47
 		if (passwordCheck === false) {
48 48
 			console.log("Passwords do not match");
49 49
 		} else if (passwordCheck === true && userData) {
50
-			const response = handleSignup(userData);
51
-			console.log(response.status);
52
-			// history.push({ pathname: "/", state: response.status });
50
+			const response = await handleSignup(userData);
51
+			if (response.status === 201) {
52
+				history.push({
53
+					pathname: "/",
54
+					state: { status: "201 : user created" }
55
+				});
56
+			}
53 57
 		}
54 58
 	}, [passwordCheck, userData]);
55 59
 

+ 3
- 14
src/pages/Home/index.jsx View File

@@ -1,4 +1,5 @@
1 1
 import React, { useEffect } from "react";
2
+import { useLocation } from "react-router";
2 3
 import { useSelector } from "react-redux";
3 4
 
4 5
 import Cookies from "js-cookie";
@@ -6,20 +7,7 @@ import Cookies from "js-cookie";
6 7
 const Home = () => {
7 8
 	const currentUser = useSelector((state) => state.currentUser);
8 9
 
9
-	// const isThereAToken = () => {
10
-	// 	let token = Cookies.get("token");
11
-	// 	if (token) {
12
-	// 		console.log("Connecté 666");
13
-	// 	} else {
14
-	// 		console.log("Non connecté 666");
15
-	// 	}
16
-	// };
17
-	// Problématique à résoudre : s'il n'y a plus de token stocké dans un cookie (supprimé volontairement ou non par un·e utilisateurice), il faut alors déconnecter l'utilisateurice (supprimer le contenu du store Redux)
18
-	// Piste : une fonction qui serait exécutée dans les useEffect des pages uniquement, pas des composants, et qui observerait la présence/absence d'un token, et viderait le store si nécessaire
19
-
20
-	// useEffect(() => {
21
-	// 	isThereAToken();
22
-	// }, []);
10
+	const location = useLocation();
23 11
 
24 12
 	return (
25 13
 		<div className="test">
@@ -35,6 +23,7 @@ const Home = () => {
35 23
 					<p>Utilisateurice non connecté·e.</p>
36 24
 				</div>
37 25
 			)}
26
+			<p>{location.state && location.state.status}</p>
38 27
 		</div>
39 28
 	);
40 29
 };

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

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

Loading…
Cancel
Save