Skip to content

Commit

Permalink
FE-73๐Ÿ› ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ–ˆ๋‹ค๊ฐ€ ์ง€์› ์„๋•Œ ๋ฒ„ํŠผ ํ™œ์„ฑํ™”๋˜์žˆ๋Š” ๋ฒ„๊ทธ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
jisurk committed Jul 24, 2024
1 parent e81bee6 commit 872d63f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
37 changes: 22 additions & 15 deletions src/pageLayout/Epigram/AddEpigram.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { KeyboardEvent, useState } from 'react';
import React, { KeyboardEvent, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import Header from '@/components/Header/Header';
Expand All @@ -15,11 +15,12 @@ import useTagManagement from '@/hooks/useTagManagementHook';
import { useMeQuery } from '@/hooks/userQueryHooks';

function AddEpigram() {
const [isAlertOpen, setIsAlertOpen] = useState(false);
const [alertContent, setAlertContent] = useState({ title: '', description: '' });
const router = useRouter();
const { data: userData, isPending, isError } = useMeQuery();
const [isAlertOpen, setIsAlertOpen] = useState(false);
const [alertContent, setAlertContent] = useState({ title: '', description: '' });
const [selectedAuthorOption, setSelectedAuthorOption] = useState('directly'); // ๊ธฐ๋ณธ๊ฐ’์„ 'directly'๋กœ ์„ค์ •
const [isFormValid, setIsFormValid] = useState(false);

const form = useForm<AddEpigramFormType>({
resolver: zodResolver(AddEpigramFormSchema),
Expand All @@ -32,6 +33,20 @@ function AddEpigram() {
},
});

// NOTE: ํ•„์ˆ˜ํ•ญ๋ชฉ๋“ค์— ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธ
const checkFormEmpty = () => {
const { content, author, tags } = form.getValues();
return content.trim() !== '' && author.trim() !== '' && tags.length > 0;
};

// NOTE: form์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€
useEffect(() => {
const subscription = form.watch(() => {
setIsFormValid(checkFormEmpty());
});
return () => subscription.unsubscribe();
}, [form]);

const { currentTag, setCurrentTag, handleAddTag, handleRemoveTag } = useTagManagement(form.setValue, form.getValues, form.setError);

const addEpigramMutation = useAddEpigram({
Expand Down Expand Up @@ -92,14 +107,15 @@ function AddEpigram() {
return <div>์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ•ด์ฃผ์„ธ์š”.</div>;
}

// NOTE: ํƒœ๊ทธ๋ฅผ ์ €์žฅํ• ๋ ค๊ณ ํ• ๋•Œ enterํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ํผ์ œ์ถœ์ด ๋˜๋Š”๊ฑธ ๋ฐฉ์ง€
// NOTE: ํƒœ๊ทธ๋ฅผ ์ €์žฅํ•˜๋ ค๊ณ  ํ• ๋•Œ enterํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ํผ์ œ์ถœ์ด ๋˜๋Š”๊ฑธ ๋ฐฉ์ง€
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault();
handleAddTag();
}
};

// NOTE: url์™€title์€ ํ•„์ˆ˜ ํ•ญ๋ชฉ์ด ์•„๋‹ˆ๋ผ์„œ ๋นˆ์นธ์œผ๋กœ ์ œ์ถœํ•  ๋•Œ ํ•ญ๋ชฉ์—์„œ ์ œ์™ธ
const handleSubmit = (data: AddEpigramFormType) => {
const submitData = { ...data };

Expand All @@ -114,11 +130,6 @@ function AddEpigram() {
addEpigramMutation.mutate(submitData);
};

const isRequiredFieldsValid = () => {
const { content, author, tags } = form.getValues();
return content.trim() !== '' && author.trim() !== '' && tags.length > 0;
};

return (
<>
<Header icon='search' routerPage='/search' isLogo insteadOfLogo='' isProfileIcon isShareIcon={false} isButton={false} textInButton='' disabled={false} onClick={() => {}} />
Expand Down Expand Up @@ -268,12 +279,8 @@ function AddEpigram() {
</FormItem>
)}
/>
{/* NOTE: disabled ์ƒํƒœ์ผ ๋•Œ์˜ ์Šคํƒ€์ผ */}
<Button
className='h-11 lg:h-16 rounded-xl text-semibold lg:text-2xl text-white bg-black-500 disabled:bg-blue-400 '
type='submit'
disabled={addEpigramMutation.isPending || !isRequiredFieldsValid()}
>
{/* NOTE: ํ•„์ˆ˜ํ•ญ๋ชฉ๋“ค์— ๊ฐ’์ด ์ฑ„์›Œ์ ธ์žˆ์œผ๋ฉด ํผ์ œ์ถœ ๋ฒ„ํŠผ ํ™œ์„ฑํ™” */}
<Button className='h-11 lg:h-16 rounded-xl text-semibold lg:text-2xl text-white bg-black-500 disabled:bg-blue-400 ' type='submit' disabled={addEpigramMutation.isPending || !isFormValid}>
{addEpigramMutation.isPending ? '์ œ์ถœ ์ค‘...' : '์ž‘์„ฑ ์™„๋ฃŒ'}
</Button>
</form>
Expand Down
3 changes: 1 addition & 2 deletions src/schema/addEpigram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const AddEpigramResponseSchema = z.object({
id: z.number().int().positive(),
});

// ํผ ์ž…๋ ฅ๊ฐ’์„ ์œ„ํ•œ ์Šคํ‚ค๋งˆ
export const AddEpigramFormSchema = z
.object({
tags: z.array(z.string().min(1).max(10)).min(1, { message: '์ตœ์†Œ 1๊ฐœ์˜ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.' }).max(3),
Expand All @@ -36,7 +35,7 @@ export const AddEpigramFormSchema = z
referenceTitle: z.union([z.string().max(100, { message: '100์ž ์ด๋‚ด๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.' }), z.literal('')]).optional(),
})
.refine((data) => (data.referenceUrl === '' && data.referenceTitle === '') || (data.referenceUrl !== '' && data.referenceTitle !== ''), {
message: 'URL๊ณผ ์ œ๋ชฉ์„ ๋ชจ๋‘ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ๋ชจ๋‘ ๋น„์›Œ๋‘์„ธ์š”.',
message: 'URL๊ณผ ์ œ๋ชฉ์„ ๋ชจ๋‘ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ๋ชจ๋‘ ๋น„์›Œ์ฃผ์„ธ์š”.',
path: ['referenceUrl', 'referenceTitle'],
});

Expand Down

0 comments on commit 872d63f

Please sign in to comment.