From 27d9bd16dd6bc4e3e111fc788bd965613ca94874 Mon Sep 17 00:00:00 2001 From: nickbar01234 Date: Sat, 27 Apr 2024 02:59:37 -0400 Subject: [PATCH] Delete senior folder from drive --- src/app/api/senior/[id]/route.ts | 48 +++++++++++++++++++++----------- src/app/api/senior/route.ts | 2 +- src/components/SeniorView.tsx | 39 ++++++++++++++++++-------- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/app/api/senior/[id]/route.ts b/src/app/api/senior/[id]/route.ts index 63e289dd..c4e5e097 100644 --- a/src/app/api/senior/[id]/route.ts +++ b/src/app/api/senior/[id]/route.ts @@ -6,10 +6,9 @@ import { patchSeniorSchema, } from "./route.schema"; import { prisma } from "@server/db/client"; +import { driveV3 } from "@server/service"; +import { Senior } from "@prisma/client"; -/** - * @TODO - Delete folder belonging to the senior - */ export const DELETE = withSessionAndRole( ["CHAPTER_LEADER"], async ({ session, params }) => { @@ -39,21 +38,24 @@ export const DELETE = withSessionAndRole( ); } - const disconnectSenior = await prisma.senior.update({ - where: { - id: seniorId, - }, - data: { - Students: { - set: [], + await driveV3.files.delete({ fileId: maybeSenior.folder }); + await prisma.$transaction([ + prisma.senior.update({ + where: { + id: seniorId, }, - }, - }); - const deleteSenior = await prisma.senior.delete({ - where: { - id: seniorId, - }, - }); + data: { + Students: { + set: [], + }, + }, + }), + prisma.senior.delete({ + where: { + id: seniorId, + }, + }), + ]); return NextResponse.json({ code: "SUCCESS" }); } @@ -118,6 +120,18 @@ export const PATCH = withSessionAndRole( }, }); + // TODO(nickbar01234) - Refactor for to sync with POST /senior + const toFolderName = (senior: Pick) => + `${senior.firstname}_${senior.lastname}-${seniorId}`; + + if (toFolderName(seniorBody) != toFolderName(maybeSenior)) { + const params = { + fileId: maybeSenior.folder, + resource: { name: toFolderName(seniorBody) }, + }; + await driveV3.files.update(params); + } + return NextResponse.json( seniorPatchResponse.parse({ code: "SUCCESS", diff --git a/src/app/api/senior/route.ts b/src/app/api/senior/route.ts index 81f6a2d3..30cb0b71 100644 --- a/src/app/api/senior/route.ts +++ b/src/app/api/senior/route.ts @@ -74,7 +74,7 @@ export const POST = withSessionAndRole( const baseFolder = chapter.chapterFolder; // TODO: make env variable const fileMetadata = { - name: [`${seniorBody.firstname}_${seniorBody.lastname}_${senior.id}`], + name: [`${seniorBody.firstname}_${seniorBody.lastname}-${senior.id}`], mimeType: "application/vnd.google-apps.folder", parents: [baseFolder], }; diff --git a/src/components/SeniorView.tsx b/src/components/SeniorView.tsx index 68f5c9e4..c5a8a58d 100644 --- a/src/components/SeniorView.tsx +++ b/src/components/SeniorView.tsx @@ -4,9 +4,12 @@ import { Senior, User } from "@prisma/client"; import SearchableContainer from "./SearchableContainer"; import { UserTile, TileEdit } from "./TileGrid"; import AddSenior from "./AddSenior"; -import { useState } from "react"; +import React, { useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faPencil, faTrashCan } from "@fortawesome/free-solid-svg-icons"; +import { useApiThrottle } from "@hooks"; +import { deleteSenior } from "@api/senior/[id]/route.client"; +import { Spinner } from "./skeleton"; type SeniorViewProps = { seniors: Senior[]; @@ -26,9 +29,22 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => { const [yearsClicked, setYearsClicked] = useState([]); + const deleteSeniorIdRef = React.useRef(""); + + const { fn: throttleDeleteSenior, fetching } = useApiThrottle({ + fn: deleteSenior, + callback: () => { + const newSeniorState = seniorsState.filter( + (senior) => senior.id !== deleteSeniorIdRef.current + ); + setSeniorsState(newSeniorState); + deleteSeniorIdRef.current = ""; + }, + }); + return ( <> -
+
Seniors {`(${seniors.length})`}
{ options.push({ name: "Delete", - onClick: (e) => { - e.stopPropagation(); - e.preventDefault(); - fetch(`/api/senior/${senior.id}`, { - method: "DELETE", - }).then(() => { - window.location.reload(); - }); + onClick: () => { + deleteSeniorIdRef.current = senior.id; + throttleDeleteSenior({ seniorId: senior.id }); }, color: "#EF6767", icon: , @@ -82,7 +93,13 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => { senior={senior} link={`/private/chapter-leader/seniors/${senior.id}`} key={senior.id} - dropdownComponent={} + dropdownComponent={ + fetching && senior.id === deleteSeniorIdRef.current ? ( + + ) : ( + + ) + } /> ); }}