Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/actions/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { adminAction } from "@/lib/server/safe-action";
import { z } from "zod";
import { db } from "@/db";
import { suborgs, cohorts } from "@/db/schema";
import { suborgs, teams } from "@/db/schema";
import { eq } from "drizzle-orm";
import { nanoid } from "nanoid";

Expand All @@ -29,7 +29,7 @@ export const createSuborg = adminAction
return suborg[0];
});

export const createCohort = adminAction
export const createTeam = adminAction
.schema(
z.object({
suborgSlug: z.string().min(1),
Expand All @@ -49,8 +49,8 @@ export const createCohort = adminAction
throw new Error("Suborg not found");
}

const cohort = await db
.insert(cohorts)
const team = await db
.insert(teams)
.values({
id: nanoid(8),
name,
Expand All @@ -60,5 +60,5 @@ export const createCohort = adminAction
})
.returning();

return cohort[0];
return team[0];
});
72 changes: 36 additions & 36 deletions src/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use server";

import { z } from "zod";
import { userToCohorts } from "@/db/schema";
import { userToTeams } from "@/db/schema";
import { revalidatePath } from "next/cache";
import { authedAction } from "@/lib/server/safe-action";
import { and, eq } from "drizzle-orm";
Expand All @@ -11,91 +11,91 @@ import { UserSettingsSchema } from "@/lib/zod";
import { user } from "@/db/schema";


// Schema for joining a cohort
const joinCohortSchema = z.object({
cohortId: z.string(),
// Schema for joining a team
const joinTeamSchema = z.object({
teamId: z.string(),
});

// Action to join a cohort
export const joinCohort = authedAction
.schema(joinCohortSchema)
// Action to join a team
export const joinTeam = authedAction
.schema(joinTeamSchema)
.action(async ({ ctx, parsedInput }) => {
const { cohortId } = parsedInput;
const { teamId } = parsedInput;
const userId = ctx.session.user.id;

try {
// Check if user is already in the cohort
const existingLink = await db.query.userToCohorts.findFirst({
// Check if user is already in the team
const existingLink = await db.query.userToTeams.findFirst({
where: (link, { eq, and }) =>
and(eq(link.userId, userId), eq(link.cohortId, cohortId)),
and(eq(link.userId, userId), eq(link.teamId, teamId)),
});

if (existingLink) {
return {
success: false,
message: "Already a member of this cohort",
message: "Already a member of this team",
};
}

// Add user to cohort
await db.insert(userToCohorts).values({
// Add user to team
await db.insert(userToTeams).values({
userId,
cohortId,
teamId,
});

// Revalidate the settings page to reflect the changes
revalidatePath("/settings");

return { success: true, message: "Successfully joined cohort" };
return { success: true, message: "Successfully joined team" };
} catch (error) {
console.error("Error joining cohort:", error);
return { success: false, message: "Failed to join cohort" };
console.error("Error joining team:", error);
return { success: false, message: "Failed to join team" };
}
});

// Schema for leaving a cohort
const leaveCohortSchema = z.object({
cohortId: z.string(),
// Schema for leaving a team
const leaveTeamSchema = z.object({
teamId: z.string(),
});

// Action to leave a cohort
export const leaveCohort = authedAction
.schema(leaveCohortSchema)
// Action to leave a team
export const leaveTeam = authedAction
.schema(leaveTeamSchema)
.action(async ({ ctx, parsedInput }) => {
const { cohortId } = parsedInput;
const { teamId } = parsedInput;
const userId = ctx.session.user.id;

try {
// Check if user is in the cohort
const existingLink = await db.query.userToCohorts.findFirst({
// Check if user is in the team
const existingLink = await db.query.userToTeams.findFirst({
where: (link, { eq, and }) =>
and(eq(link.userId, userId), eq(link.cohortId, cohortId)),
and(eq(link.userId, userId), eq(link.teamId, teamId)),
});

if (!existingLink) {
return {
success: false,
message: "Not a member of this cohort",
message: "Not a member of this team",
};
}

// Remove user from cohort
// Remove user from team
await db
.delete(userToCohorts)
.delete(userToTeams)
.where(
and(
eq(userToCohorts.userId, userId),
eq(userToCohorts.cohortId, cohortId),
eq(userToTeams.userId, userId),
eq(userToTeams.teamId, teamId),
),
);

// Revalidate the settings page to reflect the changes
revalidatePath("/settings");

return { success: true, message: "Successfully left cohort" };
return { success: true, message: "Successfully left team" };
} catch (error) {
console.error("Error leaving cohort:", error);
return { success: false, message: "Failed to leave cohort" };
console.error("Error leaving team:", error);
return { success: false, message: "Failed to leave team" };
}
});

Expand Down
20 changes: 10 additions & 10 deletions src/app/(core)/admin/teams/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { Plus } from "lucide-react";
import { createSuborg, createCohort } from "@/actions/admin";
import { createSuborg, createTeam } from "@/actions/admin";
import { toast } from "sonner";
import { useAction } from "next-safe-action/hooks";

Expand Down Expand Up @@ -66,7 +66,7 @@ export function CreateSuborgDialog() {
<DialogTitle>Create new suborg</DialogTitle>
<DialogDescription>
Create a new suborg below! Note that this is not the
same as making a cohort.
same as making a team.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4 py-4">
Expand Down Expand Up @@ -107,7 +107,7 @@ interface Suborg {
shortname: string;
}

export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
export function CreateTeamDialog({ suborgs }: { suborgs: Suborg[] }) {
const [open, setOpen] = useState(false);
const [suborgSlug, setSuborgSlug] = useState("");
const [name, setName] = useState("");
Expand All @@ -116,7 +116,7 @@ export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
const [isSubmitting, setIsSubmitting] = useState(false);
const router = useRouter();

const { executeAsync } = useAction(createCohort);
const { executeAsync } = useAction(createTeam);

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
Expand All @@ -131,7 +131,7 @@ export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
});

if (result && result.data) {
toast.success("Cohort created successfully!");
toast.success("Team created successfully!");
setOpen(false);
setSuborgSlug("");
setName("");
Expand All @@ -152,14 +152,14 @@ export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
<DialogTrigger asChild>
<Button>
<Plus className="mr-2 h-4 w-4" />
Create Cohort
Create Team
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Create new cohort</DialogTitle>
<DialogTitle>Create new team</DialogTitle>
<DialogDescription>
Create a new cohort for a suborg.
Create a new team for a suborg.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4 py-4">
Expand All @@ -183,7 +183,7 @@ export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
</select>
</div>
<div className="space-y-2">
<Label htmlFor="name">Cohort Name</Label>
<Label htmlFor="name">Team Name</Label>
<Input
id="name"
value={name}
Expand Down Expand Up @@ -214,7 +214,7 @@ export function CreateCohortDialog({ suborgs }: { suborgs: Suborg[] }) {
</div>
<DialogFooter>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Creating..." : "Create Cohort"}
{isSubmitting ? "Creating..." : "Create Team"}
</Button>
</DialogFooter>
</form>
Expand Down
28 changes: 14 additions & 14 deletions src/app/(core)/admin/teams/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { CreateSuborgDialog, CreateCohortDialog } from "./client";
import { CreateSuborgDialog, CreateTeamDialog } from "./client";

export default async function Page() {
const session = await getSession();
Expand All @@ -35,11 +35,11 @@ export default async function Page() {
<div className="space-y-6">
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<h2 className="text-2xl font-bold tracking-tight">
Cohorts
Teams
</h2>
<CreateCohortDialog suborgs={suborgs} />
<CreateTeamDialog suborgs={suborgs} />
</div>
<CohortShowcase />
<TeamShowcase />
</div>
</div>
);
Expand Down Expand Up @@ -70,8 +70,8 @@ async function SuborgShowcase() {
);
}

async function CohortShowcase() {
const cohorts = await db.query.cohorts.findMany();
async function TeamShowcase() {
const teams = await db.query.teams.findMany();

return (
<div>
Expand All @@ -84,26 +84,26 @@ async function CohortShowcase() {
</TableRow>
</TableHeader>
<TableBody>
{cohorts && cohorts.length > 0 ? (
cohorts.map((cohort) => (
<TableRow key={cohort.id}>
<TableCell>{cohort.name}</TableCell>
<TableCell>{cohort.suborgSlug}</TableCell>
{teams && teams.length > 0 ? (
teams.map((team) => (
<TableRow key={team.id}>
<TableCell>{team.name}</TableCell>
<TableCell>{team.suborgSlug}</TableCell>
<TableCell>
{new Date(
cohort.startDate,
team.startDate,
).toLocaleDateString()}{" "}
-{" "}
{new Date(
cohort.endDate,
team.endDate,
).toLocaleDateString()}
</TableCell>
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={3} className="text-center">
No cohorts found
No team found
</TableCell>
</TableRow>
)}
Expand Down
Loading
Loading