Skip to content

Commit

Permalink
Feat: 게시글 작성 로직 구현
Browse files Browse the repository at this point in the history
- 이제 유효성검사를 통한 alert를 Toast로 바꾸면 되는 상황
  • Loading branch information
soulchicken committed Oct 19, 2023
1 parent ad54a3f commit 6f1830e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/pages/community/[character_id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@ import { FormEvent, useState } from 'react';
import PostHeader from '@/components/community/PostHeader';
import DivideLine from '@/components/common/divideLine/DivideLine';
import Button from '@/components/common/button/Button';
// import { useRouter } from 'next/router';
import { createPost } from '@/utils/api/boards';
import { useRouter } from 'next/router';

const Post = () => {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
// const router = useRouter();
const router = useRouter();
const { character_id: characterId } = router.query;

const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (characterId && typeof characterId === 'string') {
if (title === '' || title.length > 30) {
alert('제목을 30자 이내로 작성해주세요! :)');
return;
}

// TODO: 게시글 유효성 검사를 해야함
if (content === '' || content.length > 3000) {
alert('내용은 3000자 이내로 작성해주세요! :)');
return;
}

alert(`${title}, ${content}`);
const result = await createPost(characterId, title, content);

// 게시글 작성 성공!
// router.push({
// pathname: '/community',
// });
if (result.status === 201) {
router.push({
pathname: `/community/${characterId}`,
});
return;
}
alert('게시글 작성에 실패했습니다 :(');
}
router.push({
pathname: '/community',
});
};

return (
Expand Down

0 comments on commit 6f1830e

Please sign in to comment.