@composable-os/pack-courses 0.0.0-stage → 0.1.0-beta.11

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.
Files changed (39) hide show
  1. package/LICENSE +109 -0
  2. package/dist/automations.d.ts +12 -0
  3. package/dist/automations.js +84 -0
  4. package/dist/blocks/courses-builder/course-builder.d.ts +5 -0
  5. package/dist/blocks/courses-builder/course-builder.js +69 -0
  6. package/dist/blocks/courses-catalogue/catalogue.d.ts +6 -0
  7. package/dist/blocks/courses-catalogue/catalogue.js +21 -0
  8. package/dist/blocks/courses-course/course-page.d.ts +10 -0
  9. package/dist/blocks/courses-course/course-page.js +51 -0
  10. package/dist/blocks/courses-my-courses/my-courses.d.ts +5 -0
  11. package/dist/blocks/courses-my-courses/my-courses.js +20 -0
  12. package/dist/blocks/courses-native-catalogue/catalogue-screen.d.ts +6 -0
  13. package/dist/blocks/courses-native-catalogue/catalogue-screen.js +15 -0
  14. package/dist/blocks/courses-native-course/course-screen.d.ts +7 -0
  15. package/dist/blocks/courses-native-course/course-screen.js +18 -0
  16. package/dist/blocks/courses-native-my-courses/my-courses-screen.d.ts +5 -0
  17. package/dist/blocks/courses-native-my-courses/my-courses-screen.js +9 -0
  18. package/dist/blocks/courses-native-player/lesson-screen.d.ts +19 -0
  19. package/dist/blocks/courses-native-player/lesson-screen.js +11 -0
  20. package/dist/blocks/courses-native-subscription/subscription-screen.d.ts +4 -0
  21. package/dist/blocks/courses-native-subscription/subscription-screen.js +17 -0
  22. package/dist/blocks/courses-player/player.d.ts +12 -0
  23. package/dist/blocks/courses-player/player.js +51 -0
  24. package/dist/blocks/courses-subscribe/subscribe.d.ts +28 -0
  25. package/dist/blocks/courses-subscribe/subscribe.js +146 -0
  26. package/dist/blocks/courses-subscription/my-subscription.d.ts +6 -0
  27. package/dist/blocks/courses-subscription/my-subscription.js +50 -0
  28. package/dist/client/index.d.ts +1319 -0
  29. package/dist/client/index.js +414 -0
  30. package/dist/dashboards.d.ts +5 -0
  31. package/dist/dashboards.js +119 -0
  32. package/dist/index.d.ts +809 -0
  33. package/dist/index.js +427 -0
  34. package/dist/outline.d.ts +85 -0
  35. package/dist/outline.js +188 -0
  36. package/dist/tools.d.ts +255 -0
  37. package/dist/tools.js +826 -0
  38. package/package.json +36 -4
  39. package/README.md +0 -3
@@ -0,0 +1,28 @@
1
+ import type { CoursesHooks } from "@composable-os/pack-courses/client";
2
+ /**
3
+ * Signing a guest in by an emailed code, as the app does it (the web sign-in block's two calls:
4
+ * the template's `/auth/code` and `/auth/sign-in`). `requestCode` says whether the host took the
5
+ * request (it answers every address the same). `confirm` gives the `account` the host's sign-in
6
+ * answer carries: `ready`, or `needs_attention` while staff sort out the person's record (the
7
+ * session is then not yet a customer's); null when the code was refused.
8
+ */
9
+ export interface SubscribeSignIn {
10
+ requestCode: (email: string) => Promise<boolean>;
11
+ confirm: (email: string, code: string) => Promise<"ready" | "needs_attention" | null>;
12
+ }
13
+ export declare function Subscribe({ courses, returnUrl, signedIn, signInUrl, onSignInForTrial, signIn, }: {
14
+ courses: Pick<CoursesHooks, "usePlans" | "useSubscribe" | "useMyOffers">;
15
+ /** Where the provider's page sends the subscriber back to (one of the host's listed origins). */
16
+ returnUrl: string;
17
+ /** A signed-in customer subscribes as themselves; a guest gives their email. */
18
+ signedIn: boolean;
19
+ /** Where a guest signs in: only a signed-in customer gets the free trial, or their offer. */
20
+ signInUrl?: string;
21
+ /** Instead of `signInUrl`: what the app does to sign a guest in for the trial, for that plan. */
22
+ onSignInForTrial?: (planId: string) => void;
23
+ /**
24
+ * Preferred: a guest confirms their address with a code right here, then goes on to the
25
+ * provider's page signed in, with the trial.
26
+ */
27
+ signIn?: SubscribeSignIn;
28
+ }): import("react").JSX.Element;
@@ -0,0 +1,146 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { day, formatPrice, noTrialWords, offerFor, offerWords, PLAN, } from "@composable-os/pack-courses/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 Subscribe({ courses, returnUrl, signedIn, signInUrl, onSignInForTrial, signIn, }) {
9
+ const { data, isPending, error } = courses.usePlans();
10
+ const subscribe = courses.useSubscribe();
11
+ const [email, setEmail] = useState("");
12
+ const [problem, setProblem] = useState(null);
13
+ // The host gave no trial on a plan that has one: say why before the provider's page asks for
14
+ // the first payment, so nobody is charged by surprise.
15
+ const [noTrial, setNoTrial] = useState(null);
16
+ // Signed in here, by a code (`signIn`): subscribe as the customer from now on.
17
+ const [confirmed, setConfirmed] = useState(false);
18
+ const [codeSent, setCodeSent] = useState(false);
19
+ const [code, setCode] = useState("");
20
+ const [busy, setBusy] = useState(false);
21
+ // Confirmed, but the account waits for staff to merge people: subscribe as a guest meanwhile.
22
+ const [waiting, setWaiting] = useState(false);
23
+ const asCustomer = signedIn || confirmed;
24
+ // Only a signed-in customer has offers; a guest's call would be refused, so it is not made.
25
+ const offers = courses.useMyOffers({ enabled: asCustomer });
26
+ if (isPending || (asCustomer && offers.isPending)) {
27
+ return _jsx("p", { "aria-busy": "true", children: "Loading the plans\u2026" });
28
+ }
29
+ if (error || !data)
30
+ return _jsx("p", { role: "alert", children: "We could not load the plans." });
31
+ if (!data.plans.length)
32
+ return _jsx("p", { children: "No plans are offered yet." });
33
+ // Only offers on the course plan object matter here (another pack's plans are its own).
34
+ const onPlans = (list) => (list ?? []).filter((o) => o.planObject === PLAN);
35
+ const mine = asCustomer ? onPlans(offers.data?.offers) : [];
36
+ const continuing = mine.length > 0;
37
+ const askUs = mine.filter((o) => o.askUs);
38
+ async function onSubscribe(planId, hasTrial, customer = asCustomer, offer, owed = continuing) {
39
+ setProblem(null);
40
+ setNoTrial(null);
41
+ try {
42
+ const started = await subscribe.mutateAsync({
43
+ plan: { object: PLAN, id: planId },
44
+ returnUrl,
45
+ ...(customer ? {} : { email }),
46
+ ...(offer ? { offerId: offer.id } : {}),
47
+ });
48
+ // Never sent to a page that charges today without a word: the host's reason for no trial,
49
+ // or, for a member with free time owed on another plan, that this one is paid from today.
50
+ if (!offer && (owed || (hasTrial && started.noTrial))) {
51
+ setNoTrial({ planId, started: { ...started, noTrial: started.noTrial ?? "continuation" } });
52
+ }
53
+ else
54
+ window.location.assign(started.url);
55
+ }
56
+ catch (e) {
57
+ setProblem(e instanceof Error ? e.message : "We could not start your subscription");
58
+ }
59
+ }
60
+ /** The code step on the plan: ask for a code, then confirm it and start signed in. */
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
+ // Signed in now: if they have a continuation offer, they see it in place of the trial
79
+ // (the plans again), never a trial page opened over it.
80
+ const fresh = onPlans((await offers.refetch()).data?.offers);
81
+ if (fresh.length) {
82
+ setNoTrial(null);
83
+ setCodeSent(false);
84
+ setCode("");
85
+ return;
86
+ }
87
+ // Back to the same plan, now as the customer: the host gives the trial or says why not.
88
+ await onSubscribe(planId, true, true, undefined, false);
89
+ }
90
+ else if (account === "needs_attention") {
91
+ // Signed in, but not yet as a customer: staff are told (the host's merge notice). Back
92
+ // to the plans, where they can subscribe as a guest and pay from today.
93
+ setWaiting(true);
94
+ setCodeSent(false);
95
+ setCode("");
96
+ setNoTrial(null);
97
+ }
98
+ else {
99
+ setProblem("That code did not work. Check it, or ask for a new one.");
100
+ }
101
+ }
102
+ }
103
+ finally {
104
+ setBusy(false);
105
+ }
106
+ }
107
+ /** A new code, after a wrong or expired one, without leaving the plan. */
108
+ async function onNewCode() {
109
+ if (!signIn)
110
+ return;
111
+ setBusy(true);
112
+ setProblem(null);
113
+ setCode("");
114
+ try {
115
+ if (!(await signIn.requestCode(email))) {
116
+ setProblem("We could not send a code. Try again in a moment.");
117
+ }
118
+ }
119
+ finally {
120
+ setBusy(false);
121
+ }
122
+ }
123
+ if (noTrial?.started.noTrial) {
124
+ const { planId, started } = noTrial;
125
+ const reason = started.noTrial;
126
+ return (_jsxs("section", { className: "grid max-w-sm gap-3", "aria-label": "Subscribe", "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: "subscriber-code", children: "The code we emailed you" }), _jsx(Input, { id: "subscriber-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" }), codeSent ? (_jsx(Button, { type: "button", variant: "outline", disabled: busy, onClick: () => void onNewCode(), children: "Send me a new code" })) : null, problem ? _jsx("p", { role: "alert", children: problem }) : null] })) : reason === "sign_in" && (onSignInForTrial || signInUrl) ? (_jsx(Button, { onClick: () => onSignInForTrial ? onSignInForTrial(planId) : window.location.assign(signInUrl ?? ""), children: "Sign in for the free trial" })) : null, _jsx(Button, { variant: "outline", onClick: () => window.location.assign(started.url), children: "Continue and pay from today" })] }));
127
+ }
128
+ return (_jsxs("section", { className: "grid gap-4", "aria-label": "Subscribe", children: [waiting ? (_jsx("p", { role: "status", children: "Your address is confirmed, but we need to sort out your record before you can have the free trial, and we have told the team. You can subscribe now and pay from today." })) : null, askUs.map((o) => (_jsx("p", { role: "status", "data-testid": "continuation-offer", children: offerWords(o) }, o.id))), data.plans.map((plan) => {
129
+ const found = offerFor(mine, plan.id);
130
+ const offer = found && !found.askUs ? found : undefined;
131
+ // A continuation replaces the trial (owner decision 7): a member with an open offer on the
132
+ // course plan object is shown no trial on its plans (the host gives none then).
133
+ const trial = !continuing && plan.trialDays && plan.trialDays > 0 ? plan.trialDays : null;
134
+ return (_jsxs(Card, { "data-offer": offer ? offer.id : undefined, children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: plan.title }), _jsx(CardDescription, { children: `${formatPrice(plan.price, data.currency)} a month · every course` })] }), _jsxs(CardContent, { children: [plan.description ? _jsx("p", { children: plan.description }) : null, offer ? _jsx("p", { "data-testid": "continuation-offer", children: offerWords(offer) }) : null, _jsxs("form", { className: "grid max-w-sm gap-2", onSubmit: (e) => {
135
+ e.preventDefault();
136
+ // While the record waits for staff, no trial can be given: straight to paying.
137
+ void onSubscribe(plan.id, trial !== null && !waiting, asCustomer, offer);
138
+ }, children: [asCustomer ? null : (_jsxs(_Fragment, { children: [trial && !waiting ? (_jsxs("p", { children: [signInUrl ? (_jsx("a", { href: signInUrl, className: "underline", children: `Sign in for your ${trial}-day free trial` })) : (`Sign in for your ${trial}-day free trial`), ", or subscribe now and pay from today."] })) : null, _jsx(Label, { htmlFor: `subscriber-email-${plan.id}`, children: "Your email" }), _jsx(Input, { id: `subscriber-email-${plan.id}`, type: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) })] })), _jsx(Button, { type: "submit", disabled: subscribe.isPending, children: offer
139
+ ? offer.chargedNow
140
+ ? "Continue my subscription"
141
+ : `Continue, free until ${day(offer.freeUntil)}`
142
+ : asCustomer && trial
143
+ ? `Start your ${trial}-day free trial`
144
+ : "Subscribe" }), offer && !offer.chargedNow ? (_jsx("p", { className: "text-sm", children: `Your card is taken now and first charged on ${day(offer.freeUntil)}, unless you cancel.` })) : null, 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));
145
+ }), problem ? _jsx("p", { role: "alert", children: problem }) : null] }));
146
+ }
@@ -0,0 +1,6 @@
1
+ import type { CoursesHooks } from "@composable-os/pack-courses/client";
2
+ export declare function MySubscription({ courses, returnUrl, }: {
3
+ courses: Pick<CoursesHooks, "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,50 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { formatMinor, isLive, subscriptionStatus } from "@composable-os/pack-courses/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 MySubscription({ courses, returnUrl, }) {
7
+ const { data, isPending, error } = courses.useMySubscriptions();
8
+ const cancel = courses.useCancelSubscription();
9
+ const resume = courses.useResumeSubscription();
10
+ const portal = courses.useBillingPortal();
11
+ const [said, setSaid] = useState(null);
12
+ const [problem, setProblem] = useState(null);
13
+ if (isPending)
14
+ return _jsx("p", { "aria-busy": "true", children: "Loading your subscription\u2026" });
15
+ if (error || !data)
16
+ return _jsx("p", { role: "alert", children: "We could not load your subscription." });
17
+ const live = data.items.filter(isLive);
18
+ if (!live.length)
19
+ return _jsx("p", { children: "You have no course subscription." });
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
+ async function onPortal() {
32
+ setProblem(null);
33
+ try {
34
+ // The provider's portal opens in its own page: it cannot be put in a frame.
35
+ const opened = await portal.mutateAsync({ returnUrl });
36
+ window.location.assign(opened.url);
37
+ }
38
+ catch (e) {
39
+ setProblem(e instanceof Error ? e.message : "We could not open your billing");
40
+ }
41
+ }
42
+ const ending = (sub) => sub.cancelAtPeriodEnd || sub.cancelAt !== null;
43
+ const busy = cancel.isPending || resume.isPending || portal.isPending;
44
+ return (_jsxs("section", { className: "grid gap-4", "aria-label": "My subscription", children: [live.map((sub) => (_jsxs(Card, { children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: sub.planTitle }), _jsx(CardDescription, { children: `${formatMinor(sub.amount, sub.currency)} a ${sub.interval} · every course` })] }), _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 subscription" })) : ending(sub) ? null : (_jsx(Button, { variant: "outline", disabled: busy, onClick: () => {
45
+ if (!window.confirm("Cancel? You keep every course until the period ends.")) {
46
+ return;
47
+ }
48
+ 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.");
49
+ }, children: "Cancel subscription" })), _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] }));
50
+ }