|
1 | 1 | import styled from '@emotion/styled'; |
2 | | -import theme from 'styles/theme'; |
3 | | -import { useEffect } from 'react'; |
4 | | -import { getUser } from 'utils/apis/userApi'; |
5 | 2 | import { useUserContext } from 'contexts/UserContext'; |
6 | | -import { useState } from 'react'; |
| 3 | +import { useState, useCallback, useMemo } from 'react'; |
7 | 4 | import { Modal, Text } from 'components'; |
8 | 5 | import getUserLevel from 'utils/functions/userLevel/getUserLevel'; |
9 | | -import { useCallback } from 'react'; |
10 | 6 | import LevelModal from './LevelModal'; |
| 7 | +import theme from 'styles/theme'; |
| 8 | + |
| 9 | +const UserLevel = () => { |
| 10 | + const { currentUser } = useUserContext(); |
| 11 | + const [isModal, setIsModal] = useState(false); |
| 12 | + |
| 13 | + const levelData = useMemo( |
| 14 | + () => |
| 15 | + getUserLevel({ |
| 16 | + posts: currentUser.posts, |
| 17 | + comments: currentUser.comments, |
| 18 | + followers: currentUser.followers, |
| 19 | + }), |
| 20 | + [currentUser], |
| 21 | + ); |
| 22 | + |
| 23 | + const closeModal = useCallback(() => { |
| 24 | + setIsModal(false); |
| 25 | + }, []); |
| 26 | + |
| 27 | + return ( |
| 28 | + <LevelContainer> |
| 29 | + {levelData.level && ( |
| 30 | + <MyLevel> |
| 31 | + <LevelIcon src={levelData.level.image} /> |
| 32 | + {levelData.level?.name} |
| 33 | + </MyLevel> |
| 34 | + )} |
| 35 | + <MyHistory> |
| 36 | + <HistoryItem> |
| 37 | + <HistoryTitle>게시글</HistoryTitle> |
| 38 | + <HistoryCount>{currentUser.posts.length}</HistoryCount> |
| 39 | + </HistoryItem> |
| 40 | + <HistoryItem> |
| 41 | + <HistoryTitle>댓글</HistoryTitle> |
| 42 | + <HistoryCount>{currentUser.comments.length}</HistoryCount> |
| 43 | + </HistoryItem> |
| 44 | + <HistoryItem> |
| 45 | + <HistoryTitle>팔로워</HistoryTitle> |
| 46 | + <HistoryCount>{currentUser.followers.length}</HistoryCount> |
| 47 | + </HistoryItem> |
| 48 | + </MyHistory> |
| 49 | + |
| 50 | + <CurrentScore fontSize={16} block> |
| 51 | + <Text color={fontBlack} fontSize={16} underline onClick={() => setIsModal(true)}> |
| 52 | + 현재 활동 점수 |
| 53 | + </Text> |
| 54 | + : |
| 55 | + <Text fontSize={16} strong style={{ marginLeft: 5 }}> |
| 56 | + {levelData.score} |
| 57 | + </Text> |
| 58 | + </CurrentScore> |
| 59 | + <Modal visible={isModal} onClose={closeModal}> |
| 60 | + <Modal.Custom> |
| 61 | + <LevelModal onClose={closeModal} /> |
| 62 | + </Modal.Custom> |
| 63 | + </Modal> |
| 64 | + </LevelContainer> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +export default UserLevel; |
11 | 69 |
|
12 | 70 | const { mainGreen, mainWhite, fontNormal, fontBlack } = theme.color; |
13 | 71 |
|
@@ -79,85 +137,3 @@ const LevelIcon = styled.div` |
79 | 137 | background-repeat: no-repeat; |
80 | 138 | border-radius: 15px; |
81 | 139 | `; |
82 | | - |
83 | | -const UserLevel = () => { |
84 | | - const { currentUser } = useUserContext(); |
85 | | - const [userData, setUserData] = useState({ |
86 | | - post: 0, |
87 | | - comment: 0, |
88 | | - follower: 0, |
89 | | - currentLevel: null, |
90 | | - currentScore: 0, |
91 | | - }); |
92 | | - |
93 | | - const [isModal, setIsModal] = useState(false); |
94 | | - |
95 | | - const getUserInfo = async (userId) => { |
96 | | - const result = await getUser(userId).then((res) => res.data); |
97 | | - const { score, level } = getUserLevel({ |
98 | | - posts: result.posts, |
99 | | - comments: result.comments, |
100 | | - followers: result.followers, |
101 | | - }); |
102 | | - |
103 | | - setUserData({ |
104 | | - post: result.posts.length, |
105 | | - comment: result.comments.length, |
106 | | - follower: result.followers.length, |
107 | | - currentLevel: level, |
108 | | - currentScore: score, |
109 | | - }); |
110 | | - }; |
111 | | - |
112 | | - const closeModal = useCallback(() => { |
113 | | - setIsModal(false); |
114 | | - }, []); |
115 | | - |
116 | | - useEffect(() => { |
117 | | - if (currentUser.id) { |
118 | | - getUserInfo(currentUser.id); |
119 | | - } |
120 | | - }, [currentUser.id]); |
121 | | - |
122 | | - return ( |
123 | | - <LevelContainer> |
124 | | - {userData.currentLevel && ( |
125 | | - <MyLevel> |
126 | | - <LevelIcon src={userData.currentLevel.image} /> |
127 | | - {userData.currentLevel?.name} |
128 | | - </MyLevel> |
129 | | - )} |
130 | | - <MyHistory> |
131 | | - <HistoryItem> |
132 | | - <HistoryTitle>게시글</HistoryTitle> |
133 | | - <HistoryCount>{userData.post}</HistoryCount> |
134 | | - </HistoryItem> |
135 | | - <HistoryItem> |
136 | | - <HistoryTitle>댓글</HistoryTitle> |
137 | | - <HistoryCount>{userData.comment}</HistoryCount> |
138 | | - </HistoryItem> |
139 | | - <HistoryItem> |
140 | | - <HistoryTitle>팔로워</HistoryTitle> |
141 | | - <HistoryCount>{userData.follower}</HistoryCount> |
142 | | - </HistoryItem> |
143 | | - </MyHistory> |
144 | | - |
145 | | - <CurrentScore fontSize={16} block> |
146 | | - <Text color={fontBlack} fontSize={16} underline onClick={() => setIsModal(true)}> |
147 | | - 현재 활동 점수 |
148 | | - </Text> |
149 | | - : |
150 | | - <Text fontSize={16} strong style={{ marginLeft: 5 }}> |
151 | | - {userData.currentScore} |
152 | | - </Text> |
153 | | - </CurrentScore> |
154 | | - <Modal visible={isModal} onClose={closeModal}> |
155 | | - <Modal.Custom> |
156 | | - <LevelModal onClose={closeModal} /> |
157 | | - </Modal.Custom> |
158 | | - </Modal> |
159 | | - </LevelContainer> |
160 | | - ); |
161 | | -}; |
162 | | - |
163 | | -export default UserLevel; |
0 commit comments