Skip to content

Commit

Permalink
fix returnValidationErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasilva414 committed Sep 27, 2024
1 parent 643c50e commit abbcf50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
16 changes: 10 additions & 6 deletions apps/web/src/actions/teams.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
"use server";

// TODO: update team /api endpoints to be actions

import { authenticatedAction } from "@/lib/safe-action";
import { z } from "zod";
import { boolean, string, z } from "zod";
import { db } from "db";
import { userHackerData, teams, invites } from "db/schema";
import { eq } from "db/drizzle";
import { revalidatePath } from "next/cache";
import { getHacker } from "db/functions";
import { returnValidationErrors } from "next-safe-action";

export const leaveTeam = authenticatedAction.action(
async ({ ctx: { userId } }) => {
export const leaveTeam = authenticatedAction
.outputSchema(
z.object({
success: z.boolean(),
message: z.string(),
}),
)
.action(async ({ ctx: { userId } }) => {
const user = await getHacker(userId, false);
if (!user)
returnValidationErrors(z.null(), { _errors: ["User not found"] });
Expand Down Expand Up @@ -79,5 +84,4 @@ export const leaveTeam = authenticatedAction.action(
});

return result;
},
);
});
25 changes: 16 additions & 9 deletions apps/web/src/components/dash/team/LeaveTeamButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@ interface LeaveTeamButtonProps {

export default function LeaveTeamButton({ issueEmail }: LeaveTeamButtonProps) {
const { execute: runLeaveTeam, status } = useAction(leaveTeam, {
onSuccess: ({ success, message }) => {
toast.dismiss();
if (success) {
toast.success(message);
onSuccess: ({ data }) => {
if (data) {
if (data.success) {
toast.success(data.message);
} else {
toast.error(data.message);
}
} else {
toast.error(message);
toast.dismiss();
toast.error(
`An unknown error occured. If this persists, please email ${issueEmail}.`,
);
}
toast.dismiss();
},
onError: (error) => {
onError: ({ error }) => {
toast.dismiss();
toast.error(
`An unknown error occured. If this persists, please email ${issueEmail}.`,
);
console.error("Fetch Error: ", error.fetchError);
// console.error("Fetch Error: ", error.fetchError);
console.error("Server Error: ", error.serverError);
console.error("Validation Error: ", error.validationError);
console.error("Validation Error: ", error.validationErrors);
},
});

function leave() {
toast.loading("Leaving team...", {
duration: 0,
});
runLeaveTeam(null);
runLeaveTeam();
}

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/rsvp/ConfirmDialogue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function ConfirmDialogue({ hasRsvped }: { hasRsvped: boolean }) {
</p>
<Button
onClick={() => {
execute(null);
execute();
toast.loading("Confirming your RSVP...", {
duration: 0,
});
Expand Down

0 comments on commit abbcf50

Please sign in to comment.