Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 36 additions & 4 deletions src/pages/RegisterForm.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import CommonForm from "/src/components/CommonForm";
import "./RegisterForm.css";

import usePostStore from "../stores/PostStore";
import { useNavigate } from "react-router-dom";
import back from "/src/assets/back.svg";
import { getCookie } from "../utils/cookie";

const Register = () => {
const { loading, createPost } = usePostStore();
const navigate = useNavigate();
const [formData, setFormData] = useState({
name: "",
studentNumber: "25",
Expand All @@ -14,6 +18,12 @@ const Register = () => {
author: "",
});

useEffect(() => {
if (getCookie("access_token") === undefined) {
navigate("/login");
}
}, []);

const isFormComplete =
formData.name.trim() !== "" &&
formData.content.trim() !== "" &&
Expand All @@ -30,6 +40,28 @@ const Register = () => {
}));
};

if (loading) {
return <div>Loading...</div>;
}

const handleSubmit = async () => {
const post = {
name: formData.name,
entranceYear: formData.studentNumber,
department: formData.major,
content: formData.content,
writer: formData.author,
};

const { error } = await createPost(post);

if (error) {
alert(`Error: ${error.message}`);
} else {
navigate("/booth");
}
};

return (
<div>
<header className="header">
Expand All @@ -38,7 +70,7 @@ const Register = () => {
</a>
<h2>새로운 인물 등록</h2>
</header>

<CommonForm formData={formData} handleChange={handleChange} />

<div className="register-button-container">
Expand All @@ -48,7 +80,7 @@ const Register = () => {
}`}
disabled={!isFormComplete}
type="submit"
onClick={() => window.history.back()}
onClick={handleSubmit}
>
완료
</button>
Expand Down
25 changes: 25 additions & 0 deletions src/stores/PostStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { create } from "zustand";
import axios from "axios";
import { getCookie } from "../utils/cookie";

const usePostStore = create((set) => ({
post: {
Expand Down Expand Up @@ -28,6 +29,30 @@ const usePostStore = create((set) => ({
set({ error, loading: false });
}
},
createPost: async (post) => {
set({ loading: true, error: null });
try {
const response = await axios.post(
`${import.meta.env.VITE_API_URL}/api/wiki`,
post,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getCookie("access_token")}`,
},
}
);
const { data } = response;
const { result } = data;
console.log(result);

set({ loading: false });
return { result };
} catch (error) {
set({ error, loading: false });
return { error };
}
},

// createPost, updatePost 등 다른 메서드 추가 하면 됩니다.
}));
Expand Down
Loading