-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[김창민] week20 #1076
Open
changmin6362
wants to merge
10
commits into
codeit-bootcamp-frontend:part3-김창민
Choose a base branch
from
changmin6362:part3-KimChangmin-week20
base: part3-김창민
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[김창민] week20 #1076
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b46ec3
add .gitignore
changmin6362 01f33e4
add templete file-week5
changmin6362 d0f6452
mk signin post api
changmin6362 bd3c715
rebuild signup post api to async
changmin6362 80025ae
add email check post api
changmin6362 ab79043
mk part2 branch
changmin6362 8aec90d
refactor: migrate code to next.js
changmin6362 4e9cbb4
feat: 템플릿 파일 등록
changmin6362 3b8e612
feat: 리액트 쿼리, devtools 설치 및 provider 적용
changmin6362 d2a8f7f
feat: api 리액트 쿼리 및 커스텀훅으로 변경하기
changmin6362 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
1-weekly-mission-week15-main/src/auth/data-access-auth/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { useQuery, useMutation } from "@tanstack/react-query"; | ||
import { useState, useEffect } from "react"; | ||
import { Link } from "@/src/link/type"; | ||
|
||
const BASE_URL = "https://bootcamp-api.codeit.kr/api/linkbrary/v1"; | ||
const DEFAULT_USER = { | ||
id: 0, | ||
name: "", | ||
email: "", | ||
imageSource: "", | ||
}; | ||
|
||
interface LoginParams { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
interface SignUpParams extends LoginParams {} | ||
|
||
export function usePostLogin() { | ||
return useMutation({ | ||
mutationFn: async ({ email, password }: LoginParams) => { | ||
const response = await fetch(`${BASE_URL}/auth/sign-in`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
const data = await response.json(); | ||
|
||
if (data?.accessToken) { | ||
localStorage.setItem("accessToken", data.accessToken); | ||
} | ||
return data; | ||
}, | ||
}); | ||
} | ||
|
||
export function usePostSignup() { | ||
return useMutation({ | ||
mutationFn: async ({ email, password }: SignUpParams) => { | ||
const response = await fetch(`${BASE_URL}/auth/sign-up`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
const data = await response.json(); | ||
return data; | ||
}, | ||
}); | ||
} | ||
|
||
export function useGetUser() { | ||
const [token, setToken] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
setToken(localStorage.getItem("accessToken")); | ||
}, []); | ||
|
||
return useQuery({ | ||
queryKey: ["userInfo"], | ||
queryFn: async () => { | ||
const response = await fetch(`${BASE_URL}/users`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
const responseData = await response.json(); | ||
|
||
const data = responseData?.[0] && { | ||
id: responseData?.[0].id, | ||
name: responseData?.[0].name, | ||
email: responseData?.[0].email, | ||
imageSource: responseData?.[0].image_source, | ||
}; | ||
localStorage.setItem("userId", data.id); | ||
|
||
return data; | ||
}, | ||
enabled: !!token, | ||
Comment on lines
+75
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존 코드에서처럼, !!userId가 추가되어야 react-query로 안정적으로 마이그레이션했다고 할 수 있을거같아요. 지금은 동작이 달라질걸로 보입니다. userId 없이 유저목록을 조회하고 그 중에 언제나 첫번째 유저를 반환할거라서요 enabled: !!token && !!userId |
||
initialData: DEFAULT_USER, | ||
}); | ||
} | ||
|
||
export function useGetFolders() { | ||
const [token, setToken] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
setToken(localStorage.getItem("accessToken")); | ||
}, []); | ||
|
||
return useQuery({ | ||
queryKey: ["folders"], | ||
queryFn: async () => { | ||
const response = await fetch(`${BASE_URL}/folders`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
const data = await response.json(); | ||
localStorage.setItem("folderId", data?.[0].id); | ||
|
||
return data; | ||
}, | ||
enabled: !!token, | ||
}); | ||
} | ||
export function useGetLinks() { | ||
const [token, setToken] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
setToken(localStorage.getItem("accessToken")); | ||
}, []); | ||
|
||
return useQuery({ | ||
queryKey: ["links"], | ||
queryFn: async () => { | ||
const response = await fetch(`${BASE_URL}/sample/links`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
const data = await response.json(); | ||
return data; | ||
}, | ||
}); | ||
} | ||
|
||
export function useGetSharedLinks() { | ||
const [userId, setUserId] = useState<number | null>(null); | ||
const [folderId, setFolderId] = useState<number | null>(null); | ||
|
||
useEffect(() => { | ||
setUserId(Number(localStorage.getItem("userId"))); | ||
setFolderId(Number(localStorage.getItem("folderId"))); | ||
}, []); | ||
|
||
return useQuery<Link[]>({ | ||
queryKey: ["userLinks"], | ||
queryFn: async () => { | ||
const response = await fetch( | ||
`${BASE_URL}/users/${userId}/links?folderId=${folderId}`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
const data = await response.json(); | ||
return data; | ||
}, | ||
enabled: !!userId, | ||
}); | ||
} |
3 changes: 0 additions & 3 deletions
3
1-weekly-mission-week15-main/src/auth/data-access-auth/index.ts
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
1-weekly-mission-week15-main/src/auth/data-access-auth/useSignIn.tsx
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
1-weekly-mission-week15-main/src/auth/data-access-auth/useSignUp.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
템플릿 코드에서 중요한차이가 userId를 인자로 받는지 여부군요.
use get user 기 때문에, 특정 user를 조회하는 기능을 가진 훅인데, userId를 받지 않는 다는 것만으로도 혼동을 줄만한 상황입니다. 템플릿코드를 참고해주세요. 아래라인에서 좀더 코멘트드릴게요