-
Notifications
You must be signed in to change notification settings - Fork 2
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
[Feat] teamId 전역 상태로 구현 #322
Conversation
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.
고생하셨습니다 !
teamId
커스텀 훅에서 고민해볼 지점이 조금 있는 것 같아요. 라우트마다 effect
로 teamId 상태를 갈아껴주는 것은 결국 그냥 기존의 방법인 global state
로 쉽게 해결되는 것 같다고 느꼈습니다.
글로벌 변수 + 로컬스토리지를 활용하는 것은 어떨까요 ? globalState
가 있으면 해당 변수로, 없으면 localstorage
를 조회해서 그 값으로 상태를 변경해주고, 로컬스토리지마저 없다면 clubQuery
의 첫번째 아이디로 하는 식 ??
package.json
Outdated
"build-storybook": "storybook build", | ||
"check": "concurrently \"pnpm lint\" \"pnpm typeCheck\"", | ||
"chromatic": "npx chromatic --project-token=chpt_f4088febbfb82b7", |
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.
왜 위로 빼신건지.. ㅋㅋㅋㅋㅋㅋㅋ
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.
주기적으로 package.json을 정렬해주는 게 좋다고 어디서 들었는데 .... 이유 찾아올게요
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.
src/shared/hook/common/useTeamId.ts
Outdated
useEffect(() => { | ||
setTeamId(localStorage.getItem('teamId')); | ||
}, [location]); |
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.
url path
가 바뀔때마다 teamId
를 갈아껴주기 위함인가요 ??
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.
네 맞습니다 !
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.
?? 연산자는 처음 알게 되었네요! 덕분에 하나 배워갑니다~~ 미천한 저는 딱히 고칠점이 없어 보여서 이만 퇴장합니다 LGTM!
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.
오 요거머지되면 대시보드에도 적용하러 가겠습니다💨💨
LGTM !! ❤️
const { data } = useClubInfoQuery(); | ||
const { setTeamId } = useTeamIdAction(); | ||
|
||
if (data && !localStorage.getItem('teamId')) { | ||
localStorage.setItem('teamId', data.belongTeamGetResponses[0].id.toString()); | ||
setTeamId(data.belongTeamGetResponses[0].id); | ||
|
||
return data.belongTeamGetResponses[0].id; | ||
} |
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.
여기서 그냥 useClubInfoQuery
로 불러오지말고 useQuery
로 바로 사용해서 enable
에 localstorage
값이 있는지 없는지의 여부를 적용해준다면, 로컬스토리지 값이 있을 때 불필요한 호출을 줄일 수도 있을 것 같아요 !
또한 네이밍은 왜 initialize
인가용 ?
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.
새로운 값으로 초기화하는 역할을 한다고 생각해서 그렇게 명명했습니다. 다른 좋은 이름이 있을까요?
주용님 말씀처럼 enabled속성 써보는 방법이 불필요한 호출을 막을 수 있겠네요. 이 방법으로 함 수정해볼게요!
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.
음 나쁘지 않은 것 같아요 !! 좋습니당
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.
여기서 그냥
useClubInfoQuery
로 불러오지말고useQuery
로 바로 사용해서enable
에localstorage
값이 있는지 없는지의 여부를 적용해준다면, 로컬스토리지 값이 있을 때 불필요한 호출을 줄일 수도 있을 것 같아요 !
enabled 속성을 사용하려고 했으나
enabled: !localStorage.getItem('teamId')
와 같이 코드를 작성할 경우 localStorage에 teamId가 있을 때 쿼리를 실행하지 않게 되어 사이드 바에 저장된 워크스페이스를 불러오지 않게 되는 문제가 있었습니다. 그래서 해당 쿼리는 항상 실행되어야 하는 방향으로 다시 변경했습니다.
그리고 isSuccess값을 받아와서 데이터를 정상적으로 불러왔을 때 localStorage의 값을 다시 검사해서 teamId가 없는 경우 할당해주는 방식으로 수정했습니다.
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.
확인했습니다!!
const { data } = useClubInfoQuery(); | ||
const { setTeamId } = useTeamIdAction(); | ||
|
||
if (data && !localStorage.getItem('teamId')) { | ||
localStorage.setItem('teamId', data.belongTeamGetResponses[0].id.toString()); | ||
setTeamId(data.belongTeamGetResponses[0].id); | ||
|
||
return data.belongTeamGetResponses[0].id; | ||
} |
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.
음 나쁘지 않은 것 같아요 !! 좋습니당
|
||
if (isSuccess && data?.data?.teams) { | ||
// localStorage에 teamId가 없는 경우 | ||
const team = data.data.teams.find((team) => team); |
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.
요게 어떤 로직인가요 ? 그냥 데이터 값이 있는 첫 번째 요소를 찾는다라는 의미인가요 ?
data.teams
의 length
가 0보다 크다면 첫 번째 요소를 가져온다는 로직으로 단순화해도 좋을 것 같아요 !
현재 teams
배열이 있는지 확인 -> 요소가 있는 지 확인, 두 과정을 거치고 있는데 그냥 배열의 값이 있는지만 확인하면 한 번에 충족되지 않을까요 ?
현재 로직 충분히 좋은 것 같고, 정상 동작하는지 로컬에서 제대로 다시 한번 확인해주시면 감사하겠슴다 !
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.
데이터 값이 있는 첫 번째 요소를 찾는다라는 의미
맞습니다 ..ㅎㅎ
넵 로컬에서 확인했고 두 개의 조건문 대신 하나의 조건문으로 수정했습니다. 확인 부탁드려용 감사합니다 :)
해당 이슈 번호
closed #311
체크리스트
📌 내가 알게 된 부분
💎 PR Point
수정했습니다 !!!
기존의 zustand를 활용한 teamId store 파일을 활용한 커스텀 훅을 구현했습니다.
team.ts
useInitializeTeamId.tsx
조건문을 통해 로컬 스토리지에 teamId 값이 없을 때, 즉 앞의 쿼리가 success됐을 때 localStorage의 teamId값을 확인하고 없다면 쿼리에서의 data teamId의 첫 번째 아이디 값을 teamStore의 action 함수와 localStorage의 값을 설정해준 다음 리턴해줬고, 반대로 teamId가 localStorage에 있다면 해당 값을 리턴해주었습니다.
📌스크린샷 (선택)
2024-11-11.4.42.35.mov