Skip to content

Commit

Permalink
Create API route returns event id; successful event creation redirect…
Browse files Browse the repository at this point in the history
…s to event page; added protection against create event spam while redirecting
  • Loading branch information
BK2004 committed Sep 8, 2024
1 parent 57d0945 commit 5962921
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/app/manage/[clubId]/create/CreateEventForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const CreateEventForm = ({ clubId, officerClubs }: { clubId: string, officerClub
});
const router = useRouter();
const [watchDescription, watchStartTime] = watch(['description', 'startTime']);
const [redirecting, setRedirecting] = useState(false);
const [eventPreview, setEventPreview] = useState<RouterOutputs['event']['findByFilters']['events'][number]>({
name: "",
clubId,
Expand Down Expand Up @@ -65,11 +66,14 @@ const CreateEventForm = ({ clubId, officerClubs }: { clubId: string, officerClub
}, [router, watch, officerClubs]);

const createMutation = api.event.create.useMutation({
onSuccess: () => { location.reload(); }
onSuccess: (data) => { if (data) {
setRedirecting(true);
router.push(`/event/${data}`);
} }
})

const onSubmit = handleSubmit((data: z.infer<typeof createEventSchema>) => {
if (!createMutation.isPending) {
if (!createMutation.isPending && !redirecting) {
createMutation.mutate(data);
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/server/api/routers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ export const eventRouter = createTRPCRouter({
throw new TRPCError({ code: 'UNAUTHORIZED' });
}

await ctx.db.insert(events).values({ ...input });
const res = await ctx.db.insert(events).values({ ...input }).returning({ id: events.id });
if (res.length == 0) throw "Failed to add event";
return res[0]?.id;
}),
byName: publicProcedure.input(byNameSchema).query(async ({ input, ctx }) => {
const { name, sortByDate } = input;
Expand Down

0 comments on commit 5962921

Please sign in to comment.