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