Skip to content
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

Merged
merged 22 commits into from
Nov 20, 2024

Conversation

namdaeun
Copy link
Member

@namdaeun namdaeun commented Nov 11, 2024

해당 이슈 번호

closed #311


체크리스트

  • 🔀 PR 제목의 형식을 잘 작성했나요? e.g. [feat] PR을 등록한다.
  • 💯 테스트는 잘 통과했나요?
  • 🏗️ 빌드는 성공했나요?
  • 🧹 불필요한 코드는 제거했나요?
  • ✅ 컨벤션을 지켰나요?
  • 💭 이슈는 등록했나요?
  • 🏷️ 라벨은 등록했나요?
  • 💻 git rebase를 사용했나요?
  • 🙇‍♂️ 리뷰어를 지정했나요?

📌 내가 알게 된 부분


💎 PR Point

수정했습니다 !!!
기존의 zustand를 활용한 teamId store 파일을 활용한 커스텀 훅을 구현했습니다.

team.ts

const useTeamStore = create<TeamStore>((set) => ({
  teamId: Number(localStorage.getItem('teamId')),

  actions: {
    setTeamId: (teamId: number) =>
      set({
        teamId,
      }),
  },
}));

useInitializeTeamId.tsx

export const useInitializeTeamId = () => {
  const { data, isSuccess } = useClubInfoQuery();
  const { setTeamId } = useTeamIdAction();

  if (isSuccess && !localStorage.getItem('teamId')) {
    const teamId = data.belongTeamGetResponses[0].id;
    localStorage.setItem('teamId', teamId.toString());

    setTeamId(teamId);

    return teamId;
  }

  return Number(localStorage.getItem('teamId'));
};

조건문을 통해 로컬 스토리지에 teamId 값이 없을 때, 즉 앞의 쿼리가 success됐을 때 localStorage의 teamId값을 확인하고 없다면 쿼리에서의 data teamId의 첫 번째 아이디 값을 teamStore의 action 함수와 localStorage의 값을 설정해준 다음 리턴해줬고, 반대로 teamId가 localStorage에 있다면 해당 값을 리턴해주었습니다.


📌스크린샷 (선택)

2024-11-11.4.42.35.mov

Copy link

🚀 Storybook 확인하기 🚀

Copy link
Member

@wuzoo wuzoo left a 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
Comment on lines 8 to 10
"build-storybook": "storybook build",
"check": "concurrently \"pnpm lint\" \"pnpm typeCheck\"",
"chromatic": "npx chromatic --project-token=chpt_f4088febbfb82b7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 위로 빼신건지.. ㅋㅋㅋㅋㅋㅋㅋ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주기적으로 package.json을 정렬해주는 게 좋다고 어디서 들었는데 .... 이유 찾아올게요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷 2024-11-14 오후 2 45 58

그렇다고 합니다
저희는 프로젝트 규모가 크진 않아서 소팅 안해도 괜찮겠네요 하하

Comment on lines 12 to 14
useEffect(() => {
setTeamId(localStorage.getItem('teamId'));
}, [location]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url path가 바뀔때마다 teamId를 갈아껴주기 위함인가요 ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞습니다 !

Copy link
Contributor

@rtttr1 rtttr1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? 연산자는 처음 알게 되었네요! 덕분에 하나 배워갑니다~~ 미천한 저는 딱히 고칠점이 없어 보여서 이만 퇴장합니다 LGTM!

Copy link

🚀 Storybook 확인하기 🚀

Copy link
Contributor

@Bowoon1216 Bowoon1216 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 요거머지되면 대시보드에도 적용하러 가겠습니다💨💨
LGTM !! ❤️

@namdaeun namdaeun changed the title [Feat] teamId 관리 커스텀 훅 구현 [Feat] teamId 전역으로 관리 Nov 14, 2024
@namdaeun namdaeun changed the title [Feat] teamId 전역으로 관리 [Feat] teamId 전역 상태 관리 구현 Nov 14, 2024
@namdaeun namdaeun changed the title [Feat] teamId 전역 상태 관리 구현 [Feat] teamId 전역 상태로 구현 Nov 14, 2024
Copy link

🚀 Storybook 확인하기 🚀

Comment on lines 5 to 13
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 그냥 useClubInfoQuery로 불러오지말고 useQuery로 바로 사용해서 enablelocalstorage 값이 있는지 없는지의 여부를 적용해준다면, 로컬스토리지 값이 있을 때 불필요한 호출을 줄일 수도 있을 것 같아요 !

또한 네이밍은 왜 initialize 인가용 ?

Copy link
Member Author

@namdaeun namdaeun Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

새로운 값으로 초기화하는 역할을 한다고 생각해서 그렇게 명명했습니다. 다른 좋은 이름이 있을까요?

주용님 말씀처럼 enabled속성 써보는 방법이 불필요한 호출을 막을 수 있겠네요. 이 방법으로 함 수정해볼게요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 나쁘지 않은 것 같아요 !! 좋습니당

Copy link
Member Author

@namdaeun namdaeun Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 그냥 useClubInfoQuery로 불러오지말고 useQuery로 바로 사용해서 enablelocalstorage 값이 있는지 없는지의 여부를 적용해준다면, 로컬스토리지 값이 있을 때 불필요한 호출을 줄일 수도 있을 것 같아요 !

enabled 속성을 사용하려고 했으나
enabled: !localStorage.getItem('teamId')
와 같이 코드를 작성할 경우 localStorage에 teamId가 있을 때 쿼리를 실행하지 않게 되어 사이드 바에 저장된 워크스페이스를 불러오지 않게 되는 문제가 있었습니다. 그래서 해당 쿼리는 항상 실행되어야 하는 방향으로 다시 변경했습니다.

그리고 isSuccess값을 받아와서 데이터를 정상적으로 불러왔을 때 localStorage의 값을 다시 검사해서 teamId가 없는 경우 할당해주는 방식으로 수정했습니다.

Copy link
Contributor

@rtttr1 rtttr1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다!!

Comment on lines 5 to 13
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 나쁘지 않은 것 같아요 !! 좋습니당

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀


if (isSuccess && data?.data?.teams) {
// localStorage에 teamId가 없는 경우
const team = data.data.teams.find((team) => team);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요게 어떤 로직인가요 ? 그냥 데이터 값이 있는 첫 번째 요소를 찾는다라는 의미인가요 ?

data.teamslength가 0보다 크다면 첫 번째 요소를 가져온다는 로직으로 단순화해도 좋을 것 같아요 !

현재 teams 배열이 있는지 확인 -> 요소가 있는 지 확인, 두 과정을 거치고 있는데 그냥 배열의 값이 있는지만 확인하면 한 번에 충족되지 않을까요 ?

현재 로직 충분히 좋은 것 같고, 정상 동작하는지 로컬에서 제대로 다시 한번 확인해주시면 감사하겠슴다 !

Copy link
Member Author

@namdaeun namdaeun Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

데이터 값이 있는 첫 번째 요소를 찾는다라는 의미 맞습니다 ..ㅎㅎ

넵 로컬에서 확인했고 두 개의 조건문 대신 하나의 조건문으로 수정했습니다. 확인 부탁드려용 감사합니다 :)

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

Copy link

🚀 Storybook 확인하기 🚀

@namdaeun namdaeun merged commit 0285fb0 into develop Nov 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

teamId 관리 커스텀 훅 구현 & 아카이빙 페이지에 적용
4 participants