Skip to content

Commit

Permalink
๐Ÿ”จ Refactoring: Password-update ํŽ˜์ด์ง€ ๋ฆฌํŒฉํ† ๋ง
Browse files Browse the repository at this point in the history
๐Ÿ”จ Refactoring: Password-update ํŽ˜์ด์ง€ ๋ฆฌํŒฉํ† ๋ง
  • Loading branch information
sscoderati authored Oct 9, 2023
2 parents ab327e1 + a8e589c commit a76b6d4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 100 deletions.
85 changes: 2 additions & 83 deletions src/pages/password-update/PasswordUpdate.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,9 @@
import { useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import putUpdatePassword from '@apis/password';
import { Alert } from '@components/Alert';
import { Button } from '@components/Button';
import { FormInput } from '@components/FormInput';
import useSessionStorage from '@hooks/useSessionStorage';
import { User } from '@/types';
import { LABEL, PASSWORD_HINT, USER_INPUT } from './constants';
import { PasswordHint } from '@pages/password-update/components';
import { LandingMain } from '@pages/landing/Landing.style';
import { PasswordUpdateForm, ButtonContainer } from './PasswordUpdate.style';
import isPasswordOk from './utils/isPasswordOk';

interface PasswordUpdateFormData {
password: string;
passwordConfirm: string;
}

import PasswordUpdateForm from './components/PasswordUpdateForm';
const PasswordUpdate = () => {
const [passwordChanged, setPasswordChanged] = useState<boolean>(false);
const [userSessionData] = useSessionStorage<Pick<User, '_id' | 'token'>>(
'userData',
{
_id: '',
token: ''
}
);
const methods = useForm<PasswordUpdateFormData>();
const { watch } = methods;
const [password, passwordConfirm] = watch(['password', 'passwordConfirm']);

const onSubmit = () => {
if (isPasswordOk(password) && password === passwordConfirm) {
putUpdatePassword({ password, token: `Bearer ${userSessionData.token}` })
.then(() => setPasswordChanged(true))
.catch((error) => console.log(error));
}
};

return (
<LandingMain>
{passwordChanged && (
<Alert
emoji={'โœ…'}
content={'๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.'}
buttonLabel={'ํ™•์ธ'}
nextPageLink={
userSessionData.token ? `/profile/${userSessionData._id}` : '/login'
}
/>
)}
<FormProvider {...methods}>
<PasswordUpdateForm onSubmit={methods.handleSubmit(onSubmit)}>
<PasswordHint text={PASSWORD_HINT} />
<FormInput
name={USER_INPUT.NEW_PASSWORD.NAME}
placeholder={USER_INPUT.NEW_PASSWORD.PLACE_HOLDER}
title={USER_INPUT.NEW_PASSWORD.TITLE}
show={password && password.length > 0}
success={password && isPasswordOk(password)}
type={USER_INPUT.NEW_PASSWORD.TYPE}
errorMessage={USER_INPUT.NEW_PASSWORD.ERROR_MESSAGE}
successMessage={USER_INPUT.NEW_PASSWORD.SUCCESS_MESSAGE}
/>
<FormInput
name={USER_INPUT.NEW_PASSWORD_CONFIRM.NAME}
placeholder={USER_INPUT.NEW_PASSWORD_CONFIRM.PLACE_HOLDER}
title={USER_INPUT.NEW_PASSWORD_CONFIRM.TITLE}
show={password && passwordConfirm.length > 0}
success={passwordConfirm && password === passwordConfirm}
type={USER_INPUT.NEW_PASSWORD_CONFIRM.TYPE}
errorMessage={USER_INPUT.NEW_PASSWORD_CONFIRM.ERROR_MESSAGE}
successMessage={USER_INPUT.NEW_PASSWORD_CONFIRM.SUCCESS_MESSAGE}
/>
<ButtonContainer>
<Button
label={LABEL.CHANGE_PASSWORD}
width={300}
height={45}
bold={false}
dark={true}
handleClick={() => methods.handleSubmit(onSubmit)}
/>
</ButtonContainer>
</PasswordUpdateForm>
</FormProvider>
<PasswordUpdateForm />
</LandingMain>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from '@emotion/styled';

const PasswordUpdateForm = styled.form`
const PasswordUpdateFormContainer = styled.form`
padding: 10px 0px;
background-color: ${({ theme }) => theme.color.white};
${({ theme }) => theme.style.flexCenter};
Expand All @@ -25,4 +25,4 @@ const ButtonContainer = styled.div`
margin-top: 20px;
`;

export { PasswordUpdateForm, ButtonContainer };
export { PasswordUpdateFormContainer, ButtonContainer };
94 changes: 94 additions & 0 deletions src/pages/password-update/components/PasswordUpdateForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import putUpdatePassword from '@apis/password';
import { Alert } from '@components/Alert';
import { Button } from '@components/Button';
import { FormInput } from '@components/FormInput';
import useSessionStorage from '@hooks/useSessionStorage';
import { User } from '@/types';
import { LABEL, PASSWORD_HINT, USER_INPUT, ALERT } from '../constants';
import { PasswordHint } from '@pages/password-update/components';
import {
PasswordUpdateFormContainer,
ButtonContainer
} from './PasswordUpdateForm.style';
import isPasswordOk from '../validations';

interface PasswordUpdateFormData {
password: string;
passwordConfirm: string;
}

const PasswordUpdateForm = () => {
const [passwordChanged, setPasswordChanged] = useState<boolean>(false);
const [userSessionData] = useSessionStorage<Pick<User, '_id' | 'token'>>(
'userData',
{
_id: '',
token: ''
}
);
const methods = useForm<PasswordUpdateFormData>();
const { watch } = methods;
const [password, passwordConfirm] = watch(['password', 'passwordConfirm']);

const onSubmit = () => {
if (isPasswordOk(password) && password === passwordConfirm) {
putUpdatePassword({ password, token: `Bearer ${userSessionData.token}` })
.then(() => setPasswordChanged(true))
.catch((error) => console.log(error));
}
};

return (
<>
{passwordChanged && (
<Alert
emoji={ALERT.EMOJI}
content={ALERT.CONTENT}
buttonLabel={ALERT.BUTTON_LABEL}
nextPageLink={
userSessionData.token ? `/profile/${userSessionData._id}` : '/login'
}
/>
)}
<FormProvider {...methods}>
<PasswordUpdateFormContainer onSubmit={methods.handleSubmit(onSubmit)}>
<PasswordHint text={PASSWORD_HINT} />
<FormInput
name={USER_INPUT.NEW_PASSWORD.NAME}
placeholder={USER_INPUT.NEW_PASSWORD.PLACE_HOLDER}
title={USER_INPUT.NEW_PASSWORD.TITLE}
show={password && password.length > 0}
success={password && isPasswordOk(password)}
type={USER_INPUT.NEW_PASSWORD.TYPE}
errorMessage={USER_INPUT.NEW_PASSWORD.ERROR_MESSAGE}
successMessage={USER_INPUT.NEW_PASSWORD.SUCCESS_MESSAGE}
/>
<FormInput
name={USER_INPUT.NEW_PASSWORD_CONFIRM.NAME}
placeholder={USER_INPUT.NEW_PASSWORD_CONFIRM.PLACE_HOLDER}
title={USER_INPUT.NEW_PASSWORD_CONFIRM.TITLE}
show={password && passwordConfirm.length > 0}
success={passwordConfirm && password === passwordConfirm}
type={USER_INPUT.NEW_PASSWORD_CONFIRM.TYPE}
errorMessage={USER_INPUT.NEW_PASSWORD_CONFIRM.ERROR_MESSAGE}
successMessage={USER_INPUT.NEW_PASSWORD_CONFIRM.SUCCESS_MESSAGE}
/>
<ButtonContainer>
<Button
label={LABEL.CHANGE_PASSWORD}
width={300}
height={45}
bold={false}
dark={true}
handleClick={() => methods.handleSubmit(onSubmit)}
/>
</ButtonContainer>
</PasswordUpdateFormContainer>
</FormProvider>
</>
);
};

export default PasswordUpdateForm;
8 changes: 7 additions & 1 deletion src/pages/password-update/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ const USER_INPUT = {

const PASSWORD_HINT = `๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์˜๋ฌธ์ž, ํŠน์ˆ˜๋ฌธ์ž, ์ˆซ์ž ์ค‘ 2๊ฐ€์ง€ ์ด์ƒ์„ ํฌํ•จํ•œ 8~32๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ค„์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.`;

const ALERT = {
EMOJI: '๐Ÿ˜€',
CONTENT: '๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.',
BUTTON_LABEL: 'ํ™•์ธ'
};

const LABEL = {
CHANGE_PASSWORD: '๋ณ€๊ฒฝํ•˜๊ธฐ'
};

export { USER_INPUT, PASSWORD_HINT, LABEL };
export { USER_INPUT, PASSWORD_HINT, LABEL, ALERT };
14 changes: 0 additions & 14 deletions src/pages/password-update/utils/isPasswordOk.ts

This file was deleted.

0 comments on commit a76b6d4

Please sign in to comment.