@jsb188/mday 1.1.4 → 1.1.6

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,777 @@
1
+ import i18n from '@jsb188/app/i18n';
2
+ import { getCalDate } from '@jsb188/app/utils/datetime';
3
+ import { formatCurrency } from '@jsb188/app/utils/number';
4
+ import { getObject } from '@jsb188/app/utils/object';
5
+ import { textWithBrackets } from '@jsb188/app/utils/string';
6
+ import { getTimeZoneCode } from '@jsb188/app/utils/timeZone';
7
+ import { ARABLE_ACTIVITIES_GROUPED, FARMERS_MARKET_ACTIVITIES_GROUPED, LIVESTOCK_ACTIVITIES_GROUPED } from '../constants/log';
8
+ import type { LogArableMetadataGQL, LogEntryGQL, LogFarmersMarketMetadataGQL, LogLivestockMetadataGQL, LogTypeEnum } from '../types/log.d';
9
+ import { getLogCategoryColor, getLogTypeFromActivity } from '../utils/log';
10
+
11
+ /**
12
+ * Get editable values based on activity & type (which is also used to display UI)
13
+ */
14
+
15
+ export const LOG_TYPENAME_TO_DETAILS_INPUT_NAME = {
16
+ LogArable: 'arableDetails',
17
+ LogFarmersMarket: 'farmersMarketDetails',
18
+ LogLivestock: 'livestockDetails',
19
+ };
20
+
21
+ /**
22
+ * Convert GQL data to GQL mutation input format
23
+ */
24
+
25
+ export function makeFormValuesFromData(logEntry: LogEntryGQL) {
26
+ const formValues = {
27
+ logEntryId: logEntry.id,
28
+ organizationId: logEntry.organizationId,
29
+ accountId: logEntry.accountId,
30
+ date: logEntry.date,
31
+ } as Record<string, any>;
32
+
33
+ switch (logEntry.details?.__typename) {
34
+ case 'LogArable': {
35
+ const details = logEntry.details as LogArableMetadataGQL;
36
+ formValues.arableDetails = {
37
+ activity: details.activity,
38
+ quantity: details.quantity,
39
+ item: details.item,
40
+ unit: details.unit,
41
+ concentration: details.concentration,
42
+ concentrationUnit: details.concentrationUnit,
43
+ location: details.location,
44
+ fieldLocation: details.fieldLocation,
45
+ referenceNumber: details.referenceNumber,
46
+ otherParty: details.otherParty,
47
+ values: details.values,
48
+ notes: details.notes,
49
+ };
50
+ } break;
51
+ case 'LogFarmersMarket': {
52
+ const details = logEntry.details as LogFarmersMarketMetadataGQL;
53
+ formValues.farmersMarketDetails = {
54
+ activity: details.activity,
55
+ referenceNumber: details.referenceNumber,
56
+ voided: details.voided,
57
+ values: details.values,
58
+ notes: details.notes,
59
+ };
60
+ } break;
61
+ case 'LogLivestock': {
62
+ const details = logEntry.details as LogLivestockMetadataGQL;
63
+ formValues.livestockDetails = {
64
+ activity: details.activity,
65
+ livestock: details.livestock,
66
+ livestockIdentifiers: details.livestockIdentifiers,
67
+ livestockGroup: details.livestockGroup,
68
+ referenceNumber: details.referenceNumber,
69
+ otherParty: details.otherParty,
70
+ values: details.values,
71
+ item: details.item,
72
+ quantity: details.quantity,
73
+ unit: details.unit,
74
+ location: details.location,
75
+ fieldLocation: details.fieldLocation,
76
+ price: details.price,
77
+ tax: details.tax,
78
+ notes: details.notes,
79
+ };
80
+ } break;
81
+ default:
82
+ console.log(`${logEntry.details?.__typename} is not done yet.`);
83
+ }
84
+
85
+ // input LogArableInput {
86
+ // activity: LogArableActivity
87
+ // item: String
88
+ // quantity: Float
89
+ // unit: String
90
+ // concentration: Float
91
+ // concentrationUnit: String
92
+ // price: Float
93
+ // notes: String
94
+ // }
95
+
96
+ // input LogFarmersMarketInput {
97
+ // activity: LogFarmersMarketActivity
98
+ // childOrgId: GenericID
99
+ // voided: Boolean
100
+ // values: [LabelAndValueInput]
101
+ // notes: String
102
+ // }
103
+
104
+ // input LogLivestockInput {
105
+ // activity: LogLivestockType
106
+ // livestock: String
107
+ // livestockIdentifiers: [String]
108
+ // item: String
109
+ // quantity: Float
110
+ // unit: String
111
+ // price: Float
112
+ // notes: String
113
+ // }
114
+
115
+
116
+ return formValues;
117
+ }
118
+
119
+ /**
120
+ * Convert HTML input values to GQL mutation input types
121
+ */
122
+
123
+ export function formatFormValuesForMutation(formValues: Record<string, any>) {
124
+ for (const key in formValues) {
125
+ if (formValues[key] && typeof formValues[key] === 'object') {
126
+
127
+ for (const subKey in formValues[key]) {
128
+ if (['quantity','price','tax','concentration'].includes(subKey)) {
129
+ formValues[key][subKey] = parseFloat(formValues[key][subKey]);
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ return formValues;
136
+ }
137
+
138
+ /**
139
+ * Arable log metadata schema
140
+ */
141
+
142
+ export type ValidMetadataFieldName =
143
+ | 'activity'
144
+
145
+ // ARABLE
146
+ | 'water_testing'
147
+ | 'concentration_unit'
148
+ | 'water_quantity'
149
+ | 'quantity'
150
+ | 'water_unit'
151
+ | 'unit'
152
+ | 'price'
153
+ | 'crop'
154
+ | 'purchased_item'
155
+ | 'fieldLocation'
156
+ | 'location_water'
157
+ | 'location_arable'
158
+
159
+ // FARMERS_MARKET
160
+ | 'receiptNumber'
161
+ // | 'childOrgId' // Not editable
162
+ | 'void'
163
+ | 'marketCredits'
164
+
165
+ // LIVESTOCK
166
+ | 'livestock'
167
+ | 'livestockIdentifiers'
168
+ | 'livestockGroup'
169
+ | 'otherParty'
170
+ | 'invoiceNumber'
171
+ | 'invoiceItems'
172
+ | 'tax'
173
+ | 'item_used'
174
+ | 'location_livestock'
175
+ | null; // Need this for eslint satisfcation, but null will never happen (because of a filter())
176
+
177
+ function makeMetadataSchema(
178
+ namespace: string,
179
+ formValues: Record<string, any>,
180
+ metadataParams: MakeMetadataSchemaParams,
181
+ basePopOverProps: Record<string, any>,
182
+ schemaFields: ValidMetadataFieldName[]
183
+ ) {
184
+ const { scrollAreaDOMId, formId, timeZone, focusedName, isCreateNew } = metadataParams;
185
+ const schemaItems = schemaFields.map((field: ValidMetadataFieldName) => {
186
+ switch (field) {
187
+ case 'activity':
188
+ return {
189
+ __type: 'input_click',
190
+ forceClickId: `input_click_${namespace}.activity`,
191
+ label: i18n.t('form.activity'),
192
+ item: {
193
+ // editable: false,
194
+ locked: () => true, // Conditional lock is possible
195
+ focused: focusedName === (formId + '_activity'),
196
+ name: `${namespace}.activity`,
197
+ placeholder: isCreateNew ? i18n.t(`form.activity_ph`) : '',
198
+ getter: (value: string) => value ? i18n.t(`log.activity.${value}`) : '',
199
+ },
200
+ };
201
+ case 'water_testing':
202
+ return {
203
+ __type: 'input',
204
+ label: i18n.t('form.concentration'),
205
+ item: {
206
+ name: `${namespace}.concentration`,
207
+ type: 'number',
208
+ step: 0.1,
209
+ min: 0.1,
210
+ max: 99999999.99, // database max is numeric(10, 2)
211
+ placeholder: isCreateNew ? i18n.t('form.concentration_ph') : '',
212
+ }
213
+ };
214
+ case 'concentration_unit':
215
+ return {
216
+ __type: 'input',
217
+ label: i18n.t('form.concentration_unit'),
218
+ item: {
219
+ name: `${namespace}.concentrationUnit`,
220
+ maxLength: 40,
221
+ placeholder: isCreateNew ? i18n.t('form.concentration_unit_ph') : '',
222
+ },
223
+ };
224
+ case 'water_quantity':
225
+ return {
226
+ __type: 'input',
227
+ label: i18n.t('form.water_quantity'),
228
+ item: {
229
+ name: `${namespace}.quantity`,
230
+ type: 'number',
231
+ step: 0.1,
232
+ min: 0.1,
233
+ max: 99999999.99, // database max is numeric(10, 2)
234
+ placeholder: isCreateNew ? i18n.t('form.quantity_ph') : '',
235
+ }
236
+ };
237
+ case 'quantity':
238
+ return {
239
+ __type: 'input',
240
+ label: i18n.t('form.quantity'),
241
+ item: {
242
+ name: `${namespace}.quantity`,
243
+ type: 'number',
244
+ step: 0.1,
245
+ min: 0.1,
246
+ max: 99999999.99, // database max is numeric(10, 2)
247
+ placeholder: isCreateNew ? i18n.t('form.quantity_ph') : '',
248
+ }
249
+ };
250
+ case 'water_unit':
251
+ return {
252
+ __type: 'input',
253
+ label: i18n.t('form.water_unit'),
254
+ item: {
255
+ name: `${namespace}.unit`,
256
+ maxLength: 40,
257
+ placeholder: isCreateNew ? i18n.t('form.water_unit_ph') : '',
258
+ }
259
+ };
260
+ case 'unit':
261
+ return {
262
+ __type: 'input',
263
+ label: i18n.t('form.unit'),
264
+ item: {
265
+ name: `${namespace}.unit`,
266
+ maxLength: 40,
267
+ placeholder: isCreateNew ? i18n.t('log.unit_arable_ph') : '',
268
+ }
269
+ };
270
+ case 'location_arable':
271
+ return {
272
+ __type: 'input',
273
+ label: i18n.t('form.location'),
274
+ item: {
275
+ name: `${namespace}.location`,
276
+ maxLength: 40,
277
+ placeholder: isCreateNew ? i18n.t('log.location_arable_ph') : '',
278
+ }
279
+ };
280
+ case 'location_water':
281
+ return {
282
+ __type: 'input',
283
+ label: i18n.t('form.location'),
284
+ item: {
285
+ name: `${namespace}.location`,
286
+ maxLength: 40,
287
+ placeholder: isCreateNew ? i18n.t('log.location_water_ph') : '',
288
+ }
289
+ };
290
+ case 'location_livestock':
291
+ return {
292
+ __type: 'input',
293
+ label: i18n.t('form.location'),
294
+ item: {
295
+ name: `${namespace}.location`,
296
+ maxLength: 100,
297
+ placeholder: isCreateNew ? i18n.t('log.location_livestock_ph') : '',
298
+ }
299
+ };
300
+ case 'fieldLocation':
301
+ return {
302
+ __type: 'input',
303
+ label: i18n.t('log.fieldLocation'),
304
+ item: {
305
+ name: `${namespace}.fieldLocation`,
306
+ maxLength: 100,
307
+ placeholder: isCreateNew ? i18n.t('log.fieldLocation_ph') : '',
308
+ }
309
+ };
310
+ case 'price':
311
+ return {
312
+ __type: 'input',
313
+ label: i18n.t('form.price'),
314
+ item: {
315
+ name: `${namespace}.price`,
316
+ type: 'number',
317
+ step: 0.1,
318
+ min: 0.1,
319
+ max: 99999999.99, // database max is numeric(10, 2)
320
+ placeholder: isCreateNew ? i18n.t('form.price_ph') : '',
321
+ },
322
+ };
323
+ case 'tax':
324
+ return {
325
+ __type: 'input',
326
+ label: i18n.t('form.tax'),
327
+ item: {
328
+ name: `${namespace}.tax`,
329
+ type: 'number',
330
+ step: 0.1,
331
+ min: 0.1,
332
+ max: 99999999.99, // database max is numeric(10, 2)
333
+ placeholder: isCreateNew ? i18n.t('form.tax_ph') : '',
334
+ },
335
+ };
336
+ case 'crop':
337
+ return {
338
+ __type: 'input',
339
+ label: i18n.t('form.crop'),
340
+ item: {
341
+ name: `${namespace}.item`,
342
+ placeholder: isCreateNew ? i18n.t('log.crop_ph') : '',
343
+ }
344
+ };
345
+ case 'purchased_item':
346
+ return {
347
+ __type: 'input',
348
+ label: i18n.t('form.purchase_summary'),
349
+ item: {
350
+ name: `${namespace}.item`,
351
+ placeholder: isCreateNew ? i18n.t('log.crop_ph') : '',
352
+ }
353
+ };
354
+ case 'livestock':
355
+ return {
356
+ __type: 'input',
357
+ label: i18n.t('log.livestock'),
358
+ item: {
359
+ name: `${namespace}.livestock`,
360
+ placeholder: isCreateNew ? i18n.t('log.livestock_ph') : '',
361
+ }
362
+ };
363
+ case 'livestockIdentifiers':
364
+ return {
365
+ __type: 'input',
366
+ label: i18n.t('log.id_tags'),
367
+ item: {
368
+ name: `${namespace}.livestockIdentifiers`,
369
+ placeholder: isCreateNew ? i18n.t('log.id_tags_ph') : '',
370
+ getter: (value: string[]) => value ? value.join(',') : '',
371
+ setter: (value: string) => value.split(',')
372
+ }
373
+ };
374
+ case 'livestockGroup':
375
+ return {
376
+ __type: 'input',
377
+ label: i18n.t('log.group'),
378
+ item: {
379
+ name: `${namespace}.livestockGroup`,
380
+ placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
381
+ }
382
+ };
383
+ case 'otherParty':
384
+ return {
385
+ __type: 'input',
386
+ label: i18n.t('form.vendor'),
387
+ item: {
388
+ name: `${namespace}.otherParty`,
389
+ }
390
+ };
391
+ case 'invoiceNumber':
392
+ return {
393
+ __type: 'input',
394
+ label: i18n.t('log.invoice_number'),
395
+ item: {
396
+ // editable: false,
397
+ locked: true,
398
+ name: `${namespace}.referenceNumber`,
399
+ }
400
+ };
401
+ case 'receiptNumber':
402
+ return {
403
+ __type: 'input',
404
+ label: i18n.t('log.receipt_number'),
405
+ item: {
406
+ // editable: false,
407
+ locked: true,
408
+ name: `${namespace}.referenceNumber`,
409
+ // placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
410
+ // getter: (value: string[]) => value ? value.join(',') : '',
411
+ // setter: (value: string) => value.split(',')
412
+ }
413
+ };
414
+
415
+ // Not allowed to edit
416
+ // case 'childOrgId':
417
+ // return {
418
+ // __type: 'input',
419
+ // label: i18n.t('log.receipt_for'),
420
+ // item: {
421
+ // name: `${namespace}.childOrgId`,
422
+ // // placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
423
+ // // getter: (value: string[]) => value ? value.join(',') : '',
424
+ // // setter: (value: string) => value.split(',')
425
+ // }
426
+ // };
427
+ case 'void':
428
+ return {
429
+ __type: 'input_click',
430
+ forceClickId: `input_click_${namespace}.voided`,
431
+ label: i18n.t('form.void'),
432
+ inputClassName: formValues[namespace]?.voided ? 'cl_red' : '',
433
+ item: {
434
+ // locked: () => true,
435
+ focused: focusedName === (formId + '_void'),
436
+ name: `${namespace}.voided`,
437
+ placeholder: isCreateNew ? i18n.t(`form.void_ph`) : '',
438
+ getter: (value: string) => i18n.t(value ? 'log.receipt_is_void' : 'log.receipt_is_not_void'),
439
+ },
440
+ popOverProps: {
441
+ id: formId + '_void',
442
+ ...basePopOverProps,
443
+ iface: {
444
+ name: 'PO_LIST',
445
+ variables: {
446
+ designClassName: 'w_250',
447
+ className: 'max_h_40vh',
448
+ options: [{
449
+ __type: 'LIST_ITEM' as const,
450
+ text: i18n.t('log.receipt_is_not_void'),
451
+ iconName: 'receipt-dollar',
452
+ selected: !formValues[namespace]?.voided,
453
+ name: `${namespace}.voided`,
454
+ value: false,
455
+ }, {
456
+ __type: 'LIST_ITEM' as const,
457
+ text: i18n.t('log.receipt_is_void'),
458
+ iconName: 'receipt-off',
459
+ selected: formValues[namespace]?.voided === true,
460
+ name: `${namespace}.voided`,
461
+ value: true,
462
+ }]
463
+ }
464
+ }
465
+ }
466
+ };
467
+ case 'invoiceItems':
468
+ return {
469
+ __type: 'input_click',
470
+ label: i18n.t('log.invoice_items'),
471
+ forceClickId: `input_click_${namespace}.values`,
472
+ item: {
473
+ // locked: () => true,
474
+ focused: focusedName === (formId + '_values'),
475
+ name: `${namespace}.values`,
476
+ placeholder: isCreateNew ? i18n.t(`log.invoice_items_ph`) : '',
477
+ getter: (labelsAndValues: string[]) => {
478
+ return labelsAndValues?.map((lv: any) => {
479
+ return textWithBrackets(
480
+ `${lv.quantity ? `${lv.quantity} ` : ''}${lv.label}`,
481
+ formatCurrency(lv.value, false),
482
+ );
483
+ }).join(', ');
484
+ },
485
+ },
486
+ popOverProps: {
487
+ id: formId + '_invoiceItems',
488
+ ...basePopOverProps,
489
+ iface: {
490
+ name: 'PO_LABELS_AND_VALUES',
491
+ variables: {
492
+ gridLayoutStyle: '70px 1fr 85px',
493
+ designClassName: 'w_400',
494
+ className: 'max_h_40vh',
495
+ // flipInputOrder: true,
496
+ forceNumericValues: true,
497
+ includeQuantity: true,
498
+ name: `${namespace}.values`,
499
+ labels: [i18n.t('form.quantity'), i18n.t('log.invoice_item'), `$ ${i18n.t('form.amount')}`],
500
+ inputs: formValues[namespace]?.values || [],
501
+ }
502
+ }
503
+ }
504
+ };
505
+ case 'marketCredits':
506
+ return {
507
+ __type: 'input_click',
508
+ label: i18n.t('log.marketCredits'),
509
+ forceClickId: `input_click_${namespace}.values`,
510
+ item: {
511
+ // locked: () => true,
512
+ focused: focusedName === (formId + '_values'),
513
+ name: `${namespace}.values`,
514
+ placeholder: isCreateNew ? i18n.t(`log.marketCredits_ph`) : '',
515
+ getter: (labelsAndValues: string[]) => {
516
+ return labelsAndValues?.map((lv: any) => {
517
+ return `${formatCurrency(lv.value, false)} ${lv.label}`;
518
+ }).join(', ');
519
+ },
520
+ },
521
+ popOverProps: {
522
+ id: formId + '_marketCredits',
523
+ ...basePopOverProps,
524
+ iface: {
525
+ name: 'PO_LABELS_AND_VALUES',
526
+ variables: {
527
+ gridLayoutStyle: '85px 1fr',
528
+ designClassName: 'w_250',
529
+ className: 'max_h_40vh',
530
+ flipInputOrder: true,
531
+ forceNumericValues: true,
532
+ name: `${namespace}.values`,
533
+ labels: [`$ ${i18n.t('form.amount')}`, i18n.t('log.name_of_credit')],
534
+ inputs: formValues[namespace]?.values || [],
535
+ }
536
+ }
537
+ }
538
+ };
539
+ default:
540
+ console.warn(`Field ${field} is not defined in metadata schema.`);
541
+ return null;
542
+ }
543
+ }).filter(Boolean);
544
+
545
+ return schemaItems;
546
+ }
547
+
548
+ /**
549
+ * Get schema fields from log type
550
+ */
551
+
552
+ export function getSchemaFieldsFromLog(__typename: string, logType: LogTypeEnum): ValidMetadataFieldName[] {
553
+
554
+ let schemaFields: ValidMetadataFieldName[];
555
+ switch (__typename) {
556
+ case 'LogArable': {
557
+ const isWaterTesting = logType === 'WATER';
558
+ const isSale = ['SALES'].includes(logType!);
559
+ const isPurchase = ['SEED'].includes(logType!);
560
+ const isSaleOrPurchase = isSale || isPurchase;
561
+
562
+ schemaFields = [
563
+ 'activity',
564
+ isWaterTesting ? 'water_testing' : null,
565
+ isWaterTesting ? 'concentration_unit' : null,
566
+ isWaterTesting ? 'water_quantity' : isSaleOrPurchase ? null : 'quantity',
567
+ isWaterTesting ? 'water_unit' : isSaleOrPurchase ? null : 'unit',
568
+ isWaterTesting ? null : isSaleOrPurchase ? 'purchased_item' : 'crop',
569
+ isSaleOrPurchase ? 'otherParty' : null,
570
+ isSaleOrPurchase ? 'invoiceNumber' : null,
571
+ isSaleOrPurchase ? 'invoiceItems' : null,
572
+ isSaleOrPurchase ? 'tax' : null,
573
+ isSaleOrPurchase ? null : isWaterTesting ? 'location_water' : 'location_arable',
574
+ isSaleOrPurchase ? null : 'fieldLocation',
575
+ ];
576
+ } break;
577
+ case 'LogFarmersMarket': {
578
+ const isReceipt = ['MARKET_RECEIPTS'].includes(logType);
579
+
580
+ schemaFields = [
581
+ 'activity',
582
+ isReceipt ? 'receiptNumber' : null,
583
+ // 'childOrgId', // not editable
584
+ isReceipt ? 'void' : null,
585
+ isReceipt ? 'marketCredits' : null,
586
+ ];
587
+ } break;
588
+ case 'LogLivestock': {
589
+ const isLivestock = ['LIVESTOCK_LIFE_CYCLE', 'LIVESTOCK_TRACKING', 'LIVESTOCK_HEALTHCARE'].includes(logType);
590
+ const isSupplyPurchase = logType === 'SUPPLY_PURCHASE';
591
+ const isLandManagement = logType === 'PASTURE_LAND_MANAGEMENT';
592
+
593
+ schemaFields = [
594
+ 'activity',
595
+ isLivestock ? 'livestock' : null,
596
+ isLivestock ? 'livestockIdentifiers' : null,
597
+ isLivestock ? 'livestockGroup' : null,
598
+ isSupplyPurchase ? 'otherParty' : null,
599
+ isSupplyPurchase ? 'invoiceNumber' : null,
600
+ isSupplyPurchase ? 'invoiceItems' : null,
601
+ isSupplyPurchase ? 'tax' : null,
602
+ isLandManagement ? 'item_used' : null,
603
+ isLivestock || isSupplyPurchase ? null : 'quantity',
604
+ isLivestock || isSupplyPurchase ? null : 'unit',
605
+ !isSupplyPurchase ? 'location_livestock' : null,
606
+ isLivestock ? 'price' : null,
607
+ isLivestock ? null : 'fieldLocation',
608
+ ];
609
+ } break;
610
+ default:
611
+ schemaFields = [];
612
+ }
613
+
614
+ return schemaFields.filter(Boolean);
615
+ }
616
+
617
+ /**
618
+ * Get editable values based on activity & type (which is also used to display UI)
619
+ */
620
+
621
+ interface MakeMetadataSchemaParams {
622
+ __typename: string;
623
+ scrollAreaDOMId: string;
624
+ formId: string;
625
+ timeZone: string | null | undefined;
626
+ focusedName: string;
627
+ isCreateNew: boolean;
628
+ }
629
+
630
+ export function makeLogMetadataSchema(
631
+ formValues: Record<string, any> = {},
632
+ metadataParams: MakeMetadataSchemaParams,
633
+ ) {
634
+ const { __typename, scrollAreaDOMId, formId, timeZone, focusedName, isCreateNew } = metadataParams;
635
+
636
+ let activitiesList: any[] = [];
637
+ let schemaItems: any[] = [];
638
+
639
+ const basePopOverProps = {
640
+ scrollAreaDOMId,
641
+ zClassName: 'z9',
642
+ position: 'bottom_left',
643
+ offsetX: 0,
644
+ offsetY: 7,
645
+ };
646
+
647
+ const minDate = new Date();
648
+ minDate.setFullYear(minDate.getFullYear() - 10);
649
+
650
+ const maxDate = new Date();
651
+ maxDate.setMonth(maxDate.getMonth() + 1);
652
+
653
+ const namespace = {
654
+ LogArable: 'arableDetails',
655
+ LogFarmersMarket: 'farmersMarketDetails',
656
+ LogLivestock: 'livestockDetails',
657
+ }[__typename] || '';
658
+
659
+ const logActivity = formValues[namespace]?.activity;
660
+ const logType = getLogTypeFromActivity(__typename, logActivity)!;
661
+ const schemaFields = getSchemaFieldsFromLog(__typename, logType);
662
+ const isAITask = logType === 'AI_TASK';
663
+
664
+ switch (__typename) {
665
+ case 'LogArable': {
666
+ activitiesList = ARABLE_ACTIVITIES_GROUPED;
667
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
668
+ } break;
669
+ case 'LogFarmersMarket': {
670
+ activitiesList = FARMERS_MARKET_ACTIVITIES_GROUPED;
671
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
672
+ } break;
673
+ case 'LogLivestock': {
674
+ activitiesList = LIVESTOCK_ACTIVITIES_GROUPED;
675
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
676
+ } break;
677
+ default:
678
+ }
679
+
680
+ if (!isAITask) {
681
+ activitiesList = activitiesList.filter(([type]) => type !== 'AI_TASK');
682
+ }
683
+
684
+ // Date, time, createdBy is common across all log types
685
+
686
+ if (schemaItems.length) {
687
+ const timeZoneCode = getTimeZoneCode(timeZone);
688
+
689
+ schemaItems = schemaItems.concat([{
690
+ __type: 'input_click',
691
+ forceClickId: 'input_click_date',
692
+ label: i18n.t('form.date'),
693
+ item: {
694
+ name: 'date',
695
+ focused: focusedName === (formId + '_date'),
696
+ getter: (value: string) => getCalDate(new Date(value), timeZone).calDate,
697
+ },
698
+ popOverProps: {
699
+ id: formId + '_date',
700
+ ...basePopOverProps,
701
+ iface: {
702
+ name: 'PO_LIST',
703
+ variables: {
704
+ designClassName: 'w_350',
705
+ className: 'max_h_40vh',
706
+ initialState: {
707
+ date: formValues.date ? new Date(formValues.date) : new Date(),
708
+ },
709
+ options: [{
710
+ __type: 'DATE_PICKER',
711
+ name: 'date',
712
+ minDate,
713
+ maxDate,
714
+ }]
715
+ }
716
+ }
717
+ }
718
+ }, {
719
+ __type: 'input_time_from_date',
720
+ label: i18n.t(timeZoneCode ? 'form.time_with_zone' : 'form.time', { timeZone: timeZoneCode }),
721
+ scrollAreaDOMId,
722
+ item: {
723
+ name: 'date', // date value will be used to extract time
724
+ type: 'time',
725
+ timeZone,
726
+ },
727
+ }]);
728
+ }
729
+
730
+ if (schemaItems[0] && activitiesList.length) {
731
+ // First item is always activity
732
+ const activityValue = getObject(formValues, schemaItems[0].item.name);
733
+ schemaItems[0].item.popOverProps = {
734
+ id: formId + '_activity',
735
+ ...basePopOverProps,
736
+ disabled: isAITask, // AI tasks' activity cannot be changed
737
+ iface: {
738
+ name: 'PO_LIST',
739
+ variables: {
740
+ designClassName: 'w_250',
741
+ className: 'max_h_40vh',
742
+ options: activitiesList.reduce((acc, [type, activities], i) => {
743
+ const colorIndicator = getLogCategoryColor(type);
744
+ const isEnd = i === activitiesList.length - 1;
745
+
746
+ const updatedList = acc.concat({
747
+ __type: 'LIST_SUBTITLE' as const,
748
+ colorIndicator,
749
+ text: i18n.t(`log.type_short.${type}`),
750
+ className: 'mt_sm',
751
+ textClassName: `cl_${colorIndicator}`,
752
+ }).concat(activities.map((activity: string) => ({
753
+ __type: 'LIST_ITEM' as const,
754
+ // colorIndicator,
755
+ text: i18n.t(`log.activity.${activity}`),
756
+ textClassName: 'ft_xs',
757
+ rightIconClassName: 'ft_xs mb_2',
758
+ name: schemaItems[0].item.name,
759
+ value: activity,
760
+ selected: activityValue === activity,
761
+ })));
762
+
763
+ if (!isEnd) {
764
+ updatedList.push({
765
+ __type: 'BREAK' as const,
766
+ });
767
+ }
768
+
769
+ return updatedList;
770
+ }, [])
771
+ }
772
+ }
773
+ };
774
+ }
775
+
776
+ return schemaItems;
777
+ }