@bahmni/widgets 0.0.1-dev.450 → 0.0.1-dev.452

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.
@@ -0,0 +1,440 @@
1
+ import { jsx as o, jsxs as _, Fragment as Ee } from "react/jsx-runtime";
2
+ import { p as fe, a as Ae, u as ge, b as Ne, c as Ce, d as De, Q as Ie, N as Le, i as Oe, v as be, X as ye, _ as X, e as Se, f as ve, x as we, W as Ue, g as Me, h as J, r as te } from "./index-ClqkhB6j.js";
3
+ import { useQuery as Re } from "@tanstack/react-query";
4
+ import { useState as p, useRef as Pe, useEffect as f, useMemo as A, useCallback as E } from "react";
5
+ import { useTranslation as ke } from "react-i18next";
6
+ const He = "DocumentsTable-module_documentsTableBody__6iR3w", Be = "DocumentsTable-module_viewAttachmentsLink__aQ1on", $e = "DocumentsTable-module_documentViewerContainer__F5m8D", Fe = "DocumentsTable-module_printContainer__rtarJ", Ve = "DocumentsTable-module_attachmentItem__17KA7", We = "DocumentsTable-module_attachmentCounter__WFiI3", ze = "DocumentsTable-module_attachmentError__RI4U2", Ye = "DocumentsTable-module_documentIframe__ptq5I", Ge = "DocumentsTable-module_attachmentContent__IaCEe", qe = "DocumentsTable-module_attachmentLoading__FAR6p", Ke = "DocumentsTable-module_hiddenWhileLoading__IxocW", Qe = "DocumentsTable-module_documentImage__KQVgS", je = "DocumentsTable-module_header__345GY", Xe = "DocumentsTable-module_documentTypeCell__uWGM8", Je = "DocumentsTable-module_printAllButton__IVYUW", Ze = "DocumentsTable-module_attachmentHeader__yI2xn", c = {
7
+ documentsTableBody: He,
8
+ viewAttachmentsLink: Be,
9
+ documentViewerContainer: $e,
10
+ printContainer: Fe,
11
+ attachmentItem: Ve,
12
+ attachmentCounter: We,
13
+ attachmentError: ze,
14
+ documentIframe: Ye,
15
+ attachmentContent: Ge,
16
+ attachmentLoading: qe,
17
+ hiddenWhileLoading: Ke,
18
+ documentImage: Qe,
19
+ header: je,
20
+ documentTypeCell: Xe,
21
+ printAllButton: Je,
22
+ attachmentHeader: Ze
23
+ }, xe = {
24
+ documentIdentifier: "DOCUMENTS_DOCUMENT_IDENTIFIER",
25
+ documentType: "DOCUMENTS_TYPE",
26
+ uploadedOn: "DOCUMENTS_UPLOADED_ON",
27
+ uploadedBy: "DOCUMENTS_UPLOADED_BY",
28
+ attachments: "DOCUMENTS_ATTACHMENTS"
29
+ };
30
+ function et(n) {
31
+ if (!n)
32
+ return "document";
33
+ const d = n.toLowerCase();
34
+ return d.includes("pdf") ? "pdf" : d.includes("image") ? "image" : "document";
35
+ }
36
+ function b(n) {
37
+ return !n || n.includes(":") ? "#" : `/openmrs/auth?requested_document=/document_images/${n}`;
38
+ }
39
+ function tt(n, d) {
40
+ return n.map((l) => ({
41
+ key: l,
42
+ header: d(xe[l])
43
+ }));
44
+ }
45
+ const nt = {
46
+ "&": "&",
47
+ "<": "&lt;",
48
+ ">": "&gt;",
49
+ '"': "&quot;",
50
+ "'": "&#39;"
51
+ }, F = (n) => n.replace(/[&<>"']/g, (d) => nt[d]), at = "body{margin:0}img{max-width:100%;max-height:100vh;object-fit:contain;display:block;margin:0 auto}", ot = ({
52
+ contentType: n
53
+ }) => et(n) === "pdf", ne = (n) => new URL(b(n), globalThis.location.origin).href;
54
+ function st({ url: n }, d) {
55
+ const l = F(ne(n));
56
+ return `<!DOCTYPE html><html><head><meta charset="utf-8" /><title>${F(
57
+ d
58
+ )}</title><style>${at}</style></head><body><img src="${l}" alt="${F(
59
+ d
60
+ )}" /></body></html>`;
61
+ }
62
+ async function rt(n, d, {
63
+ container: l,
64
+ signal: D
65
+ }) {
66
+ const r = { container: l, focus: !1, signal: D };
67
+ for (const g of n) {
68
+ if (D.aborted) break;
69
+ ot(g) ? await fe(ne(g.url), r) : await Ae(
70
+ st(g, d),
71
+ r
72
+ );
73
+ }
74
+ }
75
+ const ct = "startConsultation", lt = "add", dt = "documents", Z = "document-print-container", x = () => /* @__PURE__ */ o(te, {}), it = /* @__PURE__ */ new Set([
76
+ "ArrowUp",
77
+ "ArrowDown",
78
+ "PageUp",
79
+ "PageDown",
80
+ "Home",
81
+ "End",
82
+ " "
83
+ ]), mt = 15e3, ut = [
84
+ "documentType",
85
+ "documentIdentifier",
86
+ "attachments",
87
+ "uploadedOn",
88
+ "uploadedBy"
89
+ ], ht = /\.(jpg|jpeg|png|gif|webp|bmp|svg)$/i, ee = (n) => n.attachments.length > 0 ? n.attachments : n.documentUrl ? [{ url: n.documentUrl, contentType: n.contentType }] : [], At = ({ config: n, encounterUuids: d }) => {
90
+ const [l, D] = p(!1), [r, g] = p(
91
+ null
92
+ ), [y, U] = p(
93
+ /* @__PURE__ */ new Set()
94
+ ), [S, M] = p(
95
+ /* @__PURE__ */ new Set()
96
+ ), I = Pe(null), [R, V] = p(
97
+ null
98
+ ), W = R !== null;
99
+ f(() => () => I.current?.abort(), []);
100
+ const N = ge(), { t: s } = ke(), { addNotification: v } = Ne(), h = A(
101
+ () => n?.actions?.find(
102
+ (e) => e.type === lt
103
+ ),
104
+ [n?.actions]
105
+ ), ae = Ce(h?.requiredPrivilege), oe = !!h && ae, se = Number(n?.pageSize) || 5, [P, k] = p(1), [L, re] = p(se), [ce, H] = p(void 0), z = E((e) => {
106
+ g(e), D(!0);
107
+ }, []), le = E(() => {
108
+ I.current?.abort(), M(/* @__PURE__ */ new Set()), D(!1), g(null), U(/* @__PURE__ */ new Set());
109
+ }, []), Y = E((e) => {
110
+ M((t) => new Set(t).add(e));
111
+ }, []), G = E((e) => {
112
+ U((t) => new Set(t).add(e));
113
+ }, []), de = E(() => {
114
+ h && globalThis.dispatchEvent(
115
+ new CustomEvent(ct, {
116
+ detail: {
117
+ encounterType: h.encounterType,
118
+ editOnly: h.editOnly ?? dt,
119
+ ...h.editTitle ? { editTitle: h.editTitle } : {}
120
+ }
121
+ })
122
+ );
123
+ }, [h]), { data: O, isLoading: ie, isError: B, error: $, refetch: q } = Re({
124
+ queryKey: [
125
+ "documents",
126
+ N,
127
+ d,
128
+ P,
129
+ L
130
+ ],
131
+ enabled: !!N,
132
+ queryFn: () => De(
133
+ N,
134
+ d,
135
+ L,
136
+ P
137
+ )
138
+ });
139
+ Ie(
140
+ (e) => {
141
+ e.patientUUID === N && e.updatedResources.documents && q();
142
+ },
143
+ [N, q]
144
+ ), f(() => {
145
+ O && H(O.total);
146
+ }, [O]), f(() => {
147
+ k(1), H(void 0);
148
+ }, [N]), f(() => {
149
+ B && v({
150
+ title: s("ERROR_DEFAULT_TITLE"),
151
+ message: $?.message ?? "",
152
+ type: "error"
153
+ });
154
+ }, [B, $, v, s]), f(() => {
155
+ const e = new AbortController();
156
+ return (async () => {
157
+ if (!l || !r) return;
158
+ const a = /* @__PURE__ */ new Set(), u = ee(r);
159
+ for (let i = 0; i < u.length; i++) {
160
+ const C = b(u[i].url);
161
+ try {
162
+ (await fetch(C, {
163
+ method: "HEAD",
164
+ signal: e.signal
165
+ })).ok || a.add(i);
166
+ } catch (T) {
167
+ T instanceof Error && T.name !== "AbortError" && a.add(i);
168
+ }
169
+ }
170
+ e.signal.aborted || U(a);
171
+ })(), () => e.abort();
172
+ }, [l, r]), f(() => {
173
+ if (!l || !r) return;
174
+ const e = document.querySelector(
175
+ '[data-testid="document-view-modal"] .cds--modal-content'
176
+ );
177
+ if (!e) return;
178
+ let t = !1;
179
+ const a = () => {
180
+ t = !0;
181
+ }, u = (T) => {
182
+ T.target === e && a();
183
+ }, i = (T) => {
184
+ const pe = T.target;
185
+ it.has(T.key) && !pe?.closest("button, input, textarea, select, a") && a();
186
+ }, C = () => {
187
+ !t && e.scrollTop !== 0 && (e.scrollTop = 0);
188
+ };
189
+ return e.addEventListener("wheel", a, { passive: !0 }), e.addEventListener("touchmove", a, { passive: !0 }), e.addEventListener("mousedown", u), e.addEventListener("keydown", i), e.addEventListener("scroll", C), () => {
190
+ e.removeEventListener("wheel", a), e.removeEventListener("touchmove", a), e.removeEventListener("mousedown", u), e.removeEventListener("keydown", i), e.removeEventListener("scroll", C);
191
+ };
192
+ }, [l, r]);
193
+ const me = E(
194
+ (e, t) => {
195
+ t !== L ? (re(t), k(1), H(void 0)) : k(e);
196
+ },
197
+ [L]
198
+ ), w = A(
199
+ () => n?.fields ?? ut,
200
+ [n?.fields]
201
+ ), ue = A(() => [...O?.documents ?? []].sort((t, a) => !t.uploadedOn && !a.uploadedOn ? 0 : t.uploadedOn ? a.uploadedOn ? new Date(a.uploadedOn).getTime() - new Date(t.uploadedOn).getTime() : -1 : 1), [O]), he = A(() => tt(w, s), [w, s]), Te = A(
202
+ () => w.map((e) => ({
203
+ key: e,
204
+ sortable: e !== "attachments"
205
+ })),
206
+ [w]
207
+ ), m = A(
208
+ () => r ? ee(r) : [],
209
+ [r]
210
+ ), K = m.every(
211
+ (e, t) => S.has(t) || y.has(t) || b(e.url) === "#"
212
+ );
213
+ f(() => {
214
+ if (!l || m.length === 0) return;
215
+ const e = setTimeout(
216
+ () => M(new Set(m.keys())),
217
+ mt
218
+ );
219
+ return () => clearTimeout(e);
220
+ }, [l, m]);
221
+ const Q = E(
222
+ async (e, t) => {
223
+ if (!r || I.current) return;
224
+ const a = new AbortController(), { signal: u } = a;
225
+ I.current = a, V(t);
226
+ const i = document.getElementById(Z);
227
+ try {
228
+ await rt(
229
+ e,
230
+ r.documentIdentifier,
231
+ { container: i, signal: u }
232
+ );
233
+ } catch {
234
+ u.aborted || v({
235
+ title: s("ERROR_DEFAULT_TITLE"),
236
+ message: s("DOCUMENTS_ERROR_LOADING_ATTACHMENT"),
237
+ type: "error"
238
+ });
239
+ } finally {
240
+ I.current = null, V(null);
241
+ }
242
+ },
243
+ [r, v, s]
244
+ ), j = A(
245
+ () => m.filter(
246
+ (e, t) => b(e.url) !== "#" && !y.has(t)
247
+ ),
248
+ [m, y]
249
+ ), _e = E(
250
+ (e, t) => {
251
+ switch (t) {
252
+ case "documentIdentifier":
253
+ return /* @__PURE__ */ o("span", { children: e.documentIdentifier?.trim() || "-" });
254
+ case "documentType":
255
+ return /* @__PURE__ */ _("div", { className: c.documentTypeCell, children: [
256
+ /* @__PURE__ */ o("span", { children: e.documentType ?? s("DOCUMENTS_NOT_AVAILABLE") }),
257
+ e.description && /* @__PURE__ */ o(
258
+ be,
259
+ {
260
+ iconName: "fa-file-lines",
261
+ content: e.description,
262
+ ariaLabel: e.description,
263
+ testId: `document-notes-${e.id}`
264
+ }
265
+ )
266
+ ] });
267
+ case "uploadedOn":
268
+ return Oe(
269
+ e.uploadedOn,
270
+ s,
271
+ !0
272
+ ).formattedResult || s("DOCUMENTS_NOT_AVAILABLE");
273
+ case "uploadedBy":
274
+ return e.uploadedBy ?? s("DOCUMENTS_NOT_AVAILABLE");
275
+ case "attachments":
276
+ return e.attachments.length > 0 || !!e.documentUrl ? /* @__PURE__ */ o(
277
+ Le,
278
+ {
279
+ onClick: () => z(e),
280
+ className: c.viewAttachmentsLink,
281
+ "data-testid": `view-attachments-${e.id}`,
282
+ children: s("DOCUMENTS_VIEW_ATTACHMENTS")
283
+ }
284
+ ) : "--";
285
+ default:
286
+ return null;
287
+ }
288
+ },
289
+ [z, s]
290
+ );
291
+ return /* @__PURE__ */ _(Ee, { children: [
292
+ oe && /* @__PURE__ */ _(ye, { className: c.header, testId: "documents-header", children: [
293
+ /* @__PURE__ */ o("p", { children: s("DOCUMENTS_TABLE_HEADING") }),
294
+ /* @__PURE__ */ o(
295
+ X,
296
+ {
297
+ autoAlign: !0,
298
+ size: "lg",
299
+ kind: "ghost",
300
+ label: s(h.label),
301
+ testId: "documents-add-button",
302
+ onClick: de,
303
+ children: /* @__PURE__ */ o(Se, { id: "documents-add-icon", name: "fa-plus", size: ve.LG })
304
+ }
305
+ )
306
+ ] }),
307
+ /* @__PURE__ */ o("div", { "data-testid": "documents-table", children: /* @__PURE__ */ o(
308
+ we,
309
+ {
310
+ headers: he,
311
+ ariaLabel: s("DOCUMENTS_TABLE_HEADING"),
312
+ rows: ue,
313
+ loading: ie,
314
+ errorStateMessage: B ? $?.message : null,
315
+ sortable: Te,
316
+ emptyStateMessage: s("DOCUMENTS_NO_RECORDS"),
317
+ renderCell: _e,
318
+ className: c.documentsTableBody,
319
+ dataTestId: "documents-table",
320
+ pageSize: L,
321
+ totalItems: ce,
322
+ page: P,
323
+ onPageChange: me
324
+ }
325
+ ) }),
326
+ l && r && /* @__PURE__ */ o(
327
+ Ue,
328
+ {
329
+ id: "modalIdForActionAreaLayout",
330
+ portalId: "main-display-area",
331
+ open: l,
332
+ onRequestClose: le,
333
+ modalHeading: r.documentIdentifier,
334
+ passiveModal: !0,
335
+ size: "lg",
336
+ testId: "document-view-modal",
337
+ children: /* @__PURE__ */ _("div", { className: c.documentViewerContainer, children: [
338
+ j.length > 1 && /* @__PURE__ */ o(
339
+ Me,
340
+ {
341
+ kind: "tertiary",
342
+ size: "sm",
343
+ className: c.printAllButton,
344
+ renderIcon: R === "all" ? x : J,
345
+ disabled: W || !K,
346
+ testId: "print-all-attachments",
347
+ onClick: () => Q(j, "all"),
348
+ children: s("DOCUMENTS_PRINT_ALL")
349
+ }
350
+ ),
351
+ m.map((e, t) => {
352
+ const a = b(e.url), u = e.contentType?.toLowerCase().includes("pdf"), i = (e.contentType?.toLowerCase().includes("image") ?? !1) || ht.test(e.url ?? ""), C = y.has(t);
353
+ return a === "#" || C ? /* @__PURE__ */ _(
354
+ "div",
355
+ {
356
+ className: c.attachmentError,
357
+ "data-testid": `attachment-error-${t}`,
358
+ children: [
359
+ m.length > 1 && /* @__PURE__ */ o("p", { className: c.attachmentCounter, children: s("DOCUMENTS_ATTACHMENT_COUNTER", {
360
+ current: t + 1,
361
+ total: m.length
362
+ }) }),
363
+ /* @__PURE__ */ o("p", { children: s("DOCUMENTS_ERROR_LOADING_ATTACHMENT") })
364
+ ]
365
+ },
366
+ `${r.id}-${e.url}`
367
+ ) : /* @__PURE__ */ _(
368
+ "div",
369
+ {
370
+ className: c.attachmentItem,
371
+ "data-testid": `attachment-item-${t}`,
372
+ children: [
373
+ /* @__PURE__ */ _("div", { className: c.attachmentHeader, children: [
374
+ m.length > 1 ? /* @__PURE__ */ o("p", { className: c.attachmentCounter, children: s("DOCUMENTS_ATTACHMENT_COUNTER", {
375
+ current: t + 1,
376
+ total: m.length
377
+ }) }) : /* @__PURE__ */ o("span", {}),
378
+ /* @__PURE__ */ o(
379
+ X,
380
+ {
381
+ kind: "ghost",
382
+ size: "sm",
383
+ label: s("DOCUMENTS_PRINT_ATTACHMENT", {
384
+ index: t + 1
385
+ }),
386
+ disabled: W || !K,
387
+ testId: `print-attachment-${t}`,
388
+ onClick: () => Q([e], t),
389
+ children: R === t ? /* @__PURE__ */ o(x, {}) : /* @__PURE__ */ o(J, {})
390
+ }
391
+ )
392
+ ] }),
393
+ /* @__PURE__ */ _("div", { className: c.attachmentContent, children: [
394
+ !S.has(t) && /* @__PURE__ */ o(
395
+ "div",
396
+ {
397
+ className: c.attachmentLoading,
398
+ "data-testid": `attachment-loading-${t}`,
399
+ children: /* @__PURE__ */ o(
400
+ te,
401
+ {
402
+ description: s("DOCUMENTS_LOADING_ATTACHMENT")
403
+ }
404
+ )
405
+ }
406
+ ),
407
+ i ? /* @__PURE__ */ o(
408
+ "img",
409
+ {
410
+ src: a,
411
+ alt: r.documentIdentifier,
412
+ className: `${c.documentImage} ${S.has(t) ? "" : c.hiddenWhileLoading}`,
413
+ onLoad: () => Y(t),
414
+ onError: () => G(t)
415
+ }
416
+ ) : /* @__PURE__ */ o(
417
+ "iframe",
418
+ {
419
+ src: u ? `${a}#toolbar=0` : a,
420
+ className: `${c.documentIframe} ${S.has(t) ? "" : c.hiddenWhileLoading}`,
421
+ title: r.documentIdentifier,
422
+ onLoad: () => Y(t),
423
+ onError: () => G(t)
424
+ }
425
+ )
426
+ ] })
427
+ ]
428
+ },
429
+ `${r.id}-${e.url}`
430
+ );
431
+ }),
432
+ /* @__PURE__ */ o("div", { id: Z, className: c.printContainer })
433
+ ] })
434
+ }
435
+ )
436
+ ] });
437
+ };
438
+ export {
439
+ At as default
440
+ };