Skip to content

Commit

Permalink
Revert "Merge pull request #11 from Vero-Ventures/connect-invitations…
Browse files Browse the repository at this point in the history
…-backend"

This reverts commit f2c9a86, reversing
changes made to f839b5c.
  • Loading branch information
scottchen98 committed Oct 30, 2024
1 parent deb0216 commit 16d7783
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 246 deletions.
51 changes: 21 additions & 30 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ export type PublicApiType = {
Record<string, never>,
any
>;
deleteAuthAccount: FunctionReference<
"mutation",
"public",
{ userId: Id<"users"> },
any
>;
updateProfile: FunctionReference<
"mutation",
"public",
Expand All @@ -110,24 +104,6 @@ export type PublicApiType = {
},
any
>;
updateUserName: FunctionReference<
"mutation",
"public",
{ name: string },
any
>;
updatePartialProfile: FunctionReference<
"mutation",
"public",
{ dob?: number; height?: number; weight?: number },
any
>;
deleteUser: FunctionReference<
"mutation",
"public",
{ userId: Id<"users"> },
any
>;
};
goalLogs: {
getGoalLogById: FunctionReference<
Expand Down Expand Up @@ -253,7 +229,11 @@ export type PublicApiType = {
removeFromChallenge: FunctionReference<
"mutation",
"public",
{ challengeId: Id<"challenges">; userId: Id<"users"> },
{
challengeId: Id<"challenges">;
organizationId: Id<"organizations">;
userId: Id<"users">;
},
any
>;
};
Expand Down Expand Up @@ -290,17 +270,26 @@ export type PublicApiType = {
{ orgName: string; owner: { email: string; name: string } },
any
>;
listInvitations: FunctionReference<"query", "public", any, any>;
sendUserInvitation: FunctionReference<
"mutation",
"public",
{ emails: Array<string>; role: string },
{
email: string;
expiresAt: number;
organizationId: Id<"organizations">;
role: string;
},
any
>;
resendUserInvitation: FunctionReference<
"mutation",
"public",
{ invitationId: Id<"invitations"> },
{
email: string;
expiresAt: number;
organizationId: Id<"organizations">;
role: string;
},
any
>;
acceptInvitation: FunctionReference<
Expand All @@ -309,10 +298,12 @@ export type PublicApiType = {
{ invitationId: Id<"invitations"> },
any
>;
deleteInvitation: FunctionReference<
};
members: {
updateMemberRole: FunctionReference<
"mutation",
"public",
{ invitationId: Id<"invitations"> },
{ memberId: Id<"members">; role: string },
any
>;
};
Expand Down
40 changes: 5 additions & 35 deletions src/app/dashboard/members/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
} from "@/components/ui/dropdown-menu";
import { Checkbox } from "@/components/ui/checkbox";
import { Badge } from "@/components/ui/badge";
import { useMutation } from "convex/react";
import { api } from "@/api";

// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
Expand All @@ -23,8 +21,7 @@ export interface TableData {
name?: string;
email: string;
role: string;
status: string;
userId?: any;
status?: string;
}

export const columns: ColumnDef<TableData>[] = [
Expand Down Expand Up @@ -114,17 +111,8 @@ export const columns: ColumnDef<TableData>[] = [
{
id: "actions",
header: () => <div></div>,
cell: function Cell({ row }) {
const resendUserInvitation = useMutation(
api.invitations.resendUserInvitation
);
const deleteUserInvitation = useMutation(
api.invitations.deleteInvitation
);
const deleteAuthAccount = useMutation(api.users.deleteAuthAccount);
const deleteUser = useMutation(api.users.deleteUser);
cell: ({ row }) => {
const member = row.original;

return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
Expand All @@ -136,19 +124,7 @@ export const columns: ColumnDef<TableData>[] = [
<DropdownMenuContent align="end">
{member.status !== "pending" ? (
<DropdownMenuItem
onClick={async () => {
await Promise.all([
deleteUserInvitation({
invitationId: member._id,
}),
deleteAuthAccount({
userId: member.userId,
}),
deleteUser({
userId: member.userId,
}),
]);
}}
onClick={async () => {}}
className="flex cursor-pointer items-center gap-2 text-destructive focus:text-destructive"
>
<Trash2 className="size-4" />
Expand All @@ -157,20 +133,14 @@ export const columns: ColumnDef<TableData>[] = [
) : (
<>
<DropdownMenuItem
onClick={async () => {
await resendUserInvitation({ invitationId: member._id });
}}
onClick={async () => {}}
className="flex cursor-pointer items-center gap-2"
>
<Send className="size-4" />
<span>Resend Invitation Email</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={async () => {
await deleteUserInvitation({
invitationId: member._id,
});
}}
onClick={async () => {}}
className="flex cursor-pointer items-center gap-2 text-destructive focus:text-destructive"
>
<Ban className="size-4" />
Expand Down
98 changes: 5 additions & 93 deletions src/app/dashboard/members/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { useState } from "react";
import { DataTablePagination } from "../../../components/ui/pagination";
import { Button } from "@/components/ui/button";
import { AlertCircleIcon, Trash2, UserPlus2 } from "lucide-react";
import { Trash2, UserPlus2 } from "lucide-react";
import {
Dialog,
DialogContent,
Expand All @@ -38,9 +38,6 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { useMutation } from "convex/react";
import { api } from "@/api";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand All @@ -51,7 +48,6 @@ export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const [open, setOpen] = useState(false);
const [sorting, setSorting] = useState<SortingState>([]);
const [rowSelection, setRowSelection] = useState({});
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
Expand All @@ -71,64 +67,9 @@ export function DataTable<TData, TValue>({
columnFilters,
},
});
const [error, setError] = useState("");
const [isPending, setIsPending] = useState(false);
const [email, setEmail] = useState("");
const [role, setRole] = useState("");
const sendUserInvitation = useMutation(api.invitations.sendUserInvitation);
const deleteInvitation = useMutation(api.invitations.deleteInvitation);
const deleteUser = useMutation(api.users.deleteUser);
const deleteAuthAccount = useMutation(api.users.deleteAuthAccount);

const handleSubmit = async () => {
try {
setIsPending(true);

if (!role) {
throw new Error("Please choose a role.");
}

const emails = email.split(",");

const emailPattern = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

emails.forEach((email) => {
if (!emailPattern.test(email)) {
throw new Error("Please enter a valid email.");
}
});

await sendUserInvitation({
emails: emails,
role,
});
setEmail("");
setRole("");
setRowSelection({});
setOpen(false);
} catch (error) {
if (error instanceof Error) {
setError(error.message);
}
} finally {
setIsPending(false);
}
};

const deleteSelectedRows = async () => {
const selectedRows = table.getFilteredSelectedRowModel().rows;

await Promise.all(
selectedRows.map(async (row: any) => {
const member = row.original;
if (member.status !== "pending") {
await deleteUser({ userId: member.userId });
await deleteAuthAccount({ userId: member.userId });
}
await deleteInvitation(member._id);
})
);

console.log(selectedRows);
setRowSelection({});
};

Expand All @@ -154,15 +95,7 @@ export function DataTable<TData, TValue>({
<span>Remove Selected ({Object.keys(rowSelection).length})</span>
</Button>
)}
<Dialog
open={open}
onOpenChange={(open) => {
setEmail("");
setRole("");
setError("");
setOpen(open);
}}
>
<Dialog>
<DialogTrigger asChild>
<Button>Invite</Button>
</DialogTrigger>
Expand All @@ -174,36 +107,21 @@ export function DataTable<TData, TValue>({
</DialogTitle>
</DialogHeader>
<div className="grid gap-4 py-4">
{!!error && (
<Alert variant="destructive" className="max-w-xl">
<AlertCircleIcon className="size-4" />
<AlertTitle>Something went wrong!</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div className="grid gap-4">
<Label htmlFor="name">Email</Label>
<Input
id="email"
type="email"
placeholder="[email protected]"
className="col-span-3"
multiple
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<p className="text-sm">
Separate multiple emails with a comma.
</p>
</div>
<div className="grid gap-4">
<Label htmlFor="name">Role</Label>
<RadioGroup
defaultValue="comfortable"
className="space-y-4"
value={role}
onValueChange={(role) => setRole(role)}
>
<RadioGroup defaultValue="comfortable" className="space-y-4">
<div className="flex items-start space-x-2">
<RadioGroupItem value="admin" id="admin" />
<Label className="text-sm" htmlFor="admin">
Expand All @@ -225,13 +143,7 @@ export function DataTable<TData, TValue>({
</div>
</div>
<DialogFooter>
<Button
disabled={isPending}
type="submit"
onClick={handleSubmit}
>
{isPending ? "Sending..." : "Send Invitation"}
</Button>
<Button type="submit">Send Invitation</Button>
</DialogFooter>
</DialogContent>
</Dialog>
Expand Down
38 changes: 33 additions & 5 deletions src/app/dashboard/members/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,46 @@ import Heading from "@/components/heading";
import { Card, CardContent } from "@/components/ui/card";
import { DataTable } from "./data-table";
import { columns } from "./columns";
import { useQuery } from "convex/react";
import { api } from "@/api";

export default function Page() {
const invitations = useQuery(api.invitations.listInvitations);
const data = [
{
_id: "507f1f77bcf86cd799439011",
name: "Alice Johnson",
email: "[email protected]",
role: "user",
},
{
_id: "507f1f77bcf86cd799439012",
name: "Bob Smith",
email: "[email protected]",
role: "user",
},
{
_id: "507f1f77bcf86cd799439013",
name: "Charlie Brown",
email: "[email protected]",
role: "user",
},
{
_id: "507f1f77bcf86cd799439014",
email: "[email protected]",
role: "user",
},
{
_id: "507f1f77bcf86cd799439015",
email: "[email protected]",
role: "user",
status: "pending",
},
];

export default function Page() {
return (
<>
<Heading variant="h1">Members</Heading>
<Card className="border-none">
<CardContent className="p-0">
{invitations && <DataTable columns={columns} data={invitations} />}
{data && <DataTable columns={columns} data={data} />}
</CardContent>
</Card>
</>
Expand Down
Loading

0 comments on commit 16d7783

Please sign in to comment.