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

fix(login): 로그인 안하면 모달 안뜨는 버그 수정 #103

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/common/Aside/InfomationBox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from "styled-components";
import { color, font } from "@/styles";
import { USER } from "@/constants";
import { IUser } from "@/interfaces";
import { Row, Column } from "@/components/Flex";
import { getUserRole } from "@/helpers";
Expand Down
23 changes: 16 additions & 7 deletions src/components/common/Aside/JoinCheckBox.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import styled from "styled-components";
import { color, font } from "@/styles";
import useAside from "@/hooks/useAside";

const JoinCheckBox = () => {
const { asideInfo } = useAside();
return (
<Container>
<HGroup>
<Date>7월 1주차</Date>
<RoomNumber>334</RoomNumber>
</HGroup>
<CheckButton />
{asideInfo.room && (
<HGroup>
<Date>
{`${asideInfo.room.yearSemester.year}`.substring(2, 4)}년{" "}
{asideInfo.room.yearSemester.semester}학기
</Date>
<RoomNumber>
{asideInfo.room.dormitoryType}동 {asideInfo.room.roomNumber}
</RoomNumber>
</HGroup>
)}
<CheckButton disabled={asideInfo.isCheckIn} />
</Container>
);
};
Expand Down Expand Up @@ -46,7 +55,7 @@ const Date = styled.span`
`;

const RoomNumber = styled.span`
${font.H4};
${font.H5};

&:after {
content: "호";
Expand All @@ -69,7 +78,7 @@ const CheckButton = styled.button`
background-color: ${color.content};

&:after {
content: "미완성 기능";
content: "입사 완료";
}
}
`;
Expand Down
9 changes: 5 additions & 4 deletions src/components/common/Aside/MeisterBox.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import styled from "styled-components";
import { color, font } from "@/styles";
import { Row } from "@/components/Flex";
import useMeister from "@/hooks/useMeister";
import useAside from "@/hooks/useAside";

const MeisterBox = () => {
const { meister } = useMeister();
const { asideInfo } = useAside();
return (
<Container>
<ScoreHGroup>
<ScoreName>인증제 점수</ScoreName>
<Row gap="4px">
<Score>{meister.score}</Score>
<Score>{asideInfo.score}</Score>
<Rank>{asideInfo.ranking}</Rank>
</Row>
</ScoreHGroup>
<ScoreHGroup>
<ScoreName>상벌점</ScoreName>
<Row gap="4px">
<Score>{meister.positivePoint - meister.negativePoint}</Score>
<Score>{asideInfo.positivePoint - asideInfo.negativePoint}</Score>
</Row>
</ScoreHGroup>
</Container>
Expand Down
1 change: 1 addition & 0 deletions src/constants/key.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const KEY = {
MEISTER_ME: "useMeisterMe",
RANKING: "useRanking",
MAIN: "useMain",
ASIDE: "useAside",
} as const;

export default KEY;
43 changes: 43 additions & 0 deletions src/hooks/useAside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import httpClient from "@/apis/httpClient";
import Storage from "@/apis/storage";
import { authorization } from "@/apis/token";
import { KEY, TOKEN } from "@/constants";
Copy link
Contributor

Choose a reason for hiding this comment

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

authorization을 빼서 사용할 수 있었군요! 하나 배워갑니다. 수정하시느라 수고하셨습니다

import { asideStore } from "@/store/aside.store";
import { useQuery } from "@tanstack/react-query";
import React from "react";
import { useRecoilState } from "recoil";

const useAside = () => {
const [asideInfo, setAsideInfo] = useRecoilState(asideStore);

const { data: asideRes, isSuccess } = useQuery(
[KEY.ASIDE],
async () => {
const { data } = await httpClient.main.getByTitle({
params: { title: "aside" },
});
return data;
},
{ enabled: !!Storage.getItem(TOKEN.ACCESS) },
);

React.useEffect(() => {
if (isSuccess) {
const { ranking, room, meisterResAndAvgAndMax1, isCheckIn } = asideRes;
const { score, positivePoint, negativePoint } =
meisterResAndAvgAndMax1.meister;
setAsideInfo({
score,
positivePoint,
negativePoint,
ranking,
room,
isCheckIn,
});
}
}, [setAsideInfo, asideRes]);

return { asideInfo, setAsideInfo };
};

export default useAside;
18 changes: 12 additions & 6 deletions src/hooks/useUser.ts → src/hooks/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Storage from "@/apis/storage";
import { ERROR, TOKEN } from "@/constants";
import { authorization, refresh } from "@/apis/token";
import { isAxiosError } from "axios";
import LoginModal from "@/components/common/Modal/LoginModal";

interface UseUserOptions {
authorizedPage?: boolean;
Expand All @@ -21,7 +22,6 @@ const useUser = (options?: UseUserOptions) => {
const [user, setUser] = useRecoilState(userStore);
const router = useRouter();
const { openModal, visible } = useModal();
const { isWindow } = useWindow();

const {
data: userInfo,
Expand All @@ -44,6 +44,12 @@ const useUser = (options?: UseUserOptions) => {
remove();
};

React.useEffect(() => {
if (!Storage.getItem(TOKEN.ACCESS)) {
openModal({ component: <LoginModal /> });
}
}, [Storage]);

React.useEffect(() => {
if (isAxiosError(error) && error.response) {
const { code } = error.response.data;
Expand All @@ -57,12 +63,12 @@ const useUser = (options?: UseUserOptions) => {
}, [setUser, userInfo]);

React.useEffect(() => {
if (options?.authorizedPage && !isLoading && !userInfo && !visible) {
// openModal({
// component: <LoginModal/>,
// });
if (!isLoading && !userInfo && !visible) {
openModal({
component: <LoginModal />,
});
}
}, [options, userInfo, isLoading, router, visible, openModal, isWindow]);
}, [options, userInfo, isLoading, router, visible, openModal]);

return { user, isLogined: !!userInfo, logout };
};
Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/aside.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default interface IAside {
score: number;
positivePoint: number;
negativePoint: number;
ranking: number;
room: {
roomNumber: number;
yearSemester: {
year: number;
semester: number;
};
dormitoryType: string;
};
isCheckIn: false;
}
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export type { default as IReserve } from "./reserve.interface";
export type { default as IReserveList } from "./reserveList.interface";
export type { default as ICreateReserve } from "./createReserve.interface";
export type { default as IMeister } from "./meister.interface";
export type { default as IAside } from "./aside.interface";
21 changes: 21 additions & 0 deletions src/store/aside.store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IAside } from "@/interfaces";
import { atom } from "recoil";

export const asideStore = atom<IAside>({
key: "asideStore",
default: {
score: 0,
positivePoint: 0,
negativePoint: 0,
ranking: 0,
room: {
roomNumber: 0,
yearSemester: {
year: 0,
semester: 0,
},
dormitoryType: "",
},
isCheckIn: false,
},
});
12 changes: 0 additions & 12 deletions src/templates/home/layouts/BasicMode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { Column, Row } from "@/components/Flex";
import { Aside } from "@/components/common";
import useMeal from "@/hooks/useMeal";
import useUser from "@/hooks/useUser";
import useModal from "@/hooks/useModal";
import LoginModal from "@/components/common/Modal/LoginModal";
import { useDidMountEffect } from "@/hooks/useDidMountEffect";
import { useMainQuery } from "../../services/query.service";
import HomeMeal from "./HomeMeal";
import HomeReserve from "./HomeReserve";
Expand All @@ -17,19 +14,10 @@ import HomeMiniBanner from "./HomeMiniBanner";
import HomeBamboo from "./HomeBamboo";

const BasicMode = () => {
const { openModal, closeModal } = useModal();
const { isSuccess, data } = useMainQuery();
const { isLogined } = useUser();
const { getMealTime } = useMeal();

useDidMountEffect(() => {
if (!isLogined)
return openModal({
component: <LoginModal />,
});
return closeModal();
}, [isLogined]);

return isSuccess && isLogined ? (
<>
<Row gap="8px" width="100%">
Expand Down