HomeContainer.js
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { withRouter } from 'react-router-dom';
import HomeForm from '../../components/home/HomeForm';
import { getPROFILE, initializeProfile } from '../../modules/profile';
const HomeContainer = ({ history }) => {
const dispatch = useDispatch();
const [HMArr, setHMArr] = useState([]);
const { user, profile } = useSelector(({ user, profile }) => ({
user: user.user,
profile: profile,
}));
const makeHeatmapValues = (PSdata) => {
let obj_keys = Object.keys(PSdata);
let result = [];
for (let i = 0; i < obj_keys.length; i++) {
result.push({
date:
//2019-10-15
obj_keys[i].slice(0, 4) +
'-' +
obj_keys[i].slice(4, 6) +
'-' +
obj_keys[i].slice(6, 8),
count: PSdata[obj_keys[i]].length,
});
}
return result;
};
useEffect(() => {
if (!user) {
alert('로그인이 필요합니다 ');
history.push('/login');
} else {
let username = user.username;
dispatch(getPROFILE({ username }));
return () => {
dispatch(initializeProfile());
};
}
}, [dispatch, user, history]);
useEffect(() => {
if (profile.solvedBJ_date) {
setHMArr(makeHeatmapValues(profile.solvedBJ_date.solvedBJbyDATE));
}
}, [profile]);
useEffect(() => {
if (user) {
let username = user.username;
dispatch(getPROFILE({ username }));
}
}, [dispatch, user]);
return (
<HomeForm
PSdata={profile.solvedBJ_date}
HMArr={HMArr}
goalNum={profile.goalNum}
/>
);
};
export default withRouter(HomeContainer);