Skip to content

Commit

Permalink
fix: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPhoenix2704 committed Apr 21, 2024
1 parent bbed79e commit fb5fc66
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 49 deletions.
44 changes: 20 additions & 24 deletions app/events/ui/CurrentEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dayjs from "dayjs";
import { redirect } from "next/navigation";
import { CreateTeamModal } from "./modal/CreateTeamModal";
import { EventStatus, TeamMemberRole } from "@/utils/types";
import { UpdateTeamModal } from "./modal/UpdateTeamModal";

export const CurrentEvent = ({
user,
Expand All @@ -31,8 +32,12 @@ export const CurrentEvent = ({
team: {
id: string;
repo: string;
name: string;
eventId: string;
members: {
user: {
githubId: string;
};
role: string | null;
userId: string;
}[];
Expand All @@ -43,10 +48,13 @@ export const CurrentEvent = ({

const { event, team } = data;

const isEditable = team?.members.some(
(member) =>
member.userId === user?.id && member.role === TeamMemberRole.LEADER,
);
const isEditable =
(team?.members.some(
(member) =>
member.userId === user?.id && member.role === TeamMemberRole.LEADER,
) &&
event?.status === EventStatus.REGISTRATION) ||
false;

if (!event) {
redirect("/");
Expand All @@ -66,8 +74,8 @@ export const CurrentEvent = ({
? isEditable
? status === EventStatus.REGISTRATION
? `/events/?update=true&eventId=${event.id}`
: `/events/?view=true&eventId=${event.id}`
: `/events/?view=true&eventId=${event.id}`
: `/events/?update=true&eventId=${event.id}`
: `/events/?update=true&eventId=${event.id}`
: isProfileComplete
? status === EventStatus.REGISTRATION
? `/events/?register=true&eventId=${event.id}`
Expand All @@ -77,19 +85,12 @@ export const CurrentEvent = ({

return (
<div className="flex flex-col lg:flex-row w-full items-start gap-4">
{/* Modals (you'll likely need to keep these as React components due to state management) */}
{/* {team && isOpenUpdateModal && (
<UpdateTeamModal
teamId={team.teamID}
isOpen={isOpenUpdateModal}
onClose={onCloseUpdateModal}
image={imageWhite}
eventId={id}
isEditable={isEditable}
/>
{team && user && event && (
<UpdateTeamModal user={user} team={team} isEditable={isEditable} />
)}
{user && event?.status === EventStatus.REGISTRATION && (
<CreateTeamModal user={user} />
)}
*/}
{user && <CreateTeamModal user={user} />}

<div className="min-w-full lg:min-w-[50%] max-w-[50%] rounded-md bg-white/15 backdrop-blur-md flex flex-col">
<div className="flex justify-between items-center p-4">
Expand Down Expand Up @@ -142,12 +143,7 @@ export const CurrentEvent = ({
<button
type="button"
className={`bg-white hover:bg-primary active:bg-primary active:ring-2 active:ring-primary transition-all font-medium text-sm px-6 py-3 rounded-md ${
user &&
team &&
isEditable &&
status === EventStatus.REGISTRATION
? ""
: "bg-gray-400 cursor-not-allowed"
user && team
}`}
>
{user
Expand Down
2 changes: 0 additions & 2 deletions app/events/ui/modal/CreateTeamModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const CreateTeamModal = ({
if (isTeamLeadIncluded !== -1) {
data.members.splice(isTeamLeadIncluded, 1);
}

startTransition(() => {
createTeamWithBindings(data);
});
Expand Down Expand Up @@ -151,7 +150,6 @@ export const CreateTeamModal = ({
<div className="mt-6 flex justify-end">
<Button
onClick={() => {
console.log("clicked");
handleSubmit(onSubmit);
}}
loading={isSubmitting}
Expand Down
188 changes: 188 additions & 0 deletions app/events/ui/modal/UpdateTeamModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
"use client";

import { useSearchParams, usePathname, useRouter } from "next/navigation";

import * as Dialog from "@radix-ui/react-dialog";
import type { User } from "lucia";
import { startTransition, useEffect, useState } from "react";
import { Input } from "@/app/components/Input";
import { X } from "lucide-react";
import { Button } from "@/app/components/Button";
import type { z } from "zod";
import { FormProvider, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import createTeam from "./actions";
import { updateTeamSchema } from "@/utils/validateRequest";
import { Member } from "../Member";

type FormData = z.infer<typeof updateTeamSchema>;

export const UpdateTeamModal = ({
user,
team,
isEditable,
}: {
user: User;
team: {
id: string;
name: string;
repo: string;
eventId: string;
members: {
role: string | null;
userId: string;
user: {
githubId: string;
};
}[];
};
isEditable: boolean;
}) => {
const router = useRouter();

const [isOpen, setIsOpen] = useState(false);

const [eventID, setEventID] = useState<string | null>(null);

const searchParams = useSearchParams();
const eventId = searchParams.get("eventId");

const methods = useForm<FormData>({
resolver: zodResolver(updateTeamSchema),
});

const {
handleSubmit,
register,
setValue,
formState: { errors, isSubmitting, isDirty, isValid },
} = methods;

useEffect(() => {
setEventID(searchParams.get("eventId"));

const isOpen = !!searchParams.get("update") && !!team && !!eventID;
setIsOpen(isOpen);

if (isOpen) {
setValue("name", team.name);
setValue("repo", team.repo);
setValue(
"members",
team.members.map((member) => member.user.githubId),
);
}
}, [searchParams, eventID, team, setValue]);

const pathName = usePathname();
const onOpenChange = () => {
router.push(pathName);
};

const createTeamWithBindings = createTeam.bind(null, user.id, eventID);

const onSubmit = async (data: FormData) => {
const isTeamLeadIncluded = data.members.findIndex(
(member) => member.toLowerCase() === user.githubId.toLowerCase(),
);

if (isTeamLeadIncluded !== -1) {
data.members.splice(isTeamLeadIncluded, 1);
}
};

return (
<Dialog.Root open={isOpen} onOpenChange={onOpenChange}>
<Dialog.Portal>
<Dialog.Overlay className="fixed transition-all animate-overlayShow ease-in-out inset-0 z-40 bg-black/70" />
<Dialog.Content
className="fixed max-w-[1000px] md:h-auto w-full h-full md:w-5/6 top-1/2 z-50 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-secondary text-white animate-contentShow rounded-lg p-8 min-w-container"
onCloseAutoFocus={(e) => e.preventDefault()}
>
<div className="flex justify-end ">
<span className="border-2 rounded-full border-red">
<X
aria-label="Close Modal"
className="cursor-pointer text-red"
onClick={onOpenChange}
/>
</span>
</div>

<Dialog.Title className="text-4xl font-bold">
<div className="flex justify-between"> Team Details</div>
</Dialog.Title>
<Dialog.Description className="text-white/40 mb-6 text-xs">
<div className="bg-green/15 rounded-md text-green p-1">
Your Team Members will appear here once they accept your team
invitation
</div>

<div className="bg-red/15 mt-1 rounded-md text-red p-1">
Teams should have a leader and atleast 1 member
</div>
{errors.members && (
<div className="bg-red/15 mt-1 rounded-md text-red p-1">
{errors.members.message}
</div>
)}
</Dialog.Description>
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col items-center md:flex-row gap-4 md:gap-8">
<div className="flex flex-col w-full gap-4">
<div>
<label htmlFor="name" className="pb-2">
Team Name
</label>
<Input
type="text"
required
disabled
spellCheck="false"
placeholder="Team name"
{...register("name")}
/>
</div>
<div>
<label htmlFor="repo" className="mb-2">
Github Repository
</label>
<Input
type="text"
id="repo"
placeholder="www.github.com/example/exampleRepo"
disabled
{...register("repo")}
/>
</div>
</div>
<div className="w-full">
<Member isEditable={isEditable} loading={isSubmitting} />
{errors.members && (
<div className="bg-red/15 mt-2 text-red p-1 rounded-md w-full">
User not found or team should have at least 1 member
</div>
)}
</div>
</div>

<div className="mt-6 flex justify-end">
<Button
onClick={() => {
handleSubmit(onSubmit);
}}
loading={isSubmitting}
type="submit"
className="w-64 text-secondary bg-white font-semibold py-2 px-4 rounded"
>
{isEditable ? "Update Team" : "Close"}
</Button>
</div>
</form>
</FormProvider>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
);
};
20 changes: 19 additions & 1 deletion app/events/ui/modal/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,27 @@ export default async function createTeam(
const admin = await db.user.findUnique({
where: {
id: userId,
}
})

if(!admin) {
return "User not found!";
}


const teamLead = await db.teamMember.findUnique({
where: {
userId_eventId: {
userId: userId,
eventId: eventId,
},
},
});

if (teamLead) {
return "You are already in a Team!";
}

const userIDs = await db.user.findMany({
where: {
githubId: {
Expand Down Expand Up @@ -110,7 +128,7 @@ export default async function createTeam(
teamID: team.id,
},
"Welcome to this week's Saturday Hack Night! 🎉",
admin?.email || "",
admin.email,
),
);

Expand Down
Loading

0 comments on commit fb5fc66

Please sign in to comment.