|
1 | | -import React from 'react' |
| 1 | +import React, { useRef, useState } from 'react' |
| 2 | +import { ReactComponent as CloseIcon } from '@assets/icon_closed.svg' |
| 3 | +import { updateUserInfo } from '@apis/user' |
| 4 | +import useDebounce from '@hooks/useDebounce' |
| 5 | +import BackButton from '@components/BackButton' |
| 6 | +import { getIsValidateNickname } from '@utils/getIsValidateNickname' |
| 7 | +import { NICKNAME_MAX_LENGTH } from '@assets/constant/constant' |
| 8 | +import Button from '@components/Button' |
| 9 | +import { useLocation, useNavigate } from 'react-router-dom' |
2 | 10 |
|
3 | 11 | function ModifyInfo() { |
4 | | - return <div>λ΄μ 보μμ μΈλ°μ©</div> |
| 12 | + const navigate = useNavigate() |
| 13 | + const { state } = useLocation() |
| 14 | + const inputRef = useRef<HTMLInputElement>(null) |
| 15 | + const [isCheckedNickname, setIsCheckedNickname] = useState(false) |
| 16 | + const [nickname, setNickname] = useState(state.nickname || '') |
| 17 | + const [errorMessage, setErrorMessage] = useState('') |
| 18 | + const [inputBorderStyle, setInputBorderStyle] = useState('') |
| 19 | + |
| 20 | + useDebounce( |
| 21 | + async () => { |
| 22 | + setErrorMessage('') |
| 23 | + setInputBorderStyle('') |
| 24 | + setIsCheckedNickname(false) |
| 25 | + if (getIsValidateNickname(nickname, setErrorMessage)) { |
| 26 | + if (nickname.length > 0) { |
| 27 | + try { |
| 28 | + setIsCheckedNickname(true) |
| 29 | + setInputBorderStyle('border border-solid border-primary-2') |
| 30 | + } catch (e) { |
| 31 | + setErrorMessage('μ΄λ―Έ μ¬μ©μ€μΈ λλ€μμ΄μμ.') |
| 32 | + setIsCheckedNickname(false) |
| 33 | + setInputBorderStyle('border border-solid border-sub-1') |
| 34 | + } |
| 35 | + } |
| 36 | + } else { |
| 37 | + setInputBorderStyle('border border-solid border-sub-1') |
| 38 | + } |
| 39 | + }, |
| 40 | + 300, |
| 41 | + [nickname] |
| 42 | + ) |
| 43 | + |
| 44 | + const handleClickUpdateButton = async () => { |
| 45 | + try { |
| 46 | + await updateUserInfo(nickname) |
| 47 | + navigate('/setting') |
| 48 | + } catch (e) { |
| 49 | + setIsCheckedNickname(false) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="relative h-screen w-full"> |
| 55 | + <section id="route-backIcon-button" className="ml-[18px] mt-4"> |
| 56 | + <BackButton /> |
| 57 | + </section> |
| 58 | + <section id="modify-info-nickname" className="relative mt-11 px-6"> |
| 59 | + <p className="font-medium">λλ€μ</p> |
| 60 | + <div className="relative mt-[9px] flex w-full items-center"> |
| 61 | + <input |
| 62 | + ref={inputRef} |
| 63 | + id="modify-nickname-input" |
| 64 | + value={nickname} |
| 65 | + className={`w-full rounded-[8px] bg-grey-2 py-[17px] pl-[12px] text-[14px] font-medium placeholder:text-grey-5 focus:outline-none |
| 66 | + ${inputBorderStyle}`} |
| 67 | + autoComplete="off" |
| 68 | + onChange={(e) => setNickname(e.target.value)} |
| 69 | + maxLength={NICKNAME_MAX_LENGTH} |
| 70 | + autoFocus |
| 71 | + /> |
| 72 | + <CloseIcon |
| 73 | + className="absolute right-[10px] cursor-pointer" |
| 74 | + onClick={() => { |
| 75 | + setNickname('') |
| 76 | + inputRef.current?.focus() |
| 77 | + }} |
| 78 | + /> |
| 79 | + </div> |
| 80 | + <p |
| 81 | + className={`pt-3 text-sm ${ |
| 82 | + isCheckedNickname ? 'text-primary-2' : 'text-sub-1' |
| 83 | + }`} |
| 84 | + > |
| 85 | + {isCheckedNickname ? 'μ¬μ© κ°λ₯ν λλ€μμ΄μμ.' : errorMessage} |
| 86 | + </p> |
| 87 | + </section> |
| 88 | + <div className="relative mt-[104px] px-6"> |
| 89 | + <Button |
| 90 | + type="submit" |
| 91 | + property="solid" |
| 92 | + active={isCheckedNickname} |
| 93 | + onClick={handleClickUpdateButton} |
| 94 | + disabled={!isCheckedNickname} |
| 95 | + > |
| 96 | + μμ μλ£ |
| 97 | + </Button> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ) |
5 | 101 | } |
6 | 102 |
|
7 | 103 | export default ModifyInfo |
0 commit comments