|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useForm, Controller } from 'react-hook-form'; |
| 4 | +import { Select } from '@/shared/components/input/Select'; |
| 5 | +import { InputField } from '@/shared/components/input/InputField'; |
| 6 | +import Checkbox from '@/shared/components/input/Checkbox'; |
| 7 | +import Badge from '@/shared/components/display/Badge'; |
| 8 | +import { SelectJobResponsive } from '@/feature/auth/selectJobResponsive'; |
| 9 | +import { useFetchAppSocialSignup } from './hook'; |
| 10 | +import { KakaoSignupFormData, SocialLoginType } from './type'; |
| 11 | +import { CAREER_YEAR_OPTIONS, CAREER_YEAR_VALUES } from './const'; |
| 12 | + |
| 13 | +interface AppSocialSignupFormProps { |
| 14 | + registrationToken: string; |
| 15 | + socialType: SocialLoginType; |
| 16 | +} |
| 17 | + |
| 18 | +export const AppSocialSignupForm = ({ registrationToken, socialType }: AppSocialSignupFormProps) => { |
| 19 | + const { isSubmitting, fetchAppSocialSignup } = useFetchAppSocialSignup({ |
| 20 | + registrationToken, |
| 21 | + socialType, |
| 22 | + }); |
| 23 | + |
| 24 | + const { |
| 25 | + watch, |
| 26 | + setValue, |
| 27 | + register, |
| 28 | + control, |
| 29 | + handleSubmit, |
| 30 | + formState: { errors, isValid }, |
| 31 | + } = useForm<KakaoSignupFormData>({ |
| 32 | + mode: 'onChange', |
| 33 | + defaultValues: { |
| 34 | + name: '', |
| 35 | + jobRoleId: '', |
| 36 | + careerYear: '', |
| 37 | + privacyPolicy: false, |
| 38 | + termsOfService: false, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const jobRoleId = watch('jobRoleId'); |
| 43 | + |
| 44 | + return ( |
| 45 | + <form className="space-y-6 w-full"> |
| 46 | + {/* 이름 */} |
| 47 | + <InputField |
| 48 | + label="이름" |
| 49 | + type="text" |
| 50 | + placeholder="성함을 입력해주세요." |
| 51 | + {...register('name', { |
| 52 | + required: '이름을 입력해주세요.', |
| 53 | + maxLength: { |
| 54 | + value: 6, |
| 55 | + message: '이름은 6자 이하이어야 합니다.', |
| 56 | + }, |
| 57 | + })} |
| 58 | + isError={!!errors.name} |
| 59 | + errorMessage={errors.name?.message as string} |
| 60 | + /> |
| 61 | + |
| 62 | + {/* 직무 */} |
| 63 | + <div className="space-y-2"> |
| 64 | + <label className="block text-sm font-medium text-gray-300">직무</label> |
| 65 | + <SelectJobResponsive |
| 66 | + selectedJobId={jobRoleId} |
| 67 | + onJobSelect={jobId => setValue('jobRoleId', jobId, { shouldValidate: true })} |
| 68 | + /> |
| 69 | + {errors.jobRoleId && <p className="text-xs text-red-500">{errors.jobRoleId.message as string}</p>} |
| 70 | + </div> |
| 71 | + |
| 72 | + {/* 연차 */} |
| 73 | + <div className="space-y-2"> |
| 74 | + <label className="block text-sm font-medium text-gray-300">연차</label> |
| 75 | + <Select |
| 76 | + options={CAREER_YEAR_OPTIONS} |
| 77 | + selected={(() => { |
| 78 | + const value = watch('careerYear'); |
| 79 | + const label = Object.entries(CAREER_YEAR_VALUES).find(([, val]) => val === value)?.[0]; |
| 80 | + return label || '선택'; |
| 81 | + })()} |
| 82 | + onChange={value => |
| 83 | + setValue('careerYear', value === '선택' ? '' : CAREER_YEAR_VALUES[value], { shouldValidate: true }) |
| 84 | + } |
| 85 | + placeholder="연차를 선택해주세요" |
| 86 | + isError={!!errors.careerYear} |
| 87 | + {...(() => { |
| 88 | + const { onChange, ...rest } = register('careerYear', { required: '연차를 선택해주세요.' }); |
| 89 | + return rest; |
| 90 | + })()} |
| 91 | + /> |
| 92 | + {errors.careerYear && <p className="text-xs text-red-500">{errors.careerYear.message as string}</p>} |
| 93 | + </div> |
| 94 | + |
| 95 | + {/* 약관 동의 */} |
| 96 | + <div className="space-y-2"> |
| 97 | + <label className="flex items-center space-x-2"> |
| 98 | + <Controller |
| 99 | + name="privacyPolicy" |
| 100 | + control={control} |
| 101 | + rules={{ required: '개인정보 수집에 동의해주세요.' }} |
| 102 | + render={({ field }) => <Checkbox checked={field.value} onChange={field.onChange} />} |
| 103 | + /> |
| 104 | + <Badge |
| 105 | + type="default" |
| 106 | + size="sm" |
| 107 | + label="필수" |
| 108 | + color="bg-[rgba(255,99,99,0.16)]" |
| 109 | + textColor="text-status-negative" |
| 110 | + /> |
| 111 | + <span className="text-gray-400 text-sm">개인정보 수집 동의</span> |
| 112 | + </label> |
| 113 | + {errors.privacyPolicy && <p className="text-xs text-red-500">{errors.privacyPolicy.message as string}</p>} |
| 114 | + |
| 115 | + <label className="flex items-center space-x-2"> |
| 116 | + <Controller |
| 117 | + name="termsOfService" |
| 118 | + control={control} |
| 119 | + rules={{ required: '이용 약관에 동의해주세요.' }} |
| 120 | + render={({ field }) => <Checkbox checked={field.value} onChange={field.onChange} />} |
| 121 | + /> |
| 122 | + <Badge |
| 123 | + type="default" |
| 124 | + size="sm" |
| 125 | + label="필수" |
| 126 | + color="bg-[rgba(255,99,99,0.16)]" |
| 127 | + textColor="text-status-negative" |
| 128 | + /> |
| 129 | + <span className="text-gray-400 text-sm">이용 약관 동의</span> |
| 130 | + </label> |
| 131 | + {errors.termsOfService && <p className="text-xs text-red-500">{errors.termsOfService.message as string}</p>} |
| 132 | + </div> |
| 133 | + |
| 134 | + {/* 제출 버튼 */} |
| 135 | + <button |
| 136 | + type="button" |
| 137 | + disabled={!isValid || isSubmitting} |
| 138 | + onClick={handleSubmit(fetchAppSocialSignup)} |
| 139 | + className={`w-full py-3 rounded-lg font-medium ${ |
| 140 | + isValid && !isSubmitting ? 'bg-primary text-white' : 'bg-gray-600 text-gray-400 cursor-not-allowed' |
| 141 | + }`} |
| 142 | + > |
| 143 | + {isSubmitting ? '가입 중...' : '가입하기'} |
| 144 | + </button> |
| 145 | + </form> |
| 146 | + ); |
| 147 | +}; |
0 commit comments