Skip to content

Commit 6eb53d0

Browse files
🔧 fix: Update project switching logic
- Added current project ID tracking and project switching functionality in Dashboard and TeamSwitcher These updates ensure a more dynamic and seamless user experience when interacting with different projects within the dashboard
1 parent ff15305 commit 6eb53d0

3 files changed

Lines changed: 66 additions & 38 deletions

File tree

‎app/(dashboard)/app/Uploads.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const Upload = ({ upload }: { upload: Upload }) => {
158158

159159
export const UploadSkeleton = ({ quantity }: { quantity: number }) => {
160160
return Array.from({ length: quantity }).map((_, i) => (
161-
<div className="group animate-pulse">
161+
<div className="group animate-pulse" key={i}>
162162
<div className="relative aspect-w-16 aspect-h-9">
163163
<div
164164
className="w-full h-40 bg-gray-300 dark:bg-gray-800"

‎app/(dashboard)/app/page.tsx‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import DashboardStatistics from "@/components/DashboardStatistics";
99
import { IconShareUploadButton } from "@/app/(view)/view/[id]/ShareUploadButton";
1010
import { redirect } from "next/navigation";
1111
import Uploads from "./Uploads";
12+
import { prisma } from "@/app/utils";
1213

1314
export default async function Dashboard() {
1415
const session = await getServerSession(authOptions);
1516
if (!session) {
1617
redirect("/signin?redirect=/app");
1718
}
18-
19-
const prisma = new PrismaClient();
2019
// @ts-ignore
2120
const userId = session?.user?.id;
21+
// @ts-ignore
22+
const currentProjectId = session?.user?.currentProjectId;
2223
if (!userId) return redirect("/signin");
2324

2425
const projects = await prisma.project.findMany({
@@ -32,16 +33,41 @@ export default async function Dashboard() {
3233
},
3334
});
3435

35-
// TODO: implement some logic behind changing projectId under user column
36-
// const changeProject = async (projectIdToChangeTo: string) => {
37-
// "use server";
38-
// };
36+
const changeProject = async (projectIdToChangeTo: string) => {
37+
"use server";
38+
39+
const canUserSwitchToProject = await prisma.projectUsers.findFirst({
40+
where: {
41+
projectId: projectIdToChangeTo,
42+
userId,
43+
},
44+
});
45+
46+
if (!canUserSwitchToProject) {
47+
throw new Error("You do not have access to this project");
48+
}
49+
50+
await prisma.user.update({
51+
where: {
52+
id: userId,
53+
},
54+
data: {
55+
currentProjectId: projectIdToChangeTo,
56+
},
57+
});
58+
return;
59+
};
3960

4061
return (
4162
<section className="relative">
4263
<div className="relative max-w-6xl mx-auto px-4 sm:px-6">
4364
<div className="pt-12 md:pt-20 ">
44-
<TeamSwitcher projects={projects} className="mb-3" />
65+
<TeamSwitcher
66+
projects={projects}
67+
className="mb-3"
68+
currentProjectId={currentProjectId}
69+
changeProject={changeProject}
70+
/>
4571
<DashboardStatistics />
4672
<div
4773
key="1"

‎components/TeamSwitcher.tsx‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,58 @@ import {
2020
CommandSeparator,
2121
} from "@components/ui/command";
2222
import {
23-
Dialog,
24-
DialogContent,
25-
DialogDescription,
26-
DialogFooter,
27-
DialogHeader,
28-
DialogTitle,
29-
DialogTrigger,
23+
Dialog
3024
} from "@components/ui/dialog";
31-
import { Input } from "@components/ui/input";
32-
import { Label } from "@components/ui/label";
3325
import {
3426
Popover,
3527
PopoverContent,
3628
PopoverTrigger,
3729
} from "@components/ui/popover";
38-
import {
39-
Select,
40-
SelectContent,
41-
SelectItem,
42-
SelectTrigger,
43-
SelectValue,
44-
} from "@components/ui/select";
4530
import { Project } from "@prisma/client";
4631
import { toast } from "sonner";
4732
import { Settings, Users } from "lucide-react";
4833
import Link from "next/link";
49-
import { revalidatePath } from "next/cache";
34+
import { signIn } from "next-auth/react";
5035

5136
type PopoverTriggerProps = React.ComponentPropsWithoutRef<
5237
typeof PopoverTrigger
5338
>;
5439

5540
interface TeamSwitcherProps extends PopoverTriggerProps {
5641
projects: Project[];
42+
currentProjectId: string;
43+
changeProject: (projectId: string) => Promise<void>;
5744
}
5845

5946
export default function TeamSwitcher({
6047
className,
6148
projects,
49+
currentProjectId,
50+
changeProject,
6251
}: TeamSwitcherProps) {
6352
const [open, setOpen] = React.useState(false);
6453
const [showNewProjectDialog, setShowNewProjectDialog] = React.useState(false);
6554
const [selectedProject, setSelectedProject] = React.useState<Project>(
66-
projects[0] ?? { id: 0 }
55+
projects.find((project) => project.id === currentProjectId) ?? projects[0]
6756
);
6857

58+
const handleProjectChange = (project: Project) => {
59+
toast.promise(changeProject(project.id), {
60+
loading: "Switching project...",
61+
success() {
62+
setSelectedProject(project);
63+
// @ts-ignore
64+
signIn(null);
65+
return "Project switched. Refreshing page...";
66+
},
67+
error(errorMessage: string) {
68+
return `Failed to switch project ${errorMessage}`;
69+
},
70+
});
71+
72+
setOpen(false);
73+
};
74+
6975
const CurrentProject = () => {
7076
return (
7177
<CommandGroup key={selectedProject.id}>
@@ -139,13 +145,8 @@ export default function TeamSwitcher({
139145
.map((project) => (
140146
<CommandItem
141147
key={project.id}
142-
onSelect={() => {
143-
setSelectedProject(project);
144-
setOpen(false);
145-
revalidatePath('/app')
146-
// changeProject(project.id);
147-
}}
148-
className="text-sm"
148+
onSelect={() => handleProjectChange(project)}
149+
className="text-sm cursor-pointer"
149150
>
150151
<Avatar className="mr-2 h-5 w-5">
151152
<AvatarImage
@@ -168,8 +169,8 @@ export default function TeamSwitcher({
168169
))}
169170
</CommandGroup>
170171
</CommandList>
171-
<CommandSeparator />
172-
<CommandList>
172+
{/* <CommandSeparator /> */}
173+
{/* <CommandList>
173174
<CommandGroup>
174175
<DialogTrigger asChild>
175176
<CommandItem
@@ -183,11 +184,12 @@ export default function TeamSwitcher({
183184
</CommandItem>
184185
</DialogTrigger>
185186
</CommandGroup>
186-
</CommandList>
187+
</CommandList> */}
187188
</Command>
188189
</PopoverContent>
189190
</Popover>
190-
<DialogContent className="p-4">
191+
{/* TODO: Add logic to support creating new team */}
192+
{/* <DialogContent className="p-4">
191193
<DialogHeader>
192194
<DialogTitle>Create team</DialogTitle>
193195
<DialogDescription>
@@ -243,7 +245,7 @@ export default function TeamSwitcher({
243245
Continue
244246
</Button>
245247
</DialogFooter>
246-
</DialogContent>
248+
</DialogContent> */}
247249
</Dialog>
248250
);
249251
}

0 commit comments

Comments
 (0)