@sincpro/web-framework 2.0.0 → 2.2.0
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/README.md +10 -10
- package/dist/core/copy.d.ts +3 -1
- package/dist/core/copy.js +2 -2
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -2
- package/dist/core/lines.d.ts +12 -4
- package/dist/core/lines.js +15 -3
- package/dist/core/present.d.ts +1 -0
- package/dist/core/present.js +54 -6
- package/dist/core/related.d.ts +5 -1
- package/dist/core/related.js +13 -12
- package/dist/core/relation.d.ts +23 -1
- package/dist/core/relation.js +33 -1
- package/dist/react/context.d.ts +5 -1
- package/dist/react/context.js +11 -2
- package/dist/react/useGroups.js +2 -2
- package/dist/react/useHistory.js +2 -2
- package/dist/react/useListing.js +10 -1
- package/dist/react/useLookup.js +2 -2
- package/dist/react/useNameOf.js +2 -2
- package/dist/react/usePivot.js +2 -2
- package/dist/react/useRecord.js +17 -2
- package/dist/react/useRelated.d.ts +5 -4
- package/dist/react/useRelated.js +14 -13
- package/dist/web/index.d.ts +1 -1
- package/dist/web/index.js +1 -1
- package/dist/web/record/EntityPicker.d.ts +7 -4
- package/dist/web/record/EntityPicker.js +5 -5
- package/dist/web/record/RelatedList.js +9 -6
- package/dist/web/record/control.d.ts +3 -1
- package/dist/web/record/control.js +8 -5
- package/dist/web/record/edit.js +23 -3
- package/dist/web/record/lines.js +13 -2
- package/dist/web/record/record.d.ts +12 -2
- package/dist/web/record/record.js +10 -2
- package/dist/web/workspace/SincproApp.d.ts +8 -18
- package/dist/web/workspace/SincproApp.js +17 -5
- package/dist/web/workspace/record.d.ts +59 -12
- package/dist/web/workspace/record.js +88 -27
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -427,7 +427,7 @@ hook and draw your own, or go straight to the loose piece.
|
|
|
427
427
|
| How does a record look? | `presented(entity, { option, row, card })` | — | `presentationOf` |
|
|
428
428
|
| N records: search, filter, page | `EntityList`, `EntitySearchBar` | `useListing`, `useGroups` | `transport.list(route, criteria)` |
|
|
429
429
|
| A named slice (to collect, …) | `defineList` + `listView` | `useListing(list)` | `scopeOf` |
|
|
430
|
-
| A record's whole screen | `EntityRecord`, `entityView` without `record` |
|
|
430
|
+
| A record's whole screen | `EntityRecord`, `entityView` without `record` | `useRecordScreen` | `formOf` |
|
|
431
431
|
| Previous / next from a record | `EntityRecord` (from a list) | — | `placeOf`, `stepIn` (`Trail`) |
|
|
432
432
|
| How much each group adds up | `EntityList sums` | `useGroups` (`sums`) | `sumsOf`, `detailOf` |
|
|
433
433
|
| One record: read, edit, save | `RecordField`, `factsOf`, `RecordLines` | `useRecord` | `transport.one`, `transport.send` |
|
|
@@ -459,15 +459,15 @@ The rules behind them, in short:
|
|
|
459
459
|
|
|
460
460
|
There is no store of ours to learn: each kind of state already has its owner.
|
|
461
461
|
|
|
462
|
-
| State | Lives in
|
|
463
|
-
| -------------------------------------------- |
|
|
464
|
-
| Who answers, language, known models | `FrameworkProvider` (this package)
|
|
465
|
-
| What the backend sees on every request |
|
|
466
|
-
| What an opening carries (company, a default) | composer: the reference's `params`
|
|
467
|
-
| The record a modal covers | composer: `api.over`
|
|
468
|
-
| What a chooser was asked | composer: `pick(…, { question })`
|
|
469
|
-
| What the user is on, what is selected | composer: `api.context`
|
|
470
|
-
| A screen's list, record, groups | the hooks
|
|
462
|
+
| State | Lives in | Read with |
|
|
463
|
+
| -------------------------------------------- | --------------------------------------------------------- | ---------------------------------- |
|
|
464
|
+
| Who answers, language, known models | `FrameworkProvider` (this package) | the hooks, through context |
|
|
465
|
+
| What the backend sees on every request | `processContext` (client-framework), as `sincpro-context` | the backend's `use_context()` |
|
|
466
|
+
| What an opening carries (company, a default) | composer: the reference's `params` | `api.self.params`, `api.setParams` |
|
|
467
|
+
| The record a modal covers | composer: `api.over` | `api.over` |
|
|
468
|
+
| What a chooser was asked | composer: `pick(…, { question })` | `api.choosing` |
|
|
469
|
+
| What the user is on, what is selected | composer: `api.context` | `useWorkspaceContext(api)` |
|
|
470
|
+
| A screen's list, record, groups | the hooks | `useListing`, `useRecord`, … |
|
|
471
471
|
|
|
472
472
|
Not yet: a new record does not take the `params` it was opened with as its initial values
|
|
473
473
|
(`ROADMAP.md`, «Estado y contexto»).
|
package/dist/core/copy.d.ts
CHANGED
|
@@ -48,7 +48,9 @@ export type FrameworkCopy = {
|
|
|
48
48
|
keep: string;
|
|
49
49
|
loading: string;
|
|
50
50
|
/** Under an embedded list the record holds only one page of. */
|
|
51
|
-
|
|
51
|
+
/** A relation that brought one page of its records: how many it shows, of how many — a
|
|
52
|
+
* floor when `exact` is false («at least 1000»). */
|
|
53
|
+
partial: (shown: number, total: number, exact?: boolean) => string;
|
|
52
54
|
/** The title of a record that does not exist yet, after the model's name. */
|
|
53
55
|
creating: (name: string) => string;
|
|
54
56
|
/** The list's button that opens the form of a new record. */
|
package/dist/core/copy.js
CHANGED
|
@@ -26,7 +26,7 @@ export const ENGLISH_FRAMEWORK_COPY = {
|
|
|
26
26
|
missing: (fields) => `Missing: ${fields.join(", ")}.`,
|
|
27
27
|
keep: "Keep draft",
|
|
28
28
|
loading: "Loading the record",
|
|
29
|
-
partial: (shown, total) => `Showing ${shown} of ${total}.`,
|
|
29
|
+
partial: (shown, total, exact = true) => `Showing ${shown} of ${exact ? "" : "at least "}${total}.`,
|
|
30
30
|
creating: (name) => (name ? `${name} · new` : "New"),
|
|
31
31
|
create: "New",
|
|
32
32
|
add: "Add",
|
|
@@ -96,7 +96,7 @@ export const SPANISH_FRAMEWORK_COPY = {
|
|
|
96
96
|
missing: (fields) => `Falta: ${fields.join(", ")}.`,
|
|
97
97
|
keep: "Guardar borrador",
|
|
98
98
|
loading: "Cargando el registro",
|
|
99
|
-
partial: (shown, total) => `Se muestran ${shown} de ${total}.`,
|
|
99
|
+
partial: (shown, total, exact = true) => `Se muestran ${shown} de ${exact ? "" : "al menos "}${total}.`,
|
|
100
100
|
creating: (name) => (name ? `${name} · nuevo` : "Nuevo"),
|
|
101
101
|
create: "Nuevo",
|
|
102
102
|
add: "Agregar",
|
package/dist/core/index.d.ts
CHANGED
|
@@ -13,12 +13,12 @@ export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaults
|
|
|
13
13
|
export { explain } from "./explain.js";
|
|
14
14
|
export { type FormLayout, formOf } from "./form.js";
|
|
15
15
|
export { type HistoryEntry, historyOf } from "./history.js";
|
|
16
|
-
export { addingOf, type AddingRule, blankLine, type Declared, editableLines, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
16
|
+
export { addingOf, type AddingRule, blankLine, type Declared, editableLines, isReference, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
17
17
|
export { defineList, type GroupLevel, isList, type ListDefinition, type ListSpec, type NamedFilter, scopeOf, } from "./list.js";
|
|
18
18
|
export { lookupOf, type LookupOption } from "./lookup.js";
|
|
19
19
|
export { detailOf, measuresOf, sumsOf } from "./measure.js";
|
|
20
20
|
export { BOOKKEEPING, defaultFields, type FieldView, sortKeyOf, textOf, viewOf, } from "./present.js";
|
|
21
21
|
export { type Related, relatedOf } from "./related.js";
|
|
22
|
-
export { entityFor, type RelationField, relationFieldOf, } from "./relation.js";
|
|
22
|
+
export { backReference, entityFor, relatedScope, type RelationField, relationFieldOf, } from "./relation.js";
|
|
23
23
|
export { type RecordSummary, summaryFieldsOf, summaryOf, } from "./summary.js";
|
|
24
24
|
export { placeOf, stepIn, type Trail } from "./trail.js";
|
package/dist/core/index.js
CHANGED
|
@@ -13,12 +13,12 @@ export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt,
|
|
|
13
13
|
export { explain } from "./explain.js";
|
|
14
14
|
export { formOf } from "./form.js";
|
|
15
15
|
export { historyOf } from "./history.js";
|
|
16
|
-
export { addingOf, blankLine, editableLines, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
16
|
+
export { addingOf, blankLine, editableLines, isReference, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
17
17
|
export { defineList, isList, scopeOf, } from "./list.js";
|
|
18
18
|
export { lookupOf } from "./lookup.js";
|
|
19
19
|
export { detailOf, measuresOf, sumsOf } from "./measure.js";
|
|
20
20
|
export { BOOKKEEPING, defaultFields, sortKeyOf, textOf, viewOf, } from "./present.js";
|
|
21
21
|
export { relatedOf } from "./related.js";
|
|
22
|
-
export { entityFor, relationFieldOf, } from "./relation.js";
|
|
22
|
+
export { backReference, entityFor, relatedScope, relationFieldOf, } from "./relation.js";
|
|
23
23
|
export { summaryFieldsOf, summaryOf, } from "./summary.js";
|
|
24
24
|
export { placeOf, stepIn } from "./trail.js";
|
package/dist/core/lines.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyRecord, Meta } from "@sincpro/criteria";
|
|
1
|
+
import type { AnyRecord, FieldMeta, Meta } from "@sincpro/criteria";
|
|
2
2
|
/** The key a line is drawn and found by: its identity, or a local one while it has none. */
|
|
3
3
|
export declare function lineKey(line: AnyRecord, identity: string): string;
|
|
4
4
|
/** `next` is `line` edited: it keeps the line's local key. */
|
|
@@ -6,10 +6,18 @@ export declare function sameLine<T extends AnyRecord>(line: AnyRecord, next: T):
|
|
|
6
6
|
/** What the server answered about the list it was sent, each line keeping its key by position. */
|
|
7
7
|
export declare function linesAnswered(sent: readonly AnyRecord[], answered: readonly AnyRecord[]): AnyRecord[];
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Whether a to-many holds other aggregates rather than parts of the record — sincpro_framework's
|
|
10
|
+
* `References[Invoice]`, or a foreign key declared `owned=False` (`reference: true` on its
|
|
11
|
+
* definition): listed and searched, never edited as lines, because nothing the record saves
|
|
12
|
+
* writes them.
|
|
11
13
|
*/
|
|
12
|
-
export declare function
|
|
14
|
+
export declare function isReference(field: FieldMeta | undefined): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The lines a form can edit, or `null` when it holds only part of them — a list written back
|
|
17
|
+
* whole from one page would delete every line the page did not bring — or when they are not
|
|
18
|
+
* the record's to write (`isReference`): the record would save them and nothing would happen.
|
|
19
|
+
*/
|
|
20
|
+
export declare function editableLines(value: unknown, field?: FieldMeta): AnyRecord[] | null;
|
|
13
21
|
/** A blank line: the line model's defaults, never its identity — the server mints that. */
|
|
14
22
|
export declare function blankLine(defaults: AnyRecord, identity: string): AnyRecord;
|
|
15
23
|
/** The list with the line `key` moved before `before` (`null`: to the end). */
|
package/dist/core/lines.js
CHANGED
|
@@ -39,10 +39,22 @@ export function linesAnswered(sent, answered) {
|
|
|
39
39
|
return answered.map((line, i) => (sent[i] === undefined ? line : sameLine(sent[i], line)));
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
42
|
+
* Whether a to-many holds other aggregates rather than parts of the record — sincpro_framework's
|
|
43
|
+
* `References[Invoice]`, or a foreign key declared `owned=False` (`reference: true` on its
|
|
44
|
+
* definition): listed and searched, never edited as lines, because nothing the record saves
|
|
45
|
+
* writes them.
|
|
44
46
|
*/
|
|
45
|
-
export function
|
|
47
|
+
export function isReference(field) {
|
|
48
|
+
return field?.reference === true;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The lines a form can edit, or `null` when it holds only part of them — a list written back
|
|
52
|
+
* whole from one page would delete every line the page did not bring — or when they are not
|
|
53
|
+
* the record's to write (`isReference`): the record would save them and nothing would happen.
|
|
54
|
+
*/
|
|
55
|
+
export function editableLines(value, field) {
|
|
56
|
+
if (isReference(field))
|
|
57
|
+
return null;
|
|
46
58
|
if (Array.isArray(value))
|
|
47
59
|
return value;
|
|
48
60
|
const related = relatedOf(value);
|
package/dist/core/present.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export declare const BOOKKEEPING: ReadonlySet<string>;
|
|
|
38
38
|
* textOf(meta, "active", true, "es") -> "sí"
|
|
39
39
|
* textOf(meta, "due", "2026-03-12", "es") -> "12 mar 2026"
|
|
40
40
|
* textOf(meta, "total", "1150.5", "es") -> "1150,5" (exact: digits kept)
|
|
41
|
+
* textOf(meta, "price", { amount: "19.9", currency: "BOB" }, "es") -> "19,90 Bs"
|
|
41
42
|
* textOf(meta, "lines", {items, count: {value: 30}}) -> "30 elementos"
|
|
42
43
|
*/
|
|
43
44
|
export declare function textOf(meta: Meta | null | undefined, name: string, value: unknown, locale?: string): string | null;
|
package/dist/core/present.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { timeZoneNow } from "@sincpro/client-framework";
|
|
1
|
+
import { Money, timeZoneNow } from "@sincpro/client-framework";
|
|
2
2
|
import { choiceLabelOf, labelOf, } from "@sincpro/criteria";
|
|
3
3
|
import { frameworkCopy } from "./copy.js";
|
|
4
4
|
import { relatedOf } from "./related.js";
|
|
@@ -85,6 +85,50 @@ function numberFormat(locale, kept) {
|
|
|
85
85
|
}
|
|
86
86
|
return format;
|
|
87
87
|
}
|
|
88
|
+
/** The currency codes `Intl` refused, by locale: asked once, not on every cell. */
|
|
89
|
+
const unknownCurrencies = new Set();
|
|
90
|
+
function currencyFormat(locale, currency) {
|
|
91
|
+
const key = `c|${locale ?? ""}|${currency}`;
|
|
92
|
+
if (unknownCurrencies.has(key))
|
|
93
|
+
return null;
|
|
94
|
+
let format = formatters.get(key);
|
|
95
|
+
if (format === undefined) {
|
|
96
|
+
try {
|
|
97
|
+
format = new Intl.NumberFormat(locale, {
|
|
98
|
+
style: "currency",
|
|
99
|
+
currency,
|
|
100
|
+
minimumFractionDigits: Money.decimalsOf(currency),
|
|
101
|
+
maximumFractionDigits: Money.decimalsOf(currency),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Not an ISO 4217 code Intl knows (a token, a loyalty point): drawn as «amount CODE».
|
|
106
|
+
unknownCurrencies.add(key);
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
formatters.set(key, format);
|
|
110
|
+
}
|
|
111
|
+
return format;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A money as a person reads it: `{ amount: "1150.5", currency: "BOB" }` → «Bs 1.150,50». The
|
|
115
|
+
* amount goes to `Intl` as text, so no digit passes through a float, rounded to the currency's
|
|
116
|
+
* decimals as an invoice shows it (`Money.rounded`).
|
|
117
|
+
*/
|
|
118
|
+
function moneyText(value, locale) {
|
|
119
|
+
let money;
|
|
120
|
+
try {
|
|
121
|
+
money = Money.fromJSON(value).rounded();
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// A money of the right shape with an amount or a code that is not one (`"1,50"`, `""`): no
|
|
125
|
+
// text, as any value object — never an error that takes the whole view down.
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const amount = money.amount;
|
|
129
|
+
return (currencyFormat(locale, money.currency)?.format(amount) ??
|
|
130
|
+
`${numberFormat(locale, money.decimals).format(amount)} ${money.currency}`);
|
|
131
|
+
}
|
|
88
132
|
/** The record a many2one points at, read by the display its definition names. */
|
|
89
133
|
function displayOf(field, value) {
|
|
90
134
|
const definition = field.definition;
|
|
@@ -101,6 +145,7 @@ function displayOf(field, value) {
|
|
|
101
145
|
* textOf(meta, "active", true, "es") -> "sí"
|
|
102
146
|
* textOf(meta, "due", "2026-03-12", "es") -> "12 mar 2026"
|
|
103
147
|
* textOf(meta, "total", "1150.5", "es") -> "1150,5" (exact: digits kept)
|
|
148
|
+
* textOf(meta, "price", { amount: "19.9", currency: "BOB" }, "es") -> "19,90 Bs"
|
|
104
149
|
* textOf(meta, "lines", {items, count: {value: 30}}) -> "30 elementos"
|
|
105
150
|
*/
|
|
106
151
|
export function textOf(meta, name, value, locale) {
|
|
@@ -125,11 +170,13 @@ export function textOf(meta, name, value, locale) {
|
|
|
125
170
|
}
|
|
126
171
|
case "integer":
|
|
127
172
|
case "number": {
|
|
128
|
-
|
|
129
|
-
if (Number.isNaN(number))
|
|
173
|
+
if (Number.isNaN(Number(value)))
|
|
130
174
|
return String(value);
|
|
131
|
-
|
|
132
|
-
|
|
175
|
+
// A decimal that travels as text goes to `Intl` as text: every digit kept, none through a
|
|
176
|
+
// float (`"12345678901234567.89"` reads whole).
|
|
177
|
+
if (typeof value === "string")
|
|
178
|
+
return numberFormat(locale, decimals(value)).format(value.trim());
|
|
179
|
+
return numberFormat(locale, undefined).format(Number(value));
|
|
133
180
|
}
|
|
134
181
|
case "text[]":
|
|
135
182
|
return Array.isArray(value)
|
|
@@ -145,7 +192,8 @@ export function textOf(meta, name, value, locale) {
|
|
|
145
192
|
case "many2one":
|
|
146
193
|
return typeof value === "object" ? displayOf(field, value) : String(value);
|
|
147
194
|
case "embedded":
|
|
148
|
-
|
|
195
|
+
// A value object has no text of its own — but a money, the one every ERP holds, does.
|
|
196
|
+
return Money.isMoney(value) ? moneyText(value, locale) : null;
|
|
149
197
|
default:
|
|
150
198
|
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
151
199
|
}
|
package/dist/core/related.d.ts
CHANGED
|
@@ -6,7 +6,11 @@ import type { Count } from "@sincpro/criteria";
|
|
|
6
6
|
* `{items, count, cursor}` — so a tenant with a thousand members does not answer all of them;
|
|
7
7
|
* an older answer, or a domain declared by hand, sends a bare array. A screen that reads only
|
|
8
8
|
* `items` and counts them shows the first page and calls it the total (forge's console did:
|
|
9
|
-
* `count={repositories.length}`). This reads both shapes and keeps the real count
|
|
9
|
+
* `count={repositories.length}`). This reads both shapes and keeps the real count — a floor
|
|
10
|
+
* («at least 1000») when the backend stopped counting at its cap.
|
|
11
|
+
*
|
|
12
|
+
* Built on @sincpro/client-framework's `EntityCollection` (`isCollection`, `isPartial`,
|
|
13
|
+
* `totalOf`): a screen keeps this shape, with `count` always said.
|
|
10
14
|
*
|
|
11
15
|
* relatedOf({ items: [a, b], count: { value: 1000, exact: true }, cursor: "x" })
|
|
12
16
|
* -> { items: [a, b], count: { value: 1000, exact: true }, cursor: "x", partial: true }
|
package/dist/core/related.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { isCollection, isPartial, totalOf, } from "@sincpro/client-framework";
|
|
1
2
|
export function relatedOf(value) {
|
|
3
|
+
// The shapes, and what «partial» means, are @sincpro/client-framework's — the same rule as the
|
|
4
|
+
// backend's `is_partial` on every platform. Read without copying: a table reads this for every
|
|
5
|
+
// one2many cell of a page.
|
|
2
6
|
if (Array.isArray(value))
|
|
3
7
|
return {
|
|
4
8
|
items: value,
|
|
@@ -6,16 +10,13 @@ export function relatedOf(value) {
|
|
|
6
10
|
cursor: null,
|
|
7
11
|
partial: false,
|
|
8
12
|
};
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
return { items: [], count: { value: 0, exact: true }, cursor: null, partial: false };
|
|
13
|
+
if (!isCollection(value))
|
|
14
|
+
return { items: [], count: { value: 0, exact: true }, cursor: null, partial: false };
|
|
15
|
+
const page = value;
|
|
16
|
+
return {
|
|
17
|
+
items: (page.items ?? []),
|
|
18
|
+
count: totalOf(page),
|
|
19
|
+
cursor: page.cursor ?? null,
|
|
20
|
+
partial: isPartial(page),
|
|
21
|
+
};
|
|
21
22
|
}
|
package/dist/core/relation.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Entity } from "@sincpro/client-framework";
|
|
2
|
-
import type { FieldMeta, Meta } from "@sincpro/criteria";
|
|
2
|
+
import type { AnyRecord, Criteria, FieldMeta, Meta } from "@sincpro/criteria";
|
|
3
3
|
/**
|
|
4
4
|
* Which record a field of a form is about, read from the model — so a form draws a many2one
|
|
5
5
|
* as a picker of the right entity without being told.
|
|
@@ -36,3 +36,25 @@ export declare function relationFieldOf(meta: Meta | null | undefined, name: str
|
|
|
36
36
|
* named by its own name (`sincpro-forge-auth.Tenant`) where the application keys it by alias.
|
|
37
37
|
*/
|
|
38
38
|
export declare function entityFor(entities: readonly Entity[], target: string, metaOf?: (key: string) => Meta | undefined): Entity | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The field of the related records that points back at a record, and the value it holds for this
|
|
41
|
+
* one — what a list of them hides as a column and fills on a new one. `null` when the relation
|
|
42
|
+
* does not say. It reads the keys as @sincpro/client-framework's `relatedCriteria` asks by them.
|
|
43
|
+
*/
|
|
44
|
+
export declare function backReference(record: AnyRecord | null, field: FieldMeta | undefined, identity: string): {
|
|
45
|
+
field: string;
|
|
46
|
+
value: unknown;
|
|
47
|
+
} | null;
|
|
48
|
+
/** Whether a value can point a relation at a record: an empty key points at the orphans. */
|
|
49
|
+
export declare function isKeyValue(value: unknown): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The criteria a relation of ONE record is listed with, over the related entity — or `null` while
|
|
52
|
+
* there is no key to ask by: the record or its definition has not arrived, or the record holds no
|
|
53
|
+
* value in that key yet (a new one, a business key not written). Never a question that would
|
|
54
|
+
* match every orphan.
|
|
55
|
+
*
|
|
56
|
+
* relatedScope(customer, meta.fields.invoices, "id", criteria)
|
|
57
|
+
* -> criteria AND customer_id = customer.id (identified_by)
|
|
58
|
+
* -> criteria AND customer_code = customer.code (parent_field / related_field)
|
|
59
|
+
*/
|
|
60
|
+
export declare function relatedScope<T extends AnyRecord>(record: AnyRecord | null, field: FieldMeta | undefined, identity: string, criteria?: Criteria<T>): Criteria<T> | null;
|
package/dist/core/relation.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { entityKeyOf, kindOf } from "@sincpro/client-framework";
|
|
1
|
+
import { entityKeyOf, kindOf, relatedCriteria } from "@sincpro/client-framework";
|
|
2
2
|
export function relationFieldOf(meta, name) {
|
|
3
3
|
if (!meta)
|
|
4
4
|
return undefined;
|
|
@@ -56,3 +56,35 @@ export function entityFor(entities, target, metaOf = () => undefined) {
|
|
|
56
56
|
entities.find((one) => namedBy(one, target)) ??
|
|
57
57
|
(ofKind.length === 1 ? ofKind[0] : undefined));
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* The field of the related records that points back at a record, and the value it holds for this
|
|
61
|
+
* one — what a list of them hides as a column and fills on a new one. `null` when the relation
|
|
62
|
+
* does not say. It reads the keys as @sincpro/client-framework's `relatedCriteria` asks by them.
|
|
63
|
+
*/
|
|
64
|
+
export function backReference(record, field, identity) {
|
|
65
|
+
const there = field?.related_field ?? field?.identified_by;
|
|
66
|
+
if (record === null || !there)
|
|
67
|
+
return null;
|
|
68
|
+
const here = field?.related_field ? (field.parent_field ?? identity) : identity;
|
|
69
|
+
return { field: there, value: record[here] };
|
|
70
|
+
}
|
|
71
|
+
/** Whether a value can point a relation at a record: an empty key points at the orphans. */
|
|
72
|
+
export function isKeyValue(value) {
|
|
73
|
+
return value !== null && value !== undefined && value !== "";
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The criteria a relation of ONE record is listed with, over the related entity — or `null` while
|
|
77
|
+
* there is no key to ask by: the record or its definition has not arrived, or the record holds no
|
|
78
|
+
* value in that key yet (a new one, a business key not written). Never a question that would
|
|
79
|
+
* match every orphan.
|
|
80
|
+
*
|
|
81
|
+
* relatedScope(customer, meta.fields.invoices, "id", criteria)
|
|
82
|
+
* -> criteria AND customer_id = customer.id (identified_by)
|
|
83
|
+
* -> criteria AND customer_code = customer.code (parent_field / related_field)
|
|
84
|
+
*/
|
|
85
|
+
export function relatedScope(record, field, identity, criteria) {
|
|
86
|
+
const back = backReference(record, field, identity);
|
|
87
|
+
if (record === null || field === undefined || back === null || !isKeyValue(back.value))
|
|
88
|
+
return null;
|
|
89
|
+
return relatedCriteria(field, record, identity, criteria);
|
|
90
|
+
}
|
package/dist/react/context.d.ts
CHANGED
|
@@ -16,7 +16,9 @@ import { type ReactNode } from "react";
|
|
|
16
16
|
* `api.choosing` (what a chooser was asked, in memory) and `api.context` (what the user is on,
|
|
17
17
|
* what is selected). Read `~/dev/sincpro_web_composer/README.md` before writing a second one
|
|
18
18
|
* here. A value the backend must see on every request (the active company) goes in the
|
|
19
|
-
*
|
|
19
|
+
* process's context (`processContext.set({ company_id })`, @sincpro/client-framework): every
|
|
20
|
+
* request under this provider carries it in `sincpro-context` (`screensTransport`), and a change
|
|
21
|
+
* reads every screen again.
|
|
20
22
|
*/
|
|
21
23
|
type FrameworkContextValue = {
|
|
22
24
|
transport: Transport;
|
|
@@ -26,6 +28,8 @@ type FrameworkContextValue = {
|
|
|
26
28
|
entities: readonly Entity[];
|
|
27
29
|
/** Where each open record came from (`Trail`), by the key its reference carries. */
|
|
28
30
|
trails: Map<string, Trail>;
|
|
31
|
+
/** The process's context now: a reading reads again when it changes (another company). */
|
|
32
|
+
context: Readonly<Record<string, unknown>>;
|
|
29
33
|
};
|
|
30
34
|
export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, children, }: {
|
|
31
35
|
transport: Transport;
|
package/dist/react/context.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { installTimeZone, metaStore, } from "@sincpro/client-framework";
|
|
3
|
+
import { installTimeZone, metaStore, processContext, screensTransport, } from "@sincpro/client-framework";
|
|
4
4
|
import { entityFor } from "../core/relation.js";
|
|
5
|
+
import { useContextValues } from "./useContextValues.js";
|
|
5
6
|
import { createContext, useContext, useMemo, useRef, useState } from "react";
|
|
6
7
|
const FrameworkContext = createContext(null);
|
|
7
8
|
export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, children, }) {
|
|
@@ -16,7 +17,15 @@ export function FrameworkProvider({ transport, locale, metas: seed, entities = N
|
|
|
16
17
|
// the screens already learned.
|
|
17
18
|
const [metas] = useState(() => metaStore(seed));
|
|
18
19
|
const [trails] = useState(() => new Map());
|
|
19
|
-
|
|
20
|
+
// What the screens read and write is a use case of `screens` (`screensTransport`,
|
|
21
|
+
// client-framework): each request traced, carrying the process's context (the company, the
|
|
22
|
+
// tenant) read at the moment it is sent, and reported when it failed unexpectedly.
|
|
23
|
+
const screens = useMemo(() => screensTransport(transport), [transport]);
|
|
24
|
+
// When the context changes, every reading on screen reads its same question again — a list
|
|
25
|
+
// keeps its filter, order and page, a record what was typed (`useListing`, `useRecord`). The
|
|
26
|
+
// transport stays the same one: a new one would start every reading over.
|
|
27
|
+
const context = useContextValues(processContext);
|
|
28
|
+
const value = useMemo(() => ({ transport: screens, locale, metas, entities, trails, context }), [screens, locale, metas, entities, trails, context]);
|
|
20
29
|
return _jsx(FrameworkContext.Provider, { value: value, children: children });
|
|
21
30
|
}
|
|
22
31
|
const NO_ENTITIES = [];
|
package/dist/react/useGroups.js
CHANGED
|
@@ -8,7 +8,7 @@ const MAX_GROUPS = 100;
|
|
|
8
8
|
/** A group is known by its criteria: unique per group, and stable across renders. */
|
|
9
9
|
const keyOf = (bucket) => JSON.stringify(bucket.criteria);
|
|
10
10
|
export function useGroups(listing, options = {}) {
|
|
11
|
-
const { transport } = useFramework();
|
|
11
|
+
const { transport, context } = useFramework();
|
|
12
12
|
// A named list may open grouped (`groupBy`); a person regroups from there.
|
|
13
13
|
const [levels, setLevels] = useState(() => listing.definition?.groupBy ?? []);
|
|
14
14
|
const [state, setState] = useState({ status: "idle", buckets: [], error: null });
|
|
@@ -54,7 +54,7 @@ export function useGroups(listing, options = {}) {
|
|
|
54
54
|
setState({ status: "failed", buckets: [], error });
|
|
55
55
|
});
|
|
56
56
|
return () => aborter.abort();
|
|
57
|
-
}, [transport, route, question, levels, sums, waiting]);
|
|
57
|
+
}, [transport, route, question, levels, sums, waiting, context]);
|
|
58
58
|
/** One page of a group: the first (`after` null) or the one the cursor points at. */
|
|
59
59
|
const read = useCallback((bucket, after) => {
|
|
60
60
|
const key = keyOf(bucket);
|
package/dist/react/useHistory.js
CHANGED
|
@@ -17,7 +17,7 @@ import { useEffect, useState } from "react";
|
|
|
17
17
|
*/
|
|
18
18
|
const SHOWN = 100;
|
|
19
19
|
export function useHistory(entity, id, deps = []) {
|
|
20
|
-
const { transport, locale } = useFramework();
|
|
20
|
+
const { transport, locale, context } = useFramework();
|
|
21
21
|
const [entries, setEntries] = useState(undefined);
|
|
22
22
|
const history = entity.history;
|
|
23
23
|
useEffect(() => {
|
|
@@ -37,6 +37,6 @@ export function useHistory(entity, id, deps = []) {
|
|
|
37
37
|
transport.list(history.route, criteria, aborter.signal).then((page) => aborter.signal.aborted || setEntries(historyOf(page.rows, page.meta, locale)), () => aborter.signal.aborted || setEntries(undefined));
|
|
38
38
|
return () => aborter.abort();
|
|
39
39
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- the caller names what reloads it
|
|
40
|
-
}, [transport, history, id, locale, ...deps]);
|
|
40
|
+
}, [transport, history, id, locale, context, ...deps]);
|
|
41
41
|
return entries;
|
|
42
42
|
}
|
package/dist/react/useListing.js
CHANGED
|
@@ -9,7 +9,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
9
9
|
/** How long typing has to pause before the search goes out. */
|
|
10
10
|
const TYPING_PAUSE = 200;
|
|
11
11
|
export function useListing(given, own = {}) {
|
|
12
|
-
const { transport, locale, metas } = useFramework();
|
|
12
|
+
const { transport, locale, metas, context } = useFramework();
|
|
13
13
|
// A named list is its entity under a fixed scope: the scope ANDs under the caller's own.
|
|
14
14
|
const definition = isList(given) ? given : undefined;
|
|
15
15
|
// A named list's entity has no typed Commands of its own: the default, as `C` then is.
|
|
@@ -56,6 +56,15 @@ export function useListing(given, own = {}) {
|
|
|
56
56
|
});
|
|
57
57
|
const typing = useRef(null);
|
|
58
58
|
useEffect(() => () => void (typing.current && clearTimeout(typing.current)), []);
|
|
59
|
+
// Another company (the process's context changed): the same question read again — its filter,
|
|
60
|
+
// order and page kept, never a reading started over.
|
|
61
|
+
const askedIn = useRef(context);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (askedIn.current === context)
|
|
64
|
+
return;
|
|
65
|
+
askedIn.current = context;
|
|
66
|
+
void reading.reload();
|
|
67
|
+
}, [context, reading]);
|
|
59
68
|
const narrowed = (next) => {
|
|
60
69
|
let criteria = base;
|
|
61
70
|
const found = searchOver(meta, next.text);
|
package/dist/react/useLookup.js
CHANGED
|
@@ -24,7 +24,7 @@ export function excluding(criteria, exclude, meta) {
|
|
|
24
24
|
export function useLookup(
|
|
25
25
|
/** The entity, or a named list of it: «active customers» chooses only those. */
|
|
26
26
|
entity, options = {}) {
|
|
27
|
-
const { transport, metas } = useFramework();
|
|
27
|
+
const { transport, metas, context } = useFramework();
|
|
28
28
|
const base = isList(entity) ? entity.entity : entity;
|
|
29
29
|
// The key field to exclude by comes with the definition: known already, or with the first
|
|
30
30
|
// answer — then this renders again and the reading starts over without the excluded.
|
|
@@ -77,7 +77,7 @@ entity, options = {}) {
|
|
|
77
77
|
})
|
|
78
78
|
.catch(() => undefined); // unnamed is not an error: the field shows its key
|
|
79
79
|
return () => aborter.abort();
|
|
80
|
-
}, [transport, route, meta, missingKey, base.summary]);
|
|
80
|
+
}, [transport, route, meta, missingKey, base.summary, context]);
|
|
81
81
|
const known = value === "" ? undefined : seen.current.get(value);
|
|
82
82
|
const chosenAll = wanted.map((id) => seen.current.get(id) ?? { id, label: id });
|
|
83
83
|
return {
|
package/dist/react/useNameOf.js
CHANGED
|
@@ -15,7 +15,7 @@ import { useEffect, useState } from "react";
|
|
|
15
15
|
* const name = useNameOf(tenants, record.tenant_id) ?? record.tenant_id;
|
|
16
16
|
*/
|
|
17
17
|
export function useNameOf(entity, id) {
|
|
18
|
-
const { transport, metas } = useFramework();
|
|
18
|
+
const { transport, metas, context } = useFramework();
|
|
19
19
|
const [named, setNamed] = useState(undefined);
|
|
20
20
|
useEffect(() => {
|
|
21
21
|
if (entity === undefined || id === "")
|
|
@@ -39,6 +39,6 @@ export function useNameOf(entity, id) {
|
|
|
39
39
|
})
|
|
40
40
|
.catch(() => undefined);
|
|
41
41
|
return () => aborter.abort();
|
|
42
|
-
}, [transport, metas, entity, id]);
|
|
42
|
+
}, [transport, metas, entity, id, context]);
|
|
43
43
|
return named?.id === id ? named.name : undefined;
|
|
44
44
|
}
|
package/dist/react/usePivot.js
CHANGED
|
@@ -5,7 +5,7 @@ import { useEffect, useState } from "react";
|
|
|
5
5
|
/** The most groups an axis answers: a pivot of ten thousand rows is a list. */
|
|
6
6
|
const MAX_KEYS = 100;
|
|
7
7
|
export function usePivot(listing, axes) {
|
|
8
|
-
const { transport } = useFramework();
|
|
8
|
+
const { transport, context } = useFramework();
|
|
9
9
|
const [state, setState] = useState({
|
|
10
10
|
status: "idle",
|
|
11
11
|
crossed: [],
|
|
@@ -53,6 +53,6 @@ export function usePivot(listing, axes) {
|
|
|
53
53
|
setState({ status: "failed", crossed: [], columns: [], error });
|
|
54
54
|
});
|
|
55
55
|
return () => aborter.abort();
|
|
56
|
-
}, [transport, listing.route, question, shape, sums, waiting]);
|
|
56
|
+
}, [transport, listing.route, question, shape, sums, waiting, context]);
|
|
57
57
|
return { ...state, measures };
|
|
58
58
|
}
|
package/dist/react/useRecord.js
CHANGED
|
@@ -7,7 +7,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
7
7
|
/** How long typing has to pause before the server is asked what the record becomes. */
|
|
8
8
|
const PREVIEW_PAUSE = 300;
|
|
9
9
|
export function useRecord(entity, opened) {
|
|
10
|
-
const { transport, locale, metas } = useFramework();
|
|
10
|
+
const { transport, locale, metas, context } = useFramework();
|
|
11
11
|
const given = typeof opened === "string" ? opened : (opened.id ?? "");
|
|
12
12
|
// As written in the reference: a text compares by value, so a new render with the same
|
|
13
13
|
// reference is not a new start.
|
|
@@ -33,6 +33,8 @@ export function useRecord(entity, opened) {
|
|
|
33
33
|
const [readFor, setReadFor] = useState(null);
|
|
34
34
|
const [round, setRound] = useState(0);
|
|
35
35
|
const [typed, setTyped] = useState({});
|
|
36
|
+
const typedNow = useRef(typed);
|
|
37
|
+
typedNow.current = typed;
|
|
36
38
|
const [computed, setComputed] = useState({});
|
|
37
39
|
const [fromServer, setFromServer] = useState({});
|
|
38
40
|
const [advice, setAdvice] = useState([]);
|
|
@@ -136,6 +138,16 @@ export function useRecord(entity, opened) {
|
|
|
136
138
|
};
|
|
137
139
|
}, []);
|
|
138
140
|
const reload = useCallback(() => setRound((n) => n + 1), []);
|
|
141
|
+
// Another company (the process's context changed): a record with nothing typed reads again;
|
|
142
|
+
// one being edited keeps what was typed — losing it is never the framework's decision.
|
|
143
|
+
const askedIn = useRef(context);
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (askedIn.current === context)
|
|
146
|
+
return;
|
|
147
|
+
askedIn.current = context;
|
|
148
|
+
if (Object.keys(typedNow.current).length === 0)
|
|
149
|
+
reload();
|
|
150
|
+
}, [context, reload]);
|
|
139
151
|
const record = useMemo(() => (state.record ?? {}), [state.record]);
|
|
140
152
|
const values = useMemo(() => ({ ...record, ...computed, ...typed }), [record, computed, typed]);
|
|
141
153
|
const changed = useMemo(() => changesOf(record, typed), [record, typed]);
|
|
@@ -234,7 +246,10 @@ export function useRecord(entity, opened) {
|
|
|
234
246
|
changed,
|
|
235
247
|
dirty: Object.keys(changed).length > 0,
|
|
236
248
|
set: (name, value) => {
|
|
237
|
-
|
|
249
|
+
// Over what was typed last, not this render's: two fields set in one gesture (a key and
|
|
250
|
+
// the record held beside it) both land.
|
|
251
|
+
const next = { ...typedNow.current, [name]: value };
|
|
252
|
+
typedNow.current = next;
|
|
238
253
|
setTyped(next);
|
|
239
254
|
ask(next, name);
|
|
240
255
|
},
|
|
@@ -8,10 +8,11 @@ import type { RecordReading } from "./useRecord.js";
|
|
|
8
8
|
* Context: sincpro_framework sends a one2many inside its record as ONE page (`RecordLines` draws
|
|
9
9
|
* it). A customer with forty thousand invoices, a tenant with a thousand members, the lines of a
|
|
10
10
|
* big order need more than that page: search in them, order them, walk them. The model says how
|
|
11
|
-
* to ask — the related aggregate's key that points back (`identified_by`)
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* to ask — the related aggregate's key that points back (`identified_by`), or the two fields of a
|
|
12
|
+
* relation by a business key (`parent_field`, `related_field`) — so the relation is a criteria
|
|
13
|
+
* over the related entity (@sincpro/client-framework's `relatedCriteria`), answered by the server
|
|
14
|
+
* like any list. The relation's scope is AND-ed with everything else, so no search, filter or
|
|
15
|
+
* page can ever leave it.
|
|
15
16
|
*
|
|
16
17
|
* const facturas = useRelated(cliente, "invoices", invoicesEntity);
|
|
17
18
|
* <EntityList listing={facturas} embedded />
|