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

update debugging

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