Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat/ui: delete team

Aashish-Upadhyay-101e22523c57a94
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
index 538dc096..579d31cd 100644
--- a/components/Sidebar.tsx
+++ b/components/Sidebar.tsx
@@ -9,16 +9,13 @@ import MenuIcon from "@/components/shared/icons/menu";
import ChevronUp from "@/components/shared/icons/chevron-up";
import X from "@/components/shared/icons/x";
import Link from "next/link";
-import { cn, daysLeft } from "@/lib/utils";
+import { cn } from "@/lib/utils";
import { useRouter } from "next/router";
import { ModeToggle } from "./theme-toggle";
-import { Button } from "./ui/button";
-import { CustomUser } from "@/lib/types";
import LoadingSpinner from "./ui/loading-spinner";
import Banner from "./banner";
import SelectTeam from "./teams/select-team";
import { TeamContextType, initialState, useTeam } from "@/context/team-context";
-import { AddTeamModal } from "./teams/add-team-modal";
export default function Sidebar() {
const { data: session, status } = useSession();
diff --git a/components/teams/delete-team-modal.tsx b/components/teams/delete-team-modal.tsx
new file mode 100644
index 00000000..51719ff3
--- /dev/null
+++ b/components/teams/delete-team-modal.tsx
@@ -0,0 +1,107 @@
+import { usePlausible } from "next-plausible";
+import { useRouter } from "next/router";
+import { useRef, useState } from "react";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+import { Button } from "../ui/button";
+import { Input } from "../ui/input";
+import { Label } from "../ui/label";
+import { toast } from "sonner";
+import { useTeam } from "@/context/team-context";
+import { mutate } from "swr";
+
+export function DeleteTeamModal({ children }: { children: React.ReactNode }) {
+ const router = useRouter();
+ const plausible = usePlausible();
+ const [teamName, setTeamName] = useState<string>("");
+ const [loading, setLoading] = useState<boolean>(false);
+ const teamInfo = useTeam();
+
+ const handleDeleteTeam = async (event: React.FormEvent<HTMLFormElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+
+ if (!teamName) return;
+
+ if (teamName.trim() !== teamInfo?.currentTeam?.name) {
+ toast.error("Team name doesn't match");
+ return;
+ }
+
+ setLoading(true);
+ const response = await fetch(`/api/teams/${teamInfo?.currentTeam?.id}`, {
+ method: "DELETE",
+ });
+
+ if (response.status !== 204) {
+ const error = await response.json();
+ toast.error(error);
+ setLoading(false);
+ return;
+ }
+
+ toast.success("Team deleted successfully!");
+ await mutate("/api/teams");
+ setLoading(false);
+ router.push("/documents");
+ };
+
+ return (
+ <Dialog>
+ <DialogTrigger asChild>{children}</DialogTrigger>
+ <DialogContent
+ className="text-foreground bg-background"
+ onClick={(e) => e.stopPropagation()}>
+ <DialogHeader>
+ <DialogTitle>Delete Team</DialogTitle>
+ <DialogDescription className="py-2 mb-1 text-sm text-muted-foreground">
+ Warning: This will permanently delete your team, custom domain, and
+ all associated documents and their respective stats.
+ </DialogDescription>
+ </DialogHeader>
+ <form
+ onSubmit={handleDeleteTeam}
+ className="flex flex-col gap-8 mt-4"
+ onClick={(e) => e.stopPropagation()}>
+ <div className="flex flex-col gap-3">
+ <div>
+ <Label htmlFor="team">
+ Enter the team name{" "}
+ <strong className="font-extrabold italic">
+ {teamInfo?.currentTeam?.name}
+ </strong>{" "}
+ to continue:
+ </Label>
+ </div>
+ <div>
+ <Input
+ id="team"
+ placeholder="Enter team's name"
+ onChange={(e) => setTeamName(e.target.value)}
+ />
+ </div>
+ </div>
+ <DialogFooter className="flex justify-center">
+ <Button
+ type="submit"
+ variant={"destructive"}
+ className="w-full lg:w-1/2"
+ loading={loading}
+ // disabled={uploading || !currentFile}
+ >
+ {loading ? "Deleting..." : "Delete Team"}
+ {/* {uploading ? "Uploading..." : "Upload Document"} */}
+ </Button>
+ </DialogFooter>
+ </form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/context/team-context.tsx b/context/team-context.tsx
index 498a8c4e..b1d71dcb 100644
--- a/context/team-context.tsx
+++ b/context/team-context.tsx
@@ -1,8 +1,6 @@
-import { useGetTeam } from "@/lib/swr/use-team";
+import { createContext, useContext, useMemo, useState } from "react";
import { useTeams } from "@/lib/swr/use-teams";
-import { CustomUser, Team } from "@/lib/types";
-import { useSession } from "next-auth/react";
-import { createContext, useContext, useEffect, useMemo, useState } from "react";
+import { Team } from "@/lib/types";
interface TeamContextProps {
children: React.ReactNode;
diff --git a/lib/swr/use-team.ts b/lib/swr/use-team.ts
index 46386f11..30497607 100644
--- a/lib/swr/use-team.ts
+++ b/lib/swr/use-team.ts
@@ -6,15 +6,11 @@ import { TeamDetail } from "@/lib/types";
export function useGetTeam() {
const teamInfo = useTeam();
- if (!teamInfo?.currentTeam?.id) {
- return;
- }
-
const { data: team, error } = useSWR<TeamDetail>(
`/api/teams/${teamInfo?.currentTeam?.id}`,
fetcher,
{
- dedupingInterval: 10000,
+ dedupingInterval: 20000,
}
);
diff --git a/pages/api/teams/[teamId]/index.ts b/pages/api/teams/[teamId]/index.ts
index 35ee2d1d..dcdd93ab 100644
--- a/pages/api/teams/[teamId]/index.ts
+++ b/pages/api/teams/[teamId]/index.ts
@@ -110,7 +110,7 @@ export default async function handle(
return res.status(204).end();
} catch (error) {
- return res.status(500).json({ message: (error as Error).message });
+ return res.status(500).json((error as Error).message);
}
} else {
// We only allow GET and DELETE requests
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 4b90ecd3..69489970 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -72,7 +72,6 @@ export default async function handle(
const session = await getServerSession(req, res, authOptions);
const { teamId } = req.query as { teamId: string };
- console.log(teamId, "teamId found");
if (!session) {
res.redirect(`/login?next=/api/teams/${teamId}/invite`);
@@ -103,6 +102,8 @@ export default async function handle(
},
},
});
+ console.log(team);
+
return res.redirect("/documents");
} catch (error) {
console.log(error);
diff --git a/pages/settings/general.tsx b/pages/settings/general.tsx
index 595786cc..44e35238 100644
--- a/pages/settings/general.tsx
+++ b/pages/settings/general.tsx
@@ -1,5 +1,6 @@
import AppLayout from "@/components/layouts/app";
import Navbar from "@/components/settings/navbar";
+import { DeleteTeamModal } from "@/components/teams/delete-team-modal";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useTeam } from "@/context/team-context";
@@ -81,9 +82,11 @@ export default function General() {
undone - please proceed with caution.
</p>
</div>
- <Button variant={"destructive"} size={"lg"}>
- Delete Team
- </Button>
+ <DeleteTeamModal>
+ <Button variant={"destructive"} size={"lg"}>
+ Delete Team
+ </Button>
+ </DeleteTeamModal>
</div>
</div>
</div>
diff --git a/pages/settings/people.tsx b/pages/settings/people.tsx
index 36bd57c5..42ffba60 100644
--- a/pages/settings/people.tsx
+++ b/pages/settings/people.tsx
@@ -29,7 +29,7 @@ export default function Billing() {
const { data: session } = useSession();
const { team, loading } = useGetTeam()!;
- const teaminfo = useTeam();
+ const teamInfo = useTeam();
const { teams } = useTeams();
const router = useRouter();
@@ -75,11 +75,13 @@ export default function Billing() {
return;
}
- await mutate(`/api/teams/${teamId}`);
+ await mutate(`/api/teams/${teamInfo?.currentTeam?.id}`);
+ await mutate("/api/teams");
+
setLeavingUserId("");
if (isCurrentUser(userId)) {
- toast.success(`Successfully leaved team ${teaminfo?.currentTeam?.name}`);
- teaminfo?.setCurrentTeam({ id: teams![0].id });
+ toast.success(`Successfully leaved team ${teamInfo?.currentTeam?.name}`);
+ teamInfo?.setCurrentTeam({ id: teams![0].id });
router.push("/documents");
return;
}