Skip to content

Commit

Permalink
Delete senior folder from drive
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbar01234 committed Apr 28, 2024
1 parent 06e5c8a commit 3211974
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
48 changes: 31 additions & 17 deletions src/app/api/senior/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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" });
}
Expand Down Expand Up @@ -118,6 +120,18 @@ export const PATCH = withSessionAndRole(
},
});

// TODO(nickbar01234) - Refactor for to sync with POST /senior
const toFolderName = (senior: Pick<Senior, "firstname" | "lastname">) =>
`${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",
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/senior/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};
Expand Down
39 changes: 28 additions & 11 deletions src/components/SeniorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -26,9 +29,22 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => {

const [yearsClicked, setYearsClicked] = useState<number[]>([]);

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 (
<>
<div className="mb-6 flex flex-row items-center justify-between ">
<div className="mb-6 flex flex-row items-center justify-between">
<div className="text-2xl">Seniors {`(${seniors.length})`}</div>
<AddSenior
key="add-senior"
Expand Down Expand Up @@ -64,14 +80,9 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => {

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: <FontAwesomeIcon icon={faTrashCan} />,
Expand All @@ -82,7 +93,13 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => {
senior={senior}
link={`/private/chapter-leader/seniors/${senior.id}`}
key={senior.id}
dropdownComponent={<TileEdit options={options} />}
dropdownComponent={
fetching && senior.id === deleteSeniorIdRef.current ? (
<Spinner width={12} height={12} />
) : (
<TileEdit options={options} />
)
}
/>
);
}}
Expand Down

0 comments on commit 3211974

Please sign in to comment.