Browse Source

Un utilisateur peut supprimer son compte

Lou 3 years ago
parent
commit
aad9272c85

+ 1
- 0
src/components/buttons/LogoutButton/index.jsx View File

@@ -12,6 +12,7 @@ const LogoutButton = () => {
12 12
 		event.preventDefault();
13 13
 		Cookies.remove("token");
14 14
 		dispatch(removeCurrentUser());
15
+		localStorage.removeItem("persist:root");
15 16
 		history.push("/");
16 17
 	};
17 18
 

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

@@ -48,7 +48,7 @@ const SignupForm = () => {
48 48
 			console.log("Passwords do not match");
49 49
 		} else if (passwordCheck === true && userData) {
50 50
 			const response = await handleSignup(userData);
51
-			if (response.status === 201) {
51
+			if (response.status && response.status === 201) {
52 52
 				history.push({
53 53
 					pathname: "/",
54 54
 					state: { status: "201 : user created" }

+ 33
- 4
src/pages/Profile/index.jsx View File

@@ -1,15 +1,44 @@
1
-import React, { useEffect } 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 del from "../../services/request/Delete";
7
+import { removeCurrentUser } from "../../redux/action/index";
3 8
 
4 9
 const ProfilePage = () => {
10
+	const [deleteSuccess, setDeleteSuccess] = useState(null);
5 11
 	const currentUser = useSelector((state) => state.currentUser);
6 12
 
13
+	const dispatch = useDispatch();
14
+	const history = useHistory();
15
+
16
+	const handleDelete = async () => {
17
+		let token = Cookies.get("token");
18
+		const response = await del(`/users/${currentUser.id}`, token);
19
+		if (response.status === 204) {
20
+			setDeleteSuccess(true);
21
+		} else if (response.status === 403) {
22
+			setDeleteSuccess(false);
23
+		}
24
+	};
25
+
7 26
 	useEffect(() => {
8
-		console.log(currentUser);
9
-	}, []);
27
+		if (deleteSuccess && deleteSuccess === true) {
28
+			Cookies.remove("token");
29
+			dispatch(removeCurrentUser());
30
+			localStorage.removeItem("persist:root");
31
+			history.push({
32
+				pathname: "/",
33
+				state: { status: "204 : user deleted" }
34
+			});
35
+		}
36
+	}, [deleteSuccess]);
37
+
10 38
 	return (
11 39
 		<div>
12 40
 			<h1>Hello {currentUser.username}</h1>
41
+			<button onClick={handleDelete}>Supprimer le compte</button>
13 42
 		</div>
14 43
 	);
15 44
 };

+ 17
- 0
src/services/request/Delete.js View File

@@ -0,0 +1,17 @@
1
+import axios from 'axios';
2
+import { BASE_URL } from "./config.js";
3
+
4
+const del = async (
5
+  endpoint,
6
+  jwt_token = null,
7
+  header = { "Authorization": jwt_token }) => {
8
+
9
+  try {
10
+    const response = await axios.delete(BASE_URL + endpoint, { headers: header })
11
+    return response
12
+  } catch (err) {
13
+    console.error(`An error occurred while trying to fetch ${endpoint}. ${err}`);
14
+  }
15
+}
16
+
17
+export default del;

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

@@ -1,4 +1,4 @@
1
-const BASE_URL = "https://registra-api.herokuapp.com/api/v1"
1
+const BASE_URL = "https://registra.netlib.re/api/v1"
2 2
 // const BASE_URL = "http://127.0.0.1:4000/api/v1"
3 3
 
4 4
 export { BASE_URL }

Loading…
Cancel
Save