@vdatacloud/cx-commons 0.4.0 → 0.5.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 +4 -2
- package/package.json +1 -1
- package/src/sdk/pending-approvals.ts +65 -11
package/README.md
CHANGED
|
@@ -88,13 +88,15 @@ How a party signs one version of an agreement, mirroring daml-escrow-commons `ag
|
|
|
88
88
|
- `verifySignature(alg, publicKey, message, signature)`: `ed25519` (wallets) or `ecdsa-p256-sha256` (KMS keys such as the custodian's; strict DER, low s), through WebCrypto.
|
|
89
89
|
- `draftHash` / `draftMessage`: the frozen `tripart.draft-version/1` (Phase 77 drafts).
|
|
90
90
|
|
|
91
|
-
`test/fixtures/api-error`, `test/fixtures/canton-id
|
|
91
|
+
`test/fixtures/api-error`, `test/fixtures/canton-id`, `test/fixtures/agreement-sig` and `test/fixtures/approval` hold copies of the Go goldens, from the commit in `SOURCE`. After changing the Go package, run `npm run sync:api-error-fixtures`; CI fails if the copies drift.
|
|
92
92
|
|
|
93
93
|
---
|
|
94
94
|
|
|
95
95
|
### Pending approvals (`sdk/pending-approvals`)
|
|
96
96
|
|
|
97
|
-
One worklist for acts waiting on several people's approval, whatever the act (daml-escrow PLAN.md Phase 91.4, the shared approval process). `mountPendingApprovals(host, items, { me, onDecide })` shows each request's description, proposer, progress (n of N, with a progress bar) and who approved, with the decisions the viewer may take: the proposer withdraws; someone who approved does nothing more; anyone else approves or rejects (`noApprove` hides approve, e.g. for a stale act or the viewer's own appointment). Each decision confirms and asks for a reason before `onDecide(item, decision, reason)`; the app sends it and reloads. Text is never parsed as markup; pass a `Node` for a rich description. Strings are `cx.approvals.*` (en, fr).
|
|
97
|
+
One worklist for acts waiting on several people's approval, whatever the act (daml-escrow PLAN.md Phase 91.4, the shared approval process). `mountPendingApprovals(host, items, { me, onDecide })` shows each request's description, proposer, progress (n of N, with a progress bar) and who approved, with the decisions the viewer may take: the proposer withdraws; someone who approved does nothing more; anyone else approves or rejects (`noApprove` hides approve, e.g. for a stale act or the viewer's own appointment). Each decision confirms and asks for a reason before `onDecide(item, decision, reason)`; the app sends it and reloads. Text is never parsed as markup; pass a `Node` for a rich description. Strings are `cx.approvals.*` (en, fr). `decisionsFor` is tested against daml-escrow-commons `approval/testdata/decisions.json`, the server's rule.
|
|
98
|
+
|
|
99
|
+
An act that isn't a proposal (daml-escrow PLAN.md Phase 92.5: a staged release its approvers approve, a compliance review's outcome) leaves `proposedBy` out and passes its own `actions` (`{ id, label, tone?, reason?: 'required' | 'optional' | 'none', confirm? }`) in place of approve / reject / withdraw: each confirms, asks for a reason as it says, then calls `onAction(item, actionId, reason)`. Someone who has approved is offered none (`actionsFor`).
|
|
98
100
|
|
|
99
101
|
---
|
|
100
102
|
|
package/package.json
CHANGED
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
// withdraws; someone who has approved does nothing more; anyone else may
|
|
11
11
|
// approve or reject. Without `me` all three decisions show (operators,
|
|
12
12
|
// tests) and the server's refusal is the answer.
|
|
13
|
+
//
|
|
14
|
+
// An act that isn't a proposal (daml-escrow PLAN.md Phase 92.5: a staged
|
|
15
|
+
// release its approvers approve, a compliance review's outcome) names no
|
|
16
|
+
// proposer and brings its own actions instead: each with its label, tone and
|
|
17
|
+
// whether it asks for a reason. Someone who has approved is offered none.
|
|
13
18
|
|
|
14
19
|
import { confirmDialog, promptDialog } from './dialogs';
|
|
15
20
|
import { renderCantonId } from './canton-id-view';
|
|
@@ -21,9 +26,9 @@ export type ApprovalDecision = 'approve' | 'reject' | 'withdraw';
|
|
|
21
26
|
export interface PendingApproval {
|
|
22
27
|
/** The request's id (what the decision is sent for). */
|
|
23
28
|
id: string;
|
|
24
|
-
/** Who proposed it: also its first approval. */
|
|
25
|
-
proposedBy
|
|
26
|
-
/** Who approved so far, in order, the proposer first. */
|
|
29
|
+
/** Who proposed it: also its first approval. Empty or absent: nobody did (not shown). */
|
|
30
|
+
proposedBy?: string;
|
|
31
|
+
/** Who approved so far, in order, the proposer (if any) first. */
|
|
27
32
|
approvals: string[];
|
|
28
33
|
/** How many approvals the act needs, the proposer's included. */
|
|
29
34
|
required: number;
|
|
@@ -36,6 +41,22 @@ export interface PendingApproval {
|
|
|
36
41
|
note?: string;
|
|
37
42
|
/** Approve isn't offered: the act can't stand, or the viewer is its subject. */
|
|
38
43
|
noApprove?: boolean;
|
|
44
|
+
/** Its own actions in place of approve / reject / withdraw (sent through onAction). */
|
|
45
|
+
actions?: ApprovalAction[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** An act's own action, e.g. "approve" a release or "cleared" a review. */
|
|
49
|
+
export interface ApprovalAction {
|
|
50
|
+
/** What is sent (onAction's actionId). */
|
|
51
|
+
id: string;
|
|
52
|
+
/** The button's text. */
|
|
53
|
+
label: string;
|
|
54
|
+
/** "danger" asks with the danger confirmation (default "approve"). */
|
|
55
|
+
tone?: 'approve' | 'danger';
|
|
56
|
+
/** Whether a reason is asked: required (default), optional, or none (confirm only). */
|
|
57
|
+
reason?: 'required' | 'optional' | 'none';
|
|
58
|
+
/** The confirmation's question (default cx.approvals.confirm with the label). */
|
|
59
|
+
confirm?: string;
|
|
39
60
|
}
|
|
40
61
|
|
|
41
62
|
export interface PendingApprovalsOptions {
|
|
@@ -43,6 +64,8 @@ export interface PendingApprovalsOptions {
|
|
|
43
64
|
me?: string;
|
|
44
65
|
/** Sends a decision, with the reason given; the list doesn't change itself (the app reloads). */
|
|
45
66
|
onDecide: (item: PendingApproval, decision: ApprovalDecision, reason: string) => void | Promise<void>;
|
|
67
|
+
/** Sends one of an item's own actions, with the reason given ('' when none was asked or given). */
|
|
68
|
+
onAction?: (item: PendingApproval, actionId: string, reason: string) => void | Promise<void>;
|
|
46
69
|
/** Shows a party (default: the shared Canton id display). */
|
|
47
70
|
party?: (party: string) => Node;
|
|
48
71
|
/** Text when nothing is pending (default cx.approvals.empty). */
|
|
@@ -76,11 +99,30 @@ function el<K extends keyof HTMLElementTagNameMap>(tag: K, className = '', text?
|
|
|
76
99
|
/** The decisions viewer `me` is offered on item (all three without `me`). */
|
|
77
100
|
export function decisionsFor(item: PendingApproval, me?: string): ApprovalDecision[] {
|
|
78
101
|
if (!me) return item.noApprove ? ['reject', 'withdraw'] : ['approve', 'reject', 'withdraw'];
|
|
79
|
-
if (me === item.proposedBy) return ['withdraw'];
|
|
102
|
+
if (item.proposedBy && me === item.proposedBy) return ['withdraw'];
|
|
80
103
|
if (item.approvals.includes(me)) return [];
|
|
81
104
|
return item.noApprove ? ['reject'] : ['approve', 'reject'];
|
|
82
105
|
}
|
|
83
106
|
|
|
107
|
+
/** The item's own actions viewer `me` is offered: none once they have approved. */
|
|
108
|
+
export function actionsFor(item: PendingApproval, me?: string): ApprovalAction[] {
|
|
109
|
+
if (me && item.approvals.includes(me)) return [];
|
|
110
|
+
return item.actions ?? [];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// act confirms, asks for the reason the action wants, then hands it to the app.
|
|
114
|
+
async function act(t: Translator, item: PendingApproval, a: ApprovalAction, opts: PendingApprovalsOptions): Promise<void> {
|
|
115
|
+
if (!(await confirmDialog(a.confirm ?? t.t('cx.approvals.confirm', { decision: a.label }), { confirmLabel: a.label, danger: a.tone === 'danger' }))) return;
|
|
116
|
+
const mode = a.reason ?? 'required';
|
|
117
|
+
let reason = '';
|
|
118
|
+
if (mode !== 'none') {
|
|
119
|
+
const given = await promptDialog(t.t('cx.approvals.reasonPrompt'), { label: t.t('cx.approvals.reason'), required: mode === 'required' });
|
|
120
|
+
if (given === null || (mode === 'required' && !given)) return;
|
|
121
|
+
reason = given;
|
|
122
|
+
}
|
|
123
|
+
await opts.onAction?.(item, a.id, reason);
|
|
124
|
+
}
|
|
125
|
+
|
|
84
126
|
// decide confirms, asks for the reason, then hands the decision to the app.
|
|
85
127
|
async function decide(t: Translator, item: PendingApproval, d: ApprovalDecision, opts: PendingApprovalsOptions): Promise<void> {
|
|
86
128
|
const label = t.t(`cx.approvals.${d}`);
|
|
@@ -104,7 +146,9 @@ function renderItem(t: Translator, item: PendingApproval, opts: PendingApprovals
|
|
|
104
146
|
const count = Math.min(item.approvals.length, item.required);
|
|
105
147
|
const meta = el('div', META);
|
|
106
148
|
if (item.kindLabel) meta.append(el('span', TAG, item.kindLabel));
|
|
107
|
-
|
|
149
|
+
const progress = t.t('cx.approvals.progress', { count, required: item.required });
|
|
150
|
+
if (item.proposedBy) meta.append(t.t('cx.approvals.proposedBy') + ' ', party(item.proposedBy), ` · ${progress}`);
|
|
151
|
+
else meta.append(progress);
|
|
108
152
|
body.append(meta);
|
|
109
153
|
|
|
110
154
|
const bar = el('div', BAR);
|
|
@@ -129,12 +173,22 @@ function renderItem(t: Translator, item: PendingApproval, opts: PendingApprovals
|
|
|
129
173
|
body.append(details);
|
|
130
174
|
|
|
131
175
|
const actions = el('div', 'flex gap-2');
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
176
|
+
if (item.actions) {
|
|
177
|
+
for (const a of actionsFor(item, opts.me)) {
|
|
178
|
+
const b = el('button', `${BUTTON} ${BUTTON_TONE[a.tone === 'danger' ? 'reject' : 'approve']}`, a.label);
|
|
179
|
+
b.type = 'button';
|
|
180
|
+
b.dataset.approvalAction = a.id;
|
|
181
|
+
b.addEventListener('click', () => void act(t, item, a, opts));
|
|
182
|
+
actions.append(b);
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
for (const d of decisionsFor(item, opts.me)) {
|
|
186
|
+
const b = el('button', `${BUTTON} ${BUTTON_TONE[d]}`, t.t(`cx.approvals.${d}`));
|
|
187
|
+
b.type = 'button';
|
|
188
|
+
b.dataset.approvalDecision = d;
|
|
189
|
+
b.addEventListener('click', () => void decide(t, item, d, opts));
|
|
190
|
+
actions.append(b);
|
|
191
|
+
}
|
|
138
192
|
}
|
|
139
193
|
li.append(body, actions);
|
|
140
194
|
return li;
|