From fbaae34cccaa165c2c49af4477aa0ffdeb66141d Mon Sep 17 00:00:00 2001 From: Lee Soobeom Date: Tue, 26 Jul 2022 20:37:43 +0900 Subject: [PATCH] update debugging --- frontend/src/post/editpost.tsx | 79 ++++++++------ frontend/src/post/intopost.tsx | 25 +---- frontend/src/post/posting.tsx | 29 ++--- frontend/src/types/index.tsx | 10 +- src/controllers/post.controller.ts | 168 ++++++++++++++++++++--------- src/db/post.db.ts | 40 ++++--- src/models/post.model.ts | 2 +- src/routes/post.route.ts | 2 +- 8 files changed, 213 insertions(+), 142 deletions(-) diff --git a/frontend/src/post/editpost.tsx b/frontend/src/post/editpost.tsx index f800f28..b7b2cba 100644 --- a/frontend/src/post/editpost.tsx +++ b/frontend/src/post/editpost.tsx @@ -1,4 +1,4 @@ -import React, { FormEvent, useState, useEffect } from "react"; +import React, { FormEvent, useState } from "react"; import { useNavigate, useLocation } from "react-router-dom"; import isLength from "validator/lib/isLength"; import equals from "validator/lib/equals"; @@ -6,20 +6,19 @@ import { catchErrors } from "../helpers"; import { PostType } from "../types"; import { postApi } from "../apis"; import { PostState } from "./intopost"; -import { FileType } from "./intopost"; export function EditPost() { - const [city, setCity] = useState("city"); - const [theme, setTheme] = useState("theme"); + const location = useLocation() as PostState; + const post = location.state; + + const [city, setCity] = useState(post.city); + const [theme, setTheme] = useState(post.theme); const [title, setTitle] = useState(""); const [text, setText] = useState(""); const [file, setFile] = useState(); const [imgSrc, setImgSrc] = useState(); - const [filesList, setFilesList] = useState(); + const [change, setChange] = useState(false); const navigate = useNavigate(); - const location = useLocation() as PostState; - - const post = location.state; const [user, setUser] = useState({ title: post.title, @@ -30,6 +29,7 @@ export function EditPost() { user: post.user, counts: post.counts, _id: post._id, + file: post.file, }); const [loading, setLoading] = useState(false); @@ -37,24 +37,17 @@ export function EditPost() { const [disabled, setDisabled] = useState(false); const [success, setSuccess] = useState(false); - useEffect(() => { - getFilesList(post._id); - }, []); - - const getFilesList = async (postId: string) => { - const res = await postApi.getFileByPostId(postId); - setFilesList(res); - }; - const imgArr = new Array(); - const updateImg2Db = async (filelist: FileList) => { + // console.log("post.file", post.file); + + const updateImg2Db = async (filelist: FileList | undefined) => { const formdata = new FormData(); formdata.append("title", user.title); formdata.append("text", user.text); formdata.append("theme", user.theme); formdata.append("city", user.city); - if (filelist === undefined || filelist === null) { + if (filelist === undefined) { const res = await postApi.updateImgAndPost(user._id, formdata); } else { for (var i = 0; i < filelist.length; i++) { @@ -69,14 +62,12 @@ export function EditPost() { try { if (confirm("게시물을 수정하시겠습니까?") == true) { setError(""); - // console.log("user data", user); if (postingFormMatch(user) === true) { setLoading(true); - if (file) { - const res = updateImg2Db(file); - // console.log(res); - } + + const res = updateImg2Db(file); navigate("/board", { replace: true }); + setSuccess(true); setError(""); } @@ -92,9 +83,12 @@ export function EditPost() { } const handleInputPic = async (event: React.ChangeEvent) => { + setChange(true); const maxImg = 10; const { files } = event.target; + // console.log("update file", files); + if (!(files === null)) { setFile(files); } @@ -118,18 +112,18 @@ export function EditPost() { function postingFormMatch(user: PostType) { if (!isLength(user.title ?? "", { min: 1 })) { - setError("제목을 입력해 주세요."); alert("제목을 입력해 주세요."); + setError("제목을 입력해 주세요."); return false; } else if (!isLength(user.text ?? "", { min: 1 })) { alert("내용을 입력해 주세요."); setError("내용을 입력해 주세요."); return false; - } else if (equals(user.city, "city")) { + } else if (equals(city, "city")) { alert("도시를 선택해 주세요."); setError("도시를 선택해 주세요."); return false; - } else if (equals(user.theme, "theme")) { + } else if (equals(theme, "theme")) { alert("테마를 선택해 주세요."); setError("테마를 선택해 주세요."); return false; @@ -170,6 +164,26 @@ export function EditPost() { setUser(newUser); }; + const oldFileShow = (post: PostType) => { + const res = post.file?.map((file, i) => ( + + )); + // console.log("oldfiles", res); + return res; + }; + + const newFileShow = (imgSrc: string[] | undefined) => { + const res = imgSrc?.map((img, i) => ( + + )); + return res; + }; + return (
@@ -236,6 +250,7 @@ export function EditPost() {