Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap server actions in sentry HOF; add custom global-error files #306

Merged
merged 12 commits into from
Apr 9, 2024
5 changes: 5 additions & 0 deletions config/eslint/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const config = {
"@typescript-eslint/require-await": "off",
"no-restricted-syntax": [
"error",
{
selector:
"CallExpression[callee.name='defineServerAction'] > :nth-child(1):not(FunctionExpression[id.name][async=true])",
message: "You can only pass named, async functions into defineServerAction",
},
{
selector: "TryStatement > .block CallExpression[callee.name='redirect']",
message:
Expand Down
41 changes: 15 additions & 26 deletions core/app/c/[communitySlug]/members/[[...add]]/MemberInviteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Loader2, Mail, UserPlus } from "ui/icon";
import { Input } from "ui/input";
import { toast } from "ui/use-toast";

import { didSucceed, useServerAction } from "~/lib/serverActions";
import * as actions from "./actions";
import { MemberFormState } from "./AddMember";
import { memberInviteFormSchema } from "./memberInviteFormSchema";
Expand All @@ -38,6 +39,8 @@ export const MemberInviteForm = ({
state: MemberFormState;
email?: string;
}) => {
const runCreateUserWithMembership = useServerAction(actions.createUserWithMembership);
const runAddMember = useServerAction(actions.addMember);
const [isPending, startTransition] = useTransition();
const router = useRouter();

Expand Down Expand Up @@ -71,54 +74,40 @@ export const MemberInviteForm = ({
return;
}

const { error } = await actions.createUserWithMembership({
const result = await runCreateUserWithMembership({
email: data.email,
firstName: data.firstName!,
lastName: data.lastName!,
community,
canAdmin: Boolean(data.canAdmin),
});

if (error) {
if (didSucceed(result)) {
toast({
title: "Error",
description: error,
variant: "destructive",
title: "Success",
description: "User successfully invited",
});
return;
closeForm();
}

toast({
title: "Success",
description: "User successfully invited",
});
closeForm();

return;
}

const result = await actions.addMember({
const result = await runAddMember({
user: state.user,
canAdmin: data.canAdmin,
community,
});

if ("error" in result) {
if (didSucceed(result)) {
toast({
title: "Error",
description: `Failed to add member. ${result.error}`,
variant: "destructive",
title: "Success",
description: "Member added successfully",
});
return;
}

toast({
title: "Success",
description: "Member added successfully",
});

// navigate away from the add page to the normal member page
closeForm();
// navigate away from the add page to the normal member page
closeForm();
}
}

const debouncedEmailCheck = useDebouncedCallback(async (email: string) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import type { Community, Member, User } from "@prisma/client";
import type { Community } from "@prisma/client";

import {
AlertDialog,
Expand All @@ -16,6 +16,7 @@ import { Trash } from "ui/icon";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "ui/tooltip";
import { toast } from "ui/use-toast";

import { didSucceed, useServerAction } from "~/lib/serverActions";
import * as actions from "./actions";
import { TableMember } from "./getMemberTableColumns";

Expand All @@ -26,6 +27,7 @@ export const RemoveMemberButton = ({
member: TableMember;
community: Community;
}) => {
const runRemoveMember = useServerAction(actions.removeMember);
return (
<AlertDialog>
<TooltipProvider>
Expand Down Expand Up @@ -56,20 +58,14 @@ export const RemoveMemberButton = ({
<Button asChild variant="destructive">
<AlertDialogAction
onClick={async () => {
const response = await actions.removeMember({ member, community });
if ("error" in response) {
const response = await runRemoveMember({ member, community });
if (didSucceed(response)) {
toast({
title: "Error",
description: response.error,
variant: "destructive",
title: "Success",
description: "Member successfully removed",
variant: "default",
});
return;
}
toast({
title: "Success",
description: "Member successfully removed",
variant: "default",
});
}}
>
<Trash size="16" className="mr-2" />
Expand Down
Loading