Commit 0b091720 authored by Lee Soobeom's avatar Lee Soobeom
Browse files

update debugging

parent 32764f1e
......@@ -20,7 +20,7 @@ export default function BoardPage() {
const navigate = useNavigate();
const { user } = useAuth();
console.log("get newPost Info", newPost);
// console.log("get newPost Info", newPost);
const setNewPosts = (newpost: PostType) => {
const postArr = posts?.splice(-1, 0, newPost);
......
......@@ -39,7 +39,7 @@ export function EditPost() {
const imgArr = new Array();
// console.log("post.file", post.file);
console.log("post.file", post.file);
const updateImg2Db = async (filelist: FileList | undefined) => {
const formdata = new FormData();
......@@ -49,11 +49,15 @@ export function EditPost() {
formdata.append("city", user.city);
if (filelist === undefined) {
const res = await postApi.updateImgAndPost(user._id, formdata);
return res;
} else {
for (var i = 0; i < filelist.length; i++) {
formdata.append("picture", filelist?.[i]);
}
console.log("one file update before");
const res = await postApi.updateImgAndPost(user._id, formdata);
console.log("one file update", res);
return res;
}
};
......@@ -66,7 +70,11 @@ export function EditPost() {
setLoading(true);
const updateRes = await updateImg2Db(file);
navigate("/board", { replace: true, state: updateRes });
// console.log("find newfilename", updateRes);
navigate(
{ pathname: `/post/${post._id}` },
{ replace: true, state: updateRes }
);
setSuccess(true);
setError("");
......
......@@ -9,23 +9,28 @@ export interface PostState {
}
export function IntoPost() {
const [posts, setPosts] = useState<PostType>();
const location = useLocation() as PostState;
const post = location.state;
const navigate = useNavigate();
console.log("user info", post.user);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [addSuccess, setAddSuccess] = useState(false);
const [delSuccess, setDelSuccess] = useState(false);
// console.log("post.file", post.file);
useEffect(() => {
setPosts(post);
}, []);
const handleDeleteClick = async (event: MouseEvent<HTMLButtonElement>) => {
try {
if (confirm("삭제하시겠습니까?") == true) {
const postId = event.currentTarget.id;
const res = await postApi.deletePost(postId);
navigate("/board", { replace: true });
navigate("/board");
console.log("delete post", res);
} else {
return false;
......@@ -40,41 +45,41 @@ export function IntoPost() {
<div className="flex flex-col">
<div className="flex h-8 gap-x-1 place-content-end place-items-center">
<button
id={post._id}
id={posts?._id}
onClick={handleDeleteClick}
className=" whitespace-nowrap flex border-2 border-sky-100 place-self-center h-6 w-8 text-xs text-center transition delay-150 bg-white-400 hover:-translate-y-1 hover:scale-110 hover:bg-red-300 duration-300"
>
삭제
</button>
<Link to="/edit" state={post}>
<Link to="/edit" state={posts}>
<button className="whitespace-nowrap flex border-2 border-sky-100 place-self-center h-6 w-8 text-xs transition delay-150 bg-white-400 hover:-translate-y-1 hover:scale-110 hover:bg-sky-300 duration-300">
수정
</button>
</Link>
</div>
<div className="flex h-10 border-t-2 border-sky-500 items-center font-semibold">
{post.title}
{posts?.title}
</div>
<div className="flex h-10 items-center border-t-2 border-sky-200 md:flex-row justify-between bg-slate-50 text-sm">
<div className="flex whitespace-nowrap pr-5 ">
작성자: {post.user.name}
작성자: {posts?.user.name}
</div>
<div className="flex divide-x divide-slate-300 ">
<div className="flex basis-1/2 whitespace-nowrap px-2">
작성일 : {post.date.slice(0, 10)}
작성일 : {posts?.date.slice(0, 10)}
</div>
<div className="flex whitespace-nowrap px-2"> {post.city}</div>
<div className="flex whitespace-nowrap px-2"> {post.theme}</div>
<div className="flex whitespace-nowrap px-2"> {posts?.city}</div>
<div className="flex whitespace-nowrap px-2"> {posts?.theme}</div>
<div className="flex whitespace-nowrap px-2">
조회수 : {post.counts}
조회수 : {posts?.counts}
</div>
</div>
</div>
<div className="flex border-t-2 border-sky-200 h-44 p-2 overflow-auto mb-5 ">
{post.file?.map((file, i) => (
{posts?.file?.map((file, i) => (
<img
key={i}
src={"http://localhost:3000/images/" + file.newfilename}
......@@ -83,7 +88,7 @@ export function IntoPost() {
/>
))}
</div>
<div className="border-b-2 border-sky-500 h-44 mb-10">{post.text}</div>
<div className="border-b-2 border-sky-500 h-44 mb-10">{posts?.text}</div>
</div>
);
}
......@@ -24,6 +24,7 @@ export default function Posting() {
user: {
_id: "",
name: "",
avatar: "",
},
counts: 0,
_id: "",
......
......@@ -23,6 +23,7 @@ export interface PostType {
user: {
_id: string;
name: string;
avatar: string;
};
file: [
{
......
......@@ -197,7 +197,7 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => {
console.log("check files", files);
if (files.picture === undefined || files.picture === null) {
const postRes2 = await postDb.updatePostRow(
const postRes1 = await postDb.updatePostRow(
{
title,
text,
......@@ -207,7 +207,8 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => {
},
postId
);
console.log("no files update", postRes2);
console.log("no files update", postRes1);
return res.json(postRes1);
} else {
if (Array.isArray(files.picture)) {
const oldFilesId = await postDb.getFilesByPostId(postId);
......@@ -273,16 +274,29 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => {
}
}
}
console.log("all fileId", fileIdArr);
//post정보 + file정보 update
const postRes2 = await postDb.updatePostRow(
{
title,
text,
theme,
city,
date: Date.now(),
file: fileIdArr,
},
postId
);
console.log("plural files update", postRes2);
return res.json(postRes2);
} else {
const oldFilesId = await postDb.getFilesByPostId(postId);
if (!(oldFilesId === undefined)) {
for (var i = 0; i < oldFilesId?.length; i++) {
const name = await postDb.getOriginalFileName(
oldFilesId[i]
);
if (!(name === undefined)) {
oldSet.add(name);
}
const name = await postDb.getOriginalFileName(oldFilesId[0]);
if (!(name === undefined)) {
oldSet.add(name);
}
}
console.log("OldSet", oldSet);
......@@ -296,6 +310,8 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => {
//유지, 삭제, 추가 구분하기
const trdPart = SubTract(oldSet, newSet);
console.log("add part", trdPart.add);
//삭제
for (var i = 0; i < trdPart.drop.length; i++) {
const dropRes = await postDb.deleteFileByName(
......@@ -308,34 +324,34 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => {
const originalfilename = files.picture.originalFilename;
const newfilename = files.picture.newFilename;
const filepath = files.picture.filepath;
for (var j = 0; j < trdPart.add.length; j++) {
const check = trdPart.add[j];
if (originalfilename === check) {
const addRes = await postDb.createFilesRow(
originalfilename,
newfilename,
filepath
);
if (originalfilename === trdPart.add[0]) {
const addRes = await postDb.createFilesRow(
originalfilename,
newfilename,
filepath
);
fileIdArr.push(addRes._id);
}
fileIdArr.push(addRes._id);
}
console.log("all fileId", fileIdArr);
//post정보 + file정보 update
const postRes3 = await postDb.updatePostRow(
{
title,
text,
theme,
city,
date: Date.now(),
file: fileIdArr,
},
postId
);
console.log("singular file update", postRes3);
return res.json(postRes3);
}
}
console.log("all fileId", fileIdArr);
//post정보 + file정보 update
const postRes1 = await postDb.updatePostRow(
{
title,
text,
theme,
city,
date: Date.now(),
file: fileIdArr,
},
postId
);
}
}
}
......
......@@ -38,7 +38,6 @@ export const getPosts = async () => {
.populate("file")
.populate("user")
.sort({ date: -1 });
console.log("file nickname", posts);
return posts;
};
......@@ -64,7 +63,7 @@ export const updatePostRow = async (post: PostType, postId: string) => {
file: post.file,
},
{ new: true }
);
).populate("file");
return newPost;
};
......
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