@composable-os/pack-memberships 0.0.0-stage → 0.1.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,109 @@
1
+ # Functional Source License, Version 1.1, ALv2 Future License
2
+
3
+ > This licence covers the repository except the directories listed in NOTICE. In particular, the
4
+ > assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
5
+ > licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
6
+
7
+ ## Abbreviation
8
+
9
+ FSL-1.1-ALv2
10
+
11
+ ## Notice
12
+
13
+ Copyright 2026 Tim O'Connell
14
+
15
+ ## Terms and Conditions
16
+
17
+ ### Licensor ("We")
18
+
19
+ The party offering the Software under these Terms and Conditions.
20
+
21
+ ### The Software
22
+
23
+ The "Software" is each version of the software that we make available under
24
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
25
+ Conditions with the Software.
26
+
27
+ ### License Grant
28
+
29
+ Subject to your compliance with this License Grant and the Patents,
30
+ Redistribution and Trademark clauses below, we hereby grant you the right to
31
+ use, copy, modify, create derivative works, publicly perform, publicly display
32
+ and redistribute the Software for any Permitted Purpose identified below.
33
+
34
+ ### Permitted Purpose
35
+
36
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
37
+ means making the Software available to others in a commercial product or
38
+ service that:
39
+
40
+ 1. substitutes for the Software;
41
+
42
+ 2. substitutes for any other product or service we offer using the Software
43
+ that exists as of the date we make the Software available; or
44
+
45
+ 3. offers the same or substantially similar functionality as the Software.
46
+
47
+ Permitted Purposes specifically include using the Software:
48
+
49
+ 1. for your internal use and access;
50
+
51
+ 2. for non-commercial education;
52
+
53
+ 3. for non-commercial research; and
54
+
55
+ 4. in connection with professional services that you provide to a licensee
56
+ using the Software in accordance with these Terms and Conditions.
57
+
58
+ ### Patents
59
+
60
+ To the extent your use for a Permitted Purpose would necessarily infringe our
61
+ patents, the license grant above includes a license under our patents. If you
62
+ make a claim against any party that the Software infringes or contributes to
63
+ the infringement of any patent, then your patent license to the Software ends
64
+ immediately.
65
+
66
+ ### Redistribution
67
+
68
+ The Terms and Conditions apply to all copies, modifications and derivatives of
69
+ the Software.
70
+
71
+ If you redistribute any copies, modifications or derivatives of the Software,
72
+ you must include a copy of or a link to these Terms and Conditions and not
73
+ remove any copyright notices provided in or with the Software.
74
+
75
+ ### Disclaimer
76
+
77
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
78
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
79
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
80
+
81
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
82
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
83
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
84
+
85
+ ### Trademarks
86
+
87
+ Except for displaying the License Details and identifying us as the origin of
88
+ the Software, you have no right under these Terms and Conditions to use our
89
+ trademarks, trade names, service marks or product names.
90
+
91
+ ## Grant of Future License
92
+
93
+ We hereby irrevocably grant you an additional license to use the Software under
94
+ the Apache License, Version 2.0 that is effective on the second anniversary of
95
+ the date we make the Software available. On or after that date, you may use the
96
+ Software under the Apache License, Version 2.0, in which case the following
97
+ will apply:
98
+
99
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
100
+ this file except in compliance with the License.
101
+
102
+ You may obtain a copy of the License at
103
+
104
+ http://www.apache.org/licenses/LICENSE-2.0
105
+
106
+ Unless required by applicable law or agreed to in writing, software distributed
107
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
108
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
109
+ specific language governing permissions and limitations under the License.
@@ -0,0 +1,6 @@
1
+ import type { MembershipsHooks } from "@composable-os/pack-memberships/client";
2
+ export declare function MyMemberships({ memberships, returnUrl, }: {
3
+ memberships: Pick<MembershipsHooks, "useMine" | "useMySubscriptions" | "useCancelSubscription" | "useResumeSubscription" | "useBillingPortal">;
4
+ /** Where the provider's portal's back link goes (one of the host's listed origins). */
5
+ returnUrl: string;
6
+ }): import("react").JSX.Element;
@@ -0,0 +1,48 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { allowsWords, day, isLive, leftWords, subscriptionStatus, } from "@composable-os/pack-memberships/client";
3
+ import { useState } from "react";
4
+ import { Button } from "../ui-button/button.js";
5
+ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui-card/card.js";
6
+ export function MyMemberships({ memberships, returnUrl, }) {
7
+ const mine = memberships.useMine();
8
+ const subs = memberships.useMySubscriptions();
9
+ const cancel = memberships.useCancelSubscription();
10
+ const resume = memberships.useResumeSubscription();
11
+ const portal = memberships.useBillingPortal();
12
+ const [said, setSaid] = useState(null);
13
+ const [problem, setProblem] = useState(null);
14
+ if (mine.isPending)
15
+ return _jsx("p", { "aria-busy": "true", children: "Loading your memberships\u2026" });
16
+ if (mine.error || !mine.data)
17
+ return _jsx("p", { role: "alert", children: "We could not load your memberships." });
18
+ const live = (subs.data?.items ?? []).filter(isLive);
19
+ async function act(run, done) {
20
+ setProblem(null);
21
+ setSaid(null);
22
+ try {
23
+ await run();
24
+ setSaid(done);
25
+ }
26
+ catch (e) {
27
+ setProblem(e instanceof Error ? e.message : "That did not work");
28
+ }
29
+ }
30
+ async function onPortal() {
31
+ setProblem(null);
32
+ try {
33
+ const opened = await portal.mutateAsync({ returnUrl });
34
+ window.location.assign(opened.url);
35
+ }
36
+ catch (e) {
37
+ setProblem(e instanceof Error ? e.message : "We could not open your billing");
38
+ }
39
+ }
40
+ const ending = (sub) => sub.cancelAtPeriodEnd || sub.cancelAt !== null;
41
+ const busy = cancel.isPending || resume.isPending || portal.isPending;
42
+ return (_jsxs("section", { className: "grid gap-4", "aria-label": "My memberships", children: [mine.data.holdings.length ? null : _jsx("p", { children: "You have no membership or pass yet." }), mine.data.holdings.map((h) => (_jsxs(Card, { "data-kind": h.kind, children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: h.title }), _jsx(CardDescription, { children: allowsWords(h) })] }), _jsxs(CardContent, { className: "grid gap-1", children: [_jsx("p", { "data-testid": "holding-left", children: leftWords(h) }), h.until ? (_jsx("p", { className: "text-sm", children: `${h.kind === "pass" ? "Expires" : "Until"} ${day(h.until)}` })) : null] })] }, h.id))), live.map((sub) => (_jsxs(Card, { children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: sub.planTitle }), _jsx(CardDescription, { children: "Your membership's billing" })] }), _jsxs(CardContent, { className: "grid gap-3", children: [_jsx("p", { "data-testid": "subscription-status", children: subscriptionStatus(sub) }), _jsxs("div", { className: "flex flex-wrap gap-2", children: [sub.actionUrl ? (_jsx(Button, { onClick: () => window.location.assign(sub.actionUrl ?? ""), children: "Confirm your payment" })) : null, sub.cancelAtPeriodEnd ? (_jsx(Button, { disabled: busy, onClick: () => void act(() => resume.mutateAsync({ id: sub.id }), "Asked to keep it going. It shows here once the payment provider confirms."), children: "Keep my membership" })) : ending(sub) ? null : (_jsx(Button, { variant: "outline", disabled: busy, onClick: () => {
43
+ if (!window.confirm("Cancel? Your membership runs until the period ends.")) {
44
+ return;
45
+ }
46
+ void act(() => cancel.mutateAsync({ id: sub.id, reason: "Cancelled by the member" }), "Asked to cancel. It ends when the period does; it shows here once the payment provider confirms.");
47
+ }, children: "Cancel membership" })), _jsx(Button, { variant: "outline", disabled: busy, onClick: () => void onPortal(), children: "Card and invoices" })] })] })] }, sub.id))), said ? _jsx("p", { role: "status", children: said }) : null, problem ? _jsx("p", { role: "alert", children: problem }) : null] }));
48
+ }
@@ -0,0 +1,4 @@
1
+ import type { MembershipsHooks } from "@composable-os/pack-memberships/client";
2
+ export declare function MyMembershipsScreen({ memberships, }: {
3
+ memberships: Pick<MembershipsHooks, "useMine" | "useMySubscriptions">;
4
+ }): import("react").JSX.Element;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { allowsWords, day, isLive, leftWords, subscriptionStatus, } from "@composable-os/pack-memberships/client";
3
+ import { Text } from "react-native";
4
+ import { List } from "../native-base/list.js";
5
+ import { Row } from "../native-base/row.js";
6
+ import { Screen } from "../native-base/screen.js";
7
+ export function MyMembershipsScreen({ memberships, }) {
8
+ const { data, isPending, error } = memberships.useMine();
9
+ const subs = memberships.useMySubscriptions();
10
+ const live = subs.data?.items.filter(isLive) ?? [];
11
+ return (_jsxs(Screen, { title: "My memberships", children: [isPending ? _jsx(Text, { accessibilityRole: "progressbar", children: "Loading your memberships\u2026" }) : null, error ? _jsx(Text, { accessibilityRole: "alert", children: "We could not load your memberships." }) : null, data ? (_jsx(List, { empty: "You have no membership or pass yet.", children: data.holdings.map((h) => (_jsx(Row, { title: h.title ?? "", detail: `${leftWords(h)} · ${allowsWords(h)}${h.until ? ` · until ${day(h.until)}` : ""}` }, h.id))) })) : null, live.map((sub) => (_jsx(Text, { children: `${sub.planTitle}: ${subscriptionStatus(sub)}` }, sub.id))), live.length ? _jsx(Text, { children: "Manage your membership on the gym's website." }) : null] }));
12
+ }
@@ -0,0 +1,10 @@
1
+ import type { MembershipsHooks } from "@composable-os/pack-memberships/client";
2
+ export declare function Passes({ memberships, returnUrl, signedIn, signInUrl, }: {
3
+ memberships: Pick<MembershipsHooks, "usePassTypes" | "useBuyPass">;
4
+ /** Where the provider's page sends the buyer back to (one of the host's listed origins). */
5
+ returnUrl: string;
6
+ /** A signed-in customer buys as themselves; a guest gives their email. */
7
+ signedIn: boolean;
8
+ /** Where a guest signs in for an intro offer. */
9
+ signInUrl?: string;
10
+ }): import("react").JSX.Element;
@@ -0,0 +1,43 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { allowsWords, formatPrice, refusalWords } from "@composable-os/pack-memberships/client";
3
+ import { useState } from "react";
4
+ import { Button } from "../ui-button/button.js";
5
+ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui-card/card.js";
6
+ import { Input } from "../ui-input/input.js";
7
+ import { Label } from "../ui-label/label.js";
8
+ export function Passes({ memberships, returnUrl, signedIn, signInUrl, }) {
9
+ const { data, isPending, error } = memberships.usePassTypes();
10
+ const { start, checkout } = memberships.useBuyPass();
11
+ const [email, setEmail] = useState("");
12
+ const [problem, setProblem] = useState(null);
13
+ if (isPending)
14
+ return _jsx("p", { "aria-busy": "true", children: "Loading the passes\u2026" });
15
+ if (error || !data)
16
+ return _jsx("p", { role: "alert", children: "We could not load the passes." });
17
+ if (!data.passTypes.length)
18
+ return _jsx("p", { children: "No passes are on sale yet." });
19
+ async function onBuy(passTypeId) {
20
+ setProblem(null);
21
+ try {
22
+ const order = await start.mutateAsync({
23
+ passTypeId,
24
+ ...(signedIn ? {} : { email }),
25
+ });
26
+ const page = await checkout.mutateAsync({
27
+ id: order.id,
28
+ ...(order.token ? { token: order.token } : {}),
29
+ returnUrl,
30
+ });
31
+ window.location.assign(page.url);
32
+ }
33
+ catch (e) {
34
+ const details = e.details;
35
+ setProblem({ id: passTypeId, words: refusalWords(details) });
36
+ }
37
+ }
38
+ const busy = start.isPending || checkout.isPending;
39
+ return (_jsx("section", { className: "grid gap-4", "aria-label": "Passes", children: data.passTypes.map((type) => (_jsxs(Card, { "data-intro": type.onePerPerson ? "true" : undefined, children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: type.title }), _jsx(CardDescription, { children: `${formatPrice(type.price, data.currency)} · ${type.summary ?? allowsWords({ ...type, per: "total" })}${type.validDays ? ` · valid ${type.validDays} days` : ""}` })] }), _jsxs(CardContent, { children: [type.description ? _jsx("p", { children: type.description }) : null, type.onePerPerson ? (_jsxs("p", { className: "text-sm", children: [signedIn ? "One per person." : "One per person: sign in to buy it.", !signedIn && signInUrl ? (_jsxs(_Fragment, { children: [" ", _jsx("a", { href: signInUrl, className: "underline", children: "Sign in" })] })) : null] })) : null, _jsxs("form", { className: "grid max-w-sm gap-2", onSubmit: (e) => {
40
+ e.preventDefault();
41
+ void onBuy(type.id);
42
+ }, children: [signedIn ? null : (_jsxs(_Fragment, { children: [_jsx(Label, { htmlFor: `pass-email-${type.id}`, children: "Your email" }), _jsx(Input, { id: `pass-email-${type.id}`, type: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) })] })), _jsx(Button, { type: "submit", disabled: busy, children: "Buy" }), problem?.id === type.id ? _jsx("p", { role: "alert", children: problem.words }) : null] })] })] }, type.id))) }));
43
+ }
@@ -0,0 +1,15 @@
1
+ import type { MembershipsHooks } from "@composable-os/pack-memberships/client";
2
+ /** Signing a guest in by an emailed code, as the app does it (the web sign-in block's calls). */
3
+ export interface PlansSignIn {
4
+ requestCode: (email: string) => Promise<boolean>;
5
+ confirm: (email: string, code: string) => Promise<"ready" | "needs_attention" | null>;
6
+ }
7
+ export declare function Plans({ memberships, returnUrl, signedIn, signIn, }: {
8
+ memberships: Pick<MembershipsHooks, "usePlans" | "useSubscribe">;
9
+ /** Where the provider's page sends the member back to (one of the host's listed origins). */
10
+ returnUrl: string;
11
+ /** A signed-in customer subscribes as themselves; a guest gives their email. */
12
+ signedIn: boolean;
13
+ /** A guest confirms their address with a code here for the trial. */
14
+ signIn?: PlansSignIn;
15
+ }): import("react").JSX.Element;
@@ -0,0 +1,106 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { allowsWords, formatPrice, PLAN } from "@composable-os/pack-memberships/client";
3
+ import { useState } from "react";
4
+ import { Button } from "../ui-button/button.js";
5
+ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui-card/card.js";
6
+ import { Input } from "../ui-input/input.js";
7
+ import { Label } from "../ui-label/label.js";
8
+ /** Why the host gave no trial, in words. Display only. */
9
+ function noTrialWords(reason) {
10
+ switch (reason) {
11
+ case "sign_in":
12
+ return "The free trial is for signed-in members. Sign in for it, or continue and pay from today.";
13
+ case "used":
14
+ return "You have had your free trial, so this membership is paid from today.";
15
+ case "held":
16
+ return "A checkout with your free trial is already open. Finish that one, or continue and pay from today.";
17
+ case "unconfirmed":
18
+ return "We could not check your earlier checkout with the payment provider just now, so no free trial can be given yet. Try again in a few minutes, or continue and pay from today.";
19
+ case "permission":
20
+ return "A free trial cannot be given from here; this one is paid from today.";
21
+ case "continuation":
22
+ return "You have free time with us on a continuing membership: this one is paid from today.";
23
+ }
24
+ }
25
+ export function Plans({ memberships, returnUrl, signedIn, signIn, }) {
26
+ const { data, isPending, error } = memberships.usePlans();
27
+ const subscribe = memberships.useSubscribe();
28
+ const [email, setEmail] = useState("");
29
+ const [problem, setProblem] = useState(null);
30
+ const [noTrial, setNoTrial] = useState(null);
31
+ const [confirmed, setConfirmed] = useState(false);
32
+ const [codeSent, setCodeSent] = useState(false);
33
+ const [code, setCode] = useState("");
34
+ const [busy, setBusy] = useState(false);
35
+ const asCustomer = signedIn || confirmed;
36
+ if (isPending)
37
+ return _jsx("p", { "aria-busy": "true", children: "Loading the memberships\u2026" });
38
+ if (error || !data)
39
+ return _jsx("p", { role: "alert", children: "We could not load the memberships." });
40
+ if (!data.plans.length)
41
+ return _jsx("p", { children: "No memberships are on sale yet." });
42
+ async function onSubscribe(planId, hasTrial, customer = asCustomer) {
43
+ setProblem(null);
44
+ setNoTrial(null);
45
+ try {
46
+ const started = await subscribe.mutateAsync({
47
+ plan: { object: PLAN, id: planId },
48
+ returnUrl,
49
+ ...(customer ? {} : { email }),
50
+ });
51
+ // Never sent to a page that charges today without a word: the host's reason for no trial.
52
+ if (hasTrial && started.noTrial)
53
+ setNoTrial({ planId, started });
54
+ else
55
+ window.location.assign(started.url);
56
+ }
57
+ catch (e) {
58
+ setProblem(e instanceof Error ? e.message : "We could not start your membership");
59
+ }
60
+ }
61
+ async function onCode(event, planId) {
62
+ event.preventDefault();
63
+ if (!signIn)
64
+ return;
65
+ setBusy(true);
66
+ setProblem(null);
67
+ try {
68
+ if (!codeSent) {
69
+ if (await signIn.requestCode(email))
70
+ setCodeSent(true);
71
+ else
72
+ setProblem("We could not send a code. Try again in a moment.");
73
+ }
74
+ else {
75
+ const account = await signIn.confirm(email, code);
76
+ if (account === "ready") {
77
+ setConfirmed(true);
78
+ await onSubscribe(planId, true, true);
79
+ }
80
+ else if (account === "needs_attention") {
81
+ setProblem("Your address is confirmed, but we need to sort out your record first, and we have told the team. You can join now and pay from today.");
82
+ setCodeSent(false);
83
+ setNoTrial(null);
84
+ }
85
+ else {
86
+ setProblem("That code did not work. Check it, or ask for a new one.");
87
+ }
88
+ }
89
+ }
90
+ finally {
91
+ setBusy(false);
92
+ }
93
+ }
94
+ const reason = noTrial?.started.noTrial;
95
+ if (noTrial && reason) {
96
+ const { planId, started } = noTrial;
97
+ return (_jsxs("section", { className: "grid max-w-sm gap-3", "aria-label": "Memberships", "data-no-trial": reason, children: [_jsx("p", { role: "status", children: noTrialWords(reason) }), reason === "sign_in" && signIn ? (_jsxs("form", { className: "grid gap-2", onSubmit: (e) => void onCode(e, planId), children: [codeSent ? (_jsxs(_Fragment, { children: [_jsx("p", { className: "text-sm", children: `If ${email} can sign in here, we have emailed it a code.` }), _jsx(Label, { htmlFor: "member-code", children: "The code we emailed you" }), _jsx(Input, { id: "member-code", inputMode: "numeric", autoComplete: "one-time-code", required: true, value: code, onChange: (e) => setCode(e.target.value) })] })) : null, _jsx(Button, { type: "submit", disabled: busy || subscribe.isPending, children: codeSent ? "Confirm and start my free trial" : "Email me a code for the free trial" }), problem ? _jsx("p", { role: "alert", children: problem }) : null] })) : null, _jsx(Button, { variant: "outline", onClick: () => window.location.assign(started.url), children: "Continue and pay from today" })] }));
98
+ }
99
+ return (_jsxs("section", { className: "grid gap-4", "aria-label": "Memberships", children: [data.plans.map((plan) => {
100
+ const trial = plan.trialDays && plan.trialDays > 0 ? plan.trialDays : null;
101
+ return (_jsxs(Card, { children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: plan.title }), _jsx(CardDescription, { children: `${formatPrice(plan.price, data.currency)} a month · ${plan.summary ?? allowsWords(plan)}` })] }), _jsxs(CardContent, { children: [plan.description ? _jsx("p", { children: plan.description }) : null, _jsxs("form", { className: "grid max-w-sm gap-2", onSubmit: (e) => {
102
+ e.preventDefault();
103
+ void onSubscribe(plan.id, trial !== null);
104
+ }, children: [asCustomer ? null : (_jsxs(_Fragment, { children: [trial ? (_jsx("p", { children: `Sign in for your ${trial}-day free trial, or join now and pay from today.` })) : null, _jsx(Label, { htmlFor: `member-email-${plan.id}`, children: "Your email" }), _jsx(Input, { id: `member-email-${plan.id}`, type: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) })] })), _jsx(Button, { type: "submit", disabled: subscribe.isPending, children: asCustomer && trial ? `Start your ${trial}-day free trial` : "Join" }), asCustomer && trial ? (_jsx("p", { className: "text-sm", children: "Your card is taken now and charged when the trial ends, unless you cancel. One free trial per person." })) : null] })] })] }, plan.id));
105
+ }), problem ? _jsx("p", { role: "alert", children: problem }) : null] }));
106
+ }
@@ -0,0 +1,10 @@
1
+ import type { MembershipsHooks } from "@composable-os/pack-memberships/client";
2
+ export declare function PersonHoldings({ memberships, personId, passTypes, }: {
3
+ memberships: Pick<MembershipsHooks, "useHoldings" | "useGivePass" | "useVoidPass">;
4
+ personId: string;
5
+ /** The pass types staff may give, as the admin lists them (id and title). */
6
+ passTypes: readonly {
7
+ id: string;
8
+ title: string;
9
+ }[];
10
+ }): import("react").JSX.Element;
@@ -0,0 +1,35 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { allowsWords, day, leftWords } from "@composable-os/pack-memberships/client";
3
+ import { useState } from "react";
4
+ import { Button } from "../ui-button/button.js";
5
+ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui-card/card.js";
6
+ import { Input } from "../ui-input/input.js";
7
+ import { Label } from "../ui-label/label.js";
8
+ export function PersonHoldings({ memberships, personId, passTypes, }) {
9
+ const held = memberships.useHoldings(personId);
10
+ const give = memberships.useGivePass();
11
+ const voided = memberships.useVoidPass();
12
+ const [type, setType] = useState(passTypes[0]?.id ?? "");
13
+ const [reason, setReason] = useState("");
14
+ const [said, setSaid] = useState(null);
15
+ const [problem, setProblem] = useState(null);
16
+ if (held.isPending)
17
+ return _jsx("p", { "aria-busy": "true", children: "Loading\u2026" });
18
+ if (held.error || !held.data)
19
+ return _jsx("p", { role: "alert", children: "We could not load their memberships." });
20
+ async function act(run, done) {
21
+ setProblem(null);
22
+ setSaid(null);
23
+ try {
24
+ await run();
25
+ setSaid(done);
26
+ }
27
+ catch (e) {
28
+ setProblem(e instanceof Error ? e.message : "That did not work");
29
+ }
30
+ }
31
+ return (_jsxs("section", { className: "grid gap-4", "aria-label": "Memberships and passes", children: [held.data.holdings.map((h) => (_jsxs(Card, { "data-kind": h.kind, children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: h.title }), _jsx(CardDescription, { children: `${h.status} · ${allowsWords(h)}` })] }), _jsxs(CardContent, { className: "grid gap-2", children: [_jsx("p", { children: leftWords(h) }), h.until ? _jsx("p", { className: "text-sm", children: `Until ${day(h.until)}` }) : null, h.kind === "pass" && h.status === "active" ? (_jsx(Button, { variant: "outline", disabled: voided.isPending || reason.trim().length < 3, onClick: () => void act(() => voided.mutateAsync({ passId: h.id, reason }), "The pass is void."), children: "Void this pass" })) : null] })] }, h.id))), _jsxs("form", { className: "grid max-w-sm gap-2", onSubmit: (e) => {
32
+ e.preventDefault();
33
+ void act(() => give.mutateAsync({ personId, passTypeId: type, reason }), "The pass is given.");
34
+ }, children: [_jsx(Label, { htmlFor: "give-pass-type", children: "Pass to give" }), _jsx("select", { id: "give-pass-type", className: "rounded-md border px-2 py-1", value: type, onChange: (e) => setType(e.target.value), children: passTypes.map((t) => (_jsx("option", { value: t.id, children: t.title }, t.id))) }), _jsx(Label, { htmlFor: "give-pass-reason", children: "Why (kept on the pass)" }), _jsx(Input, { id: "give-pass-reason", required: true, minLength: 3, value: reason, onChange: (e) => setReason(e.target.value) }), _jsx(Button, { type: "submit", disabled: give.isPending || !type, children: "Give pass" })] }), said ? _jsx("p", { role: "status", children: said }) : null, problem ? _jsx("p", { role: "alert", children: problem }) : null] }));
35
+ }