Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Convert feedback submission from tRPC mutation to server action (#2665)

Luke Vellaefe6ff6e86d8
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index 31f9cb92c..04c0fa4e3 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -297,6 +297,7 @@
"featureNameExtendedPollLifetime": "Extended Poll Lifetime",
"featureNameSchedule": "Schedule Poll",
"feedback": "Feedback",
+ "feedbackDialogEnterFeedback": "Enter your feedback",
"fileTooLarge": "File too large",
"fileTooLargeDescription": "Please upload a file smaller than 2MB.",
"forgotPassword": "Forgot Password?",
diff --git a/apps/web/src/features/feedback/actions.ts b/apps/web/src/features/feedback/actions.ts
new file mode 100644
index 000000000..1dc27b2fd
--- /dev/null
+++ b/apps/web/src/features/feedback/actions.ts
@@ -0,0 +1,21 @@
+"use server";
+
+import {
+ authActionClient,
+ createRateLimitMiddleware,
+} from "@/lib/safe-action/server";
+import { submitFeedback } from "./mutations";
+import { feedbackSchema } from "./schema";
+
+export const submitFeedbackAction = authActionClient
+ .metadata({ actionName: "submit_feedback" })
+ .use(createRateLimitMiddleware(5, "1 h"))
+ .inputSchema(feedbackSchema)
+ .action(async ({ ctx, parsedInput }) => {
+ await submitFeedback({
+ userId: ctx.user.id,
+ userName: ctx.user.name,
+ userEmail: ctx.user.email,
+ content: parsedInput.content,
+ });
+ });
diff --git a/apps/web/src/features/feedback/components/feedback-dialog.tsx b/apps/web/src/features/feedback/components/feedback-dialog.tsx
index d3e00ab4f..9fc42075f 100644
--- a/apps/web/src/features/feedback/components/feedback-dialog.tsx
+++ b/apps/web/src/features/feedback/components/feedback-dialog.tsx
@@ -15,25 +15,27 @@ import { Textarea } from "@rallly/ui/textarea";
import { CheckCircle2Icon } from "lucide-react";
import { useForm } from "react-hook-form";
-import { Trans } from "@/i18n/client";
-import { isSelfHosted } from "@/lib/constants";
-import { trpc } from "@/trpc/client";
+import { Trans, useTranslation } from "@/i18n/client";
+import { useSafeAction } from "@/lib/safe-action/client";
+import { submitFeedbackAction } from "../actions";
+import { isFeedbackEnabled } from "../constants";
import { feedbackSchema } from "../schema";
export function FeedbackDialog(props: DialogProps) {
- const submitFeedback = trpc.user.submitFeedback.useMutation();
+ const { t } = useTranslation();
+ const submitFeedback = useSafeAction(submitFeedbackAction);
const form = useForm({
resolver: zodResolver(feedbackSchema),
});
- if (isSelfHosted) {
+ if (!isFeedbackEnabled) {
return null;
}
return (
<Dialog {...props}>
<DialogContent>
- {!form.formState.isSubmitSuccessful ? (
+ {!submitFeedback.hasSucceeded ? (
<>
<DialogHeader>
<DialogTitle>
@@ -49,9 +51,9 @@ export function FeedbackDialog(props: DialogProps) {
<Form {...form}>
<form
- onSubmit={form.handleSubmit((data) =>
- submitFeedback.mutateAsync(data),
- )}
+ onSubmit={form.handleSubmit(async (data) => {
+ await submitFeedback.executeAsync(data);
+ })}
>
<FormField
control={form.control}
@@ -63,7 +65,9 @@ export function FeedbackDialog(props: DialogProps) {
className="w-full"
rows={5}
{...field}
- placeholder="Enter your feedback"
+ placeholder={t("feedbackDialogEnterFeedback", {
+ defaultValue: "Enter your feedback",
+ })}
/>
<FormMessage />
</FormItem>
diff --git a/apps/web/src/features/feedback/mutations.ts b/apps/web/src/features/feedback/mutations.ts
new file mode 100644
index 000000000..3e818e1d5
--- /dev/null
+++ b/apps/web/src/features/feedback/mutations.ts
@@ -0,0 +1,28 @@
+import "server-only";
+
+import { sendRawEmail } from "@rallly/emails";
+import { posthog } from "@/lib/posthog";
+
+export async function submitFeedback({
+ userId,
+ userName,
+ userEmail,
+ content,
+}: {
+ userId: string;
+ userName: string;
+ userEmail: string;
+ content: string;
+}) {
+ await sendRawEmail({
+ replyTo: userEmail,
+ subject: "Feedback",
+ text: `User: ${userName} (${userEmail})\n\n${content}`,
+ });
+
+ posthog()?.capture({
+ event: "feedback_send",
+ distinctId: userId,
+ });
+}
diff --git a/apps/web/src/trpc/routers/user.ts b/apps/web/src/trpc/routers/user.ts
index ae575625d..dc6e812ff 100644
--- a/apps/web/src/trpc/routers/user.ts
+++ b/apps/web/src/trpc/routers/user.ts
@@ -1,20 +1,13 @@
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
-import { sendRawEmail } from "@rallly/emails";
import { TRPCError } from "@trpc/server";
import * as z from "zod";
-import { feedbackSchema } from "@/features/feedback/schema";
import { defaultNotificationPreferences } from "@/features/notifications/constants";
import { getNotificationPreferences } from "@/features/notifications/data";
import { activityEventTypes } from "@/features/notifications/schema";
import { defineAbilityFor } from "@/features/user/ability";
import { posthog } from "@/lib/posthog";
-import {
- createRateLimitMiddleware,
- privateProcedure,
- publicProcedure,
- router,
-} from "../trpc";
+import { privateProcedure, publicProcedure, router } from "../trpc";
export const user = router({
getMe: publicProcedure.query(async ({ ctx }) => {
@@ -110,20 +103,4 @@ export const user = router({
},
});
}),
- submitFeedback: privateProcedure
- .use(createRateLimitMiddleware("submit_feedback", 5, "1 h"))
- .input(feedbackSchema)
- .mutation(async ({ input, ctx }) => {
- sendRawEmail({
- replyTo: ctx.user.email ?? undefined,
- subject: "Feedback",
- text: `User: ${ctx.user.name} (${ctx.user.email})\n\n${input.content}`,
- });
-
- posthog()?.capture({
- event: "feedback_send",
- distinctId: ctx.user.id,
- });
- }),
});