Commit 914ed2b6 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

과목정보수정 및 context 정보 수정

parent e8156733
import axios from "axios";
import baseUrl from "../utils/baseUrl.js";
const addsubject = async (info) => {
console.log('info확인', info)
const addsubject = async (info, userId) => {
console.log('subject check', userId)
// info.user = userId;
const url = `${baseUrl}/api/subject/addsubject`;
const { data, status } = await axios.post(url, info);
console.log('data status', data, '|', status)
return { data, status }
const { data } = await axios.post(url, { info, userId });
return data
}
const editSubject = async (info, id) => {
console.log("editSubject check", info, ',', id)
const url = `${baseUrl}/api/subject/editsubject`
const { data } = await axios.post(url, { info, id })
return data
}
const getSubInfo = async (info) => {
console.log('info', info)
const url = `${baseUrl}/api/subject/${info}`
const { data } = await axios.get(url);
return data
}
const subjectApi = {
addsubject
addsubject,
getSubInfo,
editSubject
};
export default subjectApi
\ No newline at end of file
......@@ -3,12 +3,7 @@ import { Link } from "react-router-dom";
import styles from "../Form/form.module.scss";
const StudyPlanCard = () => {
const [studyplan, setStudyplan] = useState({
subject: "",
info: "",
contents: []
})
// studyPlanList에서 props로 받아서 뿌리기
return (
<>
<div className="d-flex justify-content-center mt-3">
......@@ -18,7 +13,7 @@ const StudyPlanCard = () => {
<div className="d-flex justify-content-between">
<h5 className="card-title col-10">운영체제</h5>
<div className="col-2 d-flex justify-content-end">
<Link className="text-decoration-none link-dark" to="/subject/edit"><i className="bi bi-pencil-square pe-2"></i></Link>
<Link className="text-decoration-none link-dark" to="/subject/edit/ed56bebd-b9ae-4065-aae2-d39aeac5f18e"><i className="bi bi-pencil-square pe-2"></i></Link>
<i className="bi bi-trash"></i>
</div>
</div>
......
import { useState, useEffect } from 'react';
import { Redirect, useParams } from 'react-router-dom';
import { useAuth } from '../../utils/context.js';
import BtnGroup from "../Buttons/BtnGroup";
import styles from "./form.module.scss";
import { useParams } from 'react-router-dom';
import subjectApi from '../../apis/subject.api';
import catchErrors from '../../utils/catchErrors.js';
const SubjectForm = () => {
const { user } = useAuth();
const { subjectId } = useParams();
const [success, setSuccess] = useState(false)
const [error, setError] = useState("");
const [subject, setSubject] = useState({
lectureName: "",
......@@ -15,6 +18,11 @@ const SubjectForm = () => {
})
const [disabled, setDisabled] = useState(true)
useEffect(() => {
getInfo(subjectId);
}, [])
useEffect(() => {
let isMounted = true;
const checkInfo = { lectureName: subject.lectureName }
......@@ -34,36 +42,60 @@ const SubjectForm = () => {
}
async function getInfo(id) {
const result = await subjectApi.getSubInfo(id)
console.log('과목수정 result확인', result)
setSubject({
lectureName: result.name,
prof: result.prof,
classRoom: result.room
})
}
async function handleSubmit(e) {
e.preventDefault();
try {
setError("")
if (subjectId) {
//수정함수 실행
await subjectApi.editSubject(subject, subjectId)
alert("과목정보가 수정되었습니다.")
setSuccess(true)
} else {
await subjectApi.addsubject(subject)
//등록함수 실행
await subjectApi.addsubject(subject, user.id)
}
} catch (error) {
catchErrors(error, setError)
setSubject({
lectureName: "",
prof: "",
classRoom: ""
})
}
}
if (success) {
return <Redirect to="/studyplan" />
}
return (
<>
<div className="position-absolute top-50 start-50 translate-middle" style={{ width: "80%" }}>
<div>
<div className="mb-5 d-flex flex-row">
<label className="form-label fs-4" style={{ width: "100px" }}>강의명</label>
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="lectureName" onChange={handleChange} />
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.lectureName} name="lectureName" onChange={handleChange} />
</div>
<div className="mb-5 pt-2 d-flex flex-row">
<label className="form-label fs-4" style={{ width: "100px" }}>교수명</label>
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="prof" onChange={handleChange} />
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.prof} name="prof" onChange={handleChange} />
</div>
<div className="mb-5 pt-2 d-flex flex-row">
<label className="form-label fs-4 " style={{ width: "100px", letterSpacing: "15px" }}>장소</label>
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="classRoom" onChange={handleChange} />
<input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.classRoom} name="classRoom" onChange={handleChange} />
</div>
</div>
<div className="pt-2">
......
......@@ -28,7 +28,7 @@ const AuthProvider = ({ children }) => {
try {
setError("");
const user = await authApi.login(data);
console.log('user in context', user)
localStorage.setItem("login", true)
setUser(user);
console.log('setUser 결과', user)
return true;
......@@ -42,6 +42,7 @@ const AuthProvider = ({ children }) => {
try {
setError("");
const user = await authApi.logout();
localStorage.removeItem("login")
setUser(user);
alert("로그아웃 되었습니다.");
} catch (error) {
......
......@@ -2,28 +2,67 @@ import { Subject } from '../db/index.js';
const addsubject = async (req, res) => {
console.log('server/addsubject req.body', req.body)
const { lectureName, prof, classRoom } = req.body;
try {
const findName = await Subject.findOne({ where: { name: lectureName } });
const { info, userId } = req.body;
const findName = await Subject.findOne({ where: { name: info.lectureName } });
if (findName) {
throw new Error("이미 있는 과목입니다.")
}
await Subject.create({
name: lectureName,
prof: prof,
room: classRoom,
name: info.lectureName,
prof: info.prof,
room: info.classRoom,
userId: userId
})
} catch (error) {
console.log(error)
return res.status(500).send(error.message || "과목저장 에러발생")
}
}
const getSubInfo = async (req, res) => {
console.log('server/getSubInfo req.body', req.params)
try {
const { subjectId } = req.params;
const findSubInfo = await Subject.findOne({ where: { id: subjectId } })
if (findSubInfo) {
res.json({
name: findSubInfo.dataValues.name,
prof: findSubInfo.dataValues.prof,
room: findSubInfo.dataValues.room
})
} else {
throw new Error("과목 찾기 실패")
}
} catch (error) {
console.log(error)
return res.status(500).send(error.message || "과목정보 가져오기 에러발생")
}
}
const editSubject = async (req, res) => {
console.log('server/editSubject req.body', req.body)
try {
const { info, id } = req.body;
const result = await Subject.update({
name: info.lectureName,
prof: info.prof,
room: info.classRoom,
}, { where: { id: id } })
if (!result) {
throw new Error("과목정보 수정 에러발생")
} else {
res.send(200)
}
} catch (error) {
console.log(error)
return res.status(500).send(error.message || "과목정보 수정 에러발생")
}
}
export default {
addsubject
addsubject,
getSubInfo,
editSubject
}
\ No newline at end of file
......@@ -53,7 +53,7 @@ const login = async (req, res) => {
if (passwordMatch) {
const signData = {
id: user.userID,
id: user.id,
role: "user",
name: user.userName,
};
......
......@@ -7,4 +7,13 @@ router
.route("/addsubject")
.post(subjectCtrl.addsubject)
router
.route("/:subjectId")
.get(subjectCtrl.getSubInfo)
router
.route("/editsubject")
.post(subjectCtrl.editSubject)
export default router;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment