Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icons and better UI flow for delete #23

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions src/pages/groups/[groupId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button } from '~/components/ui/button';
import { SplitType, type User } from '@prisma/client';
import { api } from '~/utils/api';
import { useRouter } from 'next/router';
import { Check, ChevronLeft, Share, UserPlus } from 'lucide-react';
import { Check, ChevronLeft, DoorOpen, LogOut, Share, Trash2, UserPlus } from 'lucide-react';
import { InformationCircleIcon } from '@heroicons/react/24/outline';
import { AppDrawer } from '~/components/ui/drawer';
import { UserAvatar } from '~/components/ui/avatar';
Expand Down Expand Up @@ -157,14 +157,6 @@ const BalancePage: NextPageWithUser = ({ user }) => {
<div className="mt-2 flex flex-col gap-1">
{isAdmin ? (
<>
<Button
variant="ghost"
className="justify-start p-0 text-left text-red-500 hover:text-red-500 hover:opacity-90"
disabled
>
Admin can&apos;t leave group
</Button>

<AlertDialog
open={showDeleteTrigger}
onOpenChange={(status) =>
Expand All @@ -175,30 +167,35 @@ const BalancePage: NextPageWithUser = ({ user }) => {
<Button
variant="ghost"
className="justify-start p-0 text-left text-red-500 hover:text-red-500 hover:opacity-90"
disabled={!canDelete}
onClick={() => setShowDeleteTrigger(true)}
>
{canDelete ? 'Delete group' : "Can't delete with outstanding balances"}
<Trash2 className="mr-2 h-4 w-4" /> Delete group
</Button>
</AlertDialogTrigger>
<AlertDialogContent className="max-w-xs rounded-lg">
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogTitle>
{canDelete ? 'Are you absolutely sure?' : ''}
</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be reversed
{canDelete
? 'This action cannot be reversed'
: "Can't delete the group until everyone settles up the balance"}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button
size="sm"
variant="destructive"
onClick={onGroupDelete}
disabled={deleteGroupMutation.isLoading}
loading={deleteGroupMutation.isLoading}
>
Delete
</Button>
{canDelete ? (
<Button
size="sm"
variant="destructive"
onClick={onGroupDelete}
disabled={deleteGroupMutation.isLoading}
loading={deleteGroupMutation.isLoading}
>
Delete
</Button>
) : null}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
Expand All @@ -215,30 +212,35 @@ const BalancePage: NextPageWithUser = ({ user }) => {
<Button
variant="ghost"
className="justify-start p-0 text-left text-red-500 hover:text-red-500 hover:opacity-90"
disabled={!canLeave}
onClick={() => setShowLeaveTrigger(true)}
>
{canLeave ? 'Leave group' : 'Balances should be empty to leave group'}
<DoorOpen className="mr-2 h-5 w-5" /> Leave group
</Button>
</AlertDialogTrigger>
<AlertDialogContent className="max-w-xs rounded-lg">
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogTitle>
{canLeave ? 'Are you absolutely sure?' : ''}
</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be reversed
{canLeave
? 'This action cannot be reversed'
: "Can't leave the group until your outstanding balance is settled"}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button
size="sm"
variant="destructive"
onClick={onGroupLeave}
disabled={leaveGroupMutation.isLoading}
loading={leaveGroupMutation.isLoading}
>
Leave
</Button>
{canLeave ? (
<Button
size="sm"
variant="destructive"
onClick={onGroupLeave}
disabled={leaveGroupMutation.isLoading}
loading={leaveGroupMutation.isLoading}
>
Leave
</Button>
) : null}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
Expand Down
12 changes: 12 additions & 0 deletions src/server/api/routers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,24 @@ export const groupRouter = createTRPCRouter({
where: {
id: input.groupId,
},
include: {
groupBalances: true,
},
});

if (group?.userId !== ctx.session.user.id) {
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Only creator can delete the group' });
}

const balanceWithNonZero = group?.groupBalances.find((b) => b.amount !== 0);

if (balanceWithNonZero) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'You have a non-zero balance in this group',
});
}

await ctx.db.group.delete({
where: {
id: input.groupId,
Expand Down