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