-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/#359: petProfile 관련 훅 파일 분리
- usePetProfileStep - usePetProfileValidation - usePetProfileAddition
- Loading branch information
1 parent
a051771
commit b12fe9c
Showing
12 changed files
with
140 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 4 additions & 132 deletions
136
frontend/src/hooks/petProfile.ts → ...hooks/petProfile/usePetProfileAddition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { PET_PROFILE_ADDITION_STEP, STEP_PATH } from '@/constants/petProfile'; | ||
import { usePetAdditionContext } from '@/context/petProfile/PetAdditionContext'; | ||
import { routerPath } from '@/router/routes'; | ||
|
||
import { usePetProfileValidation } from './usePetProfileValidation'; | ||
|
||
export const usePetProfileStep = () => { | ||
const navigate = useNavigate(); | ||
const { isMixedBreed } = usePetProfileValidation(); | ||
const { petProfile } = usePetAdditionContext(); | ||
|
||
const [step, setStep] = useState(0); | ||
const [isValidStep, setIsValidStep] = useState(false); | ||
|
||
const totalStep = Object.values(PET_PROFILE_ADDITION_STEP).length; | ||
const isLastStep = step === totalStep; | ||
|
||
const goBack = (): void => navigate(routerPath.back); | ||
const goNext = () => { | ||
if (step === PET_PROFILE_ADDITION_STEP.BREED && !isMixedBreed(petProfile.breed)) { | ||
navigate(STEP_PATH[step + 2]); | ||
return; | ||
} | ||
|
||
if (!isLastStep) navigate(STEP_PATH[step + 1]); | ||
}; | ||
|
||
const updateCurrentStep = (step: number) => setStep(step); | ||
const updateIsValidStep = (isValid: boolean) => setIsValidStep(isValid); | ||
|
||
return { | ||
step, | ||
totalStep, | ||
isValidStep, | ||
isLastStep, | ||
updateCurrentStep, | ||
updateIsValidStep, | ||
goBack, | ||
goNext, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ChangeEvent, useState } from 'react'; | ||
|
||
import { FEMALE, MALE, MIXED_BREED, PET_AGE_MAX, PET_AGE_MIN } from '@/constants/petProfile'; | ||
import { Gender } from '@/types/petProfile/client'; | ||
|
||
export const usePetProfileValidation = () => { | ||
const [isValidNameInput, setIsValidNameInput] = useState(true); | ||
const [isValidAgeSelect, setIsValidAgeSelect] = useState(true); | ||
const [isValidWeightInput, setIsValidWeightInput] = useState(true); | ||
|
||
const validateName = (e: ChangeEvent<HTMLInputElement>) => { | ||
const petName = e.target.value; | ||
|
||
if (isValidName(petName)) { | ||
setIsValidNameInput(true); | ||
return true; | ||
} | ||
|
||
setIsValidNameInput(false); | ||
return false; | ||
}; | ||
|
||
const validateAge = (e: ChangeEvent<HTMLSelectElement>) => { | ||
const petAge = Number(e.target.value); | ||
|
||
if (isValidAgeRange(petAge)) { | ||
setIsValidAgeSelect(true); | ||
setIsValidWeightInput(true); | ||
return true; | ||
} | ||
|
||
setIsValidAgeSelect(false); | ||
return false; | ||
}; | ||
|
||
const validateWeight = (e: ChangeEvent<HTMLInputElement>) => { | ||
const petWeight = e.target.value; | ||
|
||
if (isValidWeight(petWeight)) { | ||
setIsValidWeightInput(true); | ||
return true; | ||
} | ||
|
||
setIsValidWeightInput(false); | ||
return false; | ||
}; | ||
|
||
const isValidName = (name: string) => { | ||
const validNameCharacters = /^[a-zA-Z0-9가-힣ㄱ-ㅎㅏ-ㅣ]{1,10}$/; | ||
|
||
return validNameCharacters.test(name); | ||
}; | ||
|
||
const isValidAgeRange = (age: number) => | ||
typeof age === 'number' && age >= PET_AGE_MIN && age <= PET_AGE_MAX; | ||
|
||
const isValidGender = (gender: string): gender is Gender => gender === MALE || gender === FEMALE; | ||
|
||
const isValidWeight = (weight: string) => { | ||
const validWeightCharacters = /^(?:100(?:\.0)?|\d{1,2}(?:\.\d)?)$/; // 100.0 또는 1~2자리 숫자.소수 첫째짜리 숫자 | ||
|
||
if (Number(weight) <= 0) return false; | ||
|
||
return validWeightCharacters.test(weight); | ||
}; | ||
|
||
const isMixedBreed = (breed: string) => breed === MIXED_BREED; | ||
|
||
return { | ||
isValidNameInput, | ||
isValidAgeSelect, | ||
isValidWeightInput, | ||
|
||
validateName, | ||
validateAge, | ||
validateWeight, | ||
isValidName, | ||
isValidAgeRange, | ||
isValidGender, | ||
isValidWeight, | ||
isMixedBreed, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters