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

[FE] 토큰이 없을 때, 관리페이지로 이동하면 오류가 발생하는 부분 수정 #338

Merged
merged 7 commits into from
Aug 14, 2024
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
5 changes: 2 additions & 3 deletions client/src/apis/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {useState} from 'react';
import {NavigateFunction, useNavigate} from 'react-router-dom';

import useEventId from '@hooks/useEventId';

import sendLogToSentry from '@utils/sendLogToSentry';
import getEventIdByUrl from '@utils/getEventIdByUrl';

import {UNKNOWN_ERROR} from '@constants/errorMessage';
import {ROUTER_URLS} from '@constants/routerUrls';
Expand All @@ -21,7 +20,7 @@ export const useFetch = () => {
const {setError, clearError} = useError();
const [loading, setLoading] = useState(false);
const navigate = useNavigate();
const {eventId} = useEventId();
const eventId = getEventIdByUrl();

const fetch = async <T>({queryFunction, onSuccess, onError}: FetchProps<T>): Promise<T> => {
setLoading(true);
Expand Down
1 change: 1 addition & 0 deletions client/src/constants/regExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const REGEXP = {
eventPassword: /^[0-9]*$/,
memberName: /^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z\s]*$/,
purchaseTitle: /^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9\s]*$/,
eventUrl: /\/event\/([a-zA-Z0-9-]+)\//,
};

export default REGEXP;
5 changes: 3 additions & 2 deletions client/src/hooks/useDeleteMemberAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import {useState} from 'react';
import {useToast} from '@components/Toast/ToastProvider';
import {requestDeleteMemberAction} from '@apis/request/member';

import useEventId from '@hooks/useEventId';
import {useStepList} from '@hooks/useStepList';

import {useFetch} from '@apis/useFetch';

import getEventIdByUrl from '@utils/getEventIdByUrl';

const useDeleteMemberAction = (
memberActionList: MemberAction[],
setIsBottomSheetOpened: React.Dispatch<React.SetStateAction<boolean>>,
) => {
const {stepList, refreshStepList} = useStepList();
const [aliveActionList, setAliveActionList] = useState<MemberAction[]>(memberActionList);
const {eventId} = useEventId();
const eventId = getEventIdByUrl();
const {showToast} = useToast();
const {fetch} = useFetch();

Expand Down
25 changes: 0 additions & 25 deletions client/src/hooks/useEventId.tsx

This file was deleted.

5 changes: 3 additions & 2 deletions client/src/hooks/usePutAndDeleteBillAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import {useState} from 'react';
import {ValidateResult} from '@utils/validate/type';
import {requestDeleteBillAction, requestPutBillAction} from '@apis/request/bill';

import useEventId from '@hooks/useEventId';
import {useStepList} from '@hooks/useStepList';
import {BillInputType, InputPair} from '@hooks/useDynamicBillActionInput';

import {useFetch} from '@apis/useFetch';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import ERROR_MESSAGE from '@constants/errorMessage';

const usePutAndDeleteBillAction = (
initialValue: InputPair,
validateFunc: (inputPair: Bill) => ValidateResult,
onClose: () => void,
) => {
const {eventId} = useEventId();
const eventId = getEventIdByUrl();
const {refreshStepList} = useStepList();
const {fetch} = useFetch();

Expand Down
8 changes: 3 additions & 5 deletions client/src/hooks/useSearchInMemberList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {requestGetCurrentInMemberList} from '@apis/request/member';

import {useFetch} from '@apis/useFetch';

import useEventId from './useEventId';
import getEventIdByUrl from '@utils/getEventIdByUrl';
Copy link
Contributor

Choose a reason for hiding this comment

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

어우 이 라인을 위한 수정이 많네요. 번거로우셨을텐데 고마워요


export type ReturnUseSearchInMemberList = {
currentInputIndex: number;
Expand All @@ -17,7 +17,7 @@ export type ReturnUseSearchInMemberList = {
const useSearchInMemberList = (
setInputValueTargetIndex: (index: number, value: string) => void,
): ReturnUseSearchInMemberList => {
const {eventId} = useEventId();
const eventId = getEventIdByUrl();

const {fetch} = useFetch();
const [currentInputIndex, setCurrentInputIndex] = useState(-1);
Expand All @@ -29,15 +29,13 @@ const useSearchInMemberList = (
const [filteredInMemberList, setFilteredInMemberList] = useState<Array<string>>([]);

useEffect(() => {
if (eventId === '') return;

const getCurrentInMembers = async () => {
const currentInMemberListFromServer = await fetch({queryFunction: () => requestGetCurrentInMemberList(eventId)});
setCurrentInMemberList(currentInMemberListFromServer.members);
};

getCurrentInMembers();
}, [eventId]);
}, []);

const filterMatchItems = (keyword: string) => {
if (keyword.trim() === '') return [];
Expand Down
9 changes: 4 additions & 5 deletions client/src/hooks/useSearchMemberReportList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ import {useEffect, useState} from 'react';

import {requestGetMemberReportList} from '@apis/request/report';

import useEventId from '@hooks/useEventId';

import {useFetch} from '@apis/useFetch';

import getEventIdByUrl from '@utils/getEventIdByUrl';

type UseSearchMemberReportListParams = {
name: string;
};

const useSearchMemberReportList = ({name}: UseSearchMemberReportListParams) => {
const [memberReportList, setMemberReportList] = useState<MemberReport[]>([]);
const [memberReportSearchList, setMemberReportSearchList] = useState<MemberReport[]>([]);
const {eventId} = useEventId();
const eventId = getEventIdByUrl();
const {fetch} = useFetch();

useEffect(() => {
const fetchMemberReportList = async () => {
// TODO: (@weadie) cors 고쳐지면 주석 풀게요.
// TODO: (@weadie) eventId에 의존하는 두 개의 훅에 대한 리펙토링 필요
if (eventId === '') return;

const memberReportListData = await fetch({queryFunction: () => requestGetMemberReportList({eventId})});
setMemberReportList(memberReportListData);
};

fetchMemberReportList();
}, [eventId]);
}, []);
Comment on lines 21 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

window.location.pathname는 값이 바로 들어온다니 토다리 덕분에 처음 알게되었습니다! 좋은 방향으로 리빽또링 된 것 같아 죠습니다 ㅎ.ㅎ


// TODO: (@weadie) 글자가 완성될 때마다 아래 로직이 실행되어야 합니다.
useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions client/src/hooks/useSetAllMemberList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {MemberChange, requestDeleteAllMemberList, requestPutAllMemberList} from
import {useFetch} from '@apis/useFetch';

import isArraysEqual from '@utils/isArraysEqual';
import getEventIdByUrl from '@utils/getEventIdByUrl';

import useEventId from './useEventId';
import {useStepList} from './useStepList';

interface UseSetAllMemberListProps {
Expand All @@ -29,7 +29,7 @@ const useSetAllMemberList = ({
const [deleteMemberList, setDeleteMemberList] = useState<string[]>([]);

const {refreshStepList} = useStepList();
const {eventId} = useEventId();
const eventId = getEventIdByUrl();
const {fetch} = useFetch();

useEffect(() => {
Expand Down
19 changes: 7 additions & 12 deletions client/src/hooks/useStepList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {requestPostBillList} from '@apis/request/bill';
import {requestGetAllMemberList, requestPostMemberList} from '@apis/request/member';
import {requestGetStepList} from '@apis/request/stepList';

import useEventId from '@hooks/useEventId';

import {useFetch} from '@apis/useFetch';

import getEventIdByUrl from '@utils/getEventIdByUrl';

interface StepListContextProps {
stepList: (BillStep | MemberStep)[];
allMemberList: string[];
Expand All @@ -25,16 +25,7 @@ const StepListProvider = ({children}: PropsWithChildren) => {
const {fetch} = useFetch();
const [stepList, setStepList] = useState<(BillStep | MemberStep)[]>([]);
const [allMemberList, setAllMemberList] = useState<string[]>([]);

const {eventId} = useEventId();

useEffect(() => {
if (eventId === '') return;

refreshStepList();
Copy link
Contributor

Choose a reason for hiding this comment

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

처음 랜더링됐을 때 리프레시 시키는 로직은 없어도 버그 없었나요? 그러면 빼는게 좋은 선택+

Copy link
Contributor

Choose a reason for hiding this comment

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

stepList를 사용하는 곳에서 refresh해도되긴 합니당! 근데 번거로울 것 같긴 해요. 그래서 쿠키가 말한 부분이 궁금하네요..!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

da3b874

이부분 로직이 없으면 제대로 작동하지 않네요.
의존성 배열에서 eventId를 뺀다는걸 useEffect 를 통으로 빼버린 것 같네요... 하하


// TODO: (@weadie) useEffect를 꼭 써야하는가?
}, [eventId]);
const eventId = getEventIdByUrl();

const refreshStepList = async () => {
const stepList = await fetch({queryFunction: () => requestGetStepList({eventId})});
Expand All @@ -43,6 +34,10 @@ const StepListProvider = ({children}: PropsWithChildren) => {
setStepList(stepList);
};

useEffect(() => {
refreshStepList();
}, []);

const updateMemberList = async ({type, memberNameList}: {type: MemberType; memberNameList: string[]}) => {
try {
await fetch({queryFunction: () => requestPostMemberList({eventId, type, memberNameList})});
Expand Down
17 changes: 3 additions & 14 deletions client/src/pages/EventPage/AdminPage/AdminPage.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import {useEffect, useState} from 'react';
import {Title, FixedButton, ListButton} from 'haengdong-design';
import {useNavigate, useOutletContext} from 'react-router-dom';
import {useOutletContext} from 'react-router-dom';

import StepList from '@components/StepList/StepList';
import {requestGetEventName} from '@apis/request/event';
import {ModalBasedOnMemberCount} from '@components/Modal/index';

import {useStepList} from '@hooks/useStepList';
import useEventId from '@hooks/useEventId';
import useAuth from '@hooks/useAuth';

import {ROUTER_URLS} from '@constants/routerUrls';

import {EventPageContextProps} from '../EventPageLayout';

import {receiptStyle, titleAndListButtonContainerStyle} from './AdminPage.style';
Expand All @@ -24,21 +20,14 @@ const AdminPage = () => {

const {getTotalPrice, allMemberList} = useStepList();
const {postAuthentication} = useAuth();
const navigate = useNavigate();

useEffect(() => {
if (eventId === '') return;

const postAuth = async () => {
try {
await postAuthentication({eventId: eventId});
} catch (error) {
navigate(`${ROUTER_URLS.event}/${eventId}/login`);
Copy link
Contributor

Choose a reason for hiding this comment

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

에러일 때 navigate시키는 기능은 없어진걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이거는 useFetch 내부에서 이미 처리해주고 있기 때문에, 여기 catch에서 잡히는게 별도로 없었어용 그래서 제외했습니다

}
await postAuthentication({eventId: eventId});
};

postAuth();
}, [eventId]);
}, []);

const handleOpenAllMemberListButton = () => {
setIsOpenFixedBottomBottomSheet(prev => !prev);
Expand Down
9 changes: 5 additions & 4 deletions client/src/pages/EventPage/AdminPage/EventLoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {useState} from 'react';
import {useLocation, useNavigate} from 'react-router-dom';
import {FixedButton, MainLayout, LabelInput, Title, TopNav, Back, Switch} from 'haengdong-design';
import {useNavigate} from 'react-router-dom';
import {FixedButton, MainLayout, LabelInput, Title, TopNav, Switch} from 'haengdong-design';

import validateEventPassword from '@utils/validate/validateEventPassword';

import useEventId from '@hooks/useEventId';
import useAuth from '@hooks/useAuth';
import useNavSwitch from '@hooks/useNavSwitch';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import RULE from '@constants/rule';
import {ROUTER_URLS} from '@constants/routerUrls';

Expand All @@ -17,7 +18,7 @@ const EventLoginPage = () => {
const [errorMessage, setErrorMessage] = useState('');
const [canSubmit, setCanSubmit] = useState(false);
const navigate = useNavigate();
const {eventId} = useEventId();
const eventId = getEventIdByUrl();
const {postLogin} = useAuth();

const submitPassword = async (event: React.FormEvent<HTMLFormElement>) => {
Expand Down
9 changes: 5 additions & 4 deletions client/src/pages/EventPage/EventPageLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {MainLayout, TopNav, Switch, Button, Flex} from 'haengdong-design';
import {Outlet, useLocation, useMatch, useNavigate} from 'react-router-dom';
import {Outlet, useMatch} from 'react-router-dom';
import {useEffect, useState} from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';

Expand All @@ -8,7 +8,8 @@ import {requestGetEventName} from '@apis/request/event';

import useNavSwitch from '@hooks/useNavSwitch';
import StepListProvider from '@hooks/useStepList';
import useEventId from '@hooks/useEventId';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import {ROUTER_URLS} from '@constants/routerUrls';

Expand All @@ -21,8 +22,8 @@ export type EventPageContextProps = {
const EventPageLayout = () => {
const {nav, paths, onChange} = useNavSwitch();

const {eventId} = useEventId();
const [eventName, setEventName] = useState('');
const eventId = getEventIdByUrl();

useEffect(() => {
const getEventName = async () => {
Expand All @@ -32,7 +33,7 @@ const EventPageLayout = () => {
};

getEventName();
}, [eventId]);
}, []);

const isAdmin = useMatch(ROUTER_URLS.eventManage) !== null;

Expand Down
13 changes: 13 additions & 0 deletions client/src/utils/getEventIdByUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import REGEXP from '@constants/regExp';

const extractEventIdFromUrl = (url: string) => {
const regex = REGEXP.eventUrl;
const match = url.match(regex);
return match ? match[1] : null;
};

const getEventIdByUrl = () => {
return extractEventIdFromUrl(window.location.pathname) || '';
};

export default getEventIdByUrl;
Loading