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] EventLoader하위로 EditAccountPage를 옮겨 데이터를 유지하기 위한 로직을 제거 #877

Merged
merged 11 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
6 changes: 3 additions & 3 deletions client/src/apis/request/event.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {CreatedEvents, Event, EventCreationData, EventId, EventName, User} from 'types/serviceType';
import {CreatedEvents, Event, EventCreationData, EventName, User} from 'types/serviceType';
import {WithErrorHandlingStrategy} from '@errors/RequestGetError';

import {ADMIN_API_PREFIX, MEMBER_API_PREFIX, USER_API_PREFIX} from '@apis/endpointPrefix';
import {requestGet, requestPatch, requestPostWithResponse} from '@apis/fetcher';
import {WithEventId} from '@apis/withId.type';

export const requestPostGuestEvent = async (postEventArgs: EventCreationData) => {
return await requestPostWithResponse<EventId>({
return await requestPostWithResponse<WithEventId>({
endpoint: `${USER_API_PREFIX}/guest`,
body: {
...postEventArgs,
Expand All @@ -15,7 +15,7 @@ export const requestPostGuestEvent = async (postEventArgs: EventCreationData) =>
};

export const requestPostUserEvent = async (eventName: EventName) => {
return await requestPostWithResponse<EventId>({
return await requestPostWithResponse<WithEventId>({
endpoint: USER_API_PREFIX,
body: {
eventName,
Expand Down
22 changes: 22 additions & 0 deletions client/src/components/Loader/EventDataProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {createContext, PropsWithChildren} from 'react';

import {Event, EventId} from 'types/serviceType';

import {useAuthStore} from '@store/authStore';

type EventDataContextType = Event & {
eventToken: EventId;
isAdmin: boolean;
};

type EventDataProviderProps = PropsWithChildren<Omit<EventDataContextType, 'isAdmin'>>;

export const EventDataContext = createContext<EventDataContextType | null>(null);

const EventDataProvider = ({children, ...props}: EventDataProviderProps) => {
const {isAdmin} = useAuthStore();

return <EventDataContext.Provider value={{isAdmin, ...props}}>{children}</EventDataContext.Provider>;
};

export default EventDataProvider;
53 changes: 10 additions & 43 deletions client/src/components/Loader/EventLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,17 @@
import {useSuspenseQueries} from '@tanstack/react-query';
import {useEffect} from 'react';
import {Outlet} from 'react-router-dom';

import {requestGetEvent} from '@apis/request/event';
import {requestGetReports} from '@apis/request/report';
import {requestGetSteps} from '@apis/request/step';
import {WithErrorHandlingStrategy} from '@errors/RequestGetError';
import {requestGetAllMembers} from '@apis/request/member';
import useEventLoader from '@hooks/useEventLoader';

import {useTotalExpenseAmountStore} from '@store/totalExpenseAmountStore';
import EventDataProvider from './EventDataProvider';

import getEventIdByUrl from '@utils/getEventIdByUrl';
const EventLoader = () => {
const eventData = useEventLoader();

import QUERY_KEYS from '@constants/queryKeys';

const EventLoader = ({children, ...props}: React.PropsWithChildren<WithErrorHandlingStrategy | null> = {}) => {
const eventId = getEventIdByUrl();

const queries = useSuspenseQueries({
queries: [
{queryKey: [QUERY_KEYS.event], queryFn: () => requestGetEvent({eventId, ...props})},
{
queryKey: [QUERY_KEYS.reports],
queryFn: () => requestGetReports({eventId, ...props}),
},
{
queryKey: [QUERY_KEYS.steps],
queryFn: () => requestGetSteps({eventId, ...props}),
},
{
queryKey: [QUERY_KEYS.allMembers],
queryFn: () => requestGetAllMembers({eventId, ...props}),
},
],
});

const {updateTotalExpenseAmount} = useTotalExpenseAmountStore();

const stepsData = queries[2];

useEffect(() => {
if (stepsData.isSuccess && stepsData.data) {
updateTotalExpenseAmount(stepsData.data);
}
}, [stepsData.data, stepsData.isSuccess, updateTotalExpenseAmount]);

return children;
return (
<EventDataProvider {...eventData}>
<Outlet />
</EventDataProvider>
);
};

export default EventLoader;
26 changes: 6 additions & 20 deletions client/src/hooks/useAccount.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
import {useEffect, useState} from 'react';
import {useLocation, useNavigate} from 'react-router-dom';

import validateAccountNumber from '@utils/validate/validateAccountNumber';
import {BankAccount} from 'types/serviceType';

import getEventBaseUrl from '@utils/getEventBaseUrl';

import RULE from '@constants/rule';

import useRequestPatchUser from './queries/event/useRequestPatchUser';
import useEventDataContext from './useEventDataContext';

const useAccount = () => {
const location = useLocation();
const locationState = location.state as BankAccount | null;
Comment on lines -14 to -15
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 Author

Choose a reason for hiding this comment

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

버그를 해결했다는게 더 유용한거죠!!!! 저는 그냥 숟가락만 얹었을 뿐,,

const {bankName, accountNumber} = useEventDataContext();

const navigate = useNavigate();
const [bankNameState, setBankName] = useState<string>();
const [accountNumberState, setAccountNumber] = useState<string>();
const [bankNameState, setBankName] = useState<string>(bankName);
const [accountNumberState, setAccountNumber] = useState<string>(accountNumber);
const [accountNumberErrorMessage, setAccountNumberErrorMessage] = useState<string | null>(null);
const [canSubmit, setCanSubmit] = useState(false);
const [isPasting, setIsPasting] = useState(false);

useEffect(() => {
if (locationState === null) {
navigate(`${getEventBaseUrl(location.pathname)}/admin`);
} else {
setBankName(locationState.bankName);
setAccountNumber(locationState.accountNumber);
}
}, [locationState]);

const {patchUser} = useRequestPatchUser();

const selectBank = (name: string) => {
Expand Down Expand Up @@ -71,10 +57,10 @@ const useAccount = () => {

useEffect(() => {
const existEmptyField = bankNameState?.trim() === '' || accountNumberState?.trim() === '';
const isChanged = locationState?.bankName !== bankNameState || locationState?.accountNumber !== accountNumberState;
const isChanged = bankName !== bankNameState || accountNumber !== accountNumberState;

setCanSubmit(!existEmptyField && isChanged && accountNumberErrorMessage === null);
}, [locationState, bankNameState, accountNumberState, accountNumberErrorMessage]);
}, [bankNameState, accountNumberState, accountNumberErrorMessage]);

return {
bankName: bankNameState,
Expand Down
7 changes: 2 additions & 5 deletions client/src/hooks/useAdminPage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {useOutletContext} from 'react-router-dom';

import {EventPageContextProps} from '@pages/event/[eventId]/EventPageLayout';

import {useTotalExpenseAmountStore} from '@store/totalExpenseAmountStore';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import useRequestGetSteps from './queries/step/useRequestGetSteps';
import useBanner from './useBanner';
import useEventDataContext from './useEventDataContext';

const useAdminPage = () => {
const eventId = getEventIdByUrl();
const {isAdmin, eventName, bankName, accountNumber} = useOutletContext<EventPageContextProps>();
const {isAdmin, bankName, accountNumber, eventName} = useEventDataContext();

const {totalExpenseAmount} = useTotalExpenseAmountStore();

Expand Down
15 changes: 15 additions & 0 deletions client/src/hooks/useEventDataContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useContext} from 'react';

import {EventDataContext} from '@components/Loader/EventDataProvider';

const useEventDataContext = () => {
const value = useContext(EventDataContext);

if (!value) {
throw new Error('EventDataProvider와 함께 사용해주세요.');
}
Comment on lines +8 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

에러메세지까지 친절하게 띄워주기..👍🏻


return value;
};

export default useEventDataContext;
62 changes: 62 additions & 0 deletions client/src/hooks/useEventLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {useSuspenseQueries} from '@tanstack/react-query';
import {useEffect} from 'react';

import {requestGetEvent} from '@apis/request/event';
import {requestGetAllMembers} from '@apis/request/member';
import {requestGetReports} from '@apis/request/report';
import {requestGetSteps} from '@apis/request/step';

import {useTotalExpenseAmountStore} from '@store/totalExpenseAmountStore';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import QUERY_KEYS from '@constants/queryKeys';

/**
* 이 훅이 하는 일:
* 이벤트 정보 불러오기 + 총액 상태 계산하기
*/
Comment on lines +15 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

제가 작성했던 방식은 여기서 데이터를 fetch 해두고 하위 컴포넌트에서 캐시된 데이터를 불러와서 사용하는 방식이었어요. 이렇게 바꾸게 되면 여기서 데이터를 fetch 하고 하위 컴포넌트에서 context를 사용해서 값을 불러올 수 있게 될 것 같아요! 하지만 하위에서는 context와 의존성이 생긴다는 제약은 있지만..;;

const useEventLoader = () => {
const eventId = getEventIdByUrl();

const {updateTotalExpenseAmount} = useTotalExpenseAmountStore();

const queries = useSuspenseQueries({
queries: [
{queryKey: [QUERY_KEYS.event], queryFn: () => requestGetEvent({eventId})},
{
queryKey: [QUERY_KEYS.reports],
queryFn: () => requestGetReports({eventId}),
},
{
queryKey: [QUERY_KEYS.steps],
queryFn: () => requestGetSteps({eventId}),
},
{
queryKey: [QUERY_KEYS.allMembers],
queryFn: () => requestGetAllMembers({eventId}),
},
Comment on lines +35 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 이 부분도 논의해보고 싶어요. 이 데이터를 여기서 fetch하는 이유는 순전히 amplitude에 데이터를 보내기 위해서 였어요. 행사 당 평균 인원 수를 확인해보고 싶어서 호출하는 것인데 지금 amplitude에서 이를 확인하고 있지 않아서 빠져도 되지 않을까 하는 생각도 있습니다. 이 내용은 회의 때 이야기해봐도 좋을 것 같아요

],
});

const [eventData, reportsData, stepsData, membersData] = queries;
const {data, isSuccess} = stepsData;

useEffect(() => {
if (isSuccess && data) {
updateTotalExpenseAmount(data);
}
}, [data, isSuccess]);

const {eventName, bankName, accountNumber, createdByGuest} = eventData.data;

return {
eventName,
bankName,
accountNumber,
createdByGuest,
eventToken: eventId,
};
};

export default useEventLoader;
6 changes: 2 additions & 4 deletions client/src/hooks/useEventPageLayout.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {useAuthStore} from '@store/authStore';
import {useTotalExpenseAmountStore} from '@store/totalExpenseAmountStore';

import getEventIdByUrl from '@utils/getEventIdByUrl';

import useRequestGetEvent from './queries/event/useRequestGetEvent';
import useRequestGetAllMembers from './queries/member/useRequestGetAllMembers';
import useRequestGetSteps from './queries/step/useRequestGetSteps';
import useRequestGetUserInfo from './queries/auth/useRequestGetUserInfo';
import useEventDataContext from './useEventDataContext';

const useEventPageLayout = () => {
const eventId = getEventIdByUrl();
const {eventName, bankName, accountNumber, createdByGuest} = useRequestGetEvent();
const {isAdmin} = useAuthStore();
const {isAdmin, eventName, bankName, accountNumber, createdByGuest} = useEventDataContext();
const {totalExpenseAmount} = useTotalExpenseAmountStore();
const {members} = useRequestGetAllMembers();
const {steps} = useRequestGetSteps();
Expand Down
7 changes: 3 additions & 4 deletions client/src/hooks/useReportsPage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {useState} from 'react';
import {useLocation, useNavigate, useOutletContext} from 'react-router-dom';

import {EventPageContextProps} from '@pages/event/[eventId]/EventPageLayout';
import {useLocation, useNavigate} from 'react-router-dom';

import getEventBaseUrl from '@utils/getEventBaseUrl';

import {useSearchReports} from './useSearchReports';
import toast from './useToast/toast';
import useEventDataContext from './useEventDataContext';

export type SendInfo = {
bankName: string;
Expand All @@ -18,7 +17,7 @@ export type SendInfo = {

const useReportsPage = () => {
const [memberName, setMemberName] = useState('');
const {eventName, eventToken, bankName, accountNumber} = useOutletContext<EventPageContextProps>();
const {eventName, eventToken, bankName, accountNumber} = useEventDataContext();
const {matchedReports, reports} = useSearchReports({memberName});

const location = useLocation();
Expand Down
2 changes: 1 addition & 1 deletion client/src/mocks/handlers/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const eventHandler = [
);
}

const eventId: EventId = {eventId: 'mock-event-id'};
const eventId: {eventId: EventId} = {eventId: 'mock-event-id'};
return HttpResponse.json(eventId, {
status: 201,
headers: {
Expand Down
10 changes: 3 additions & 7 deletions client/src/pages/event/[eventId]/EventPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ export type EventPageContextProps = Event & {
};

const EventPageLayout = () => {
const {isAdmin, event, eventId, eventSummary} = useEventPageLayout();
const outletContext: EventPageContextProps = {
isAdmin,
eventToken: eventId,
...event,
};
const {event, eventSummary} = useEventPageLayout();

const {trackShareEvent} = useAmplitude();

const isMobile = isMobileDevice();
Expand Down Expand Up @@ -82,7 +78,7 @@ const EventPageLayout = () => {
)}
</Flex>
</Flex>
<Outlet context={outletContext} />
<Outlet />
<Footer />
</MainLayout>
);
Expand Down
8 changes: 4 additions & 4 deletions client/src/pages/event/[eventId]/home/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {EventPageContextProps} from '@pages/event/[eventId]/EventPageLayout';

import {useMatch, useNavigate, useOutletContext} from 'react-router-dom';
import {useMatch, useNavigate} from 'react-router-dom';

import StepList from '@components/StepList/Steps';
import useRequestGetSteps from '@hooks/queries/step/useRequestGetSteps';
import Reports from '@components/Reports/Reports';
import useRequestGetImages from '@hooks/queries/images/useRequestGetImages';

import useEventDataContext from '@hooks/useEventDataContext';

import {useTotalExpenseAmountStore} from '@store/totalExpenseAmountStore';

import {Icon, Tab, Tabs, Title} from '@HDesign/index';
Expand All @@ -18,7 +18,7 @@ import {ROUTER_URLS} from '@constants/routerUrls';
import {receiptStyle} from './HomePage.style';

const HomePage = () => {
const {isAdmin, eventName} = useOutletContext<EventPageContextProps>();
const {isAdmin, eventName} = useEventDataContext();
const isInHomePage = useMatch(ROUTER_URLS.home) !== null;

const {steps} = useRequestGetSteps();
Expand Down
Loading
Loading