Skip to content

Commit 431eafb

Browse files
committed
Use redirectWithErrorMessage instead of throw error
1 parent 51b437b commit 431eafb

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

apps/webapp/app/routes/resources.orgs.$organizationSlug.select-plan.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
XMarkIcon,
66
} from "@heroicons/react/20/solid";
77
import { ArrowDownCircleIcon, XCircleIcon } from "@heroicons/react/24/outline";
8-
import { Form, useFetcher, useLocation, useNavigation } from "@remix-run/react";
8+
import { Form, useLocation, useNavigation } from "@remix-run/react";
99
import { ActionFunctionArgs } from "@remix-run/server-runtime";
1010
import { PlainClient, uiComponent } from "@team-plain/typescript-sdk";
1111
import { GitHubLightIcon } from "@trigger.dev/companyicons";
@@ -17,7 +17,7 @@ import {
1717
SetPlanBody,
1818
SubscriptionResult,
1919
} from "@trigger.dev/platform/v3";
20-
import { useState } from "react";
20+
import React, { useState } from "react";
2121
import { inspect } from "util";
2222
import { z } from "zod";
2323
import { DefinitionTip } from "~/components/DefinitionTooltip";
@@ -44,7 +44,6 @@ import { logger } from "~/services/logger.server";
4444
import { setPlan } from "~/services/platform.v3.server";
4545
import { requireUser } from "~/services/session.server";
4646
import { cn } from "~/utils/cn";
47-
import React from "react";
4847

4948
const Params = z.object({
5049
organizationSlug: z.string(),
@@ -68,8 +67,15 @@ export async function action({ request, params }: ActionFunctionArgs) {
6867
const user = await requireUser(request);
6968

7069
const formData = await request.formData();
70+
71+
// Log the form data for debugging
72+
console.log("Form data:", Object.fromEntries(formData));
73+
7174
const form = schema.parse(Object.fromEntries(formData));
7275

76+
// Log the parsed form data
77+
console.log("Parsed form data:", form);
78+
7379
const organization = await prisma.organization.findUnique({
7480
where: { slug: organizationSlug },
7581
});
@@ -82,6 +88,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
8288

8389
switch (form.type) {
8490
case "free": {
91+
console.log("Entering free case");
8592
try {
8693
if (!env.PLAIN_API_KEY) {
8794
console.error("PLAIN_API_KEY is not set");
@@ -122,7 +129,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
122129
colors: true,
123130
})
124131
);
125-
throw new Error(upsertCustomerRes.error.message);
132+
throw redirectWithErrorMessage(form.callerPath, request, upsertCustomerRes.error.message);
126133
}
127134

128135
const formData = await request.formData();
@@ -145,8 +152,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
145152
? [
146153
uiComponent.spacer({ size: "L" }),
147154
uiComponent.text({
148-
size: "S",
149-
color: "ERROR",
155+
size: "L",
156+
color: "NORMAL",
150157
text: "Reasons:",
151158
}),
152159
uiComponent.text({
@@ -158,8 +165,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
158165
? [
159166
uiComponent.spacer({ size: "L" }),
160167
uiComponent.text({
161-
size: "S",
162-
color: "ERROR",
168+
size: "L",
169+
color: "NORMAL",
163170
text: "Comment:",
164171
}),
165172
uiComponent.text({
@@ -178,10 +185,11 @@ export async function action({ request, params }: ActionFunctionArgs) {
178185
colors: true,
179186
})
180187
);
181-
throw new Error(createThreadRes.error.message);
188+
throw redirectWithErrorMessage(form.callerPath, request, createThreadRes.error.message);
182189
}
183190
}
184191
} catch (e) {
192+
console.error("Error in free case:", e);
185193
logger.error("Failed to submit to Plain the unsubscribe reason", { error: e });
186194
}
187195
payload = {
@@ -201,8 +209,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
201209
};
202210
break;
203211
}
212+
default: {
213+
console.error("Invalid form type:", form.type);
214+
throw new Error("Invalid form type");
215+
}
204216
}
205217

218+
console.log("Final payload:", payload);
206219
return setPlan(organization, request, form.callerPath, payload);
207220
}
208221

@@ -414,7 +427,7 @@ export function TierFree({
414427
) : (
415428
<>
416429
{subscription?.plan?.type !== "free" && subscription?.canceledAt === undefined ? (
417-
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
430+
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen} key="cancel">
418431
<DialogTrigger asChild>
419432
<Button variant="tertiary/large" fullWidth className="text-md font-medium">
420433
{`Downgrade to ${plan.title}`}
@@ -482,6 +495,7 @@ export function TierFree({
482495
LeadingIcon={
483496
isLoading && "submitting" ? () => <Spinner color="white" /> : undefined
484497
}
498+
type="submit"
485499
form="subscribe"
486500
>
487501
Downgrade plan
@@ -569,7 +583,7 @@ export function TierHobby({
569583
subscription.plan.type !== "free" &&
570584
subscription.canceledAt === undefined &&
571585
subscription.plan.code !== plan.code ? (
572-
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
586+
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen} key="downgrade">
573587
<DialogTrigger asChild>
574588
<Button variant="tertiary/large" fullWidth className="text-md font-medium">
575589
{`Downgrade to ${plan.title}`}
@@ -608,6 +622,7 @@ export function TierHobby({
608622
variant={isHighlighted ? "primary/large" : "tertiary/large"}
609623
fullWidth
610624
className="text-md font-medium"
625+
form="subscribe-hobby"
611626
disabled={
612627
isLoading ||
613628
(subscription?.plan?.code === plan.code && subscription.canceledAt === undefined)
@@ -667,14 +682,15 @@ export function TierPro({
667682
<TierLimit href="https://trigger.dev/pricing#computePricing">
668683
${plan.limits.includedUsage / 100} usage included
669684
</TierLimit>
670-
<Form action={formAction} method="post" id="subscribe">
685+
<Form action={formAction} method="post" id="subscribe-pro">
671686
<div className="py-6">
672687
<input type="hidden" name="type" value="paid" />
673688
<input type="hidden" name="planCode" value={plan.code} />
674689
<input type="hidden" name="callerPath" value={location.pathname} />
675690
<Button
676691
variant="tertiary/large"
677692
fullWidth
693+
form="subscribe-pro"
678694
className="text-md font-medium"
679695
disabled={
680696
isLoading ||

0 commit comments

Comments
 (0)