Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 프로필 편집 시 변경점이 없다면 버튼 비활성화 #459

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
53 changes: 45 additions & 8 deletions features/profile/components/organisms/profile-edit-form.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
'use client';

import { useRouter } from 'next/navigation';
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form';
import { useEffect } from 'react';
import {
FormProvider,
SubmitHandler,
useForm,
useWatch,
} from 'react-hook-form';

import { useImagePresignUrl } from '@/apis';
import { Button } from '@/components/atoms';
import { FormTextArea, FormTextField } from '@/components/molecules';
import { useCurrentMemberInfo, useToast } from '@/hooks';
import { css } from '@/styled-system/css';
import { flex } from '@/styled-system/patterns';
Expand All @@ -17,7 +24,6 @@ import {
} from '../../apis';
import { useProfileData, useProfileEditForm } from '../../hooks';
import { ProfileEditImageSection } from './profile-edit-image-section';
import { ProfileEditTextInfoSection } from './profile-edit-text-info-section';

interface ProfileEditFormProps {
nickname: string;
Expand Down Expand Up @@ -147,7 +153,32 @@ export function ProfileEditForm() {
}
};

const nickname = useWatch({ control: methods.control, name: 'nickname' });
const introduction = useWatch({
control: methods.control,
name: 'introduction',
});

useEffect(() => {
if (!profileData) return;

const { nickname: currentNickname, introduction: currentIntroduction } =
profileData;

if (currentNickname) methods.setValue('nickname', currentNickname);
if (currentIntroduction)
methods.setValue('introduction', currentIntroduction);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [profileData]);

if (!profileData) return null;

const isDirty = () => {
const { nickname: prevNickname, introduction: prevIntroduction } =
profileData;
return !(nickname === prevNickname && introduction === prevIntroduction);
};

return (
<FormProvider {...methods}>
<ProfileEditImageSection
Expand All @@ -161,12 +192,17 @@ export function ProfileEditForm() {
onSubmit={methods.handleSubmit(onSubmit)}
className={layoutStyles.form}
>
<ProfileEditTextInfoSection
nickNameLabel="닉네임"
nickNameSubText="14자까지 입력할 수 있어요"
introductionPlaceholder="한 줄 소개를 입력해주세요 (수린이 1년차 / 접영 드릴 연습중)"
currentNickname={profileData?.nickname.trim()}
currentIntroduction={profileData?.introduction?.trim()}
<FormTextField
{...methods.register('nickname')}
registerdFieldValue={nickname}
label="닉네임"
subText="14자까지 입력할 수 있어요"
maxLength={14}
wrapperClassName={css({ marginBottom: '24px' })}
/>
<FormTextArea
{...methods.register('introduction')}
placeholder="한 줄 소개를 입력해주세요 (수린이 1년차 / 접영 드릴 연습중)"
/>
<div className={buttonStyles.layout}>
<Button
Expand All @@ -176,6 +212,7 @@ export function ProfileEditForm() {
label="저장하기"
size="large"
className={buttonStyles.content}
disabled={isDirty() === false}
/>
</div>
</form>
Expand Down
Loading