From 1941192d2f55f7f5902174869c3777206698ba5f Mon Sep 17 00:00:00 2001 From: 2018007956 Date: Tue, 8 Oct 2024 14:34:23 +0900 Subject: [PATCH] feat: modify sub's description #66 --- client/src/components/Sidebar.tsx | 52 +++++++++++++++++++++++++++++-- server/src/routes/subs.ts | 18 +++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/client/src/components/Sidebar.tsx b/client/src/components/Sidebar.tsx index b7f66c8..ba92e61 100644 --- a/client/src/components/Sidebar.tsx +++ b/client/src/components/Sidebar.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useState } from 'react' import { useAuthState } from '../context/auth' import { Sub } from '../types' import Link from 'next/link' @@ -14,6 +14,8 @@ type Props = { const Sidebar = ({ sub }: Props) => { const { authenticated, user } = useAuthState(); const router = useRouter(); + const [isEditing, setIsEditing] = useState(false); + const [editedDescription, setEditedDescription] = useState(sub?.description); const deleteSub = async () => { if (confirm("Are you sure you want to delete this?")) { @@ -27,11 +29,57 @@ const Sidebar = ({ sub }: Props) => { } }; + const handleEdit = async () => { + try { + const { data } = await axios.put(`/subs/${sub.name}`, { + description: editedDescription + }); + + mutate(`/subs/${sub.name}`, { ...sub, description: data.description }, false); + + setIsEditing(false); + } catch (error) { + console.log(error); + } + }; + return (
-

{sub?.description}

+
+ {isEditing ? ( + <> + setEditedDescription(e.target.value)} + className="flex-grow p-1 text-base border rounded" + /> + + + + ) : ( + <> +

{sub?.description}

+ {user && user.username === sub.username && ( + + )} + + )} +

100

diff --git a/server/src/routes/subs.ts b/server/src/routes/subs.ts index 63085b9..c36e0ab 100644 --- a/server/src/routes/subs.ts +++ b/server/src/routes/subs.ts @@ -147,6 +147,23 @@ const deleteSub = async (req: Request, res: Response) => { } }; +const updateSub = async (req: Request, res: Response) => { + const { description } = req.body; + const name = req.params.name; + + try { + const sub = await Sub.findOneOrFail({ + where: { name }, + }); + sub.description = description; + await sub.save(); + return res.json(sub); + } catch (error) { + console.log(error); + return res.status(500).json({ error: "Something went wrong" }); + } +}; + const topSubs = async (_: Request, res: Response) => { try { const imageUrlExp = `COALESCE('${process.env.APP_URL}/images/' || s."imageUrn", @@ -251,6 +268,7 @@ const router = Router(); router.get("/:name", userMiddleware, getSub); router.post("/", userMiddleware, authMiddleware, createSub); router.delete("/:name", userMiddleware, authMiddleware, deleteSub) +router.put("/:name", userMiddleware, authMiddleware, updateSub) router.get("/sub/topSubs", topSubs); router.post("/:name/upload", userMiddleware, authMiddleware, ownSub, upload.single("file"), uploadSubImage); export default router; \ No newline at end of file