-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Chore(inertia): Migrate Subscription Manager Page to Inertia #3242
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
base: main
Are you sure you want to change the base?
Changes from all commits
355ae1e
836586e
46a6f80
04fde5a
57be31e
a994d56
d7a66ed
42f8614
8185803
fcfd3ab
d9fc628
66ea21f
fb97b3f
d6fbfa7
675e709
0f655f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File change summary:• Converted from ReactOnRails server component to Inertia page component |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import { router, useForm, usePage } from "@inertiajs/react"; | ||
| import { parseISO } from "date-fns"; | ||
| import * as React from "react"; | ||
| import { createCast } from "ts-safe-cast"; | ||
| import { cast } from "ts-safe-cast"; | ||
|
|
||
| import { confirmLineItem } from "$app/data/purchase"; | ||
| import { cancelSubscriptionByUser, updateSubscription } from "$app/data/subscription"; | ||
| import { updateSubscription } from "$app/data/subscription"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@Pradumn27 let me know if you have any opinion/approach in mind here? cc @EmCousin |
||
| import { SavedCreditCard } from "$app/parsers/card"; | ||
| import { Discount } from "$app/parsers/checkout"; | ||
| import { CustomFieldDescriptor, ProductNativeType } from "$app/parsers/product"; | ||
|
|
@@ -13,10 +14,7 @@ import { | |
| formatUSDCentsWithExpandedCurrencySymbol, | ||
| getMinPriceCents, | ||
| } from "$app/utils/currency"; | ||
| import { asyncVoid } from "$app/utils/promise"; | ||
| import { recurrenceLabels, RecurrenceId } from "$app/utils/recurringPricing"; | ||
| import { assertResponseError } from "$app/utils/request"; | ||
| import { register } from "$app/utils/serverComponentUtil"; | ||
|
|
||
| import { Button } from "$app/components/Button"; | ||
| import { Creator } from "$app/components/Checkout/cartState"; | ||
|
|
@@ -38,10 +36,9 @@ import { | |
| import { showAlert } from "$app/components/server-components/Alert"; | ||
| import { Alert } from "$app/components/ui/Alert"; | ||
| import { Card, CardContent } from "$app/components/ui/Card"; | ||
| import { useOnChangeSync } from "$app/components/useOnChange"; | ||
| import { useOriginalLocation } from "$app/components/useOriginalLocation"; | ||
|
|
||
| import { useOnChangeSync } from "../useOnChange"; | ||
|
|
||
| type Props = { | ||
| product: { | ||
| permalink: string; | ||
|
|
@@ -98,22 +95,23 @@ type Props = { | |
| paypal_client_id: string; | ||
| }; | ||
|
|
||
| const SubscriptionManager = ({ | ||
| product, | ||
| subscription, | ||
| recaptcha_key, | ||
| paypal_client_id, | ||
| contact_info, | ||
| countries, | ||
| us_states, | ||
| ca_provinces, | ||
| used_card, | ||
|
Comment on lines
-101
to
-110
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Props now accessed via usePage().props instead of function parameters |
||
| }: Props) => { | ||
| export default function SubscriptionsManage() { | ||
| const { | ||
| product, | ||
| subscription, | ||
| recaptcha_key, | ||
| paypal_client_id, | ||
| contact_info, | ||
| countries, | ||
| us_states, | ||
| ca_provinces, | ||
| used_card, | ||
| } = cast<Props>(usePage().props); | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Props now accessed via usePage().props instead of function parameters |
||
| const url = new URL(useOriginalLocation()); | ||
|
|
||
| const subscriptionEntity = subscription.is_installment_plan ? "installment plan" : "membership"; | ||
| const restartable = !subscription.alive || subscription.pending_cancellation; | ||
| const [cancelled, setCancelled] = React.useState(restartable); | ||
| const initialSelection = { | ||
| recurrence: subscription.recurrence, | ||
| rent: false, | ||
|
|
@@ -209,7 +207,7 @@ const SubscriptionManager = ({ | |
| nativeType: product.native_type, | ||
| canGift: false, | ||
| }; | ||
| const payLabel = cancelled ? `Restart ${subscriptionEntity}` : `Update ${subscriptionEntity}`; | ||
| const payLabel = restartable ? `Restart ${subscriptionEntity}` : `Update ${subscriptionEntity}`; | ||
| const { require_email_typo_acknowledgment } = useFeatureFlags(); | ||
| const reducer = createReducer({ | ||
| country: contact_info.country, | ||
|
|
@@ -268,10 +266,10 @@ const SubscriptionManager = ({ | |
| }); | ||
| if (result.type === "done") { | ||
| showAlert(result.message, "success"); | ||
| setCancelled(false); | ||
| setCancellationStatus("initial"); | ||
| if (result.next != null) { | ||
| window.location.href = result.next; | ||
| router.visit(result.next); | ||
| } else { | ||
| router.reload(); | ||
| } | ||
| } else if (result.type === "requires_card_action") { | ||
| await confirmLineItem({ | ||
|
|
@@ -282,8 +280,7 @@ const SubscriptionManager = ({ | |
| }).then((itemResult) => { | ||
| if (itemResult.success) { | ||
| showAlert(`Your ${subscriptionEntity} has been updated.`, "success"); | ||
| setCancelled(false); | ||
| setCancellationStatus("initial"); | ||
| router.reload(); | ||
| } | ||
| }); | ||
| } else { | ||
|
|
@@ -300,20 +297,13 @@ const SubscriptionManager = ({ | |
| if (state.status.type === "offering") dispatchAction({ type: "validate" }); | ||
| }, [state.status.type]); | ||
|
|
||
| const [cancellationStatus, setCancellationStatus] = React.useState<"initial" | "processing" | "done">("initial"); | ||
| const handleCancel = asyncVoid(async () => { | ||
| if (cancellationStatus === "processing" || cancellationStatus === "done") return; | ||
| setCancellationStatus("processing"); | ||
| try { | ||
| await cancelSubscriptionByUser(subscription.id); | ||
| setCancellationStatus("done"); | ||
| setCancelled(true); | ||
| } catch (e) { | ||
| assertResponseError(e); | ||
| setCancellationStatus("initial"); | ||
| showAlert("Sorry, something went wrong.", "error"); | ||
| } | ||
| }); | ||
| const cancelForm = useForm({}); | ||
| const unsubscribe = () => { | ||
| cancelForm.post(Routes.unsubscribe_by_user_subscription_path(subscription.id), { | ||
| preserveScroll: true, | ||
| onError: () => showAlert("Sorry, something went wrong.", "error"), | ||
| }); | ||
| }; | ||
|
|
||
| const hasSavedCard = state.savedCreditCard != null; | ||
| const isPendingFirstGifteePayment = subscription.is_gift && subscription.successful_purchases_count === 1; | ||
|
|
@@ -378,16 +368,14 @@ const SubscriptionManager = ({ | |
| <Button | ||
| color="danger" | ||
| outline | ||
| onClick={handleCancel} | ||
| disabled={cancellationStatus === "processing" || cancellationStatus === "done"} | ||
| onClick={unsubscribe} | ||
| disabled={cancelForm.processing} | ||
| className="grow basis-0" | ||
| > | ||
| {cancellationStatus === "done" ? "Cancelled" : `Cancel ${subscriptionEntity}`} | ||
| {`Cancel ${subscriptionEntity}`} | ||
| </Button> | ||
| </CardContent> | ||
| ) : null} | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| export default register({ component: SubscriptionManager, propParser: createCast() }); | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted cancelSubscriptionByUser function because cancellation is now handled via Inertia form submission with server redirect