Skip to content

Commit 3c7c679

Browse files
committed
✨ Feat: 추가 마이페이지 기능
- 사이즈 정보 수정 기능 추가 - 키/몸무게만 입력되어 있는 경우 sizeAPI를 이용해서 값을 채움
1 parent 801f9c8 commit 3c7c679

File tree

11 files changed

+218
-97
lines changed

11 files changed

+218
-97
lines changed

Diff for: src/routes/UserInfo.js

+81-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ const router = express.Router()
55
const mongoose = require('mongoose')
66
const { Schema } = mongoose
77

8-
const User = require('../models/User')
8+
const axios = require('axios')
99

10-
// size api가 없어서, 테스트용으로 임시로 만듦
11-
const sizeProfileSchema = new Schema({
12-
userId: { type: String },
13-
length: { type: String },
14-
shoulderWidth: { type: String },
15-
chestWidth: { type: String },
16-
})
10+
const User = require('../models/User')
11+
const SizeProfile = require('../models/SizeProfile')
1712

18-
const SizeProfile = mongoose.model('sizeProfiles', sizeProfileSchema)
13+
const sizeAPI = process.env.AI_SIZE_API_URL
1914

2015
// 사이즈 : backend -> frontend
2116
router.get('/api/size', async (req, res) => {
17+
console.log('get /userInfo/api/size')
2218
const { userId } = req.query
2319
try {
2420
const sizeProfile = await SizeProfile.findOne({ userId: userId })
@@ -30,6 +26,8 @@ router.get('/api/size', async (req, res) => {
3026
})
3127

3228
router.get('/api/info', async (req, res) => {
29+
console.log('get /userInfo/api/info')
30+
3331
const { userId } = req.query
3432

3533
try {
@@ -51,6 +49,7 @@ router.get('/api/info', async (req, res) => {
5149

5250
// 사용자 정보 업데이트
5351
router.put('/api/privacy', async (req, res) => {
52+
console.log('put /userInfo/api/privacy')
5453
const user = req.body
5554
const userId = user.userId
5655

@@ -61,12 +60,82 @@ router.put('/api/privacy', async (req, res) => {
6160
try {
6261
const result = await User.findByIdAndUpdate({ _id: userId }, update)
6362

64-
res.status(201).json({ success: true, code: 0, user: result })
63+
res.status(201).json({ success: true, code: 'UPDATE_DONE', errno: 0 })
64+
} catch (error) {
65+
res.status(500).json({
66+
success: false,
67+
code: error.codeName,
68+
errno: error.code,
69+
/* message: 'An error occurred while updating the document.', */
70+
})
71+
}
72+
})
73+
74+
// 사이즈 정보 업데이트
75+
router.put('/api/size', async (req, res) => {
76+
console.log('put /userInfo/api/size')
77+
const reqSize = req.body
78+
const userId = reqSize.userId
79+
80+
let isShoulderWidthNull = reqSize.shoulderWidth ? false : true
81+
let isChestWidthNull = reqSize.chestWidth ? false : true
82+
let isLengthNull = reqSize.length ? false : true
83+
84+
let sizeData = {
85+
shoulderWidth: reqSize.shoulderWidth,
86+
chestWidth: reqSize.chestWidth,
87+
length: reqSize.length,
88+
}
89+
let userData = { height: reqSize.height, weight: reqSize.weight }
90+
let sizeResponse
91+
92+
// 하나라도 null인 경우 size api 사용해서 값 가져옴
93+
if (isShoulderWidthNull || isChestWidthNull || isLengthNull) {
94+
sizeResponse = await axios.get(sizeAPI, {
95+
params: userData,
96+
})
97+
// null인 값 sizeRes로 채워줌
98+
if (isShoulderWidthNull)
99+
sizeData.shoulderWidth = sizeResponse.data.size.shoulderWidth
100+
if (isChestWidthNull)
101+
sizeData.chestWidth = sizeResponse.data.size.chestWidth
102+
if (isLengthNull) sizeData.length = sizeResponse.data.size.length
103+
104+
if (sizeResponse.data.error) {
105+
console.error('Error from AI API:', sizeResponse.data.error)
106+
res
107+
.status(500)
108+
.json({ success: false, code: 'SIZE_API_FAILED', errno: -1 })
109+
return
110+
}
111+
}
112+
113+
try {
114+
const userUpdateRes = await User.findByIdAndUpdate(
115+
{ _id: userId },
116+
{
117+
$set: userData,
118+
},
119+
{ new: true }
120+
)
121+
console.log(userUpdateRes)
122+
const sizeUpdateRes = await SizeProfile.findOneAndUpdate(
123+
{ userId: userId },
124+
{
125+
$set: sizeData,
126+
},
127+
{ new: true }
128+
)
129+
console.log(sizeUpdateRes)
130+
131+
res.status(201).json({ success: true, code: 'UPDATE_DONE', errno: 0 })
65132
} catch (error) {
133+
console.log('asdofjh')
134+
// console.log(error)
66135
res.status(500).json({
67136
success: false,
68-
code: error.code,
69-
message: 'An error occurred while updating the document.',
137+
code: error.codeName,
138+
errno: error.code,
70139
})
71140
}
72141
})

Diff for: style/src/components/FittingPage/FittingPage.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import Navigation from '../Navigationbar/Nav'
22

33
import LeftFitContainer from './LeftFitContainer'
44
import RightFitContainer from './RightFitContainer'
5-
import AlertMessage from '../common/AlertMessage'
65

76
import styles from './FittingPage.module.css'
87

98
import React from 'react'
109
import { useState } from 'react'
11-
import { useSelector } from 'react-redux'
1210

1311
import shoppingImage from '../../images/shopping-mall.png'
1412
import { Navigate } from 'react-router-dom'
@@ -28,7 +26,7 @@ function FittingPage() {
2826
setIsShowAlert(true)
2927
setTimeout(() => {
3028
setIsShowAlert(false)
31-
}, 3000)
29+
}, time)
3230
}
3331

3432
if (lsUser && lsToken) {

Diff for: style/src/components/MyPage/MyPage.js

+14-28
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import LeftSubMyPage from './LeftSubMyPage'
44
import RightSubMyPage from './RightSubMyPage'
55
import Navigation from '../Navigationbar/Nav.js'
66
import { Navigate } from 'react-router-dom'
7-
import AlertMessage from '../common/AlertMessage'
7+
import MyPageAlert from './MyPageAlert'
88

99
function MyPage({ currPage = 'privacy' }) {
1010
const [page, setPage] = useState(currPage)
11-
const [isSaveComplete, setIsSaveComplete] = useState(false)
1211
const [isShowAlert, setIsShowAlert] = useState(false)
1312
const [errorCode, setErrorCode] = useState(false)
1413

14+
const showAlert = (time = 3000) => {
15+
setIsShowAlert(true)
16+
setTimeout(() => {
17+
setIsShowAlert(false)
18+
}, time)
19+
}
20+
1521
const changePage = (pageName) => {
1622
setPage(pageName)
1723
}
@@ -27,35 +33,15 @@ function MyPage({ currPage = 'privacy' }) {
2733
<LeftSubMyPage page={page} onClickHandler={changePage} />
2834
<RightSubMyPage
2935
page={page}
30-
setIsShowAlert={setIsShowAlert}
31-
setIsSaveComplete={setIsSaveComplete}
36+
showAlert={showAlert}
3237
setErrorCode={setErrorCode}
3338
/>
3439
</div>
35-
{isSaveComplete && (
36-
<AlertMessage
37-
variant="success"
38-
message={'저장에 성공하였습니다.'}
39-
show={isShowAlert}
40-
setShow={setIsShowAlert}
41-
/>
42-
)}
43-
{!isSaveComplete && errorCode === 11000 && (
44-
<AlertMessage
45-
variant="danger"
46-
message={'저장에 실패하였습니다. 중복되는 이메일입니다.'}
47-
show={isShowAlert}
48-
setShow={setIsShowAlert}
49-
/>
50-
)}
51-
{!isSaveComplete && errorCode !== 11000 && (
52-
<AlertMessage
53-
variant="danger"
54-
message={'저장에 실패하였습니다. 다시 한번 시도해 주세요.'}
55-
show={isShowAlert}
56-
setShow={setIsShowAlert}
57-
/>
58-
)}
40+
<MyPageAlert
41+
errorCode={errorCode}
42+
isShowAlert={isShowAlert}
43+
setIsShowAlert={setIsShowAlert}
44+
/>
5945
</>
6046
)
6147
} else {

Diff for: style/src/components/MyPage/MyPageAlert.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import AlertMessage from '../common/AlertMessage'
2+
3+
function MyPageAlert({ errorCode, isShowAlert, setIsShowAlert }) {
4+
const errorCodeList = ['UPDATE_DONE', 'DuplicateKey']
5+
return (
6+
<>
7+
{errorCode === 'UPDATE_DONE' && (
8+
<AlertMessage
9+
variant={errorCode === 'UPDATE_DONE' ? 'success' : 'danger'}
10+
message={'저장에 성공하였습니다.'}
11+
show={isShowAlert}
12+
setShow={setIsShowAlert}
13+
/>
14+
)}
15+
{errorCode === 'DuplicateKey' && (
16+
<AlertMessage
17+
variant={errorCode === 'UPDATE_DONE' ? 'success' : 'danger'}
18+
message={'저장에 실패하였습니다. 중복되는 이메일입니다.'}
19+
show={isShowAlert}
20+
setShow={setIsShowAlert}
21+
/>
22+
)}
23+
{!errorCodeList.includes(errorCode) && (
24+
<AlertMessage
25+
variant={errorCode === 'UPDATE_DONE' ? 'success' : 'danger'}
26+
message={'저장에 실패하였습니다. 다시 한번 시도해 주세요.'}
27+
show={isShowAlert}
28+
setShow={setIsShowAlert}
29+
/>
30+
)}
31+
</>
32+
)
33+
}
34+
35+
export default MyPageAlert

Diff for: style/src/components/MyPage/MyPrivacy.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import authenticatedAxios from '../../api/authenticatedAxios'
66
import { API_URL } from '../../api/apiConfig'
77
import { updateUser } from '../User/UpdateInfo.js'
88

9-
function MyPrivacy({
10-
userId,
11-
setIsShowAlert,
12-
setIsSaveComplete,
13-
setErrorCode,
14-
}) {
9+
function MyPrivacy({ userId, showAlert, setErrorCode }) {
1510
const [name, setName] = useState('')
1611
const [gender, setGender] = useState('')
1712
const [email, setEmail] = useState('')
@@ -69,12 +64,8 @@ function MyPrivacy({
6964
modifyUser.userId = userId
7065
modifyUser.gender = gender
7166
updateUser(modifyUser).then((result) => {
72-
setIsSaveComplete(result.success)
7367
setErrorCode(result.code)
74-
setIsShowAlert(true)
75-
setTimeout(() => {
76-
setIsShowAlert(false)
77-
}, 3000)
68+
showAlert()
7869
})
7970
}
8071
}
@@ -84,7 +75,8 @@ function MyPrivacy({
8475
const fetchSize = async () => {
8576
try {
8677
const response = await authenticatedAxios.get(
87-
`${API_URL}/userInfo/api/info?userId=${userId}`
78+
`${API_URL}/userInfo/api/info`,
79+
{ params: { userId: userId } }
8880
)
8981
if (response.status === 200 || response.status === 201) {
9082
user.current = response.data.user
@@ -105,7 +97,7 @@ function MyPrivacy({
10597
}
10698
}
10799
}
108-
fetchSize()
100+
if (userId) fetchSize()
109101
}, [userId])
110102
return (
111103
<form onSubmit={onSubmit} noValidate>

0 commit comments

Comments
 (0)