feat: useNoticeWrite 훅 추가 및 post 적용

This commit is contained in:
정기영 2024-07-26 15:20:04 +09:00
parent 74512baab4
commit c8416d3f80
3 changed files with 36 additions and 13 deletions

View File

@ -1,22 +1,13 @@
import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { Link } from 'react-router-dom';
import styles from './CreateArticle.module.css';
import EditIcon from '/src/assets/icons/edit.svg?react';
import BackIcon from '/src/assets/icons/back.svg?react';
export default function CreateArticle({ topic, title }) {
const navigate = useNavigate();
export default function CreateArticle({ topic, title, onSubmit }) {
const [articleTitle, setArticleTitle] = useState('');
const [articleContent, setArticleContent] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
// TODO:
if (articleTitle && articleContent) {
navigate('..');
}
};
const handleInput = (e) => {
setArticleContent(e.target.value);
e.target.style.height = 'auto';
@ -37,7 +28,7 @@ export default function CreateArticle({ topic, title }) {
</header>
<form
className={styles.formWrapper}
onSubmit={handleSubmit}
onSubmit={(e) => onSubmit(e, articleTitle, articleContent)}
>
<div className={styles.fieldWrapper}>
<label className={styles.label}>제목</label>
@ -61,7 +52,7 @@ export default function CreateArticle({ topic, title }) {
<button
type="button"
className={styles.button}
onClick={handleSubmit}
onClick={(e) => onSubmit(e, articleTitle, articleContent)}
disabled={!articleTitle || !articleContent}
>
<EditIcon />

View File

@ -0,0 +1,10 @@
import instance from '../../utils/axios/instance';
import { API_URL } from '../../constants';
export function useNoticeWrite() {
const noticeWrite = (newNotice) => {
return instance.post(`${API_URL}/board`, newNotice);
};
return { noticeWrite };
}

View File

@ -1,10 +1,32 @@
import { CreateArticle } from '../../components/Article';
import { useNavigate } from 'react-router-dom';
import { useNoticeWrite } from '../../hooks/api/useNoticeWrite';
import { useParams } from 'react-router-dom';
export default function NoticeWritePage() {
const navigate = useNavigate();
const { lectureId } = useParams();
const { noticeWrite } = useNoticeWrite();
const handleSubmit = async (e, title, content) => {
e.preventDefault();
console.log('handleSubmit');
console.log(title, content);
const response = await noticeWrite({
lectureId: Number(lectureId),
title,
category: 'announcement',
content,
});
console.log('response : ', response);
navigate('..');
};
return (
<CreateArticle
topic="글 쓰기"
title="공지사항"
onSubmit={handleSubmit}
/>
);
}