Skip to content

Commit

Permalink
[fix] #36 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kr-nius committed Mar 13, 2024
1 parent f81138c commit ea840fa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { confirmAlert } from '@/utils/function/utilFunction'
import { useRouter } from 'next/navigation'
import { FormEvent, useState } from 'react'
import { useAuth } from '../../../contexts/AuthContext'
import AuthUserEmailStore from '@/Store/AuthUserEmail'

export default function LoginForm() {
const [email, setEmail] = useState<string>('')
const [password, setPassword] = useState<string>('')
const { userEmail, setUserEmail } = AuthUserEmailStore()
const router = useRouter()
const { login } = useAuth()

Expand All @@ -34,7 +36,9 @@ export default function LoginForm() {
const loginRes = await res.json()
console.log('로그인 loginRes: ', loginRes)

login(loginRes.email, loginRes.token)
setUserEmail(loginRes.email)

// login(loginRes.email, loginRes.token)

confirmAlert(`${loginRes.nickname}님 환영합니다!`)

Expand Down
22 changes: 22 additions & 0 deletions weatherfit_refactoring/src/Store/AuthUserEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'

interface UserEmailStore {
userEmail: string | null
setUserEmail: (userEmail: string | null) => void
}

const AuthUserEmailStore = create(
persist<UserEmailStore>(
(set, get) => ({
userEmail: null,
setUserEmail: (userEmail: string | null) =>
set(() => ({ userEmail: userEmail })),
}),
{
name: 'user_email',
},
),
)

export default AuthUserEmailStore
79 changes: 41 additions & 38 deletions weatherfit_refactoring/src/app/mypage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react'
import ProfileInfo from '@/Components/Molecules/ProfileInfo'
import ProfileHeader from '@/Components/Organisms/ProfileHeader'
import ProfileBoard from '@/Components/Organisms/ProfileBoard'
import AuthUserEmailStore from '@/Store/AuthUserEmail'

export default function Mypage() {
// 회원 정보
Expand All @@ -12,53 +13,55 @@ export default function Mypage() {
const [refreshProfile, setRefreshProfile] = useState<boolean>(false) // 회원 정보 변경했을 때
const [myPostData, setMyPostData] = useState<FEEDDATA[]>([])
const [myLikePostData, setMyLikePostData] = useState<FEEDDATA[]>([])
const { userEmail, setUserEmail } = AuthUserEmailStore()

console.log('user email: ', userEmail)
// 회원 정보 불러오기
useEffect(() => {
const fetchData = async () => {
try {
// 프로필 데이터 가져오기
const res = await fetch(`https://www.jerneithe.site/user/api/profile`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer',
},
body: JSON.stringify({
email: localStorage.getItem('user_email'),
}),
})
// useEffect(() => {
// const fetchData = async () => {
// try {
// // 프로필 데이터 가져오기
// const res = await fetch(`https://www.jerneithe.site/user/api/profile`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: 'Bearer',
// },
// body: JSON.stringify({
// email: localStorage.getItem('user_email'),
// }),
// })

const profileRes = await res.json()
// const profileRes = await res.json()

setUserProfile(profileRes)
setUserImage(profileRes.image)
// setUserProfile(profileRes)
// setUserImage(profileRes.image)

console.log('유저 data: ', profileRes)
// console.log('유저 data: ', profileRes)

// -----------------------------------------
// // -----------------------------------------

// 게시물 데이터 가져오기
const boardListRes = await fetch(
'https://www.jerneithe.site/board/list',
)
const boardListdata: FEEDDATA[] = await boardListRes.json()
// // 게시물 데이터 가져오기
// const boardListRes = await fetch(
// 'https://www.jerneithe.site/board/list',
// )
// const boardListdata: FEEDDATA[] = await boardListRes.json()

const filteredData = boardListdata.filter(
item => item.nickName === userPofile.nickname,
)
setMyPostData(filteredData)
// const filteredData = boardListdata.filter(
// item => item.nickName === userPofile.nickname,
// )
// setMyPostData(filteredData)

const filteredLikeData = boardListdata.filter(item =>
item.likelist.some(like => like.nickName === userPofile.nickname),
)
setMyLikePostData(filteredLikeData)
} catch (error) {
console.error('데이터 로딩 에러: ', error)
}
}
fetchData()
}, [refreshProfile])
// const filteredLikeData = boardListdata.filter(item =>
// item.likelist.some(like => like.nickName === userPofile.nickname),
// )
// setMyLikePostData(filteredLikeData)
// } catch (error) {
// console.error('데이터 로딩 에러: ', error)
// }
// }
// fetchData()
// }, [refreshProfile])

return (
<>
Expand Down

0 comments on commit ea840fa

Please sign in to comment.