From 5ac73718cf477c5aaf13d8e899658ef755278f78 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sat, 26 Mar 2022 00:24:18 -0700 Subject: [PATCH] server/client: add post deletion --- client/components/new-post/post.module.css | 39 ++++++++++--------- client/components/post-list/index.tsx | 25 +++++++++--- client/components/post-list/list-item.tsx | 8 ++-- .../components/view-document-list/index.tsx | 22 ----------- client/components/view-document/index.tsx | 9 +++-- server/src/routes/posts.ts | 1 - 6 files changed, 50 insertions(+), 54 deletions(-) delete mode 100644 client/components/view-document-list/index.tsx diff --git a/client/components/new-post/post.module.css b/client/components/new-post/post.module.css index 9925298..1a57424 100644 --- a/client/components/new-post/post.module.css +++ b/client/components/new-post/post.module.css @@ -1,28 +1,29 @@ .buttons { - position: relative; - display: flex; - justify-content: space-between; - width: 100%; - margin-top: var(--gap-double); + position: relative; + display: flex; + justify-content: space-between; + width: 100%; + margin-top: var(--gap-double); } .title { - display: flex; - flex-direction: row; - align-items: center; - justify-content: space-between; + display: flex; + flex-direction: row; + align-items: center; + justify-content: space-between; + margin-bottom: var(--gap); } @media screen and (max-width: 650px) { - .title { - align-items: flex-start; - flex-direction: column; - } + .title { + align-items: flex-start; + flex-direction: column; + } - .buttons { - flex-direction: column; - margin: 0; - justify-content: space-between; - min-height: 95px; - } + .buttons { + flex-direction: column; + margin: 0; + justify-content: space-between; + min-height: 95px; + } } diff --git a/client/components/post-list/index.tsx b/client/components/post-list/index.tsx index 194d45c..feba682 100644 --- a/client/components/post-list/index.tsx +++ b/client/components/post-list/index.tsx @@ -6,7 +6,7 @@ import styles from './post-list.module.css' import ListItemSkeleton from "./list-item-skeleton" import ListItem from "./list-item" import { Post } from "@lib/types" -import { ChangeEvent, MouseEventHandler, useCallback, useEffect, useMemo, useState } from "react" +import { ChangeEvent, useCallback, useEffect, useMemo, useState } from "react" import debounce from "lodash.debounce" import Cookies from "js-cookie" @@ -36,9 +36,7 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { } } ) - console.log(res) const json = await res.json() - console.log(json) setPosts([...posts, ...json.posts]) setHasMorePosts(json.morePosts) } @@ -85,6 +83,23 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { } }, [debouncedSearchHandler]); + const deletePost = useCallback((postId: string) => async () => { + const res = await fetch(`/server-api/posts/${postId}`, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${Cookies.get("drift-token")}` + }, + }) + + if (!res.ok) { + console.error(res) + return + } else { + setPosts((posts) => posts.filter(post => post.id !== postId)) + } + }, []) + return (
@@ -94,7 +109,7 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { onChange={debouncedSearchHandler} />
{error && Failed to load.} - {!posts && searching &&
{isOwner && - +