@joinskips/common 0.0.0-stage → 0.1.0-develop.138
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 +102 -2
- package/dist/chat/SkipsChat.d.ts +37 -0
- package/dist/chat/SkipsChat.js +103 -0
- package/dist/chat/client.d.ts +53 -0
- package/dist/chat/client.js +178 -0
- package/dist/chat/connection.d.ts +4 -0
- package/dist/chat/connection.js +6 -0
- package/dist/chat/generated/skips-app/index.d.ts +1 -0
- package/dist/chat/generated/skips-app/index.js +2 -0
- package/dist/chat/generated/skips-app/types.gen.d.ts +733 -0
- package/dist/chat/generated/skips-app/types.gen.js +2 -0
- package/dist/chat/index.d.ts +3 -0
- package/dist/chat/index.js +2 -0
- package/dist/chat/platform.d.ts +4 -0
- package/dist/chat/platform.js +46 -0
- package/dist/chat/session.d.ts +19 -0
- package/dist/chat/session.js +95 -0
- package/dist/chat/useSkipsChat.d.ts +79 -0
- package/dist/chat/useSkipsChat.js +507 -0
- package/dist/chat/verify.d.ts +35 -0
- package/dist/chat/verify.js +84 -0
- package/dist/elements/skips-elements.js +18521 -0
- package/dist/helpers/attachments.d.ts +12 -0
- package/dist/helpers/attachments.js +34 -0
- package/dist/helpers/cable.d.ts +34 -0
- package/dist/helpers/cable.js +88 -0
- package/dist/helpers/cableFrames.d.ts +40 -0
- package/dist/helpers/cableFrames.js +70 -0
- package/dist/helpers/cardNote.d.ts +79 -0
- package/dist/helpers/cardNote.js +152 -0
- package/dist/helpers/commandNote.d.ts +4 -0
- package/dist/helpers/commandNote.js +23 -0
- package/dist/helpers/index.d.ts +19 -0
- package/dist/helpers/index.js +19 -0
- package/dist/helpers/markdown.d.ts +47 -0
- package/dist/helpers/markdown.js +129 -0
- package/dist/helpers/medicationOrder.d.ts +333 -0
- package/dist/helpers/medicationOrder.js +383 -0
- package/dist/helpers/message.d.ts +46 -0
- package/dist/helpers/message.js +12 -0
- package/dist/helpers/noteView.d.ts +35 -0
- package/dist/helpers/noteView.js +65 -0
- package/dist/helpers/orderForm.d.ts +126 -0
- package/dist/helpers/orderForm.js +271 -0
- package/dist/helpers/ordersMenu.d.ts +56 -0
- package/dist/helpers/ordersMenu.js +129 -0
- package/dist/helpers/regimenChoice.d.ts +42 -0
- package/dist/helpers/regimenChoice.js +73 -0
- package/dist/helpers/scrollBox.d.ts +5 -0
- package/dist/helpers/scrollBox.js +15 -0
- package/dist/helpers/skipTyping.d.ts +26 -0
- package/dist/helpers/skipTyping.js +29 -0
- package/dist/helpers/skipsMessage.d.ts +55 -0
- package/dist/helpers/skipsMessage.js +130 -0
- package/dist/helpers/stageRefill.d.ts +33 -0
- package/dist/helpers/stageRefill.js +41 -0
- package/dist/helpers/thread.d.ts +54 -0
- package/dist/helpers/thread.js +122 -0
- package/dist/helpers/typing.d.ts +13 -0
- package/dist/helpers/typing.js +68 -0
- package/dist/helpers/usrxNote.d.ts +22 -0
- package/dist/helpers/usrxNote.js +26 -0
- package/dist/helpers/visibleArea.d.ts +15 -0
- package/dist/helpers/visibleArea.js +11 -0
- package/dist/helpers/welcome.d.ts +25 -0
- package/dist/helpers/welcome.js +51 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useCardNoteActions.d.ts +82 -0
- package/dist/hooks/useCardNoteActions.js +210 -0
- package/dist/hooks/useCodeEntry.d.ts +6 -0
- package/dist/hooks/useCodeEntry.js +14 -0
- package/dist/hooks/useComposer.d.ts +20 -0
- package/dist/hooks/useComposer.js +40 -0
- package/dist/hooks/useOrderForm.d.ts +80 -0
- package/dist/hooks/useOrderForm.js +403 -0
- package/dist/hooks/useOrdersMenu.d.ts +114 -0
- package/dist/hooks/useOrdersMenu.js +337 -0
- package/dist/hooks/useRegimenChoice.d.ts +23 -0
- package/dist/hooks/useRegimenChoice.js +50 -0
- package/dist/hooks/useResendCountdown.d.ts +2 -0
- package/dist/hooks/useResendCountdown.js +19 -0
- package/dist/hooks/useStepField.d.ts +8 -0
- package/dist/hooks/useStepField.js +17 -0
- package/dist/hooks/useTypingCue.d.ts +11 -0
- package/dist/hooks/useTypingCue.js +41 -0
- package/dist/hooks/useWelcomeTyping.d.ts +2 -0
- package/dist/hooks/useWelcomeTyping.js +25 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/staff/CardNoteActions.d.ts +10 -0
- package/dist/staff/CardNoteActions.js +16 -0
- package/dist/staff/OrderFormNote.d.ts +8 -0
- package/dist/staff/OrderFormNote.js +8 -0
- package/dist/staff/OrdersMenuNote.d.ts +8 -0
- package/dist/staff/OrdersMenuNote.js +8 -0
- package/dist/staff/RegimenChoiceNote.d.ts +7 -0
- package/dist/staff/RegimenChoiceNote.js +8 -0
- package/dist/staff/features.d.ts +9 -0
- package/dist/staff/features.js +8 -0
- package/dist/staff/index.d.ts +11 -0
- package/dist/staff/index.js +7 -0
- package/dist/ui/Bubble.d.ts +5 -0
- package/dist/ui/Bubble.js +9 -0
- package/dist/ui/CardActions.d.ts +34 -0
- package/dist/ui/CardActions.js +28 -0
- package/dist/ui/Conversation.d.ts +69 -0
- package/dist/ui/Conversation.js +192 -0
- package/dist/ui/Markdown.d.ts +11 -0
- package/dist/ui/Markdown.js +51 -0
- package/dist/ui/NoteFold.d.ts +19 -0
- package/dist/ui/NoteFold.js +12 -0
- package/dist/ui/NoteViewControl.d.ts +19 -0
- package/dist/ui/NoteViewControl.js +20 -0
- package/dist/ui/OrderForm.d.ts +71 -0
- package/dist/ui/OrderForm.js +118 -0
- package/dist/ui/OrdersMenu.d.ts +71 -0
- package/dist/ui/OrdersMenu.js +128 -0
- package/dist/ui/RegimenChoice.d.ts +23 -0
- package/dist/ui/RegimenChoice.js +16 -0
- package/dist/ui/SkipAvatar.d.ts +7 -0
- package/dist/ui/SkipAvatar.js +8 -0
- package/dist/ui/StaffButton.d.ts +18 -0
- package/dist/ui/StaffButton.js +17 -0
- package/dist/ui/Welcome.d.ts +91 -0
- package/dist/ui/Welcome.js +128 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/parts.d.ts +38 -0
- package/dist/ui/parts.js +6 -0
- package/package.json +115 -4
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { reachesPharmacy, submitCommand } from "../helpers/cardNote.js";
|
|
3
|
+
import { buildOrder, isQueued, nextDispenseAmount, noteText, } from "../helpers/medicationOrder.js";
|
|
4
|
+
import { canSearchPharmacies, cardChanges, cardItem, cardOfOrder, chartsToPick, computedAmount, focusField, isPastOrder, lookupFailureOf, newItem, newOrderItems, newPicker, pharmacyChange, picked, placeOf, queuedChange, refusalOf, renumbered, reorderItem, SHORTEST_TERM, TYPING_PAUSE_MS, } from "../helpers/orderForm.js";
|
|
5
|
+
// A change to these is a change to what the dispense amount is computed from.
|
|
6
|
+
const AMOUNT_INPUTS = ['quantity', 'frequencyId', 'days', 'maxPerDay'];
|
|
7
|
+
let lastKey = 0;
|
|
8
|
+
const nextKey = () => {
|
|
9
|
+
lastKey += 1;
|
|
10
|
+
return `new-${String(lastKey)}`;
|
|
11
|
+
};
|
|
12
|
+
const medicineOf = (rounds, picker = newPicker()) => ({
|
|
13
|
+
key: nextKey(),
|
|
14
|
+
rounds,
|
|
15
|
+
picker,
|
|
16
|
+
});
|
|
17
|
+
const without = (keys, key) => {
|
|
18
|
+
const left = new Set(keys);
|
|
19
|
+
left.delete(key);
|
|
20
|
+
return left;
|
|
21
|
+
};
|
|
22
|
+
const cardOf = (card) => (isPastOrder(card) ? cardOfOrder(card) : card);
|
|
23
|
+
/** The form a request opens: blank, a staged card to change or send, or a sent order to order again. */
|
|
24
|
+
function opened(request) {
|
|
25
|
+
const blank = { medicines: [medicineOf([newItem(nextKey())])], editing: null, queued: false, viewing: false, charts: null };
|
|
26
|
+
if (!request?.card)
|
|
27
|
+
return blank;
|
|
28
|
+
const { options } = request;
|
|
29
|
+
const card = cardOf(request.card);
|
|
30
|
+
if (options.reorder) {
|
|
31
|
+
const sent = options.rounds?.length ? options.rounds.map(cardOf) : [card];
|
|
32
|
+
const rounds = renumbered(sent.map((each) => reorderItem(each, nextKey())));
|
|
33
|
+
const pharmacy = options.samePharmacy === false ? null : (card.pharmacy ?? null);
|
|
34
|
+
return { ...blank, medicines: [medicineOf(rounds, { ...newPicker(), pharmacy })], charts: options.charts ?? null };
|
|
35
|
+
}
|
|
36
|
+
const queued = isQueued(card);
|
|
37
|
+
const key = queued ? `queued-${String(card.queuedId)}` : `card-${card.uid}`;
|
|
38
|
+
return { ...blank, medicines: [medicineOf([cardItem(card, key)])], editing: card, queued, viewing: options.view === true };
|
|
39
|
+
}
|
|
40
|
+
/** The Order form: a new order, a staged card's change or Send, a queued medicine's save, or a sent order again. */
|
|
41
|
+
export function useOrderForm(source) {
|
|
42
|
+
const { conversationId, role, request } = source;
|
|
43
|
+
// The calls below read the newest source when they run, without running again when the host passes new callbacks.
|
|
44
|
+
const latest = useRef(source);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
latest.current = source;
|
|
47
|
+
});
|
|
48
|
+
const [form, setForm] = useState(() => opened(request));
|
|
49
|
+
const [lookupError, setLookupError] = useState(null);
|
|
50
|
+
const [refusal, setRefusal] = useState('');
|
|
51
|
+
const [postFailed, setPostFailed] = useState(false);
|
|
52
|
+
const [busy, setBusy] = useState(false);
|
|
53
|
+
const [pickedChart, setPickedChart] = useState('');
|
|
54
|
+
const [focus, setFocus] = useState(null);
|
|
55
|
+
const [searchingPharmacies, setSearchingPharmacies] = useState(new Set());
|
|
56
|
+
// The form as it is now, for the answers that arrive after a render.
|
|
57
|
+
const now = useRef(form);
|
|
58
|
+
now.current = form;
|
|
59
|
+
// A new request or conversation starts a new generation, whose answers alone write.
|
|
60
|
+
const generation = useRef(0);
|
|
61
|
+
const updateItem = useCallback((itemKey, change) => {
|
|
62
|
+
setForm((was) => ({
|
|
63
|
+
...was,
|
|
64
|
+
medicines: was.medicines.map((medicine) => ({
|
|
65
|
+
...medicine,
|
|
66
|
+
rounds: medicine.rounds.map((item) => (item.key === itemKey ? change(item) : item)),
|
|
67
|
+
})),
|
|
68
|
+
}));
|
|
69
|
+
}, []);
|
|
70
|
+
const updatePicker = useCallback((medicineKey, change) => {
|
|
71
|
+
setForm((was) => ({
|
|
72
|
+
...was,
|
|
73
|
+
medicines: was.medicines.map((medicine) => (medicine.key === medicineKey ? { ...medicine, picker: change(medicine.picker) } : medicine)),
|
|
74
|
+
}));
|
|
75
|
+
}, []);
|
|
76
|
+
const itemNow = (itemKey) => now.current.medicines.flatMap((medicine) => medicine.rounds).find((item) => item.key === itemKey);
|
|
77
|
+
/** Runs a Treat call, showing a failed lookup and clearing the last one once a call answers. Null when it failed or the form moved on. */
|
|
78
|
+
const lookup = useCallback(async (ask, isCurrent) => {
|
|
79
|
+
const ours = generation.current;
|
|
80
|
+
const stillOurs = () => ours === generation.current && isCurrent();
|
|
81
|
+
try {
|
|
82
|
+
const answer = await ask();
|
|
83
|
+
if (!stillOurs())
|
|
84
|
+
return null;
|
|
85
|
+
setLookupError(null);
|
|
86
|
+
return answer;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (stillOurs())
|
|
90
|
+
setLookupError(lookupFailureOf(error));
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}, []);
|
|
94
|
+
// The choices load one round at a time, after the menu's Treat reads, since usrx takes one job at a time.
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
generation.current += 1;
|
|
97
|
+
const ours = generation.current;
|
|
98
|
+
const next = opened(request);
|
|
99
|
+
setForm(next);
|
|
100
|
+
setLookupError(null);
|
|
101
|
+
setRefusal('');
|
|
102
|
+
setPostFailed(false);
|
|
103
|
+
setBusy(false);
|
|
104
|
+
setPickedChart('');
|
|
105
|
+
setFocus(null);
|
|
106
|
+
setSearchingPharmacies(new Set());
|
|
107
|
+
if (!request?.card)
|
|
108
|
+
return;
|
|
109
|
+
const items = next.medicines.flatMap((medicine) => medicine.rounds);
|
|
110
|
+
void (async () => {
|
|
111
|
+
try {
|
|
112
|
+
await request.readsDone;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// A failed read of the menu still leaves the form its own reads.
|
|
116
|
+
}
|
|
117
|
+
for (const item of items) {
|
|
118
|
+
if (ours !== generation.current)
|
|
119
|
+
return;
|
|
120
|
+
const ddid = item.drug?.ddid;
|
|
121
|
+
if (!ddid)
|
|
122
|
+
continue;
|
|
123
|
+
updateItem(item.key, (was) => ({ ...was, loadingChoices: true }));
|
|
124
|
+
const choices = await lookup(() => latest.current.readChoices(conversationId, ddid), () => itemNow(item.key)?.drug?.ddid === ddid);
|
|
125
|
+
updateItem(item.key, (was) => {
|
|
126
|
+
const loaded = { ...was, loadingChoices: false, choices: choices ?? was.choices };
|
|
127
|
+
return { ...loaded, filledAmount: computedAmount(loaded) };
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const { focus: asked } = request.options;
|
|
131
|
+
const [first] = next.medicines;
|
|
132
|
+
const [firstItem] = items;
|
|
133
|
+
if (asked && first && firstItem && ours === generation.current) {
|
|
134
|
+
setFocus({ itemKey: firstItem.key, medicineKey: first.key, field: focusField(asked) });
|
|
135
|
+
}
|
|
136
|
+
})();
|
|
137
|
+
}, [request, conversationId, lookup, updateItem]);
|
|
138
|
+
// A drug search waits for a pause in typing, and the newest term's answer alone is shown.
|
|
139
|
+
const timers = useRef(new Map());
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
const running = timers.current;
|
|
142
|
+
return () => {
|
|
143
|
+
for (const timer of running.values())
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
};
|
|
146
|
+
}, []);
|
|
147
|
+
// One pharmacy search at a time across the form: production's lookup answers a second with 429.
|
|
148
|
+
const pharmacySearch = useRef({ running: false, waiting: new Set() });
|
|
149
|
+
const searchPharmacies = useCallback(async (medicineKey) => {
|
|
150
|
+
const run = pharmacySearch.current;
|
|
151
|
+
if (run.running) {
|
|
152
|
+
run.waiting.add(medicineKey);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const medicine = now.current.medicines.find((each) => each.key === medicineKey);
|
|
156
|
+
if (!medicine || !canSearchPharmacies(medicine.picker)) {
|
|
157
|
+
setSearchingPharmacies((was) => without(was, medicineKey));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const place = placeOf(medicine.picker);
|
|
161
|
+
run.running = true;
|
|
162
|
+
setSearchingPharmacies((was) => new Set([...was, medicineKey]));
|
|
163
|
+
const found = await lookup(() => latest.current.findPharmacies(conversationId, {
|
|
164
|
+
zip: medicine.picker.zip.trim(),
|
|
165
|
+
mailOrder: medicine.picker.mailOrder,
|
|
166
|
+
}), () => {
|
|
167
|
+
const current = now.current.medicines.find((each) => each.key === medicineKey);
|
|
168
|
+
return current !== undefined && placeOf(current.picker) === place;
|
|
169
|
+
});
|
|
170
|
+
run.running = false;
|
|
171
|
+
if (found)
|
|
172
|
+
updatePicker(medicineKey, (picker) => ({ ...picker, pharmacies: found }));
|
|
173
|
+
setSearchingPharmacies((was) => without(was, medicineKey));
|
|
174
|
+
const [waiting] = run.waiting;
|
|
175
|
+
if (waiting !== undefined) {
|
|
176
|
+
run.waiting.delete(waiting);
|
|
177
|
+
void searchPharmacies(waiting);
|
|
178
|
+
}
|
|
179
|
+
}, [conversationId, lookup, updatePicker]);
|
|
180
|
+
const { medicines, editing, queued, viewing, charts } = form;
|
|
181
|
+
const [firstMedicine] = medicines;
|
|
182
|
+
const firstItem = firstMedicine?.rounds[0];
|
|
183
|
+
const mayReachPharmacy = reachesPharmacy(role);
|
|
184
|
+
/** The Update or Save a staged card or a queued medicine runs, or null while nothing changed. */
|
|
185
|
+
const saveOf = (card, item, picker) => {
|
|
186
|
+
if (queued) {
|
|
187
|
+
const whole = queuedChange(card, item, picker);
|
|
188
|
+
return whole && (() => latest.current.updateQueued(conversationId, String(card.queuedId), whole));
|
|
189
|
+
}
|
|
190
|
+
const changes = cardChanges(card, item, picker);
|
|
191
|
+
return changes && (() => latest.current.updateCard(conversationId, card.uid, changes, pharmacyChange(picker)));
|
|
192
|
+
};
|
|
193
|
+
const save = editing && firstMedicine && firstItem ? saveOf(editing, firstItem, firstMedicine.picker) : null;
|
|
194
|
+
const orderItems = editing ? null : newOrderItems(medicines);
|
|
195
|
+
const done = () => {
|
|
196
|
+
latest.current.close();
|
|
197
|
+
generation.current += 1;
|
|
198
|
+
setForm(opened(null));
|
|
199
|
+
};
|
|
200
|
+
const create = async (items) => {
|
|
201
|
+
if (!charts) {
|
|
202
|
+
// Any failure to read the charts posts without one, and usrx then asks for /treat-patient.
|
|
203
|
+
const tied = chartsToPick(await latest.current.readCharts(conversationId).catch(() => null));
|
|
204
|
+
if (tied.charts.length > 0) {
|
|
205
|
+
setForm((was) => ({ ...was, charts: tied }));
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const order = buildOrder({ items, patientUid: pickedChart });
|
|
210
|
+
try {
|
|
211
|
+
await latest.current.postOrder(conversationId, noteText(order), order);
|
|
212
|
+
done();
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
setPostFailed(true);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
const update = async (run) => {
|
|
219
|
+
let answer;
|
|
220
|
+
try {
|
|
221
|
+
answer = await run();
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
setLookupError(lookupFailureOf(error));
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
latest.current.changed();
|
|
228
|
+
const refused = refusalOf(answer);
|
|
229
|
+
if (refused === null)
|
|
230
|
+
done();
|
|
231
|
+
else
|
|
232
|
+
setRefusal(refused);
|
|
233
|
+
};
|
|
234
|
+
const send = async (card) => {
|
|
235
|
+
try {
|
|
236
|
+
await latest.current.sendNote(conversationId, submitCommand([card.uid]));
|
|
237
|
+
done();
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
setPostFailed(true);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
/** What the form's main button runs, or null while it may not run. */
|
|
244
|
+
const action = viewing
|
|
245
|
+
? mayReachPharmacy && editing
|
|
246
|
+
? () => send(editing)
|
|
247
|
+
: null
|
|
248
|
+
: editing
|
|
249
|
+
? save && (() => update(save))
|
|
250
|
+
: orderItems && (() => create(orderItems));
|
|
251
|
+
return {
|
|
252
|
+
medicines,
|
|
253
|
+
editing,
|
|
254
|
+
viewing,
|
|
255
|
+
mayReachPharmacy,
|
|
256
|
+
ready: action !== null,
|
|
257
|
+
busy,
|
|
258
|
+
lookupError,
|
|
259
|
+
refusal,
|
|
260
|
+
postFailed,
|
|
261
|
+
charts,
|
|
262
|
+
pickedChart,
|
|
263
|
+
focus,
|
|
264
|
+
searchingPharmacies,
|
|
265
|
+
typeTerm: (itemKey, typed) => {
|
|
266
|
+
const term = typed.trim();
|
|
267
|
+
updateItem(itemKey, (item) => ({
|
|
268
|
+
...item,
|
|
269
|
+
term: typed,
|
|
270
|
+
drug: null,
|
|
271
|
+
choices: null,
|
|
272
|
+
found: term.length < SHORTEST_TERM ? [] : item.found,
|
|
273
|
+
searching: term.length >= SHORTEST_TERM,
|
|
274
|
+
}));
|
|
275
|
+
const timer = timers.current.get(itemKey);
|
|
276
|
+
if (timer)
|
|
277
|
+
clearTimeout(timer);
|
|
278
|
+
if (term.length < SHORTEST_TERM)
|
|
279
|
+
return;
|
|
280
|
+
timers.current.set(itemKey, setTimeout(() => {
|
|
281
|
+
const stillTyped = () => itemNow(itemKey)?.term.trim() === term;
|
|
282
|
+
void (async () => {
|
|
283
|
+
const found = await lookup(() => latest.current.findDrugs(conversationId, term), stillTyped);
|
|
284
|
+
if (stillTyped())
|
|
285
|
+
updateItem(itemKey, (item) => ({ ...item, searching: false, found: found ?? [] }));
|
|
286
|
+
})();
|
|
287
|
+
}, TYPING_PAUSE_MS));
|
|
288
|
+
},
|
|
289
|
+
pickDrug: (itemKey, drug) => {
|
|
290
|
+
updateItem(itemKey, (item) => ({
|
|
291
|
+
...item,
|
|
292
|
+
drug,
|
|
293
|
+
term: drug.name,
|
|
294
|
+
found: [],
|
|
295
|
+
choices: null,
|
|
296
|
+
loadingChoices: true,
|
|
297
|
+
cardSigInputs: '',
|
|
298
|
+
doseUnitId: '',
|
|
299
|
+
quantity: '',
|
|
300
|
+
frequencyId: '',
|
|
301
|
+
maxPerDay: '',
|
|
302
|
+
days: '',
|
|
303
|
+
dispenseUnitId: '',
|
|
304
|
+
dispenseAmount: '',
|
|
305
|
+
filledAmount: '',
|
|
306
|
+
}));
|
|
307
|
+
void (async () => {
|
|
308
|
+
const choices = await lookup(() => latest.current.readChoices(conversationId, drug.ddid), () => itemNow(itemKey)?.drug?.ddid === drug.ddid);
|
|
309
|
+
updateItem(itemKey, (item) => {
|
|
310
|
+
if (item.drug?.ddid !== drug.ddid)
|
|
311
|
+
return item;
|
|
312
|
+
const [onlyUnit, ...moreUnits] = choices?.doseUnits ?? [];
|
|
313
|
+
return {
|
|
314
|
+
...item,
|
|
315
|
+
loadingChoices: false,
|
|
316
|
+
choices,
|
|
317
|
+
doseUnitId: onlyUnit && moreUnits.length === 0 ? onlyUnit.id : item.doseUnitId,
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
})();
|
|
321
|
+
},
|
|
322
|
+
setField: (itemKey, field, value) => {
|
|
323
|
+
updateItem(itemKey, (item) => {
|
|
324
|
+
const changed = { ...item, [field]: value };
|
|
325
|
+
if (!AMOUNT_INPUTS.includes(field))
|
|
326
|
+
return changed;
|
|
327
|
+
const computed = computedAmount(changed);
|
|
328
|
+
return {
|
|
329
|
+
...changed,
|
|
330
|
+
dispenseAmount: nextDispenseAmount({ current: changed.dispenseAmount, lastFilled: changed.filledAmount, computed }),
|
|
331
|
+
filledAmount: computed,
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
setSubstituteOk: (itemKey, substituteOk) => {
|
|
336
|
+
updateItem(itemKey, (item) => ({ ...item, substituteOk }));
|
|
337
|
+
},
|
|
338
|
+
setDiscontinueDuplicate: (itemKey, discontinueDuplicate) => {
|
|
339
|
+
updateItem(itemKey, (item) => ({ ...item, discontinueDuplicate }));
|
|
340
|
+
},
|
|
341
|
+
setPlace: (medicineKey, was, place) => {
|
|
342
|
+
const picker = { ...was, ...place };
|
|
343
|
+
if (placeOf(picker) === placeOf(was))
|
|
344
|
+
return;
|
|
345
|
+
// A new place drops the last one's list at once, and a place that cannot search asks nothing.
|
|
346
|
+
const next = { ...picker, pharmacies: null };
|
|
347
|
+
now.current = {
|
|
348
|
+
...now.current,
|
|
349
|
+
medicines: now.current.medicines.map((each) => (each.key === medicineKey ? { ...each, picker: next } : each)),
|
|
350
|
+
};
|
|
351
|
+
updatePicker(medicineKey, () => next);
|
|
352
|
+
void searchPharmacies(medicineKey);
|
|
353
|
+
},
|
|
354
|
+
setWords: (medicineKey, words) => {
|
|
355
|
+
updatePicker(medicineKey, (picker) => ({ ...picker, words }));
|
|
356
|
+
},
|
|
357
|
+
pickPharmacy: (medicineKey, pharmacy) => {
|
|
358
|
+
updatePicker(medicineKey, (picker) => picked(picker, pharmacy));
|
|
359
|
+
},
|
|
360
|
+
addMedication: () => {
|
|
361
|
+
setForm((was) => ({ ...was, medicines: [...was.medicines, medicineOf([newItem(nextKey())])] }));
|
|
362
|
+
},
|
|
363
|
+
removeMedication: (medicineKey) => {
|
|
364
|
+
setForm((was) => ({ ...was, medicines: was.medicines.filter((medicine) => medicine.key !== medicineKey) }));
|
|
365
|
+
},
|
|
366
|
+
addRound: (medicineKey) => {
|
|
367
|
+
setForm((was) => ({
|
|
368
|
+
...was,
|
|
369
|
+
medicines: was.medicines.map((medicine) => medicine.key === medicineKey ? { ...medicine, rounds: renumbered([...medicine.rounds, newItem(nextKey())]) } : medicine),
|
|
370
|
+
}));
|
|
371
|
+
},
|
|
372
|
+
removeRound: (medicineKey, itemKey) => {
|
|
373
|
+
setForm((was) => ({
|
|
374
|
+
...was,
|
|
375
|
+
medicines: was.medicines.map((medicine) => medicine.key === medicineKey
|
|
376
|
+
? { ...medicine, rounds: renumbered(medicine.rounds.filter((item) => item.key !== itemKey)) }
|
|
377
|
+
: medicine),
|
|
378
|
+
}));
|
|
379
|
+
},
|
|
380
|
+
edit: () => {
|
|
381
|
+
setForm((was) => ({ ...was, viewing: false }));
|
|
382
|
+
},
|
|
383
|
+
// The button that calls this is off while there is no action and while one runs.
|
|
384
|
+
confirm: () => {
|
|
385
|
+
setBusy(true);
|
|
386
|
+
setRefusal('');
|
|
387
|
+
setPostFailed(false);
|
|
388
|
+
void (async () => {
|
|
389
|
+
try {
|
|
390
|
+
await action?.();
|
|
391
|
+
}
|
|
392
|
+
finally {
|
|
393
|
+
setBusy(false);
|
|
394
|
+
}
|
|
395
|
+
})();
|
|
396
|
+
},
|
|
397
|
+
pickChart: setPickedChart,
|
|
398
|
+
backToOrder: () => {
|
|
399
|
+
setForm((was) => ({ ...was, charts: null }));
|
|
400
|
+
setPickedChart('');
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type OrderRow, type PastOrder, type StagedCard } from '../helpers/medicationOrder.ts';
|
|
2
|
+
import { type OrderAction, type OrderList, type OrderLists } from '../helpers/ordersMenu.ts';
|
|
3
|
+
import type { ChartsToPick } from '../helpers/orderForm.ts';
|
|
4
|
+
import type { StaffMessage } from '../helpers/usrxNote.ts';
|
|
5
|
+
/** How the app's order form opens: blank, on a staged card, or on a sent order to order again. */
|
|
6
|
+
export interface OrderFormOptions {
|
|
7
|
+
/** Read only, with Send. */
|
|
8
|
+
view?: boolean;
|
|
9
|
+
queued?: boolean;
|
|
10
|
+
/** A new order from a sent one, with its fill date cleared. */
|
|
11
|
+
reorder?: boolean;
|
|
12
|
+
/** Keep the sent order's pharmacy. */
|
|
13
|
+
samePharmacy?: boolean;
|
|
14
|
+
/** Every round of a set, in order, for a re-order of the set. */
|
|
15
|
+
rounds?: PastOrder[];
|
|
16
|
+
/** The field usrx refused a Send for, which the form opens at. */
|
|
17
|
+
focus?: string;
|
|
18
|
+
/** Treat's charts that tie for the patient, which a refill opens the form on to pick from. */
|
|
19
|
+
charts?: ChartsToPick;
|
|
20
|
+
}
|
|
21
|
+
export interface OrderFormRequest {
|
|
22
|
+
/** Null for a blank new order. */
|
|
23
|
+
card: StagedCard | PastOrder | null;
|
|
24
|
+
/** Settles once no Treat read is on the way, since usrx takes one job at a time. */
|
|
25
|
+
readsDone: Promise<void>;
|
|
26
|
+
options: OrderFormOptions;
|
|
27
|
+
}
|
|
28
|
+
export interface OrdersMenuSource {
|
|
29
|
+
conversationId: number;
|
|
30
|
+
/** The conversation's messages: a new usrx note among them means Treat's cards may have changed. */
|
|
31
|
+
messages: readonly StaffMessage[] | undefined;
|
|
32
|
+
/** The signed-in staff member's skips_role. Only a role that reaches a pharmacy sends or cancels. */
|
|
33
|
+
role: string | null;
|
|
34
|
+
/** The conversation's staged cards. Rejects with treat_lookups' answer for a failed read. */
|
|
35
|
+
readStaged: (conversationId: number) => Promise<StagedCard[]>;
|
|
36
|
+
/** The conversation's sent orders, newest first. */
|
|
37
|
+
readPast: (conversationId: number) => Promise<PastOrder[]>;
|
|
38
|
+
/** Asks usrx to delete a staged card, resolving null for a delete and usrx's refusal otherwise. */
|
|
39
|
+
deleteCard: (conversationId: number, uid: string) => Promise<unknown>;
|
|
40
|
+
/** The same for a queued card, by its queue number. */
|
|
41
|
+
deleteQueued: (conversationId: number, queuedId: string) => Promise<unknown>;
|
|
42
|
+
/** Posts a private note in the conversation. */
|
|
43
|
+
sendNote: (conversationId: number, note: string) => Promise<unknown>;
|
|
44
|
+
/** Opens the app's order form. Without it the menu orders nothing. */
|
|
45
|
+
openForm?: ((request: OrderFormRequest) => void) | undefined;
|
|
46
|
+
/** Stages a refill of a sent order to the same pharmacy, opening the form only for what it lacks. Rejects when it fails. */
|
|
47
|
+
stageRefill?: ((order: PastOrder, readsDone: Promise<void>) => Promise<unknown>) | undefined;
|
|
48
|
+
/** A count the app raises each time its order form changed Treat's cards, which makes the menu read them again. */
|
|
49
|
+
formChanges?: number | undefined;
|
|
50
|
+
}
|
|
51
|
+
export interface PendingAction {
|
|
52
|
+
action: OrderAction;
|
|
53
|
+
row: OrderRow;
|
|
54
|
+
}
|
|
55
|
+
export interface OrdersMenuView {
|
|
56
|
+
/** The staged cards' count for the button, kept through a failed read. */
|
|
57
|
+
count: number;
|
|
58
|
+
reading: boolean;
|
|
59
|
+
open: boolean;
|
|
60
|
+
/** The list the menu shows, or null for the menu's top level. */
|
|
61
|
+
list: OrderList | null;
|
|
62
|
+
/** The staged cards, none after a failed read, since a card Treat can no longer vouch for could be sent or gone. */
|
|
63
|
+
cards: StagedCard[];
|
|
64
|
+
/** The reference of a failed staged read, '' when it had none, or null. */
|
|
65
|
+
cardsFailed: string | null;
|
|
66
|
+
pastFailed: boolean;
|
|
67
|
+
lists: OrderLists;
|
|
68
|
+
/** Past orders' rows the status filter shows. */
|
|
69
|
+
shownPast: OrderRow[];
|
|
70
|
+
ticked: ReadonlySet<string>;
|
|
71
|
+
filterOpen: boolean;
|
|
72
|
+
refillChoicesFor: string | null;
|
|
73
|
+
actionsFor: string | null;
|
|
74
|
+
/** The row a refill to the same pharmacy is staging for. */
|
|
75
|
+
staging: string | null;
|
|
76
|
+
/** The row whose refill to the same pharmacy failed to stage. */
|
|
77
|
+
stageFailed: string | null;
|
|
78
|
+
mayReachPharmacy: boolean;
|
|
79
|
+
/** The app has an order form to open. */
|
|
80
|
+
canOrder: boolean;
|
|
81
|
+
today: string;
|
|
82
|
+
toDelete: StagedCard | null;
|
|
83
|
+
deleting: boolean;
|
|
84
|
+
deleteRefused: string | null;
|
|
85
|
+
deleteFailed: boolean;
|
|
86
|
+
pending: PendingAction | null;
|
|
87
|
+
reason: string;
|
|
88
|
+
acting: boolean;
|
|
89
|
+
actionFailed: boolean;
|
|
90
|
+
toggle: () => void;
|
|
91
|
+
close: () => void;
|
|
92
|
+
show: (list: OrderList | null) => void;
|
|
93
|
+
refetch: () => Promise<void>;
|
|
94
|
+
toggleFilter: () => void;
|
|
95
|
+
toggleStatus: (status: string) => void;
|
|
96
|
+
deselectAll: () => void;
|
|
97
|
+
newOrder: () => void;
|
|
98
|
+
editCard: (card: StagedCard) => void;
|
|
99
|
+
sendCard: (card: StagedCard) => void;
|
|
100
|
+
askDelete: (card: StagedCard) => void;
|
|
101
|
+
cancelDelete: () => void;
|
|
102
|
+
confirmDelete: () => void;
|
|
103
|
+
showRefillChoices: (row: OrderRow) => void;
|
|
104
|
+
stageSamePharmacy: (row: OrderRow) => void;
|
|
105
|
+
stageNewPharmacy: (row: OrderRow) => void;
|
|
106
|
+
reorder: (row: OrderRow) => void;
|
|
107
|
+
showActions: (row: OrderRow) => void;
|
|
108
|
+
askAction: (action: OrderAction, row: OrderRow) => void;
|
|
109
|
+
setReason: (reason: string) => void;
|
|
110
|
+
cancelAction: () => void;
|
|
111
|
+
runAction: () => void;
|
|
112
|
+
}
|
|
113
|
+
/** The Orders menu of a conversation: what Treat holds for it, and what staff can do from it. */
|
|
114
|
+
export declare function useOrdersMenu(source: OrdersMenuSource): OrdersMenuView;
|