feat: open purchase flow

This commit is contained in:
Antonella Sgarlatta
2021-10-04 17:03:20 -03:00
parent 3867f625f1
commit e134598c20
2 changed files with 60 additions and 20 deletions

View File

@@ -1,23 +1,63 @@
import { FunctionalComponent } from "preact"; import { FunctionalComponent } from "preact";
import { Text } from '@/preferences/components'; import { Text } from '@/preferences/components';
import { Button } from '@/components/Button'; import { Button } from '@/components/Button';
import { WebApplication } from "@/ui_models/application";
import { useState } from "preact/hooks";
import { isDesktopApplication } from "@/utils";
export const NoSubscription: FunctionalComponent = () => ( export const NoSubscription: FunctionalComponent<{
<> application: WebApplication;
<Text>You don't have a Standard Notes subscription yet.</Text> }> = ({ application }) => {
<div className="flex"> const [isLoadingPurchaseFlow, setIsLoadingPurchaseFlow] = useState(false);
<Button const [purchaseFlowError, setPurchaseFlowError] = useState<string | undefined>(undefined);
className="min-w-20 mt-3 mr-3"
type="normal" const onPurchaseClick = async () => {
label="Refresh" const errorMessage = 'There was an error when attempting to redirect you to the subscription page.';
onClick={() => null} setIsLoadingPurchaseFlow(true);
/> try {
<Button const url = await application.getPurchaseFlowUrl();
className="min-w-20 mt-3" if (url) {
type="primary" const currentUrl = window.location.href;
label="Purchase subscription" const successUrl = isDesktopApplication() ? `standardnotes://${currentUrl}` : currentUrl;
onClick={() => null} console.log(successUrl);
/> window.location.assign(`${url}&success_url=${successUrl}`);
</div> } else {
</> setPurchaseFlowError(errorMessage);
); }
} catch (e) {
setPurchaseFlowError(errorMessage);
} finally {
setIsLoadingPurchaseFlow(false);
}
};
return (
<>
<Text>You don't have a Standard Notes subscription yet.</Text>
{isLoadingPurchaseFlow && (
<Text>
Redirecting you to the subscription page...
</Text>
)}
{purchaseFlowError && (
<Text className="color-danger">
{purchaseFlowError}
</Text>
)}
<div className="flex">
<Button
className="min-w-20 mt-3 mr-3"
type="normal"
label="Refresh"
onClick={() => null}
/>
<Button
className="min-w-20 mt-3"
type="primary"
label="Purchase subscription"
onClick={onPurchaseClick}
/>
</div>
</>
);
};

View File

@@ -77,7 +77,7 @@ export const Subscription: FunctionComponent<Props> = observer(({
) : userSubscription && userSubscription.endsAt > now ? ( ) : userSubscription && userSubscription.endsAt > now ? (
<SubscriptionInformation subscriptionState={subscriptionState} /> <SubscriptionInformation subscriptionState={subscriptionState} />
) : ( ) : (
<NoSubscription /> <NoSubscription application={application} />
)} )}
</div> </div>
</div> </div>