2023-04-13 18:43:43 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-04-17 21:22:00 +00:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2023-05-12 17:49:21 +00:00
|
|
|
import { Link, useNavigate } from "react-router-dom";
|
2023-04-13 18:43:43 +00:00
|
|
|
|
|
|
|
import PageSpinner from "Element/PageSpinner";
|
2023-09-21 20:02:59 +00:00
|
|
|
import useEventPublisher from "Hooks/useEventPublisher";
|
2023-04-17 20:33:55 +00:00
|
|
|
import SnortApi, { Subscription, SubscriptionError } from "SnortApi";
|
2023-04-17 21:22:00 +00:00
|
|
|
import { mapSubscriptionErrorCode } from ".";
|
|
|
|
import SubscriptionCard from "./SubscriptionCard";
|
2023-04-13 18:43:43 +00:00
|
|
|
|
|
|
|
export default function ManageSubscriptionPage() {
|
|
|
|
const publisher = useEventPublisher();
|
|
|
|
const api = new SnortApi(undefined, publisher);
|
2023-05-12 17:49:21 +00:00
|
|
|
const navigate = useNavigate();
|
2023-04-13 18:43:43 +00:00
|
|
|
|
|
|
|
const [subs, setSubs] = useState<Array<Subscription>>();
|
2023-04-17 20:33:55 +00:00
|
|
|
const [error, setError] = useState<SubscriptionError>();
|
2023-04-13 18:43:43 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const s = await api.listSubscriptions();
|
|
|
|
setSubs(s);
|
|
|
|
} catch (e) {
|
2023-04-17 20:33:55 +00:00
|
|
|
if (e instanceof SubscriptionError) {
|
|
|
|
setError(e);
|
2023-04-13 18:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (subs === undefined) {
|
|
|
|
return <PageSpinner />;
|
|
|
|
}
|
|
|
|
return (
|
2023-09-22 08:33:21 +00:00
|
|
|
<div className="main-content p flex-column g16">
|
2023-04-13 18:43:43 +00:00
|
|
|
<h2>
|
|
|
|
<FormattedMessage defaultMessage="Subscriptions" />
|
|
|
|
</h2>
|
2023-04-17 21:22:00 +00:00
|
|
|
{subs.map(a => (
|
|
|
|
<SubscriptionCard sub={a} key={a.id} />
|
|
|
|
))}
|
2023-05-12 17:49:21 +00:00
|
|
|
{subs.length !== 0 && (
|
2023-09-22 08:33:21 +00:00
|
|
|
<button className="primary" onClick={() => navigate("/subscribe")}>
|
2023-05-12 17:49:21 +00:00
|
|
|
<FormattedMessage defaultMessage="Buy Subscription" />
|
|
|
|
</button>
|
|
|
|
)}
|
2023-04-13 18:43:43 +00:00
|
|
|
{subs.length === 0 && (
|
|
|
|
<p>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="It looks like you dont have any subscriptions, you can get one {link}"
|
|
|
|
values={{
|
|
|
|
link: (
|
|
|
|
<Link to="/subscribe">
|
|
|
|
<FormattedMessage defaultMessage="here" />
|
|
|
|
</Link>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
)}
|
2023-04-17 20:33:55 +00:00
|
|
|
{error && <b className="error">{mapSubscriptionErrorCode(error)}</b>}
|
2023-08-21 13:58:57 +00:00
|
|
|
</div>
|
2023-04-13 18:43:43 +00:00
|
|
|
);
|
|
|
|
}
|