@olenbetong/synergi-react 5.4.0 → 5.6.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.
Files changed (133) hide show
  1. package/dist/styles/styles.css +1 -1
  2. package/dist/styles/styles.css.map +1 -1
  3. package/es/charts/CallOffDeliveries/TicketScanDialog.d.ts +7 -0
  4. package/es/charts/CallOffDeliveries/TicketScanDialog.js +47 -0
  5. package/es/charts/CallOffDeliveries/chartOptions.d.ts +8 -0
  6. package/es/charts/CallOffDeliveries/chartOptions.js +138 -0
  7. package/es/charts/CallOffDeliveries/data.d.ts +35 -0
  8. package/es/charts/CallOffDeliveries/data.js +30 -0
  9. package/es/charts/CallOffDeliveries/index.css +1 -0
  10. package/es/charts/CallOffDeliveries/index.d.ts +24 -0
  11. package/es/charts/CallOffDeliveries/index.js +115 -0
  12. package/es/charts/CallOffDeliveries/model.d.ts +37 -0
  13. package/es/charts/CallOffDeliveries/model.js +59 -0
  14. package/es/charts/ProductionPlan/PlanValueDialog.d.ts +14 -0
  15. package/es/charts/ProductionPlan/PlanValueDialog.js +47 -0
  16. package/es/charts/ProductionPlan/chartOptions.d.ts +20 -0
  17. package/es/charts/ProductionPlan/chartOptions.js +353 -0
  18. package/es/charts/ProductionPlan/data.d.ts +105 -0
  19. package/es/charts/ProductionPlan/data.js +61 -0
  20. package/es/charts/ProductionPlan/index.css +1 -0
  21. package/es/charts/ProductionPlan/index.d.ts +31 -0
  22. package/es/charts/ProductionPlan/index.js +203 -0
  23. package/es/charts/ProductionPlan/model.d.ts +29 -0
  24. package/es/charts/ProductionPlan/model.js +65 -0
  25. package/es/charts/Pumping/chartOptions.d.ts +12 -0
  26. package/es/charts/Pumping/chartOptions.js +197 -0
  27. package/es/charts/Pumping/data.d.ts +65 -0
  28. package/es/charts/Pumping/data.js +20 -0
  29. package/es/charts/Pumping/index.css +1 -0
  30. package/es/charts/Pumping/index.d.ts +27 -0
  31. package/es/charts/Pumping/index.js +124 -0
  32. package/es/charts/Pumping/model.d.ts +34 -0
  33. package/es/charts/Pumping/model.js +88 -0
  34. package/es/charts/UnboundPreview/chartOptions.d.ts +3 -0
  35. package/es/charts/UnboundPreview/chartOptions.js +126 -0
  36. package/es/charts/UnboundPreview/index.css +1 -0
  37. package/es/charts/UnboundPreview/index.d.ts +20 -0
  38. package/es/charts/UnboundPreview/index.js +55 -0
  39. package/es/charts/UnboundPreview/model.d.ts +49 -0
  40. package/es/charts/UnboundPreview/model.js +67 -0
  41. package/es/charts/VehicleDeliveries/chartOptions.d.ts +13 -0
  42. package/es/charts/VehicleDeliveries/chartOptions.js +270 -0
  43. package/es/charts/VehicleDeliveries/data.d.ts +71 -0
  44. package/es/charts/VehicleDeliveries/data.js +20 -0
  45. package/es/charts/VehicleDeliveries/deliveryXRange.d.ts +4 -0
  46. package/es/charts/VehicleDeliveries/deliveryXRange.js +62 -0
  47. package/es/charts/VehicleDeliveries/index.css +1 -0
  48. package/es/charts/VehicleDeliveries/index.d.ts +24 -0
  49. package/es/charts/VehicleDeliveries/index.js +129 -0
  50. package/es/charts/VehicleDeliveries/model.d.ts +24 -0
  51. package/es/charts/VehicleDeliveries/model.js +106 -0
  52. package/es/charts/format.d.ts +12 -0
  53. package/es/charts/format.js +34 -0
  54. package/es/charts/index.d.ts +5 -0
  55. package/es/charts/index.js +5 -0
  56. package/es/charts/storage.d.ts +5 -0
  57. package/es/charts/storage.js +35 -0
  58. package/es/chrome/assistant.d.ts +75 -0
  59. package/es/chrome/assistant.js +116 -0
  60. package/es/components/ColorCard/index.css +8 -8
  61. package/es/index.css +1 -1
  62. package/es/index.d.ts +1 -0
  63. package/es/index.js +1 -0
  64. package/es/vehicles/VehicleInfoDialog/OutOfServiceForm.d.ts +4 -0
  65. package/es/vehicles/VehicleInfoDialog/OutOfServiceForm.js +51 -0
  66. package/es/vehicles/VehicleInfoDialog/data/dsDeliveryTickets.d.ts +99 -0
  67. package/es/vehicles/VehicleInfoDialog/data/dsDeliveryTickets.js +499 -0
  68. package/es/vehicles/VehicleInfoDialog/data/dsDriverStatus.d.ts +13 -0
  69. package/es/vehicles/VehicleInfoDialog/data/dsDriverStatus.js +69 -0
  70. package/es/vehicles/VehicleInfoDialog/data/dsVehicleInfo.d.ts +33 -0
  71. package/es/vehicles/VehicleInfoDialog/data/dsVehicleInfo.js +164 -0
  72. package/es/vehicles/VehicleInfoDialog/data/dsVehicleInfoLog.d.ts +15 -0
  73. package/es/vehicles/VehicleInfoDialog/data/dsVehicleInfoLog.js +80 -0
  74. package/es/vehicles/VehicleInfoDialog/formatting.d.ts +13 -0
  75. package/es/vehicles/VehicleInfoDialog/formatting.js +48 -0
  76. package/es/vehicles/VehicleInfoDialog/index.css +1 -0
  77. package/es/vehicles/VehicleInfoDialog/index.d.ts +11 -0
  78. package/es/vehicles/VehicleInfoDialog/index.js +46 -0
  79. package/es/vehicles/VehicleInfoDialog/tabs.d.ts +6 -0
  80. package/es/vehicles/VehicleInfoDialog/tabs.js +85 -0
  81. package/es/vehicles/index.d.ts +1 -0
  82. package/es/vehicles/index.js +1 -0
  83. package/package.json +17 -8
  84. package/src/charts/CallOffDeliveries/TicketScanDialog.tsx +79 -0
  85. package/src/charts/CallOffDeliveries/chartOptions.ts +160 -0
  86. package/src/charts/CallOffDeliveries/data.ts +67 -0
  87. package/src/charts/CallOffDeliveries/index.scss +51 -0
  88. package/src/charts/CallOffDeliveries/index.tsx +217 -0
  89. package/src/charts/CallOffDeliveries/model.test.ts +90 -0
  90. package/src/charts/CallOffDeliveries/model.ts +87 -0
  91. package/src/charts/ProductionPlan/PlanValueDialog.tsx +107 -0
  92. package/src/charts/ProductionPlan/chartOptions.ts +406 -0
  93. package/src/charts/ProductionPlan/data.ts +170 -0
  94. package/src/charts/ProductionPlan/index.scss +148 -0
  95. package/src/charts/ProductionPlan/index.tsx +432 -0
  96. package/src/charts/ProductionPlan/model.test.ts +132 -0
  97. package/src/charts/ProductionPlan/model.ts +102 -0
  98. package/src/charts/Pumping/chartOptions.ts +224 -0
  99. package/src/charts/Pumping/data.ts +91 -0
  100. package/src/charts/Pumping/index.scss +42 -0
  101. package/src/charts/Pumping/index.tsx +222 -0
  102. package/src/charts/Pumping/model.test.ts +122 -0
  103. package/src/charts/Pumping/model.ts +104 -0
  104. package/src/charts/UnboundPreview/chartOptions.ts +137 -0
  105. package/src/charts/UnboundPreview/index.scss +17 -0
  106. package/src/charts/UnboundPreview/index.tsx +100 -0
  107. package/src/charts/UnboundPreview/model.test.ts +129 -0
  108. package/src/charts/UnboundPreview/model.ts +110 -0
  109. package/src/charts/VehicleDeliveries/chartOptions.ts +311 -0
  110. package/src/charts/VehicleDeliveries/data.ts +98 -0
  111. package/src/charts/VehicleDeliveries/deliveryXRange.ts +80 -0
  112. package/src/charts/VehicleDeliveries/index.scss +43 -0
  113. package/src/charts/VehicleDeliveries/index.tsx +262 -0
  114. package/src/charts/VehicleDeliveries/model.test.ts +117 -0
  115. package/src/charts/VehicleDeliveries/model.ts +139 -0
  116. package/src/charts/format.ts +38 -0
  117. package/src/charts/index.ts +20 -0
  118. package/src/charts/storage.ts +36 -0
  119. package/src/chrome/assistant.test.tsx +106 -0
  120. package/src/chrome/assistant.ts +178 -0
  121. package/src/components/ColorCard/index.css +8 -8
  122. package/src/index.ts +9 -0
  123. package/src/vehicles/VehicleInfoDialog/OutOfServiceForm.tsx +127 -0
  124. package/src/vehicles/VehicleInfoDialog/data/dsDeliveryTickets.ts +600 -0
  125. package/src/vehicles/VehicleInfoDialog/data/dsDriverStatus.ts +84 -0
  126. package/src/vehicles/VehicleInfoDialog/data/dsVehicleInfo.ts +198 -0
  127. package/src/vehicles/VehicleInfoDialog/data/dsVehicleInfoLog.ts +97 -0
  128. package/src/vehicles/VehicleInfoDialog/formatting.test.ts +57 -0
  129. package/src/vehicles/VehicleInfoDialog/formatting.ts +58 -0
  130. package/src/vehicles/VehicleInfoDialog/index.scss +131 -0
  131. package/src/vehicles/VehicleInfoDialog/index.tsx +106 -0
  132. package/src/vehicles/VehicleInfoDialog/tabs.tsx +188 -0
  133. package/src/vehicles/index.ts +1 -0
@@ -0,0 +1,148 @@
1
+ .ObProductionPlan {
2
+ padding-block: 0.5rem;
3
+ }
4
+
5
+ .ObProductionPlan-options {
6
+ display: flex;
7
+ gap: 0.5rem 1rem;
8
+ justify-content: flex-end;
9
+ position: sticky;
10
+ top: 0;
11
+ z-index: 1;
12
+ padding: 5px;
13
+ padding-inline-start: 2.5rem;
14
+ background-color: var(--ds-color-neutral-background-default);
15
+ }
16
+
17
+ // Out of the flow, so the spinner never changes the height of the bar while it loads.
18
+ .ObProductionPlan-status {
19
+ position: absolute;
20
+ inset-block: 0;
21
+ inset-inline-start: 5px;
22
+ display: flex;
23
+ align-items: center;
24
+ pointer-events: none;
25
+ }
26
+
27
+ .ObProductionPlan-legend {
28
+ display: flex;
29
+ flex-wrap: wrap;
30
+ align-items: center;
31
+ gap: 0.25rem 1rem;
32
+ margin: 0;
33
+ }
34
+
35
+ .ObProductionPlan-legend-item {
36
+ display: inline-flex;
37
+ align-items: center;
38
+ gap: 0.375rem;
39
+ }
40
+
41
+ .ObProductionPlan-legend-icon {
42
+ display: inline-block;
43
+ width: 20px;
44
+ height: 0.875em;
45
+ border-radius: 5px;
46
+ opacity: 0.8;
47
+ }
48
+
49
+ .ObProductionPlan-unplanned {
50
+ text-align: center;
51
+ }
52
+
53
+ .ObProductionPlan-header {
54
+ display: grid;
55
+ grid-template-columns: 1fr auto 1fr;
56
+ align-items: center;
57
+ gap: 0.5rem;
58
+ padding-inline-start: 0.6rem;
59
+ padding-block-start: 0.5rem;
60
+ color: var(--ds-color-neutral-text-subtle);
61
+ }
62
+
63
+ .ObProductionPlan-plantname {
64
+ text-align: center;
65
+ }
66
+
67
+ .ObProductionPlan-actions {
68
+ display: flex;
69
+ flex-wrap: wrap;
70
+ justify-content: flex-end;
71
+ gap: 0.5rem;
72
+ }
73
+
74
+ // Highcharts sets `light dark` here, which follows the OS instead of the colour scheme of the site.
75
+ .ObProductionPlan-chart .highcharts-container {
76
+ color-scheme: inherit;
77
+ }
78
+
79
+ // The columns left of the planned loads, and their headers in the subtitle.
80
+ .ObProductionPlan-row {
81
+ display: flex;
82
+ align-items: baseline;
83
+ width: 440px;
84
+ color: var(--ds-color-neutral-text-default);
85
+ white-space: nowrap;
86
+
87
+ > span {
88
+ flex: none;
89
+ text-align: end;
90
+ }
91
+
92
+ a {
93
+ color: var(--ds-color-info-text-subtle);
94
+ }
95
+
96
+ .ObProductionPlan-title {
97
+ width: 185px;
98
+ overflow: hidden;
99
+ text-overflow: ellipsis;
100
+ text-align: start;
101
+ }
102
+
103
+ .ObProductionPlan-qty,
104
+ .ObProductionPlan-arrival {
105
+ width: 60px;
106
+ }
107
+
108
+ .ObProductionPlan-minutes {
109
+ width: 45px;
110
+ }
111
+ }
112
+
113
+ .ObProductionPlan-icon {
114
+ display: inline-block;
115
+ width: 1em;
116
+ height: 1em;
117
+ vertical-align: -0.125em;
118
+ background-color: currentColor;
119
+ mask: center / contain no-repeat;
120
+ }
121
+
122
+ .ObProductionPlan-icon-refresh {
123
+ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M20 12a8 8 0 1 1-2.34-5.66'/%3E%3Cpath d='M20 4v4.5h-4.5'/%3E%3C/svg%3E");
124
+ }
125
+
126
+ .ObProductionPlan-icon-warning {
127
+ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.5 1 21.5h22L12 2.5Zm-1 7h2v6h-2v-6Zm0 8h2v2h-2v-2Z'/%3E%3C/svg%3E");
128
+ }
129
+
130
+ .ObProductionPlan-headers {
131
+ border-block-end: 1px solid var(--ds-color-neutral-border-default);
132
+ font-size: 0.75em;
133
+ color: var(--ds-color-neutral-text-subtle);
134
+ }
135
+
136
+ .ObProductionPlan-warning {
137
+ color: var(--ds-color-warning-base-default);
138
+ }
139
+
140
+ .ObProductionPlan-dialog-actions {
141
+ display: flex;
142
+ gap: 0.5rem;
143
+ }
144
+
145
+ .ObProductionPlan hr {
146
+ border: 0;
147
+ border-block-start: 1px solid var(--ds-color-neutral-border-subtle);
148
+ }
@@ -0,0 +1,432 @@
1
+ import { Alert, Button, Heading, Paragraph, Spinner } from "@digdir/designsystemet-react";
2
+ import { ArrowsCirclepathIcon, TruckIcon } from "@navikt/aksel-icons";
3
+ import Highcharts from "highcharts";
4
+ // In this order: draggable-points only sets up the drag behaviour of series types that already exist.
5
+ import "highcharts/modules/xrange";
6
+ import "highcharts/modules/draggable-points";
7
+ import { useCallback, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
8
+ import type { Ref } from "react";
9
+
10
+ import { getLocalizedString, localize } from "../../i18n.js";
11
+ import { formatTime } from "../format.js";
12
+ import {
13
+ type ChartSettings,
14
+ getDeliveriesChartOptions,
15
+ getVehiclesChartOptions,
16
+ getVolumeChartOptions,
17
+ METHOD_COLORS,
18
+ type MovePlannedDelivery,
19
+ } from "./chartOptions.js";
20
+ import {
21
+ type PlannedDeliveryRecord,
22
+ procCreateAutoPlan,
23
+ procCreateAutoVehicles,
24
+ procMovePlannedDelivery,
25
+ procProdPlan,
26
+ procProdVolume,
27
+ procUpdatePlannedDeliveryParam,
28
+ type ProdPlanResult,
29
+ type ProdVolumeResult,
30
+ } from "./data.js";
31
+ import { getChartHeight, getPlanData, type PlanParameter, type PlantPlan } from "./model.js";
32
+ import { ConfirmResetDialog, PlanValueDialog } from "./PlanValueDialog.js";
33
+
34
+ import "./index.scss";
35
+ import { writeStored, readOffset } from "../storage.js";
36
+
37
+ export type ProductionPlanHandle = {
38
+ /** Fetches the data again, keeping the zoom. */
39
+ reload: () => void;
40
+ /** Sets the hours shown around the day. An undefined offset forgets the stored one. */
41
+ setOffsets: (min?: number, max?: number) => void;
42
+ };
43
+
44
+ export type ProductionPlanProps = {
45
+ plants: number[];
46
+ /** Midnight UTC of the day to show. */
47
+ date: Date;
48
+ /** Shows the plan without the buttons, links and dragging that change it. */
49
+ readOnly?: boolean;
50
+ /** Prefix of the settings in localStorage. */
51
+ storageKey: string;
52
+ /** Called after the charts have drawn, when the height of the page may have changed. */
53
+ onRendered?: () => void;
54
+ /** Called after the planned loads have been spread on the vehicles again, which other charts show too. */
55
+ onVehiclesReassigned?: () => void;
56
+ ref?: Ref<ProductionPlanHandle>;
57
+ };
58
+
59
+ type PlanResult = { volume: ProdVolumeResult; plan: ProdPlanResult };
60
+ type Editing = { parameter: PlanParameter; row: PlannedDeliveryRecord; value: string };
61
+
62
+ function getErrorMessage(reason: unknown) {
63
+ return reason instanceof Error ? reason.message : String(reason);
64
+ }
65
+
66
+ function formatDay(date: Date) {
67
+ let text = new Intl.DateTimeFormat(globalThis.af?.userSession?.culture, {
68
+ weekday: "long",
69
+ day: "2-digit",
70
+ month: "2-digit",
71
+ year: "numeric",
72
+ timeZone: "UTC",
73
+ }).format(date);
74
+ return text.charAt(0).toUpperCase() + text.slice(1);
75
+ }
76
+
77
+ function PlanChart({
78
+ options,
79
+ minHeight,
80
+ onClick,
81
+ onRendered,
82
+ }: {
83
+ options: Highcharts.Options;
84
+ minHeight: number;
85
+ onClick?: (event: React.MouseEvent<HTMLDivElement>) => void;
86
+ onRendered?: () => void;
87
+ }) {
88
+ let container = useRef<HTMLDivElement>(null);
89
+ let zoom = useRef<{ min: number; max: number } | null>(null);
90
+
91
+ useLayoutEffect(() => {
92
+ let chart = new Highcharts.Chart(container.current as HTMLElement, options);
93
+
94
+ if (zoom.current) {
95
+ chart.xAxis[0].setExtremes(zoom.current.min, zoom.current.max, true, false);
96
+ chart.showResetZoom();
97
+ }
98
+
99
+ onRendered?.();
100
+
101
+ return () => {
102
+ let { userMin, userMax } = chart.xAxis[0].getExtremes();
103
+ zoom.current = userMin !== undefined && userMax !== undefined ? { min: userMin, max: userMax } : null;
104
+ chart.destroy();
105
+ };
106
+ }, [options, onRendered]);
107
+
108
+ // Reserved up front, so the page keeps its scroll position while a chart is replaced.
109
+ // oxlint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- the clicks come from links in the chart
110
+ return <div ref={container} className="ObProductionPlan-chart" style={{ minHeight }} onClick={onClick} />;
111
+ }
112
+
113
+ function PlantSection({
114
+ plan,
115
+ settings,
116
+ onRendered,
117
+ onLabelClick,
118
+ onResetPlan,
119
+ onSpreadVehicles,
120
+ }: {
121
+ plan: PlantPlan;
122
+ settings: ChartSettings;
123
+ onRendered?: () => void;
124
+ onLabelClick: (plan: PlantPlan, event: React.MouseEvent<HTMLDivElement>) => void;
125
+ onResetPlan: (plantId: number) => void;
126
+ onSpreadVehicles: (plantId: number) => void;
127
+ }) {
128
+ let volumeOptions = useMemo(() => getVolumeChartOptions(plan, settings), [plan, settings]);
129
+ let vehiclesOptions = useMemo(() => getVehiclesChartOptions(plan, settings), [plan, settings]);
130
+ let deliveriesOptions = useMemo(() => getDeliveriesChartOptions(plan, settings), [plan, settings]);
131
+ let plantId = plan.plant.PlantID;
132
+
133
+ return (
134
+ <section className="ObProductionPlan-plant">
135
+ <div className="ObProductionPlan-header">
136
+ <Heading level={2} data-size="2xs">
137
+ {formatDay(settings.date)}
138
+ </Heading>
139
+ <Heading level={2} data-size="2xs" className="ObProductionPlan-plantname">
140
+ {plantId} - {plan.plant.Description}
141
+ </Heading>
142
+ <div className="ObProductionPlan-actions">
143
+ {!settings.readOnly && plan.deliveries.length > 0 && (
144
+ <>
145
+ <Button
146
+ data-size="sm"
147
+ variant="secondary"
148
+ title={getLocalizedString("Plan all the call-offs again (manual changes are discarded)")}
149
+ onClick={() => onResetPlan(plantId)}
150
+ >
151
+ <ArrowsCirclepathIcon aria-hidden />
152
+ {getLocalizedString("Update/reset all")}
153
+ </Button>
154
+ <Button
155
+ data-size="sm"
156
+ variant="secondary"
157
+ title={getLocalizedString("Spreads all the planned loads on as few vehicles as possible")}
158
+ onClick={() => onSpreadVehicles(plantId)}
159
+ >
160
+ <TruckIcon aria-hidden />
161
+ {getLocalizedString("Update spread on vehicles")}
162
+ </Button>
163
+ </>
164
+ )}
165
+ </div>
166
+ </div>
167
+ {plan.volume.length > 0 && <PlanChart options={volumeOptions} minHeight={150} onRendered={onRendered} />}
168
+ {plan.vehicleCounts.length > 0 && <PlanChart options={vehiclesOptions} minHeight={150} onRendered={onRendered} />}
169
+ {plan.deliveries.length > 0 && (
170
+ <PlanChart
171
+ options={deliveriesOptions}
172
+ minHeight={getChartHeight(plan.deliveries.length)}
173
+ onRendered={onRendered}
174
+ onClick={(event) => onLabelClick(plan, event)}
175
+ />
176
+ )}
177
+ <hr />
178
+ </section>
179
+ );
180
+ }
181
+
182
+ function Legend() {
183
+ let items = [
184
+ ["pump", getLocalizedString("Pump")],
185
+ ["telescopic", getLocalizedString("Telescopic chute")],
186
+ ["conveyor", getLocalizedString("Conveyor belt")],
187
+ ["none", getLocalizedString("No equipment")],
188
+ ] as const;
189
+
190
+ return (
191
+ <Paragraph data-size="xs" className="ObProductionPlan-legend">
192
+ <span>{getLocalizedString("Delivery method")}:</span>
193
+ {items.map(([method, label]) => (
194
+ <span key={method} className="ObProductionPlan-legend-item">
195
+ <span className="ObProductionPlan-legend-icon" style={{ backgroundColor: METHOD_COLORS[method] }} />
196
+ {label}
197
+ </span>
198
+ ))}
199
+ </Paragraph>
200
+ );
201
+ }
202
+
203
+ /**
204
+ * The production plan of a day: the planned production volume, the number of vehicles out and the
205
+ * planned loads of every call-off, one set of charts per plant. Unless `readOnly` is set, loads can be
206
+ * dragged to another time, and the arrival, interval, driving and unloading times changed from the
207
+ * columns left of the loads.
208
+ */
209
+ export function ProductionPlan({
210
+ plants,
211
+ date,
212
+ readOnly = false,
213
+ storageKey,
214
+ onRendered,
215
+ onVehiclesReassigned,
216
+ ref,
217
+ }: ProductionPlanProps) {
218
+ let [minOffset, setMinOffset] = useState(() => readOffset(`${storageKey}_minoffset`, -2));
219
+ let [maxOffset, setMaxOffset] = useState(() => readOffset(`${storageKey}_maxoffset`, 28));
220
+ let [result, setResult] = useState<PlanResult | null>(null);
221
+ let [error, setError] = useState<string | null>(null);
222
+ let [reloadCount, setReloadCount] = useState(0);
223
+ let [loadedKey, setLoadedKey] = useState<string | null>(null);
224
+ let [pending, setPending] = useState(0);
225
+ let [editing, setEditing] = useState<Editing | null>(null);
226
+ let [resetPlant, setResetPlant] = useState<number | null>(null);
227
+
228
+ let reload = useCallback(() => setReloadCount((count) => count + 1), []);
229
+ let plantsKey = plants.join(",");
230
+ let dateKey = date.getTime();
231
+ let requestKey = `${plantsKey}|${dateKey}|${reloadCount}`;
232
+
233
+ useEffect(() => {
234
+ if (!plantsKey) return;
235
+
236
+ let controller = new AbortController();
237
+ // The windows app sends the plants and the date as separate messages.
238
+ let timeout = setTimeout(async () => {
239
+ let parameters = {
240
+ PlantIDs: plantsKey.split(",").map((plant) => [Number(plant)] as [number]),
241
+ Date: new Date(dateKey),
242
+ };
243
+
244
+ try {
245
+ let [volume, plan] = await Promise.all([procProdVolume.execute(parameters), procProdPlan.execute(parameters)]);
246
+ if (!controller.signal.aborted) {
247
+ setResult({ volume, plan });
248
+ setError(null);
249
+ }
250
+ } catch (reason) {
251
+ if (!controller.signal.aborted) setError(getErrorMessage(reason));
252
+ }
253
+ if (!controller.signal.aborted) setLoadedKey(requestKey);
254
+ }, 100);
255
+
256
+ return () => {
257
+ controller.abort();
258
+ clearTimeout(timeout);
259
+ };
260
+ }, [plantsKey, dateKey, requestKey]);
261
+
262
+ useImperativeHandle(
263
+ ref,
264
+ () => ({
265
+ reload,
266
+ setOffsets(min, max) {
267
+ writeStored(`${storageKey}_minoffset`, min === undefined ? null : String(min));
268
+ writeStored(`${storageKey}_maxoffset`, max === undefined ? null : String(max));
269
+ if (min !== undefined) setMinOffset(min);
270
+ if (max !== undefined) setMaxOffset(max);
271
+ },
272
+ }),
273
+ [reload, storageKey],
274
+ );
275
+
276
+ /** Runs a change to the plan and fetches the plan again, after a refused change as well. */
277
+ let runAction = useCallback(
278
+ async (action: () => Promise<unknown>) => {
279
+ setPending((count) => count + 1);
280
+ try {
281
+ await action();
282
+ setError(null);
283
+ } catch (reason) {
284
+ setError(getErrorMessage(reason));
285
+ }
286
+ setPending((count) => count - 1);
287
+ reload();
288
+ },
289
+ [reload],
290
+ );
291
+
292
+ let onMove = useCallback<MovePlannedDelivery>(
293
+ (delivery, prodTime) =>
294
+ void runAction(() => procMovePlannedDelivery.execute({ PrimKey: delivery.PrimKey, ProdDateTime: prodTime })),
295
+ [runAction],
296
+ );
297
+
298
+ let data = useMemo(
299
+ () => (result ? getPlanData(result.volume, result.plan, plantsKey.split(",").map(Number)) : null),
300
+ [result, plantsKey],
301
+ );
302
+ let settings = useMemo<ChartSettings>(
303
+ () => ({ date: new Date(dateKey), minOffset, maxOffset, readOnly, onMove }),
304
+ [dateKey, minOffset, maxOffset, readOnly, onMove],
305
+ );
306
+
307
+ let onLabelClick = useCallback(
308
+ (plan: PlantPlan, event: React.MouseEvent<HTMLDivElement>) => {
309
+ let link = (event.target as Element).closest<HTMLAnchorElement>('a[href^="#plan-"]');
310
+ if (!link) return;
311
+
312
+ event.preventDefault();
313
+ let match = /^#plan-(reset|arrival|interval|drivetime|unloadtime)-(.+)$/.exec(link.hash);
314
+ if (!match) return;
315
+
316
+ let [, action, id] = match;
317
+ if (action === "reset") {
318
+ void runAction(() => procCreateAutoPlan.execute({ CallOffID: Number(id), DeleteExisting: true }));
319
+ return;
320
+ }
321
+
322
+ let row = plan.deliveries.find((delivery) => delivery.PrimKey === id);
323
+ if (!row) return;
324
+
325
+ let value = {
326
+ arrival: formatTime(row.Arrival),
327
+ interval: String(row.DeliveryInterval ?? ""),
328
+ drivetime: String(row.DriveToSiteTime ?? ""),
329
+ unloadtime: String(row.UnloadTime ?? ""),
330
+ }[action as PlanParameter];
331
+ setEditing({ parameter: action as PlanParameter, row, value });
332
+ },
333
+ [runAction],
334
+ );
335
+
336
+ let saveValue = (value: number | string) => {
337
+ if (!editing) return;
338
+
339
+ let { parameter, row } = editing;
340
+ setEditing(null);
341
+ void runAction(() =>
342
+ procUpdatePlannedDeliveryParam.execute({
343
+ PrimKey: row.PrimKey,
344
+ DriveTime: parameter === "drivetime" ? Number(value) : null,
345
+ Interval: parameter === "interval" ? Number(value) : null,
346
+ UnloadTime: parameter === "unloadtime" ? Number(value) : null,
347
+ Arrival: parameter === "arrival" ? String(value) : null,
348
+ }),
349
+ );
350
+ };
351
+
352
+ let onResetPlan = useCallback((plantId: number) => setResetPlant(plantId), []);
353
+ let onSpreadVehicles = useCallback(
354
+ (plantId: number) =>
355
+ void runAction(async () => {
356
+ await procCreateAutoVehicles.execute({ PlantID: plantId, Date: new Date(dateKey), ResetExisting: true });
357
+ onVehiclesReassigned?.();
358
+ }),
359
+ [runAction, dateKey, onVehiclesReassigned],
360
+ );
361
+
362
+ let generateUnplanned = () => {
363
+ if (!data) return;
364
+
365
+ let plantIds = data.plants.map((plan) => plan.plant.PlantID);
366
+ void runAction(async () => {
367
+ for (let plantId of plantIds) {
368
+ await procCreateAutoPlan.execute({ PlantID: plantId, Date: new Date(dateKey) });
369
+ }
370
+ });
371
+ };
372
+
373
+ if (plants.length === 0) {
374
+ return <Alert data-color="warning">{getLocalizedString("No plant specified")}</Alert>;
375
+ }
376
+
377
+ let loading = loadedKey !== requestKey || pending > 0;
378
+
379
+ return (
380
+ <div className="ObProductionPlan">
381
+ <div className="ObProductionPlan-options">
382
+ <div className="ObProductionPlan-status">
383
+ {loading && <Spinner data-size="sm" aria-label={getLocalizedString("Loading")} />}
384
+ </div>
385
+ <Legend />
386
+ </div>
387
+ {error && <Alert data-color="danger">{error}</Alert>}
388
+ {data && data.unplanned > 0 && (
389
+ <Paragraph className="ObProductionPlan-unplanned">
390
+ {readOnly ? (
391
+ localize`${data.unplanned} call-offs are not planned yet.`
392
+ ) : (
393
+ <Button variant="tertiary" data-size="sm" onClick={generateUnplanned}>
394
+ {localize`${data.unplanned} call-offs are not planned yet. Click here to generate a plan...`}
395
+ </Button>
396
+ )}
397
+ </Paragraph>
398
+ )}
399
+ {data?.plants.map((plan) => (
400
+ <PlantSection
401
+ key={`${dateKey}-${plan.plant.PlantID}`}
402
+ plan={plan}
403
+ settings={settings}
404
+ onRendered={onRendered}
405
+ onLabelClick={onLabelClick}
406
+ onResetPlan={onResetPlan}
407
+ onSpreadVehicles={onSpreadVehicles}
408
+ />
409
+ ))}
410
+ {editing && (
411
+ <PlanValueDialog
412
+ parameter={editing.parameter}
413
+ initialValue={editing.value}
414
+ onCancel={() => setEditing(null)}
415
+ onSubmit={saveValue}
416
+ />
417
+ )}
418
+ {resetPlant !== null && (
419
+ <ConfirmResetDialog
420
+ onCancel={() => setResetPlant(null)}
421
+ onConfirm={() => {
422
+ let plantId = resetPlant;
423
+ setResetPlant(null);
424
+ void runAction(() =>
425
+ procCreateAutoPlan.execute({ PlantID: plantId, Date: new Date(dateKey), DeleteExisting: true }),
426
+ );
427
+ }}
428
+ />
429
+ )}
430
+ </div>
431
+ );
432
+ }
@@ -0,0 +1,132 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import type { PlannedDeliveryRecord, ProdPlanResult, ProdVolumeResult } from "./data.js";
4
+ import { getAxisMax, getDeliveryMethod, getPlanData, isFirstLoad, parsePlanValue } from "./model.js";
5
+
6
+ function delivery(PlantID: number, CallOffID: number, PrimKey: string): PlannedDeliveryRecord {
7
+ return {
8
+ PlantID,
9
+ CallOffID,
10
+ PrimKey,
11
+ DeliveryStartDateTime: new Date(0),
12
+ CustomerName: null,
13
+ BuildSite: null,
14
+ Qty: 10,
15
+ Pump: null,
16
+ Pumping: false,
17
+ Telescopic: false,
18
+ ConveyorBelt: false,
19
+ VehicleID: 1,
20
+ VehicleNo: 1,
21
+ Driver: null,
22
+ LoadQty: 5,
23
+ DeliveryTicketID: null,
24
+ DriveToSiteTime: 20,
25
+ DeliveryInterval: null,
26
+ UnloadTime: 15,
27
+ ProdDateTime: new Date(0),
28
+ ProdFinishDateTime: null,
29
+ Arrival: null,
30
+ UnloadStart: null,
31
+ UnloadComplete: null,
32
+ ReturnTime: new Date(1),
33
+ LoadNo: 1,
34
+ };
35
+ }
36
+
37
+ function results(): { volume: ProdVolumeResult; plan: ProdPlanResult } {
38
+ let cutOff = { CutOff: new Date(0), CutOffStart: new Date(0), CutOffEnd: new Date(0) };
39
+
40
+ return {
41
+ volume: {
42
+ Table: [
43
+ { PlantID: 10, Qty: 5, ...cutOff },
44
+ { PlantID: 20, Qty: 7, ...cutOff },
45
+ ],
46
+ Table1: [
47
+ { PlantID: 10, Description: "Ten" },
48
+ { PlantID: 20, Description: "Twenty" },
49
+ ],
50
+ },
51
+ plan: {
52
+ Table: [{ PlantID: 20, VehicleCount: 2, ...cutOff }],
53
+ Table1: [delivery(10, 1, "a"), delivery(10, 1, "b"), delivery(20, 2, "c")],
54
+ Table2: [{ Unplanned: 3 }],
55
+ Table3: [
56
+ { PlantID: 10, Description: "Ten" },
57
+ { PlantID: 20, Description: "Twenty" },
58
+ ],
59
+ Table4: [
60
+ { PlantID: 10, CallOffID: 1, WarningType: "Qty", WarningText: "Quantity differs" },
61
+ { PlantID: 10, CallOffID: 1, WarningType: "DeliveryTime", WarningText: "Time differs" },
62
+ ],
63
+ },
64
+ };
65
+ }
66
+
67
+ describe("getPlanData", () => {
68
+ it("splits the rows per plant, in the order the plants were selected", () => {
69
+ let { volume, plan } = results();
70
+ let data = getPlanData(volume, plan, [20, 10]);
71
+
72
+ expect(data.unplanned).toBe(3);
73
+ expect(data.plants.map((entry) => entry.plant.PlantID)).toEqual([20, 10]);
74
+ expect(data.plants[0].volume.map((row) => row.Qty)).toEqual([7]);
75
+ expect(data.plants[0].vehicleCounts).toHaveLength(1);
76
+ expect(data.plants[1].vehicleCounts).toHaveLength(0);
77
+ expect(data.plants[1].deliveries.map((row) => row.PrimKey)).toEqual(["a", "b"]);
78
+ });
79
+
80
+ it("joins the warnings of a call-off", () => {
81
+ let { volume, plan } = results();
82
+ let data = getPlanData(volume, plan, [10]);
83
+
84
+ expect(data.plants[0].warnings.get(1)).toBe("Quantity differs\nTime differs");
85
+ });
86
+
87
+ it("leaves out plants the server does not know", () => {
88
+ let { volume, plan } = results();
89
+ expect(getPlanData(volume, plan, [99]).plants).toEqual([]);
90
+ });
91
+ });
92
+
93
+ describe("isFirstLoad", () => {
94
+ it("is true for the first load of each call-off", () => {
95
+ let rows = [delivery(10, 1, "a"), delivery(10, 1, "b"), delivery(10, 2, "c")];
96
+ expect(rows.map((_, index) => isFirstLoad(rows, index))).toEqual([true, false, true]);
97
+ });
98
+ });
99
+
100
+ describe("getDeliveryMethod", () => {
101
+ it("prefers pumping, then the telescopic chute, then the conveyor belt", () => {
102
+ expect(getDeliveryMethod({ Pumping: true, Telescopic: true, ConveyorBelt: true })).toBe("pump");
103
+ expect(getDeliveryMethod({ Pumping: false, Telescopic: true, ConveyorBelt: true })).toBe("telescopic");
104
+ expect(getDeliveryMethod({ Pumping: null, Telescopic: null, ConveyorBelt: true })).toBe("conveyor");
105
+ expect(getDeliveryMethod({ Pumping: null, Telescopic: null, ConveyorBelt: null })).toBe("none");
106
+ });
107
+ });
108
+
109
+ describe("getAxisMax", () => {
110
+ it("rounds up to the step and is never below one step", () => {
111
+ expect(getAxisMax([3, 26], 25)).toBe(50);
112
+ expect(getAxisMax([25], 25)).toBe(25);
113
+ expect(getAxisMax([], 10)).toBe(10);
114
+ });
115
+ });
116
+
117
+ describe("parsePlanValue", () => {
118
+ it("reads a time of day for the arrival", () => {
119
+ expect(parsePlanValue("arrival", "7:30")).toBe("07:30");
120
+ expect(parsePlanValue("arrival", "07.30")).toBe("07:30");
121
+ expect(parsePlanValue("arrival", "0730")).toBe("07:30");
122
+ expect(parsePlanValue("arrival", "24:00")).toBeNull();
123
+ expect(parsePlanValue("arrival", "abc")).toBeNull();
124
+ });
125
+
126
+ it("reads whole minutes for the other values", () => {
127
+ expect(parsePlanValue("interval", " 30 ")).toBe(30);
128
+ expect(parsePlanValue("drivetime", "0")).toBe(0);
129
+ expect(parsePlanValue("unloadtime", "12.5")).toBeNull();
130
+ expect(parsePlanValue("unloadtime", "")).toBeNull();
131
+ });
132
+ });