@openmrs/esm-patient-common-lib 11.3.1-pre.9520 → 11.3.1-pre.9537

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-patient-common-lib",
3
- "version": "11.3.1-pre.9520",
3
+ "version": "11.3.1-pre.9537",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Library for common patient chart components",
6
6
  "browser": "dist/openmrs-esm-patient-common-lib.js",
@@ -1,28 +1,86 @@
1
- import { showSnackbar } from '@openmrs/esm-framework';
2
- import { type TFunction } from 'i18next';
1
+ import { showSnackbar, translateFrom } from '@openmrs/esm-framework';
3
2
  import { type OrderBasketItem } from './types';
4
3
 
5
- export function showOrderSuccessToast(t: TFunction, patientOrderItems: OrderBasketItem[]) {
6
- const orderedString = patientOrderItems
7
- .filter((item) => ['NEW', 'RENEW'].includes(item.action))
8
- .map((item) => item.display)
9
- .join(', ');
10
- const updatedString = patientOrderItems
11
- .filter((item) => item.action === 'REVISE')
12
- .map((item) => item.display)
13
- .join(', ');
14
- const discontinuedString = patientOrderItems
15
- .filter((item) => item.action === 'DISCONTINUE')
16
- .map((item) => item.display)
17
- .join(', ');
4
+ type OrderAction = 'placed' | 'updated' | 'discontinued';
5
+
6
+ function getNotificationTitle(
7
+ moduleName: string,
8
+ placedOrders: OrderBasketItem[],
9
+ updatedOrders: OrderBasketItem[],
10
+ discontinuedOrders: OrderBasketItem[],
11
+ activeActions: OrderAction[],
12
+ ): string {
13
+ if (activeActions.length > 1) {
14
+ return translateFrom(moduleName, 'ordersCompleted', 'Orders completed');
15
+ }
16
+
17
+ const action = activeActions[0];
18
+
19
+ if (action === 'placed') {
20
+ return placedOrders.length === 1
21
+ ? translateFrom(moduleName, 'orderPlaced', 'Order placed')
22
+ : translateFrom(moduleName, 'ordersPlaced', 'Orders placed');
23
+ }
24
+
25
+ if (action === 'updated') {
26
+ return updatedOrders.length === 1
27
+ ? translateFrom(moduleName, 'orderUpdated', 'Order updated')
28
+ : translateFrom(moduleName, 'ordersUpdated', 'Orders updated');
29
+ }
30
+
31
+ // action === 'discontinued'
32
+ return discontinuedOrders.length === 1
33
+ ? translateFrom(moduleName, 'orderDiscontinued', 'Order discontinued')
34
+ : translateFrom(moduleName, 'ordersDiscontinued', 'Orders discontinued');
35
+ }
36
+
37
+ /**
38
+ * Shows a success toast notification for order operations.
39
+ * The notification title dynamically reflects the action(s) taken (placed, updated, discontinued)
40
+ * and whether it's singular or plural.
41
+ *
42
+ * @param moduleName - The module name (e.g., '@openmrs/esm-patient-orders-app') to use for translations
43
+ * @param patientOrderItems - Array of order basket items that were processed
44
+ */
45
+ export function showOrderSuccessToast(moduleName: string, patientOrderItems: OrderBasketItem[]) {
46
+ if (patientOrderItems.length === 0) {
47
+ return;
48
+ }
49
+
50
+ const placedOrders = patientOrderItems.filter((item) => ['NEW', 'RENEW'].includes(item.action));
51
+ const updatedOrders = patientOrderItems.filter((item) => item.action === 'REVISE');
52
+ const discontinuedOrders = patientOrderItems.filter((item) => item.action === 'DISCONTINUE');
53
+
54
+ const orderedString = placedOrders.map((item) => item.display).join(', ');
55
+ const updatedString = updatedOrders.map((item) => item.display).join(', ');
56
+ const discontinuedString = discontinuedOrders.map((item) => item.display).join(', ');
57
+
58
+ const activeActions: OrderAction[] = [
59
+ orderedString && 'placed',
60
+ updatedString && 'updated',
61
+ discontinuedString && 'discontinued',
62
+ ].filter(Boolean) as OrderAction[];
63
+
64
+ const title = getNotificationTitle(moduleName, placedOrders, updatedOrders, discontinuedOrders, activeActions);
65
+
66
+ const subtitleParts: string[] = [];
67
+
68
+ if (orderedString) {
69
+ subtitleParts.push(`${translateFrom(moduleName, 'orderedFor', 'Placed order for')} ${orderedString}.`);
70
+ }
71
+
72
+ if (updatedString) {
73
+ subtitleParts.push(`${translateFrom(moduleName, 'updated', 'Updated')} ${updatedString}.`);
74
+ }
75
+
76
+ if (discontinuedString) {
77
+ subtitleParts.push(`${translateFrom(moduleName, 'discontinued', 'Discontinued')} ${discontinuedString}.`);
78
+ }
18
79
 
19
80
  showSnackbar({
20
81
  isLowContrast: true,
21
82
  kind: 'success',
22
- title: t('orderCompleted', 'Placed orders'),
23
- subtitle:
24
- (orderedString && `${t('ordered', 'Placed order for')} ${orderedString}. `) +
25
- (updatedString && `${t('updated', 'Updated')} ${updatedString}. `) +
26
- (discontinuedString && `${t('discontinued', 'Discontinued')} ${discontinuedString}.`),
83
+ title,
84
+ subtitle: subtitleParts.join(' '),
27
85
  });
28
86
  }