Skip to content

Commit

Permalink
feat: added group delete dialog, and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
must108 committed Aug 17, 2024
1 parent 5e244ff commit af5aaf7
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/app/_components/dashboard/groups/CreateGroupDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default function CreateGroupDialog({
value={groupDescription}
placeholder="Your group description..."
onChange={(e) =>
setGroupName(e.target.value)
setGroupDescription(e.target.value)
}
required
className="w-full"
Expand Down
111 changes: 101 additions & 10 deletions src/app/_components/dashboard/groups/DeleteGroupDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,89 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
"use client";

import { Button } from "@radix-ui/themes";
import { Button, Flex, Text, Dialog, TextField } from "@radix-ui/themes";
import { useAtom } from "jotai";
import { useState } from "react";
import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import { userAtom } from "~/server/lib/stores";
import * as Dialog from "@radix-ui/react-dialog";
import { selectedGroupId, userAtom } from "~/server/lib/stores";
import { api } from "~/trpc/react";
import React from "react";
import { FiTrash2 } from "react-icons/fi";

export default function DeleteGroupDialog() {
interface DeleteGroupProps {
refetch: () => void;
groupId: string;
}

export default function DeleteGroupDialog({ refetch, groupId }: DeleteGroupProps) {
const [user, setUser] = useAtom(userAtom);

const [dialogOpen, setDialogOpen] = useState(false);
const [addingDevice, setAddingDevice] = useState(false);
const [deletingGroup, setDeletingGroup] = useState(false);

const [groupName, setGroupName] = useState("");
const [groupDescription, setGroupDescription] = useState("");

const groupQuery = api.device.list.useQuery({
userId: user?.id || "",
const { mutate, error } = api.group.read.useMutation({
onSuccess: (group) => {
setGroupName(group.name);
setDialogOpen(true);
},
onError: () => {
toast.error("Failed to get group!");

setGroupName(groupName);
},
});

const groupDeleteQuery = api.group.delete.useMutation({
onSuccess: () => {
toast.success(`${groupName} has been deleted!`);
setGroupName("");

setDeletingGroup(false);
setDialogOpen(false);
refetch();
},
onError: () => {
toast.error(`Failed to update ${groupName}`);

setDeletingGroup(false);
setDialogOpen(false);
}
});

useEffect(() => {
if (dialogOpen) {
readGroup();
}
}, [dialogOpen]);

const deleteGroup = () => {
if (deletingGroup) return;
if (!user) {
setDialogOpen(false);
return toast.error("You must be logged in to delete a device!");
}

setDeletingGroup(true);

groupDeleteQuery.mutate({
id: groupId,
userId: user.id,
});
};

const readGroup = () => {
mutate({ id: groupId, userId: user!.id });
};

if (error) {
toast.error("Couldn't get the group!");
}

return (
<Dialog.Root open={dialogOpen} onOpenChange={setDialogOpen}>
<Dialog.Trigger asChild onClick={() => setDialogOpen(!dialogOpen)}>
<Dialog.Trigger onClick={() => setDialogOpen(!dialogOpen)}>
<Button
variant="ghost"
color="red"
Expand All @@ -35,6 +92,40 @@ export default function DeleteGroupDialog() {
<FiTrash2 />
</Button>
</Dialog.Trigger>
<Dialog.Content maxWidth={"400px"}>
<Dialog.Title align={"center"}>
Delete Your Group
</Dialog.Title>

<div className="flex flex-col gap-2">
<div className="flex flex-col text-center">
<span className="text-red-400">
This is a destructive action.
</span>{" "}
<span>
All group data, including places and
connected members, &#40;not member data&#41;
will be deleted. Are you sure you want to delete this group?
</span>
</div>
<div className="mt-2 flex items-center justify-between">
<Button
onClick={() => setDialogOpen(false)}
variant="ghost"
>
Cancel
</Button>
<Button
onClick={deleteGroup}
disabled={deletingGroup}
variant="solid"
color="red"
>
Yes, I&apos;m sure
</Button>
</div>
</div>
</Dialog.Content>
</Dialog.Root>
);
}
Expand Down
1 change: 0 additions & 1 deletion src/app/_components/dashboard/groups/EditGroupDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useAtom } from "jotai";
import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import { userAtom } from "~/server/lib/stores";
// import * as Dialog from "@radix-ui/react-dialog";
import { api } from "~/trpc/react";
import { RxPencil1 } from "react-icons/rx";
import { FaCheck } from "react-icons/fa";
Expand Down
4 changes: 3 additions & 1 deletion src/app/_components/dashboard/groups/GroupButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export default function GroupButtons({ groupId }: { groupId: string }) {
refetch={() => groupQuery.refetch()}
groupId={groupId}
/>
<DeleteGroupDialog />
<DeleteGroupDialog
refetch={() => groupQuery.refetch()}
groupId={groupId} />
</div>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/_components/dashboard/groups/GroupRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function GroupRow({
<LuCrown className="text text-amber-400" />
</Tooltip>
) : // <LuCrown className="text text-transparent" />
null}
null}
{name}
</Table.RowHeaderCell>
<Table.Cell className="h-full">
Expand Down
8 changes: 8 additions & 0 deletions src/server/api/routers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,21 @@ export const groupRouter = createTRPCRouter({
.input(
z.object({
id: z.string(),
userId: z.string(),
// TODO: add ownerid security.
}),
)
.mutation(async ({ ctx, input }) => {
// TODO: AHHHH REMEMBER WE NEED TO PROTECT ALL THESE ENDPOINTS
try {
await ctx.db.place.deleteMany({
where: {
groupId: input.id,
},
});

await redis.del(`group:${input.id}`);
await redis.del(`user:${input.userId}:groups`);

return await ctx.db.group.delete({
where: {
Expand Down
5 changes: 3 additions & 2 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ html {
scroll-snap-type: y mandatory;
}

h1 {
/* h1 {
font-size: 56px;
font-weight: 700;
letter-spacing: -3px;
line-height: 1.2;
text-align: center;
margin: 100px 0;
}
} */
/* gonna make modals without the padding for now */

h2 {
margin: 0;
Expand Down

0 comments on commit af5aaf7

Please sign in to comment.