Skip to content
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
83 changes: 49 additions & 34 deletions components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useValidation } from 'hooks/useValidation';
import Image from 'next/image';
import React, { useState } from 'react';
import { format } from 'date-fns';

Expand All @@ -15,6 +16,7 @@ interface InputFieldProps {
width?: string;
onValidation?: (isValid: boolean) => void;
ref?: React.Ref<HTMLInputElement>;
disabled?: boolean;
}

function InputField({
Expand All @@ -28,6 +30,7 @@ function InputField({
width,
onValidation,
ref,
disabled,
...props
}: InputFieldProps) {
const { errorMessage, validate } = useValidation({
Expand All @@ -36,6 +39,7 @@ function InputField({
});

const [showDayPicker, setShowDayPicker] = useState(false);
const [showPassword, setShowPassword] = useState(false);

const handleFocus = () => {
if (
Expand All @@ -53,39 +57,34 @@ function InputField({
};

const handleBlur = () => {
if (typeof layout === 'string' && layout === 'vertical') {
if (value) {
const error = validate(value);
const isValid = !error && typeof value === 'string' && value.length > 0;
const isValid = !error && value.length > (type === 'name' ? 1 : 0);
onValidation?.(isValid);
}
};

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value.trim();
onChange(e);
if (
typeof layout === 'string' &&
layout === 'vertical' &&
typeof errorMessage === 'string' &&
errorMessage.length > 0
) {
const error = validate(e.target.value);
const isValid =
!error &&
typeof e.target.value === 'string' &&
e.target.value.length > 0;
onValidation?.(isValid);
}
const error = validate(newValue);
const isValid = !error && newValue.length > (type === 'name' ? 1 : 0);
onValidation?.(isValid);
};

const getInputType = () => {
if (type === 'name') {
return 'text';
} else if (type === 'passwordConfirm') {
return 'password';
} else if (type === 'passwordConfirm' || type === 'password') {
return showPassword ? 'text' : 'password';
}
return type;
};

const togglePasswordVisibility = () => {
setShowPassword(!showPassword);
};

//μŠ€νƒ€μΌμ— λ”°λ₯Έ 클래슀
const variantClass = {
containerVertical: 'flex flex-col gap-[10px]',
Expand All @@ -111,7 +110,7 @@ function InputField({
errorMessage.length > 0
? variantClass.error
: variantClass.normal
}`;
} ${disabled ? 'opacity-50 pointer-events-none' : ''}`;

return (
<div
Expand All @@ -127,23 +126,39 @@ function InputField({
{typeof label === 'string' && label.length > 0 && (
<label className={labelClass}>{label}</label>
)}
<input
type={getInputType()}
value={value}
onChange={handleChange}
placeholder={placeholder}
onBlur={handleBlur}
onFocus={handleFocus}
className={inputClass}
ref={ref}
{...props}
/>
{typeof layout === 'string' &&
layout === 'vertical' &&
typeof errorMessage === 'string' &&
errorMessage.length > 0 && (
<span className={variantClass.errorText}>{errorMessage}</span>
<div className="relative">
<input
type={getInputType()}
value={value}
onChange={handleChange}
placeholder={placeholder}
onBlur={handleBlur}
onFocus={handleFocus}
className={inputClass}
ref={ref}
disabled={disabled}
{...props}
/>
{(type === 'password' || type === 'passwordConfirm') && (
<button
type="button"
onClick={togglePasswordVisibility}
className="absolute right-3 top-1/2 -translate-y-1/2 cursor-pointer"
>
<Image
src={
showPassword ? '/icon/icon-eye.svg' : '/icon/icon-eye-off.svg'
}
alt={showPassword ? 'λΉ„λ°€λ²ˆν˜Έ 숨기기' : 'λΉ„λ°€λ²ˆν˜Έ 보기'}
width={20}
height={20}
/>
</button>
)}
</div>
{errorMessage && (
<span className={variantClass.errorText}>{errorMessage}</span>
)}
{typeof showDayPicker === 'boolean' && showDayPicker && (
<div className="absolute left-0 top-full z-50 mt-2 rounded bg-white p-4 shadow-md">
<div>
Expand Down
2 changes: 1 addition & 1 deletion hooks/useValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function useValidation({ type, compareValue }: UseValidationProps) {
}
break;
case 'name':
if (value.length < 1 || value.length > 10) {
if (value.length <= 1 || value.length > 10) {
return '1자 이상 10자 μ΄ν•˜λ‘œ μž‘μ„±ν•΄μ£Όμ„Έμš”.';
}
break;
Expand Down
18 changes: 12 additions & 6 deletions pages/signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ function SignUp() {
const [password, setPassword] = useState('');
const [passwordConfirm, setPasswordConfirm] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [isCompleted, setIsCompleted] = useState(false);
const [name, setName] = useState('');
const [snackbarOpen, setSnackbarOpen] = useState(false);
const [snackbarMessage, setSnackbarMessage] = useState('');
const [snackbarSeverity, setSnackbarSeverity] = useState<'success' | 'fail'>(
'success'
);
const [validFields, setValidFields] = useState({
name: '',
email: '',
password: '',
passwordConfirm: '',
name: false,
email: false,
password: false,
passwordConfirm: false,
});
const router = useRouter();

Expand Down Expand Up @@ -53,7 +54,7 @@ function SignUp() {

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (isSubmitting || !isFormValid) return;
if (isSubmitting || !isFormValid || isCompleted) return;

setIsSubmitting(true);

Expand All @@ -65,6 +66,7 @@ function SignUp() {
name,
});

setIsCompleted(true);
// νšŒμ›κ°€μž… 성곡 μ‹œ μŠ€λ‚΅λ°” ν‘œμ‹œ
setSnackbarMessage('νšŒμ›κ°€μž…μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€');
setSnackbarSeverity('success');
Expand Down Expand Up @@ -106,6 +108,7 @@ function SignUp() {
onChange={handleNameChange}
placeholder="이름을 μž…λ ₯ν•΄ μ£Όμ„Έμš”"
onValidation={(isValid) => handleValidation('name', isValid)}
disabled={isCompleted}
/>
<InputField
label="이메일"
Expand All @@ -115,6 +118,7 @@ function SignUp() {
onChange={handleEmailChange}
placeholder="이메일을 μž…λ ₯ν•΄ μ£Όμ„Έμš”"
onValidation={(isValid) => handleValidation('email', isValid)}
disabled={isCompleted}
/>
<InputField
label="λΉ„λ°€λ²ˆν˜Έ"
Expand All @@ -124,6 +128,7 @@ function SignUp() {
onChange={handlePasswordChange}
placeholder="λΉ„λ°€λ²ˆν˜Έλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”"
onValidation={(isValid) => handleValidation('password', isValid)}
disabled={isCompleted}
/>
<InputField
label="λΉ„λ°€λ²ˆν˜Έ 확인"
Expand All @@ -136,9 +141,10 @@ function SignUp() {
onValidation={(isValid) =>
handleValidation('passwordConfirm', isValid)
}
disabled={isCompleted}
/>
<Button
disabled={!isFormValid}
disabled={!isFormValid || isSubmitting || isCompleted}
isLoading={isSubmitting}
variant="primary"
className="mt-[6px] h-[45px] w-full"
Expand Down
4 changes: 4 additions & 0 deletions public/icon/icon-eye-off.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/icon/icon-eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions services/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ export const AuthAPI = {
if (error instanceof AxiosError) {
// 400 μ—λŸ¬ (이미 μ‘΄μž¬ν•˜λŠ” 이메일)
if (error.response?.status === 400) {
throw new Error('이미 μ‘΄μž¬ν•˜λŠ” μ΄λ©”μΌμž…λ‹ˆλ‹€.');
throw new Error('이미 κ°€μž…λœ 이메일 μ£Όμ†Œμž…λ‹ˆλ‹€.');
}
// 500 μ—λŸ¬ (μ„œλ²„ λ‚΄λΆ€ 였λ₯˜)
if (error.response?.status === 500) {
throw new Error(
'μ„œλ²„ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.'
);
throw new Error('μ„œλ²„ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
}
}
// 기타 μ˜ˆμƒμΉ˜ λͺ»ν•œ μ—λŸ¬
Expand Down
Loading