Skip to content

Commit 04ae33d

Browse files
committed
๐Ÿ› Fix : ์ˆ˜์ • app.js - dispatch
- dispatch ํ•  ๋•Œ ์ธ์ž๋กœ token๊ฐ’์ด ์ฃผ์–ด์ง€์ง€ ์•Š์•„, ์ƒˆ๋กœ๊ณ ์นจ ์‹œ ํ† ํฐ์ด ์‚ญ์ œ๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ
1 parent b6e9eb4 commit 04ae33d

File tree

8 files changed

+29
-21
lines changed

8 files changed

+29
-21
lines changed

Diff for: โ€Žstyle/src/App.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect } from 'react'
2-
import { useDispatch } from 'react-redux'
2+
import { useDispatch, useSelector } from 'react-redux'
33
import { login } from './features/authSlices'
44
import MainPage from './components/MainPage/MainPage'
55
import FittingPage from './components/FittingPage/FittingPage.js'
@@ -13,12 +13,13 @@ import MyPage from './components/MyPage/MyPage.js'
1313
function App() {
1414
const dispatch = useDispatch()
1515

16-
useEffect(() => {
17-
const token = localStorage.getItem('token')
18-
const user = JSON.parse(localStorage.getItem('user'))
16+
const isAuthenticated = useSelector((state) => state.auth.isAuthenticated)
17+
const lsUser = JSON.parse(localStorage.getItem('user'))
18+
const lsToken = localStorage.getItem('token')
1919

20-
if (user && token) {
21-
dispatch(login(user))
20+
useEffect(() => {
21+
if (!isAuthenticated && lsUser && lsToken) {
22+
dispatch(login({ user: lsUser, token: lsToken }))
2223
}
2324
}, [dispatch])
2425

@@ -27,7 +28,7 @@ function App() {
2728
<Route path="/" element={<MainPage />} />
2829
<Route path="/fitting" element={<FittingPage />} />
2930
<Route path="/recommend" element={<RecommendPage />} />
30-
<Route path="/myPage" element={<MyPage />} />
31+
<Route path="/mypage" element={<MyPage />} />
3132
<Route exact path="/policyterms" element={<PolicyTerms />} />
3233
<Route exact path="/privacymain" element={<PrivacyMain />} />
3334
</Routes>

Diff for: โ€Žstyle/src/components/MainPage/Content.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import sizeListIcon1 from '../../images/sizeListIcon1.png'
88
import sizeListIcon2 from '../../images/sizeListIcon2.png'
99
import GrayButton from './GrayButton'
1010
import { Link } from 'react-router-dom'
11+
import { useSelector } from 'react-redux'
1112

1213
function Content() {
14+
const isAuthenticated = useSelector((state) => state.auth.isAuthenticated)
15+
console.log(`auth1 : ${isAuthenticated}`)
1316
return (
1417
<div className="mainPageContent">
1518
<section className="homeMainSection">

Diff for: โ€Žstyle/src/components/MainPage/MainPage.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import Navigation from '../Navigationbar/Nav.js'
22
import Footer from '../Footer.js'
33
import Content from './Content.js'
4+
import { useSelector } from 'react-redux'
45

56
function MainPage() {
7+
const user = useSelector((state) => state.auth.user)
8+
console.log('aa')
9+
console.log(user)
610
return (
711
<div className="mainPage">
812
<Navigation></Navigation>

Diff for: โ€Žstyle/src/components/MyPage/MyPage.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useState } from 'react'
22
import styles from './MyPage.module.css'
33
import LeftSubMyPage from './LeftSubMyPage'
44
import RightSubMyPage from './RightSubMyPage'
55
import Navigation from '../Navigationbar/Nav.js'
6-
import { useSelector } from 'react-redux'
76

87
function MyPage() {
98
const [page, setPage] = useState('privacy')

Diff for: โ€Žstyle/src/components/MyPage/MyPrivacy.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function MyPrivacy({ userId }) {
1414
// ์‹ ์ฒด ์ •๋ณด fetch
1515
useEffect(() => {
1616
const fetchSize = async () => {
17-
console.log('fet2')
1817
try {
1918
const response = await authenticatedAxios.get(
2019
`${API_URL}/userInfo/api/info?userId=${userId}`

Diff for: โ€Žstyle/src/components/MyPage/MySize.js

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function MySize({ userId }) {
3838
}
3939
}
4040
const fetchSize2 = async () => {
41-
console.log('fet2')
4241
try {
4342
const response = await authenticatedAxios.get(
4443
`${API_URL}/userInfo/api/info?userId=${userId}`

Diff for: โ€Žstyle/src/components/MyPage/RightSubMyPage.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ function RightSubMyPage({ page }) {
77
// ์ƒˆ๋กœ๊ณ ์นจ or ์ œ์ถœ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์›น ํŽ˜์ด์ง€ ๋ฆฌํ”„๋ ˆ์‰ฌ ๋˜๋ฉด์„œ redux ์ดˆ๊ธฐํ™” ๋จ
88
// redux ์ดˆ๊ธฐํ™” ๋˜๋ฉด์„œ user์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์—†์–ด์ ธ์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ.
99
const user = useSelector((state) => state.auth.user)
10-
console.log(`${user} ${user._id}`)
1110

1211
switch (page) {
1312
case 'privacy':
1413
return <MyPrivacy userId={user._id} />
1514
case 'size':
1615
return <MySize userId={user._id} />
17-
case 'closet':
18-
return (
19-
<>์˜ท์žฅ - ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•  ์ง€ ์ƒ๊ฐ ์ค‘. ๋ฌด์Šจ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ๊นŒ์š”?</>
20-
)
21-
case 'style':
22-
return (
23-
<>์Šคํƒ€์ผ - ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•  ์ง€ ์ƒ๊ฐ ์ค‘. ๋ฌด์Šจ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ๊นŒ์š”?</>
24-
)
16+
// case 'closet':
17+
// return (
18+
// <>์˜ท์žฅ - ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•  ์ง€ ์ƒ๊ฐ ์ค‘. ๋ฌด์Šจ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ๊นŒ์š”?</>
19+
// )
20+
// case 'style':
21+
// return (
22+
// <>์Šคํƒ€์ผ - ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•  ์ง€ ์ƒ๊ฐ ์ค‘. ๋ฌด์Šจ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ๊นŒ์š”?</>
23+
// )
2524
default:
2625
return <>error!</>
2726
}

Diff for: โ€Žstyle/src/features/authSlices.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { createSlice } from '@reduxjs/toolkit'
22

33
const initialState = {
44
isAuthenticated: false,
5-
user: null,
5+
// user: null,
6+
user: localStorage.getItem('user'),
67
token: localStorage.getItem('token'),
78
}
89

@@ -11,6 +12,9 @@ export const authSlice = createSlice({
1112
initialState,
1213
reducers: {
1314
login: (state, action) => {
15+
console.log('login called!')
16+
console.log(action)
17+
console.log(`in login ${action.payload.user} ${action.payload.token}`)
1418
state.isAuthenticated = true
1519
state.user = action.payload.user
1620
state.token = action.payload.token

0 commit comments

Comments
ย (0)