@webitel/ui-datalist 26.8.27 → 26.8.29

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": "@webitel/ui-datalist",
3
- "version": "26.8.27",
3
+ "version": "26.8.29",
4
4
  "description": "Toolkit for building data lists in webitel ui system",
5
5
  "scripts": {
6
6
  "build:types": "vue-tsc -b tsconfig.build.json",
@@ -0,0 +1,149 @@
1
+ import { mount } from '@vue/test-utils';
2
+ import {
3
+ getCalendarDayRangeErrors,
4
+ getResourceGroupTimeRangeErrors,
5
+ } from '@webitel/api-services/validations';
6
+ import { describe, expect, it } from 'vitest';
7
+ import { defineComponent, ref } from 'vue';
8
+
9
+ import { useTimeRangesValidation } from '../useTimeRangesValidation';
10
+
11
+ type CalendarDay = {
12
+ day: number;
13
+ disabled: boolean;
14
+ start: number;
15
+ end: number;
16
+ };
17
+
18
+ const row = (day: number, start: number, end: number): CalendarDay => ({
19
+ day,
20
+ disabled: false,
21
+ start,
22
+ end,
23
+ });
24
+
25
+ const setup = (rows: CalendarDay[]) => {
26
+ const dataList = ref(rows);
27
+
28
+ const Comp = defineComponent({
29
+ setup: () => useTimeRangesValidation(dataList, getCalendarDayRangeErrors),
30
+ template: '<div />',
31
+ });
32
+
33
+ return {
34
+ dataList,
35
+ ...(mount(Comp).vm as unknown as ReturnType<
36
+ typeof useTimeRangesValidation
37
+ >),
38
+ };
39
+ };
40
+
41
+ describe('useTimeRangesValidation with calendar days', () => {
42
+ it('marks nothing for rows that do not overlap', () => {
43
+ const { getRangeValidation } = setup([
44
+ row(0, 540, 541),
45
+ row(0, 600, 660),
46
+ ]);
47
+
48
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
49
+ expect(getRangeValidation(1, 'end')).toBeUndefined();
50
+ });
51
+
52
+ it('marks both ends of a row that starts after it ends', () => {
53
+ const { getRangeValidation } = setup([
54
+ row(0, 600, 540),
55
+ ]);
56
+
57
+ expect(getRangeValidation(0, 'start')).toEqual({
58
+ $error: true,
59
+ $errors: [
60
+ 'Time From cannot be greater than To',
61
+ ],
62
+ });
63
+ expect(getRangeValidation(0, 'end')?.$error).toBe(true);
64
+ });
65
+
66
+ it('marks every row of an overlap, and clears once it is resolved', () => {
67
+ const { dataList, getRangeValidation } = setup([
68
+ row(0, 540, 720),
69
+ row(0, 600, 780),
70
+ ]);
71
+
72
+ expect(getRangeValidation(0, 'start')?.$errors).toEqual([
73
+ 'Time intervals on the same day cannot overlap',
74
+ ]);
75
+ expect(getRangeValidation(1, 'start')?.$error).toBe(true);
76
+
77
+ dataList.value[1].start = 900;
78
+ dataList.value[1].end = 960;
79
+
80
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
81
+ expect(getRangeValidation(1, 'start')).toBeUndefined();
82
+ });
83
+
84
+ it('marks a time outside the day', () => {
85
+ const { getRangeValidation } = setup([
86
+ row(0, 0, 24 * 60),
87
+ ]);
88
+
89
+ expect(getRangeValidation(0, 'end')?.$errors).toEqual([
90
+ 'Hours must be from 00 to 23',
91
+ ]);
92
+ });
93
+ });
94
+
95
+ describe('useTimeRangesValidation with resource group ranges', () => {
96
+ const setupRanges = (
97
+ rows: {
98
+ start: number;
99
+ end: number;
100
+ }[],
101
+ ) => {
102
+ const ranges = ref(rows);
103
+
104
+ const Comp = defineComponent({
105
+ setup: () =>
106
+ useTimeRangesValidation(ranges, getResourceGroupTimeRangeErrors),
107
+ template: '<div />',
108
+ });
109
+
110
+ return {
111
+ ranges,
112
+ ...(mount(Comp).vm as unknown as ReturnType<
113
+ typeof useTimeRangesValidation
114
+ >),
115
+ };
116
+ };
117
+
118
+ it('marks both ends of a reversed range', () => {
119
+ const { getRangeValidation } = setupRanges([
120
+ {
121
+ start: 600,
122
+ end: 540,
123
+ },
124
+ ]);
125
+
126
+ expect(getRangeValidation(0, 'start')?.$error).toBe(true);
127
+ expect(getRangeValidation(0, 'end')?.$error).toBe(true);
128
+ });
129
+
130
+ it('marks intersecting ranges regardless of day', () => {
131
+ const { ranges, getRangeValidation } = setupRanges([
132
+ {
133
+ start: 540,
134
+ end: 720,
135
+ },
136
+ {
137
+ start: 600,
138
+ end: 780,
139
+ },
140
+ ]);
141
+
142
+ expect(getRangeValidation(1, 'end')?.$error).toBe(true);
143
+
144
+ ranges.value[1].start = 900;
145
+ ranges.value[1].end = 960;
146
+
147
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
148
+ });
149
+ });
@@ -1,7 +1,14 @@
1
1
  import type { RegleSchema } from '@regle/schemas';
2
2
  import { refDebounced } from '@vueuse/core';
3
3
  import { type StoreDefinition, storeToRefs } from 'pinia';
4
- import { onUnmounted, type Ref, watch } from 'vue';
4
+ import {
5
+ computed,
6
+ type MaybeRefOrGetter,
7
+ onUnmounted,
8
+ type Ref,
9
+ toValue,
10
+ watch,
11
+ } from 'vue';
5
12
 
6
13
  import { useCardAnyFieldEditedWatcher } from './useCardAnyFieldEditedWatcher';
7
14
  import { useCardIsNew } from './useCardIsNew';
@@ -21,6 +28,7 @@ export const useCardComponent = <
21
28
  useCardStore,
22
29
  onLoadErrorHandler,
23
30
  manualSetup = false,
31
+ hasSaveAccess,
24
32
  }: {
25
33
  useCardStore: StoreDefinition;
26
34
  onLoadErrorHandler?: (err: unknown) => void;
@@ -32,6 +40,13 @@ export const useCardComponent = <
32
40
  * @default false
33
41
  */
34
42
  manualSetup?: boolean;
43
+ /**
44
+ * [Claude] Save access from the app's access control, folded into
45
+ * `disabledSave`. Omitted means access is granted.
46
+ *
47
+ * @default true
48
+ */
49
+ hasSaveAccess?: MaybeRefOrGetter<boolean>;
35
50
  }) => {
36
51
  const cardStore = useCardStore();
37
52
 
@@ -71,6 +86,13 @@ export const useCardComponent = <
71
86
  isAnyFieldEdited,
72
87
  });
73
88
 
89
+ const disabledSave = computed<boolean>(
90
+ () =>
91
+ !(toValue(hasSaveAccess) ?? true) ||
92
+ !isAnyFieldEdited.value ||
93
+ hasValidationErrors.value,
94
+ );
95
+
74
96
  const { save } = useCardSaveAction<CardEntity>({
75
97
  validate, // fixme: type
76
98
  saveItem,
@@ -106,6 +128,7 @@ export const useCardComponent = <
106
128
  saveText,
107
129
  hasValidationErrors,
108
130
  isAnyFieldEdited,
131
+ disabledSave,
109
132
  validationFields,
110
133
 
111
134
  // actions
@@ -33,16 +33,19 @@ export const useNestedCardComponent = <
33
33
  onLoadErrorHandler,
34
34
  routeParamName,
35
35
  parentId,
36
+ hasSaveAccess,
36
37
  }: {
37
38
  useCardStore: StoreDefinition;
38
39
  onLoadErrorHandler?: (err: unknown) => void;
39
40
  routeParamName: string;
40
41
  parentId?: MaybeRefOrGetter<CardParentId>;
42
+ hasSaveAccess?: MaybeRefOrGetter<boolean>;
41
43
  }) => {
42
44
  const cardSetup = useCardComponent<CardEntity>({
43
45
  useCardStore,
44
46
  onLoadErrorHandler,
45
47
  manualSetup: true,
48
+ hasSaveAccess,
46
49
  });
47
50
 
48
51
  const cardStore = useCardStore();
@@ -0,0 +1,38 @@
1
+ import type { TimeRangeError } from '@webitel/api-services/validations';
2
+ import { computed, type MaybeRefOrGetter, toValue } from 'vue';
3
+ import { useI18n } from 'vue-i18n';
4
+
5
+ export function useTimeRangesValidation(
6
+ items: MaybeRefOrGetter<unknown>,
7
+ getErrors: (items: unknown) => TimeRangeError[],
8
+ ) {
9
+ const { t } = useI18n();
10
+
11
+ const errors = computed(() => {
12
+ const byField = new Map<string, string>();
13
+
14
+ for (const { index, prop, key } of getErrors(toValue(items))) {
15
+ const field = `${index}.${prop}`;
16
+ if (!byField.has(field)) byField.set(field, key);
17
+ }
18
+
19
+ return byField;
20
+ });
21
+
22
+ const getRangeValidation = (index: number, prop: string) => {
23
+ const key = errors.value.get(`${index}.${prop}`);
24
+
25
+ if (!key) return undefined;
26
+
27
+ return {
28
+ $error: true,
29
+ $errors: [
30
+ t(`validation.${key}`),
31
+ ],
32
+ };
33
+ };
34
+
35
+ return {
36
+ getRangeValidation,
37
+ };
38
+ }
@@ -4,5 +4,6 @@ export * from './composables/useCardListNavigation';
4
4
  export * from './composables/useCardStoreProvider';
5
5
  export * from './composables/useCardTabs';
6
6
  export * from './composables/useNestedCardComponent';
7
+ export * from './composables/useTimeRangesValidation';
7
8
  export * from './stores/createCardStore';
8
9
  export * from './types/CardValidationFields.types';