@composable-os/pack-forms 0.0.0-stage → 0.1.0-beta.12
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 +109 -0
- package/dist/automations.d.ts +22 -0
- package/dist/automations.js +100 -0
- package/dist/blocks/forms-form/form.d.ts +46 -0
- package/dist/blocks/forms-form/form.js +196 -0
- package/dist/blocks/forms-native-form/form-screen.d.ts +35 -0
- package/dist/blocks/forms-native-form/form-screen.js +161 -0
- package/dist/client/index.d.ts +302 -0
- package/dist/client/index.js +431 -0
- package/dist/client/page.d.ts +67 -0
- package/dist/client/page.js +634 -0
- package/dist/index.d.ts +605 -0
- package/dist/index.js +406 -0
- package/dist/routine-mail.d.ts +23 -0
- package/dist/routine-mail.js +25 -0
- package/dist/tools.d.ts +92 -0
- package/dist/tools.js +288 -0
- package/package.json +40 -4
- package/README.md +0 -3
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
// A form to fill in, for the mobile app (stage 29 step 4): the native skin over the same headless
|
|
3
|
+
// entry as the web block, with every question kind the question schema has, then its thank-you.
|
|
4
|
+
// `FormScreen` opens a form by its address; `LinkedFormScreen` opens a signed-in member's own
|
|
5
|
+
// personal link from Needs your attention (`me.attention`, kind form_to_fill, by the link's id):
|
|
6
|
+
// the app's prompt to answer the feedback after a class.
|
|
7
|
+
//
|
|
8
|
+
// It holds no rules: who may answer, once only, the spam checks and the caps are the host's, and
|
|
9
|
+
// its answer is the one shown; the answers are checked first with the host's own check, for the
|
|
10
|
+
// words beside each question. A member in the app is signed in, so the host asks no spam check of
|
|
11
|
+
// them; for a visitor on a public form, the app passes its Turnstile widget (`Turnstile`, in a web
|
|
12
|
+
// view), and the host's token of when the form was shown travels with the send. What a person
|
|
13
|
+
// types stays in this screen's state and nowhere else.
|
|
14
|
+
import { isAsked, ratingLabel, ratingValues, refusalOf, } from "@composable-os/pack-forms/client";
|
|
15
|
+
import { useState } from "react";
|
|
16
|
+
import { Pressable, StyleSheet, View } from "react-native";
|
|
17
|
+
import { Button } from "../native-base/button.js";
|
|
18
|
+
import { Field } from "../native-base/field.js";
|
|
19
|
+
import { Screen } from "../native-base/screen.js";
|
|
20
|
+
import { Text } from "../native-base/text.js";
|
|
21
|
+
import { theme } from "../native-base/theme.js";
|
|
22
|
+
/** Rich text's inline runs as text: bold and italic kept, links drawn as their words. */
|
|
23
|
+
function inlineText(content) {
|
|
24
|
+
return content
|
|
25
|
+
.map((run) => {
|
|
26
|
+
const r = run;
|
|
27
|
+
return r.type === "break" ? "\n" : r.type === "text" ? (r.text ?? "") : "";
|
|
28
|
+
})
|
|
29
|
+
.join("");
|
|
30
|
+
}
|
|
31
|
+
function Choice({ label, checked, several, invalid, onPress, }) {
|
|
32
|
+
return (_jsxs(Pressable, { accessibilityRole: several ? "checkbox" : "radio", accessibilityState: { checked }, accessibilityLabel: label, onPress: onPress, style: [styles.option, checked ? styles.optionOn : null, invalid ? styles.invalid : null], children: [_jsx(View, { style: [styles.mark, several ? null : styles.round, checked ? styles.markOn : null] }), _jsx(Text, { style: styles.optionLabel, children: label })] }));
|
|
33
|
+
}
|
|
34
|
+
/** One question (or a heading or paragraph), drawn by its kind. */
|
|
35
|
+
export function NativeQuestion({ item, value, error, onChange, }) {
|
|
36
|
+
if (item.kind === "heading") {
|
|
37
|
+
return (_jsx(Text, { accessibilityRole: "header", style: styles.heading, children: item.text }));
|
|
38
|
+
}
|
|
39
|
+
if (item.kind === "paragraph")
|
|
40
|
+
return _jsx(Text, { children: inlineText(item.content) });
|
|
41
|
+
const required = item.required ? " (required)" : "";
|
|
42
|
+
const notes = (_jsxs(_Fragment, { children: [item.kind !== "consent" && item.help ? _jsx(Text, { tone: "muted", children: item.help }) : null, error &&
|
|
43
|
+
(item.kind === "consent" ||
|
|
44
|
+
item.kind === "choice" ||
|
|
45
|
+
item.kind === "rating" ||
|
|
46
|
+
item.kind === "yesNo") ? (_jsx(Text, { tone: "danger", children: error })) : null] }));
|
|
47
|
+
if (item.kind === "consent") {
|
|
48
|
+
return (_jsxs(View, { style: styles.question, children: [_jsx(Choice, { label: `${item.words}${required}`, checked: value === true, several: true, invalid: Boolean(error), onPress: () => onChange(value !== true) }), notes] }));
|
|
49
|
+
}
|
|
50
|
+
if (item.kind === "choice" || item.kind === "rating" || item.kind === "yesNo") {
|
|
51
|
+
const options = item.kind === "choice"
|
|
52
|
+
? item.options.map((o) => ({ value: o, label: o }))
|
|
53
|
+
: item.kind === "rating"
|
|
54
|
+
? ratingValues(item).map((n) => ({ value: n, label: ratingLabel(item, n) }))
|
|
55
|
+
: [
|
|
56
|
+
{ value: true, label: "Yes" },
|
|
57
|
+
{ value: false, label: "No" },
|
|
58
|
+
];
|
|
59
|
+
const several = item.kind === "choice" && item.multiple === true;
|
|
60
|
+
const chosen = (v) => several ? Array.isArray(value) && value.includes(v) : value === v;
|
|
61
|
+
return (_jsxs(View, { style: styles.question, accessibilityRole: several ? undefined : "radiogroup", children: [_jsxs(Text, { style: styles.label, children: [item.label, required] }), notes, _jsx(View, { style: item.kind === "choice" ? styles.list : styles.row, children: options.map((option) => (_jsx(Choice, { label: option.label, checked: chosen(option.value), several: several, invalid: Boolean(error), onPress: () => {
|
|
62
|
+
if (!several)
|
|
63
|
+
return onChange(option.value);
|
|
64
|
+
const now = Array.isArray(value) ? value : [];
|
|
65
|
+
const v = option.value;
|
|
66
|
+
onChange(now.includes(v) ? now.filter((x) => x !== v) : [...now, v]);
|
|
67
|
+
} }, String(option.value)))) })] }));
|
|
68
|
+
}
|
|
69
|
+
const text = typeof value === "string" ? value : "";
|
|
70
|
+
return (_jsxs(View, { style: styles.question, children: [_jsx(Field, { label: `${item.label}${required}`, error: error ?? null, value: text, onChangeText: (v) => onChange(v), multiline: item.kind === "text" && item.long === true, maxLength: item.kind === "text" ? (item.max ?? (item.long ? 5000 : 200)) : undefined, keyboardType: item.kind === "email"
|
|
71
|
+
? "email-address"
|
|
72
|
+
: item.kind === "phone"
|
|
73
|
+
? "phone-pad"
|
|
74
|
+
: item.kind === "number"
|
|
75
|
+
? "decimal-pad"
|
|
76
|
+
: "default", autoCapitalize: item.kind === "email" ? "none" : "sentences", autoComplete: item.kind === "email" ? "email" : item.kind === "phone" ? "tel" : undefined, placeholder: item.kind === "date" ? "YYYY-MM-DD" : undefined, numberOfLines: item.kind === "text" && item.long ? 6 : 1 }), item.help ? _jsx(Text, { tone: "muted", children: item.help }) : null] }));
|
|
77
|
+
}
|
|
78
|
+
function nameOf(item) {
|
|
79
|
+
if (!isAsked(item))
|
|
80
|
+
return "";
|
|
81
|
+
return (item.kind === "consent" ? item.words : item.label).slice(0, 80);
|
|
82
|
+
}
|
|
83
|
+
/** A form being filled: its questions, what to fix, the spam check where asked, and the send. */
|
|
84
|
+
export function NativeFormFields({ form, filler, Turnstile, }) {
|
|
85
|
+
const [token, setToken] = useState(null);
|
|
86
|
+
const faults = filler.items.filter((item) => filler.errors[item.key]);
|
|
87
|
+
return (_jsxs(View, { style: styles.body, children: [faults.length || filler.problem ? (_jsxs(View, { accessibilityRole: "alert", accessibilityLiveRegion: "assertive", style: styles.summary, children: [_jsx(Text, { style: styles.label, children: faults.length ? "Some answers need another look" : "Your answers were not sent" }), filler.problem && filler.problem.kind !== "answers" ? (_jsx(Text, { tone: "danger", children: filler.problem.message })) : null, faults.map((item) => (_jsxs(Text, { tone: "danger", children: [nameOf(item), ": ", filler.errors[item.key]] }, item.key)))] })) : null, filler.items.map((item) => (_jsx(NativeQuestion, { item: item, value: filler.draft[item.key], error: filler.errors[item.key], onChange: (v) => filler.set(item.key, v) }, item.key))), form.turnstile ? (Turnstile ? (_jsx(Turnstile, { onToken: setToken })) : (_jsx(Text, { tone: "muted", children: "This form cannot be sent from the app just now." }))) : null, _jsx(Button, { label: "Send", busy: filler.status === "sending", disabled: form.turnstile && !Turnstile, onPress: () => {
|
|
88
|
+
void filler.send(form.turnstile ? { turnstileToken: token } : {});
|
|
89
|
+
} })] }));
|
|
90
|
+
}
|
|
91
|
+
function Opened({ form, filler, Turnstile, onDone, }) {
|
|
92
|
+
const sent = filler.status === "sent";
|
|
93
|
+
return (_jsx(Screen, { title: form.title ?? "Form", children: sent ? (_jsxs(View, { style: styles.body, children: [_jsx(Text, { accessibilityLiveRegion: "polite", children: filler.thanks }), onDone ? _jsx(Button, { label: "Done", onPress: onDone }) : null] })) : (_jsx(NativeFormFields, { form: form, filler: filler, Turnstile: Turnstile })) }));
|
|
94
|
+
}
|
|
95
|
+
function NotOpen({ error, link }) {
|
|
96
|
+
if (!error) {
|
|
97
|
+
return (_jsx(Screen, { children: _jsx(Text, { tone: "muted", children: "Loading\u2026" }) }));
|
|
98
|
+
}
|
|
99
|
+
const refusal = refusalOf(error);
|
|
100
|
+
return (_jsx(Screen, { children: _jsx(Text, { accessibilityRole: "alert", children: refusal.kind === "offline" || refusal.kind === "other" || refusal.kind === "later"
|
|
101
|
+
? "This form could not be opened just now. Try again shortly."
|
|
102
|
+
: link
|
|
103
|
+
? "This link cannot be used: it may have been used already or have expired."
|
|
104
|
+
: "This form is not taking answers just now." }) }));
|
|
105
|
+
}
|
|
106
|
+
/** A form by its address, in the app. `Turnstile`: the app's widget, for a visitor's public form. */
|
|
107
|
+
export function FormScreen({ forms, slug, Turnstile, onDone, }) {
|
|
108
|
+
const opened = forms.useForm({ slug });
|
|
109
|
+
const filler = forms.useAnswer(opened.data, { via: "app" });
|
|
110
|
+
if (!opened.data)
|
|
111
|
+
return _jsx(NotOpen, { error: opened.error, link: false });
|
|
112
|
+
return _jsx(Opened, { form: opened.data, filler: filler, Turnstile: Turnstile, onDone: onDone });
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A signed-in member's own personal link (or a child's they are a guardian of), opened from Needs
|
|
116
|
+
* your attention by the link's id. Answered once; the item goes once sent.
|
|
117
|
+
*/
|
|
118
|
+
export function LinkedFormScreen({ forms, linkId, onDone, }) {
|
|
119
|
+
const opened = forms.useLinkedForm(linkId);
|
|
120
|
+
const filler = forms.useAnswerLinked(opened.data, linkId);
|
|
121
|
+
if (!opened.data)
|
|
122
|
+
return _jsx(NotOpen, { error: opened.error, link: true });
|
|
123
|
+
return _jsx(Opened, { form: opened.data, filler: filler, onDone: onDone });
|
|
124
|
+
}
|
|
125
|
+
const styles = StyleSheet.create({
|
|
126
|
+
body: { gap: theme.space.md },
|
|
127
|
+
question: { gap: theme.space.xs },
|
|
128
|
+
heading: { fontSize: theme.font.body + 2, fontWeight: "600", color: theme.color.text },
|
|
129
|
+
label: { fontSize: theme.font.body, fontWeight: "600", color: theme.color.text },
|
|
130
|
+
list: { gap: theme.space.xs },
|
|
131
|
+
row: { flexDirection: "row", flexWrap: "wrap", gap: theme.space.xs },
|
|
132
|
+
option: {
|
|
133
|
+
minHeight: 44,
|
|
134
|
+
flexDirection: "row",
|
|
135
|
+
alignItems: "center",
|
|
136
|
+
gap: theme.space.sm,
|
|
137
|
+
paddingHorizontal: theme.space.sm + 4,
|
|
138
|
+
borderWidth: 1,
|
|
139
|
+
borderColor: theme.color.border,
|
|
140
|
+
borderRadius: theme.radius,
|
|
141
|
+
},
|
|
142
|
+
optionOn: { borderColor: theme.color.primary, backgroundColor: theme.color.surface },
|
|
143
|
+
invalid: { borderColor: theme.color.danger },
|
|
144
|
+
optionLabel: { fontSize: theme.font.body, color: theme.color.text, flexShrink: 1 },
|
|
145
|
+
mark: {
|
|
146
|
+
width: 20,
|
|
147
|
+
height: 20,
|
|
148
|
+
borderWidth: 2,
|
|
149
|
+
borderColor: theme.color.muted,
|
|
150
|
+
borderRadius: 4,
|
|
151
|
+
},
|
|
152
|
+
round: { borderRadius: 10 },
|
|
153
|
+
markOn: { borderColor: theme.color.primary, backgroundColor: theme.color.primary },
|
|
154
|
+
summary: {
|
|
155
|
+
gap: theme.space.xs,
|
|
156
|
+
padding: theme.space.sm + 4,
|
|
157
|
+
borderWidth: 2,
|
|
158
|
+
borderColor: theme.color.danger,
|
|
159
|
+
borderRadius: theme.radius,
|
|
160
|
+
},
|
|
161
|
+
});
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { type ClientHolding, type PackClient, type UseMutation } from "@composable-os/client";
|
|
2
|
+
import type { z } from "@composable-os/sdk";
|
|
3
|
+
import { type AnswerIssue, type QuestionItem, type Questions } from "@composable-os/sdk/questions";
|
|
4
|
+
import type { form, linkedForm, submit, submitLinked } from "../tools.ts";
|
|
5
|
+
type Io<T extends {
|
|
6
|
+
input: z.ZodType;
|
|
7
|
+
output: z.ZodType;
|
|
8
|
+
}> = {
|
|
9
|
+
input: z.input<T["input"]>;
|
|
10
|
+
output: z.output<T["output"]>;
|
|
11
|
+
};
|
|
12
|
+
/** The operations this entry calls, as the generated client types them. */
|
|
13
|
+
export type FormsOperations = {
|
|
14
|
+
"x.forms.form": {
|
|
15
|
+
kind: "query";
|
|
16
|
+
} & Io<typeof form>;
|
|
17
|
+
"x.forms.linkedForm": {
|
|
18
|
+
kind: "query";
|
|
19
|
+
} & Io<typeof linkedForm>;
|
|
20
|
+
"x.forms.submit": {
|
|
21
|
+
kind: "mutation";
|
|
22
|
+
} & Io<typeof submit>;
|
|
23
|
+
"x.forms.submitLinked": {
|
|
24
|
+
kind: "mutation";
|
|
25
|
+
} & Io<typeof submitLinked>;
|
|
26
|
+
};
|
|
27
|
+
type Name = keyof FormsOperations & string;
|
|
28
|
+
type In<N extends Name> = FormsOperations[N]["input"];
|
|
29
|
+
type Out<N extends Name> = FormsOperations[N]["output"];
|
|
30
|
+
/** A form as the host opened it for the caller: its version, questions, thanks and checks. */
|
|
31
|
+
export type OpenedForm = Out<"x.forms.form">;
|
|
32
|
+
export type SubmitInput = In<"x.forms.submit">;
|
|
33
|
+
/** What a client says about each operation at run time (a generated client's specs). */
|
|
34
|
+
export declare const FORMS_OPERATIONS: {
|
|
35
|
+
readonly "x.forms.form": {
|
|
36
|
+
readonly kind: "query";
|
|
37
|
+
readonly invalidates: readonly [];
|
|
38
|
+
};
|
|
39
|
+
readonly "x.forms.linkedForm": {
|
|
40
|
+
readonly kind: "query";
|
|
41
|
+
readonly invalidates: readonly [];
|
|
42
|
+
};
|
|
43
|
+
readonly "x.forms.submit": {
|
|
44
|
+
readonly kind: "mutation";
|
|
45
|
+
readonly invalidates: readonly [];
|
|
46
|
+
};
|
|
47
|
+
readonly "x.forms.submitLinked": {
|
|
48
|
+
readonly kind: "mutation";
|
|
49
|
+
readonly invalidates: readonly ["me.attention"];
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/** The client of every operation this entry calls. */
|
|
53
|
+
export type FormsClient = PackClient<FormsOperations>;
|
|
54
|
+
export type { QuestionItem, Questions } from "@composable-os/sdk/questions";
|
|
55
|
+
/** The questions of a form the host opened, typed: the host checked them when it was published. */
|
|
56
|
+
export declare function formQuestions(opened: Pick<OpenedForm, "questions">): Questions;
|
|
57
|
+
/** A question a person answers (not a heading or a paragraph). */
|
|
58
|
+
export type AskedItem = Exclude<QuestionItem, {
|
|
59
|
+
kind: "heading" | "paragraph";
|
|
60
|
+
}>;
|
|
61
|
+
/** Whether an item asks something. */
|
|
62
|
+
export declare function isAsked(item: QuestionItem): item is AskedItem;
|
|
63
|
+
/**
|
|
64
|
+
* What a screen holds for each question while a person fills the form, by key: text, email,
|
|
65
|
+
* phone, number and date as the text typed; a single choice as the option; a choice of several as
|
|
66
|
+
* the options; a rating as its number; yes/no as true, false or null (not answered); a tick box as
|
|
67
|
+
* true or false.
|
|
68
|
+
*/
|
|
69
|
+
export type DraftValue = string | string[] | number | boolean | null;
|
|
70
|
+
export type Draft = Readonly<Record<string, DraftValue>>;
|
|
71
|
+
/** The ratings a rating question offers: 1 to 5 stars, or 0 to 10. */
|
|
72
|
+
export declare function ratingValues(item: {
|
|
73
|
+
scale: "stars" | "ten";
|
|
74
|
+
}): number[];
|
|
75
|
+
/** The words beside a rating's number, for a screen reader and a button's label. */
|
|
76
|
+
export declare function ratingLabel(item: {
|
|
77
|
+
scale: "stars" | "ten";
|
|
78
|
+
}, value: number): string;
|
|
79
|
+
/**
|
|
80
|
+
* The answers to send, from what the screen holds: trimmed and converted as `checkAnswers` takes
|
|
81
|
+
* them, unanswered questions left out, and nothing for a question that is not shown.
|
|
82
|
+
*/
|
|
83
|
+
export declare function answersFrom(questions: Questions, draft: Draft): Record<string, unknown>;
|
|
84
|
+
/** The items to draw now: those with no "show only if", or whose condition holds. */
|
|
85
|
+
export declare function shownItems(questions: Questions, draft: Draft): QuestionItem[];
|
|
86
|
+
/** The name a form field has on a page drawn as HTML (the host's form page): `a.<key>`. */
|
|
87
|
+
export declare const fieldName: (key: string) => string;
|
|
88
|
+
/** The hidden field a person leaves empty (the honeypot): a bot fills every field. */
|
|
89
|
+
export declare const HONEYPOT_FIELD = "website";
|
|
90
|
+
/**
|
|
91
|
+
* What a page drawn as HTML posted, as a draft: `values(name)` gives every value posted under a
|
|
92
|
+
* field's name (a choice of several, several). The reverse of the page's fields.
|
|
93
|
+
*/
|
|
94
|
+
export declare function draftFromFields(questions: Questions, values: (name: string) => string[]): Draft;
|
|
95
|
+
/** Faults by question key, in a person's words, and anything about the form as a whole. */
|
|
96
|
+
export interface AnswerErrors {
|
|
97
|
+
byKey: Readonly<Record<string, string>>;
|
|
98
|
+
general: string | null;
|
|
99
|
+
}
|
|
100
|
+
/** The words to show beside a question for one fault the check found (never its value). */
|
|
101
|
+
export declare function issueWords(issue: AnswerIssue, item: QuestionItem | undefined): string;
|
|
102
|
+
/** The faults of a check, by key and in a person's words. */
|
|
103
|
+
export declare function errorsOf(questions: Questions, issues: readonly AnswerIssue[]): AnswerErrors;
|
|
104
|
+
/**
|
|
105
|
+
* The answers to send, or what to fix first: the same check the host makes (the host makes it
|
|
106
|
+
* again and its answer is the one that counts).
|
|
107
|
+
*/
|
|
108
|
+
export declare function checkDraft(questions: Questions, draft: Draft): {
|
|
109
|
+
ok: true;
|
|
110
|
+
answers: Record<string, unknown>;
|
|
111
|
+
} | ({
|
|
112
|
+
ok: false;
|
|
113
|
+
} & AnswerErrors);
|
|
114
|
+
/**
|
|
115
|
+
* What the host said when it did not take the answers, for the screen to show:
|
|
116
|
+
* - `answers`: some answers need another look (by key);
|
|
117
|
+
* - `reload`: the form changed, or was open a long time: load it again;
|
|
118
|
+
* - `check`: the spam check (Turnstile) did not run or did not pass: try again;
|
|
119
|
+
* - `later`: too many answers just now, or the check could not be made: try again later;
|
|
120
|
+
* - `gone`: the form is not open to this caller now, or a personal link cannot be used;
|
|
121
|
+
* - `closed`: the form takes no answers from people who are not signed in here;
|
|
122
|
+
* - `offline`: the host could not be reached (what was typed is still on the screen);
|
|
123
|
+
* - `other`: anything else.
|
|
124
|
+
*/
|
|
125
|
+
export type Refusal = ({
|
|
126
|
+
kind: "answers";
|
|
127
|
+
message: string;
|
|
128
|
+
} & AnswerErrors) | {
|
|
129
|
+
kind: "reload" | "check" | "later" | "gone" | "closed" | "offline" | "other";
|
|
130
|
+
message: string;
|
|
131
|
+
};
|
|
132
|
+
/** The host's answer as a Refusal. */
|
|
133
|
+
export declare function refusalOf(error: unknown, questions?: Questions): Refusal;
|
|
134
|
+
/** The query parameter a personal link's token travels in (the SDK's PERSONAL_LINK_PARAM). */
|
|
135
|
+
export declare const LINK_PARAM = "t";
|
|
136
|
+
/**
|
|
137
|
+
* A personal link's token, read from the page's address once and taken out of it at once
|
|
138
|
+
* (`history.replaceState`), so it is not left in the address bar, the history or a bookmark, nor
|
|
139
|
+
* sent on as a referrer. The caller keeps it in memory only, for the send. Null with none.
|
|
140
|
+
*/
|
|
141
|
+
export declare function takeLinkToken(page: {
|
|
142
|
+
location: {
|
|
143
|
+
href: string;
|
|
144
|
+
};
|
|
145
|
+
history: {
|
|
146
|
+
replaceState(data: unknown, unused: string, url?: string): void;
|
|
147
|
+
};
|
|
148
|
+
}): string | null;
|
|
149
|
+
/** The app's `useState`. */
|
|
150
|
+
export type UseState = <T>(initial: T) => [T, (next: T | ((previous: T) => T)) => void];
|
|
151
|
+
export type { UseMutation } from "@composable-os/client";
|
|
152
|
+
/**
|
|
153
|
+
* The app's `useQuery` (TanStack Query's), as far as these hooks use it: with the options that
|
|
154
|
+
* keep an opened form as it was first opened (`staleTime`, no refetch on focus or reconnect), so
|
|
155
|
+
* the host's token of when it was shown never restarts while the person fills it in.
|
|
156
|
+
*/
|
|
157
|
+
export type UseQuery = <T>(options: {
|
|
158
|
+
queryKey: readonly unknown[];
|
|
159
|
+
queryFn: () => Promise<T>;
|
|
160
|
+
enabled?: boolean;
|
|
161
|
+
gcTime?: number;
|
|
162
|
+
staleTime?: number;
|
|
163
|
+
refetchOnWindowFocus?: boolean;
|
|
164
|
+
refetchOnReconnect?: boolean;
|
|
165
|
+
}) => {
|
|
166
|
+
data: T | undefined;
|
|
167
|
+
isPending: boolean;
|
|
168
|
+
error: unknown;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* How a form is kept once opened: never refetched while it is on the screen. A refetch would
|
|
172
|
+
* bring a new shown time, and a send within three seconds of it would be dropped as a bot's
|
|
173
|
+
* (review of PR #461); a form that changed meanwhile is the host's "load it again".
|
|
174
|
+
*/
|
|
175
|
+
export declare const OPENED_FORM_QUERY: {
|
|
176
|
+
readonly gcTime: 0;
|
|
177
|
+
readonly staleTime: number;
|
|
178
|
+
readonly refetchOnWindowFocus: false;
|
|
179
|
+
readonly refetchOnReconnect: false;
|
|
180
|
+
};
|
|
181
|
+
/** Where a form is filled in (the submission's `via`): the business's website or its app. */
|
|
182
|
+
export type Via = "site" | "app" | "page";
|
|
183
|
+
/** What a filler is doing. */
|
|
184
|
+
export type FillStatus = "filling" | "sending" | "sent";
|
|
185
|
+
/** The extra a send carries: a visitor's Turnstile token and the honeypot field's value. */
|
|
186
|
+
export interface SendChecks {
|
|
187
|
+
turnstileToken?: string | null;
|
|
188
|
+
honeypot?: string;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The operations each hook calls (ADR 0453). A customer's or the public's client gets `useForm`
|
|
192
|
+
* and `useAnswer`; a signed-in customer's also `useLinkedForm` and `useAnswerLinked`, for a form
|
|
193
|
+
* from Needs your attention.
|
|
194
|
+
*/
|
|
195
|
+
export declare const FORMS_HOOKS_CALL: {
|
|
196
|
+
readonly useForm: readonly ["x.forms.form"];
|
|
197
|
+
readonly useLinkedForm: readonly ["x.forms.linkedForm"];
|
|
198
|
+
readonly useAnswer: readonly ["x.forms.submit"];
|
|
199
|
+
readonly useAnswerLinked: readonly ["x.forms.submitLinked"];
|
|
200
|
+
};
|
|
201
|
+
type React = {
|
|
202
|
+
useQuery: UseQuery;
|
|
203
|
+
useMutation: UseMutation;
|
|
204
|
+
useState: UseState;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Hooks for a React or React Native app:
|
|
208
|
+
* `const forms = createFormsHooks({ useQuery, useMutation, useState }, api)`. `api` is the
|
|
209
|
+
* customer's generated client (signed in, or the public).
|
|
210
|
+
*/
|
|
211
|
+
export declare function createFormsHooks<C extends ClientHolding>(react: React, client: C): any;
|
|
212
|
+
declare function allFormsHooks(react: React, api: FormsClient): {
|
|
213
|
+
/**
|
|
214
|
+
* A form as the caller may answer it now: by its address (`slug`), or by a personal link's
|
|
215
|
+
* token (`link`, which opening uses nothing of). Not found when it is not open to them.
|
|
216
|
+
*/
|
|
217
|
+
useForm(which: {
|
|
218
|
+
slug: string;
|
|
219
|
+
} | {
|
|
220
|
+
link: string;
|
|
221
|
+
} | null): {
|
|
222
|
+
data: {
|
|
223
|
+
id: string;
|
|
224
|
+
version: number;
|
|
225
|
+
title: string | null;
|
|
226
|
+
questions: {
|
|
227
|
+
items: Record<string, unknown>[];
|
|
228
|
+
};
|
|
229
|
+
thanks: string | null;
|
|
230
|
+
anonymous: boolean;
|
|
231
|
+
turnstile: boolean;
|
|
232
|
+
shown: string | null;
|
|
233
|
+
} | undefined;
|
|
234
|
+
isPending: boolean;
|
|
235
|
+
error: unknown;
|
|
236
|
+
};
|
|
237
|
+
/** The form a signed-in member's own personal link is for (Needs your attention's `linkId`). */
|
|
238
|
+
useLinkedForm(linkId: string | null): {
|
|
239
|
+
data: {
|
|
240
|
+
id: string;
|
|
241
|
+
version: number;
|
|
242
|
+
title: string | null;
|
|
243
|
+
questions: {
|
|
244
|
+
items: Record<string, unknown>[];
|
|
245
|
+
};
|
|
246
|
+
thanks: string | null;
|
|
247
|
+
anonymous: boolean;
|
|
248
|
+
turnstile: boolean;
|
|
249
|
+
shown: string | null;
|
|
250
|
+
} | undefined;
|
|
251
|
+
isPending: boolean;
|
|
252
|
+
error: unknown;
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* Filling in a form `useForm` opened, and sending it: on the business's website (`site`) or
|
|
256
|
+
* its app (`app`), or by a personal link's token (`link`).
|
|
257
|
+
*/
|
|
258
|
+
useAnswer(form: OpenedForm | undefined, way: {
|
|
259
|
+
via: Via;
|
|
260
|
+
link?: string | null;
|
|
261
|
+
}): {
|
|
262
|
+
draft: Readonly<Record<string, DraftValue>>;
|
|
263
|
+
/** The items to draw now, in order (headings and paragraphs among them). */
|
|
264
|
+
items: QuestionItem[];
|
|
265
|
+
/** What to fix, by question key. */
|
|
266
|
+
errors: Readonly<Record<string, string>>;
|
|
267
|
+
/** What the host (or the connection) said, for the form as a whole. */
|
|
268
|
+
problem: Refusal | null;
|
|
269
|
+
status: FillStatus;
|
|
270
|
+
/** The words to show once sent: the form's own thank-you, or a plain one. */
|
|
271
|
+
thanks: string;
|
|
272
|
+
set(key: string, value: DraftValue): void;
|
|
273
|
+
/**
|
|
274
|
+
* Checks the answers as the host will, then sends them. True when the host took them. A
|
|
275
|
+
* refusal stays on the screen (`problem`, `errors`), with the answers as typed.
|
|
276
|
+
*/
|
|
277
|
+
send(checks?: SendChecks): Promise<boolean>;
|
|
278
|
+
};
|
|
279
|
+
/** Filling in and sending the form of a signed-in member's own personal link, by its id. */
|
|
280
|
+
useAnswerLinked(form: OpenedForm | undefined, linkId: string | null): {
|
|
281
|
+
draft: Readonly<Record<string, DraftValue>>;
|
|
282
|
+
/** The items to draw now, in order (headings and paragraphs among them). */
|
|
283
|
+
items: QuestionItem[];
|
|
284
|
+
/** What to fix, by question key. */
|
|
285
|
+
errors: Readonly<Record<string, string>>;
|
|
286
|
+
/** What the host (or the connection) said, for the form as a whole. */
|
|
287
|
+
problem: Refusal | null;
|
|
288
|
+
status: FillStatus;
|
|
289
|
+
/** The words to show once sent: the form's own thank-you, or a plain one. */
|
|
290
|
+
thanks: string;
|
|
291
|
+
set(key: string, value: DraftValue): void;
|
|
292
|
+
/**
|
|
293
|
+
* Checks the answers as the host will, then sends them. True when the host took them. A
|
|
294
|
+
* refusal stays on the screen (`problem`, `errors`), with the answers as typed.
|
|
295
|
+
*/
|
|
296
|
+
send(checks?: SendChecks): Promise<boolean>;
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
/** Every hook of this entry. A block takes the ones it calls: `Pick<FormsHooks, "useForm">`. */
|
|
300
|
+
export type FormsHooks = ReturnType<typeof allFormsHooks>;
|
|
301
|
+
/** What a filler hook returns (`useAnswer`, `useAnswerLinked`). */
|
|
302
|
+
export type Filler = ReturnType<FormsHooks["useAnswer"]>;
|