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] 관리자 로그인 페이지에서 스위치가 관리로 활성화되지 않는 버그 #469

Merged
merged 2 commits into from
Aug 21, 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
42 changes: 42 additions & 0 deletions client/src/hooks/useEventLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {useState} from 'react';

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

import RULE from '@constants/rule';

import useRequestPostLogin from './queries/useRequestPostLogin';

const useEventLogin = () => {
const [password, setPassword] = useState('');
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [canSubmit, setCanSubmit] = useState(false);
const {mutate: postLogin} = useRequestPostLogin();

const submitPassword = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

postLogin({password}, {onError: () => setErrorMessage('비밀번호가 틀렸어요')});
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.value;
const validation = validateEventPassword(newValue);
setErrorMessage(validation.errorMessage);

if (validation.isValid) {
setPassword(newValue);
}

setCanSubmit(newValue.length === RULE.maxEventPasswordLength);
};

return {
password,
errorMessage,
handleChange,
canSubmit,
submitPassword,
};
};

export default useEventLogin;
Comment on lines +1 to +42
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.

ㅋㅋㅋ 복붙이 끝이긴 하지만ㅋㅋ

7 changes: 6 additions & 1 deletion client/src/hooks/useNavSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useState} from 'react';
import {useEffect, useState} from 'react';
import {useLocation, useNavigate} from 'react-router-dom';

const PATH_TABLE: Record<string, string> = {
Expand All @@ -22,6 +22,11 @@ const useNavSwitch = () => {

const [nav, setNav] = useState(PATH_DISPLAY_TABLE[lastPath]);

useEffect(() => {
const isLogin = lastPath === 'login';
setNav(isLogin ? '관리' : PATH_DISPLAY_TABLE[lastPath]);
}, [location]);

const onChange = (displayName: string) => {
setNav(displayName);
navigate(`${basePath}/${PATH_TABLE[displayName]}`);
Expand Down
43 changes: 5 additions & 38 deletions client/src/pages/EventPage/AdminPage/EventLoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,15 @@
import {useState} from 'react';
import {FixedButton, MainLayout, LabelInput, Title, TopNav, Switch} from 'haengdong-design';
import {FixedButton, LabelInput, Title} from 'haengdong-design';

import validateEventPassword from '@utils/validate/validateEventPassword';
import useRequestPostLogin from '@hooks/queries/useRequestPostLogin';

import useNavSwitch from '@hooks/useNavSwitch';
import useEventLogin from '@hooks/useEventLogin';

import RULE from '@constants/rule';
import {PASSWORD_LENGTH} from '@constants/password';

const EventLoginPage = () => {
const [password, setPassword] = useState('');
const {nav, paths, onChange} = useNavSwitch();
const [errorMessage, setErrorMessage] = useState('');
const [canSubmit, setCanSubmit] = useState(false);
const {mutate: postLogin} = useRequestPostLogin();

const submitPassword = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

postLogin({password}, {onError: () => setErrorMessage('비밀번호가 틀렸어요')});
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.value;
const validation = validateEventPassword(newValue);

setCanSubmit(newValue.length === RULE.maxEventPasswordLength);

if (validation.isValid) {
setPassword(newValue);
setErrorMessage('');
} else {
event.target.value = password;
setErrorMessage(validation.errorMessage ?? '');
}
};
const {password, errorMessage, handleChange, canSubmit, submitPassword} = useEventLogin();

return (
<MainLayout>
<TopNav>
<Switch value={nav} values={paths} onChange={onChange} />
{/* <Back /> */}
</TopNav>
<>
<Title
title="행사 비밀번호 입력"
description={`관리를 위해선 비밀번호가 필요해요. 행사 생성 시 설정한 ${PASSWORD_LENGTH} 자리의 숫자 비밀번호를 입력해 주세요.`}
Expand All @@ -61,7 +28,7 @@ const EventLoginPage = () => {
></LabelInput>
<FixedButton disabled={!canSubmit}>관리 페이지로</FixedButton>
</form>
</MainLayout>
</>
);
};

Expand Down
35 changes: 19 additions & 16 deletions client/src/pages/EventPage/EventPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const EventPageLayout = () => {
const eventId = getEventIdByUrl();

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

const outletContext: EventPageContextProps = {
isAdmin,
Expand All @@ -37,22 +38,24 @@ const EventPageLayout = () => {
<MainLayout backgroundColor="gray">
<TopNav>
<Switch value={nav} values={paths} onChange={onChange} />
<CopyToClipboard
text={`[행동대장]\n"${eventName}"에 대한 정산을 시작할게요:)\n아래 링크에 접속해서 정산 내역을 확인해 주세요!\n${url}`}
onCopy={() =>
showToast({
showingTime: 3000,
message: '링크가 복사되었어요 :) \n참여자들에게 링크를 공유해 주세요!',
type: 'confirm',
position: 'bottom',
bottom: '8rem',
})
}
>
<Button size="small" variants="secondary">
정산 초대하기
</Button>
</CopyToClipboard>
{!isLoginPage && (
<CopyToClipboard
text={`[행동대장]\n"${eventName}"에 대한 정산을 시작할게요:)\n아래 링크에 접속해서 정산 내역을 확인해 주세요!\n${url}`}
onCopy={() =>
showToast({
showingTime: 3000,
message: '링크가 복사되었어요 :) \n참여자들에게 링크를 공유해 주세요!',
type: 'confirm',
position: 'bottom',
bottom: '8rem',
})
}
>
<Button size="small" variants="secondary">
정산 초대하기
</Button>
</CopyToClipboard>
)}
</TopNav>
<Outlet context={outletContext} />
</MainLayout>
Expand Down
8 changes: 4 additions & 4 deletions client/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ const router = createBrowserRouter([
path: ROUTER_URLS.eventCreateComplete,
element: <CompleteCreateEventPage />,
},
{
path: ROUTER_URLS.eventLogin,
element: <EventLoginPage />,
},
{
path: ROUTER_URLS.event,
element: <EventPage />,
children: [
{path: ROUTER_URLS.eventManage, element: <AdminPage />},
{path: ROUTER_URLS.home, element: <HomePage />},
{
path: ROUTER_URLS.eventLogin,
element: <EventLoginPage />,
},
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

별도 페이지가 아니라, event 내부로 넣어버렸군뇨 좋와요~!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

같은 형식의 url을 가지고 있어서 안으로 옮기고 중복되는 MainLayout TopNav를 지워봤어요!

],
},
{
Expand Down
Loading