feat: implement renewal/complete new
This commit is contained in:
parent
c59dda1e49
commit
b6cc1765db
@ -42,6 +42,15 @@ export default class SnortServiceProvider extends ServiceProvider {
|
|||||||
return this.getJsonAuthd<object>(`/${id}`, "PATCH", obj);
|
return this.getJsonAuthd<object>(`/${id}`, "PATCH", obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async registerForSubscription(handle: string, domain: string, id: string) {
|
||||||
|
return this.getJsonAuthd<object>(`/registration/register/${id}`, "PUT", {
|
||||||
|
name: handle,
|
||||||
|
domain,
|
||||||
|
pk: "",
|
||||||
|
ref: "snort",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async getJsonAuthd<T>(
|
async getJsonAuthd<T>(
|
||||||
path: string,
|
path: string,
|
||||||
method?: "GET" | string,
|
method?: "GET" | string,
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { FormattedDate, FormattedMessage, FormattedNumber } from "react-intl";
|
import { FormattedDate, FormattedMessage, FormattedNumber, useIntl } from "react-intl";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
import PageSpinner from "Element/PageSpinner";
|
import PageSpinner from "Element/PageSpinner";
|
||||||
import useEventPublisher from "Feed/EventPublisher";
|
import useEventPublisher from "Feed/EventPublisher";
|
||||||
import SnortApi, { Subscription } from "SnortApi";
|
import SnortApi, { Subscription, SubscriptionError } from "SnortApi";
|
||||||
import { mapPlanName } from ".";
|
import { mapPlanName, mapSubscriptionErrorCode } from ".";
|
||||||
import Icon from "Icons/Icon";
|
import Icon from "Icons/Icon";
|
||||||
|
import AsyncButton from "Element/AsyncButton";
|
||||||
|
import SendSats from "Element/SendSats";
|
||||||
|
|
||||||
export default function ManageSubscriptionPage() {
|
export default function ManageSubscriptionPage() {
|
||||||
const publisher = useEventPublisher();
|
const publisher = useEventPublisher();
|
||||||
|
const { formatMessage } = useIntl();
|
||||||
const api = new SnortApi(undefined, publisher);
|
const api = new SnortApi(undefined, publisher);
|
||||||
|
|
||||||
const [subs, setSubs] = useState<Array<Subscription>>();
|
const [subs, setSubs] = useState<Array<Subscription>>();
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState<SubscriptionError>();
|
||||||
|
const [invoice, setInvoice] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
@ -21,15 +25,24 @@ export default function ManageSubscriptionPage() {
|
|||||||
const s = await api.listSubscriptions();
|
const s = await api.listSubscriptions();
|
||||||
setSubs(s);
|
setSubs(s);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error) {
|
if (e instanceof SubscriptionError) {
|
||||||
setError(e.message);
|
setError(e);
|
||||||
} else {
|
|
||||||
setError("Unknown error");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
async function renew(id: string) {
|
||||||
|
try {
|
||||||
|
const rsp = await api.renewSubscription(id);
|
||||||
|
setInvoice(rsp.pr);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof SubscriptionError) {
|
||||||
|
setError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (subs === undefined) {
|
if (subs === undefined) {
|
||||||
return <PageSpinner />;
|
return <PageSpinner />;
|
||||||
}
|
}
|
||||||
@ -44,7 +57,8 @@ export default function ManageSubscriptionPage() {
|
|||||||
const now = new Date();
|
const now = new Date();
|
||||||
const daysToExpire = Math.floor((expires.getTime() - now.getTime()) / 8.64e7);
|
const daysToExpire = Math.floor((expires.getTime() - now.getTime()) / 8.64e7);
|
||||||
const hoursToExpire = Math.floor((expires.getTime() - now.getTime()) / 3.6e6);
|
const hoursToExpire = Math.floor((expires.getTime() - now.getTime()) / 3.6e6);
|
||||||
const isExpired = expires < now;
|
const isExpired = a.state === "expired";
|
||||||
|
const isNew = a.state === "new";
|
||||||
return (
|
return (
|
||||||
<div key={a.id} className="card">
|
<div key={a.id} className="card">
|
||||||
<div className="flex card-title">
|
<div className="flex card-title">
|
||||||
@ -87,17 +101,26 @@ export default function ManageSubscriptionPage() {
|
|||||||
</time>
|
</time>
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
{daysToExpire < 0 && (
|
{isExpired && (
|
||||||
<p className="f-1 error">
|
<p className="f-1 error">
|
||||||
<FormattedMessage defaultMessage="Expired" />
|
<FormattedMessage defaultMessage="Expired" />
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
|
{isNew && (
|
||||||
|
<p className="f-1">
|
||||||
|
<FormattedMessage defaultMessage="Unpaid" />
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{isExpired && (
|
{(isExpired || isNew) && (
|
||||||
<div className="flex">
|
<div className="flex">
|
||||||
<button>
|
<AsyncButton onClick={() => renew(a.id)}>
|
||||||
<FormattedMessage defaultMessage="Renew" />
|
{isExpired ? (
|
||||||
</button>
|
<FormattedMessage defaultMessage="Renew" />
|
||||||
|
) : (
|
||||||
|
<FormattedMessage defaultMessage="Pay Now" />
|
||||||
|
)}
|
||||||
|
</AsyncButton>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@ -117,7 +140,15 @@ export default function ManageSubscriptionPage() {
|
|||||||
/>
|
/>
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
{error && <b className="error">{error}</b>}
|
{error && <b className="error">{mapSubscriptionErrorCode(error)}</b>}
|
||||||
|
<SendSats
|
||||||
|
invoice={invoice}
|
||||||
|
show={invoice !== ""}
|
||||||
|
onClose={() => setInvoice("")}
|
||||||
|
title={formatMessage({
|
||||||
|
defaultMessage: "Renew subscription",
|
||||||
|
})}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import { LockedFeatures, Plans, SubscriptionType } from "Subscription";
|
|||||||
import ManageSubscriptionPage from "Pages/subscribe/ManageSubscription";
|
import ManageSubscriptionPage from "Pages/subscribe/ManageSubscription";
|
||||||
import AsyncButton from "Element/AsyncButton";
|
import AsyncButton from "Element/AsyncButton";
|
||||||
import useEventPublisher from "Feed/EventPublisher";
|
import useEventPublisher from "Feed/EventPublisher";
|
||||||
import SnortApi from "SnortApi";
|
import SnortApi, { SubscriptionError, SubscriptionErrorCode } from "SnortApi";
|
||||||
import SendSats from "Element/SendSats";
|
import SendSats from "Element/SendSats";
|
||||||
|
|
||||||
export function mapPlanName(id: number) {
|
export function mapPlanName(id: number) {
|
||||||
@ -38,67 +38,91 @@ export function mapFeatureName(k: LockedFeatures) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mapSubscriptionErrorCode(c: SubscriptionError) {
|
||||||
|
switch (c.code) {
|
||||||
|
case SubscriptionErrorCode.InternalError:
|
||||||
|
return <FormattedMessage defaultMessage="Internal error: {msg}" values={{ msg: c.message }} />;
|
||||||
|
case SubscriptionErrorCode.SubscriptionActive:
|
||||||
|
return <FormattedMessage defaultMessage="You subscription is still active, you can't renew yet" />;
|
||||||
|
case SubscriptionErrorCode.Duplicate:
|
||||||
|
return <FormattedMessage defaultMessage="You already have a subscription of this type, please renew or pay" />;
|
||||||
|
default:
|
||||||
|
return c.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function SubscribePage() {
|
export function SubscribePage() {
|
||||||
const publisher = useEventPublisher();
|
const publisher = useEventPublisher();
|
||||||
const api = new SnortApi(undefined, publisher);
|
const api = new SnortApi(undefined, publisher);
|
||||||
const [invoice, setInvoice] = useState("");
|
const [invoice, setInvoice] = useState("");
|
||||||
|
const [error, setError] = useState<SubscriptionError>();
|
||||||
|
|
||||||
async function subscribe(type: number) {
|
async function subscribe(type: number) {
|
||||||
const rsp = await api.createSubscription(type);
|
setError(undefined);
|
||||||
setInvoice(rsp.pr);
|
try {
|
||||||
|
const rsp = await api.createSubscription(type);
|
||||||
|
setInvoice(rsp.pr);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof SubscriptionError) {
|
||||||
|
setError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex subscribe-page">
|
<>
|
||||||
{Plans.map(a => {
|
<div className="flex subscribe-page">
|
||||||
const lower = Plans.filter(b => b.id < a.id);
|
{Plans.map(a => {
|
||||||
return (
|
const lower = Plans.filter(b => b.id < a.id);
|
||||||
<div className={`card flex f-col${a.disabled ? " disabled" : ""}`}>
|
return (
|
||||||
<div className="f-grow">
|
<div className={`card flex f-col${a.disabled ? " disabled" : ""}`}>
|
||||||
<h2>{mapPlanName(a.id)}</h2>
|
<div className="f-grow">
|
||||||
<p>
|
<h2>{mapPlanName(a.id)}</h2>
|
||||||
<FormattedMessage
|
<p>
|
||||||
defaultMessage="Subscribe to Snort {plan} for {price} and receive the following rewards"
|
<FormattedMessage
|
||||||
values={{
|
defaultMessage="Subscribe to Snort {plan} for {price} and receive the following rewards"
|
||||||
plan: mapPlanName(a.id),
|
values={{
|
||||||
price: <b>{formatShort(a.price)} sats/mo</b>,
|
plan: mapPlanName(a.id),
|
||||||
}}
|
price: <b>{formatShort(a.price)} sats/mo</b>,
|
||||||
/>
|
}}
|
||||||
:
|
/>
|
||||||
</p>
|
:
|
||||||
<b>
|
</p>
|
||||||
<FormattedMessage defaultMessage="Not all features are built yet, more features to be added soon!" />
|
<b>
|
||||||
</b>
|
<FormattedMessage defaultMessage="Not all features are built yet, more features to be added soon!" />
|
||||||
<ul>
|
</b>
|
||||||
{a.unlocks.map(b => (
|
<ul>
|
||||||
<li>{mapFeatureName(b)} </li>
|
{a.unlocks.map(b => (
|
||||||
))}
|
<li>{mapFeatureName(b)} </li>
|
||||||
{lower.map(b => (
|
))}
|
||||||
<li>
|
{lower.map(b => (
|
||||||
<FormattedMessage
|
<li>
|
||||||
defaultMessage="Everything in {plan}"
|
<FormattedMessage
|
||||||
values={{
|
defaultMessage="Everything in {plan}"
|
||||||
plan: mapPlanName(b.id),
|
values={{
|
||||||
}}
|
plan: mapPlanName(b.id),
|
||||||
/>
|
}}
|
||||||
</li>
|
/>
|
||||||
))}
|
</li>
|
||||||
</ul>
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div className="flex f-center w-max mb10">
|
||||||
|
<AsyncButton className="button" disabled={a.disabled} onClick={() => subscribe(a.id)}>
|
||||||
|
{a.disabled ? (
|
||||||
|
<FormattedMessage defaultMessage="Coming soon" />
|
||||||
|
) : (
|
||||||
|
<FormattedMessage defaultMessage="Subscribe" />
|
||||||
|
)}
|
||||||
|
</AsyncButton>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex f-center w-max mb10">
|
);
|
||||||
<AsyncButton className="button" disabled={a.disabled} onClick={() => subscribe(a.id)}>
|
})}
|
||||||
{a.disabled ? (
|
</div>
|
||||||
<FormattedMessage defaultMessage="Coming soon" />
|
{error && <b className="error">{mapSubscriptionErrorCode(error)}</b>}
|
||||||
) : (
|
|
||||||
<FormattedMessage defaultMessage="Subscribe" />
|
|
||||||
)}
|
|
||||||
</AsyncButton>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
<SendSats invoice={invoice} show={invoice !== ""} onClose={() => setInvoice("")} />
|
<SendSats invoice={invoice} show={invoice !== ""} onClose={() => setInvoice("")} />
|
||||||
</div>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,22 @@ export interface Subscription {
|
|||||||
type: SubscriptionType;
|
type: SubscriptionType;
|
||||||
created: string;
|
created: string;
|
||||||
expires: string;
|
expires: string;
|
||||||
|
state: "new" | "expired" | "paid";
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum SubscriptionErrorCode {
|
||||||
|
InternalError = 1,
|
||||||
|
SubscriptionActive = 2,
|
||||||
|
Duplicate = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SubscriptionError extends Error {
|
||||||
|
code: SubscriptionErrorCode;
|
||||||
|
|
||||||
|
constructor(msg: string, code: SubscriptionErrorCode) {
|
||||||
|
super(msg);
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class SnortApi {
|
export default class SnortApi {
|
||||||
@ -49,6 +65,10 @@ export default class SnortApi {
|
|||||||
return this.#getJsonAuthd<InvoiceResponse>(`api/v1/subscription?type=${type}`, "PUT");
|
return this.#getJsonAuthd<InvoiceResponse>(`api/v1/subscription?type=${type}`, "PUT");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renewSubscription(id: string) {
|
||||||
|
return this.#getJsonAuthd<InvoiceResponse>(`api/v1/subscription/${id}/renew`, "GET");
|
||||||
|
}
|
||||||
|
|
||||||
listSubscriptions() {
|
listSubscriptions() {
|
||||||
return this.#getJsonAuthd<Array<Subscription>>("api/v1/subscription");
|
return this.#getJsonAuthd<Array<Subscription>>("api/v1/subscription");
|
||||||
}
|
}
|
||||||
@ -93,7 +113,7 @@ export default class SnortApi {
|
|||||||
|
|
||||||
const obj = await rsp.json();
|
const obj = await rsp.json();
|
||||||
if ("error" in obj) {
|
if ("error" in obj) {
|
||||||
throw new Error(obj.error);
|
throw new SubscriptionError(obj.error, obj.code);
|
||||||
}
|
}
|
||||||
return obj as T;
|
return obj as T;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user