Skip to content

Commit e964818

Browse files
committed
continued dialog changes
1 parent 252101c commit e964818

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

app/classroom/[classroomId]/manage/buttons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export default function ClassroomManagementButtons({
245245

246246
{classroomInfo.Classroom_Members &&
247247
classroomInfo.Classroom_Members.length > 0 && (
248-
<MemberList classroom={classroomInfo} enableDeletion={true} />
248+
<MemberList classroom={classroomInfo} enableDeletion={true} userId={userAndClassData.userData.id}/>
249249
)}
250250
<p>Invite Member:</p>
251251
<InviteMember classroomId={classroomId} />

app/classroom/classroomList.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
DialogTrigger,
4141
} from "@/components/ui/dialog";
4242

43-
import { useSearchParams } from "next/navigation";
43+
import { useRouter, useSearchParams } from "next/navigation";
4444
import { toast } from "@/hooks/use-toast";
4545
import { Edit, LogOut, MessageSquareMore, Trash2, Users } from "lucide-react";
4646
import { SheetTrigger } from "@/components/ui/sheet";
@@ -50,6 +50,8 @@ import { Input } from "@/components/ui/input";
5050

5151
export default function ClassroomList() {
5252
const searchParams = useSearchParams();
53+
const router = useRouter();
54+
5355
const [isDialogOpen, setIsDialogOpen] = useState(false);
5456
const [newClassName, setNewClassName] = useState("");
5557

@@ -89,25 +91,27 @@ export default function ClassroomList() {
8991
setUserAndClassData(refreshedData);
9092
}
9193
};
92-
// useEffect(() => {
93-
// const joinedClassSuccess = searchParams.get("join_success");
94-
// if (joinedClassSuccess && !isNaN(Number(joinedClassSuccess))) {
95-
// const joinClassInfo = userAndClassData.classroomsData.find(
96-
// (x) => x.id === Number(joinedClassSuccess)
97-
// );
98-
// if (joinClassInfo) {
99-
// refreshClassrooms();
100-
// toast({
101-
// description: (
102-
// <div>
103-
// Successfully joined classroom
104-
// <span className="font-bold"> {joinClassInfo.name}</span>!
105-
// </div>
106-
// ),
107-
// duration: 10000,
108-
// });
109-
// }
110-
// }
94+
95+
const joinedClassSuccess = searchParams.get("join_success");
96+
if (joinedClassSuccess && !isNaN(Number(joinedClassSuccess))) {
97+
const joinClassInfo = userAndClassData.classroomsData.find(
98+
(x) => x.id === Number(joinedClassSuccess)
99+
);
100+
if (joinClassInfo) {
101+
// Join class doesn't need to refresh classroom data since we know its fresh
102+
// since it's coming from a redirect from a reoute
103+
toast({
104+
description: (
105+
<div>
106+
Successfully joined classroom
107+
<span className="font-bold"> {joinClassInfo.name}</span>!
108+
</div>
109+
),
110+
duration: 10000,
111+
});
112+
router.replace("/classroom", {scroll: false})
113+
}
114+
}
111115

112116
// const deleteClassSuccess = searchParams.get("delete_success");
113117

@@ -236,6 +240,7 @@ export default function ClassroomList() {
236240
</Tooltip>
237241
</TooltipProvider>
238242
}
243+
userId={userId}
239244
/>
240245
)}
241246
{!isAdmin && (

app/classroom/clientUtils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@ import {
1818
export const optimisticUpdateAndFetchClassroomData = async <
1919
K extends keyof ClassroomWithMembers,
2020
>(
21-
classroomId: number,
2221
action: () => Promise<unknown>,
23-
newValue: { [k in K]: ClassroomWithMembers[k] } | "remove",
22+
newValue: { [k in K]: ClassroomWithMembers[k] } | ClassroomWithMembers | "remove",
2423
setUserAndClassDataFunction: React.Dispatch<
2524
React.SetStateAction<UserWithClassroomsData>
2625
>,
26+
classroomId?: number,
2727
refreshFunction?: () => Promise<unknown>
2828
) => {
2929
setUserAndClassDataFunction((prevData) => ({
3030
userData: prevData.userData,
3131
classroomsData: prevData.classroomsData.flatMap((classroom) => {
32-
console.log(classroom.name);
3332
if (classroom.id === classroomId) {
3433
return newValue === "remove" ? [] : { ...classroom, ...newValue };
3534
}
3635
return classroom;
37-
}),
36+
}).concat(typeof newValue === 'object' && "id" in newValue ? newValue : [] ),
3837
}));
3938
const returnVal = await action();
4039
if (refreshFunction) {

app/classroom/memberList.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ import { getCurrentUserId, removeMember } from "./actions";
3030
*/
3131
export default function MemberList({
3232
classroom,
33+
userId,
3334
enableDeletion,
3435
triggerButton,
35-
}: {
36+
} : {
3637
classroom: ClassroomWithMembers;
38+
userId: string
39+
3740
enableDeletion: boolean;
3841
triggerButton?: ReactNode;
3942
}) {
40-
const [adminId, setAdminId] = useState<string | null>(null);
4143
const [members, setMembers] = useState<Tables<"Users">[]>([]);
4244

4345
useEffect(() => {
@@ -46,14 +48,6 @@ export default function MemberList({
4648
}
4749
}, [classroom.Classroom_Members]);
4850

49-
useEffect(() => {
50-
const fetchAdminId = async () => {
51-
const id = await getCurrentUserId();
52-
setAdminId(id);
53-
};
54-
55-
fetchAdminId();
56-
}, []); // Fetch once when the component mounts
5751

5852
if (!classroom.Classroom_Members) {
5953
return <h1>No members found!</h1>;
@@ -105,7 +99,7 @@ export default function MemberList({
10599
id: "actions",
106100
cell: ({ row }: { row: Row<Tables<"Users">> }) => {
107101
// const adminId = getCurrentUserId();
108-
const isAdmin = row.original.id === adminId; // Check if the current row is the admin
102+
const isAdmin = row.original.id === userId; // Check if the current row is the admin
109103

110104
// Only render the delete button if the current row is NOT the admin
111105
if (isAdmin) {

0 commit comments

Comments
 (0)