@sankhyalabs/core 5.20.0-dev.49 → 5.20.0-dev.50

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.
@@ -52,7 +52,8 @@ export default class DataUnit {
52
52
  private _savingLockers: Promise<any>[] = [];
53
53
  private _defaultSorting: Array<Sort>;
54
54
  private _allowReleaseCallbacks: boolean;
55
-
55
+ private _waitingToReload: boolean = false;
56
+
56
57
  public metadataLoader?: (dataUnit: DataUnit) => Promise<UnitMetadata>;
57
58
  public dataLoader?: (dataUnit: DataUnit, request: LoadDataRequest) => Promise<LoadDataResponse>;
58
59
  public saveLoader?: (dataUnit: DataUnit, changes: Array<Change>) => Promise<Array<SavedRecord>>;
@@ -150,6 +151,21 @@ export default class DataUnit {
150
151
  return this._uuid;
151
152
  }
152
153
 
154
+ /**
155
+ * Retorna se o dataUnit está com recarregamento pendente.
156
+ */
157
+ public isWaitingToReload(): boolean {
158
+ return this._waitingToReload;
159
+ }
160
+
161
+ /**
162
+ * Define se o dataUnit tem um recarregamento pendente.
163
+ * @param isWaiting
164
+ */
165
+ public setWaitingToReload(isWaiting: boolean){
166
+ this._waitingToReload = isWaiting;
167
+ }
168
+
153
169
  /**
154
170
  *
155
171
  * Obtém o nome de identificação do DataUnit (geralmente em formato de URI - Uniform Resource Identifier).
@@ -973,16 +989,15 @@ export default class DataUnit {
973
989
  *
974
990
  */
975
991
  public async setFieldValue(fieldName: string, newValue: any, records?: Array<string>, options?:DataUnitEventOptions): Promise<boolean> {
976
-
977
992
  if(!this.hasNewRecord() && !this.getSelectedRecord()) await this.addRecord();
978
-
993
+
979
994
  const typedValue = this.validateAndTypeValue(fieldName, newValue);
980
995
  const currentValue = this.getFieldValue(fieldName);
981
996
 
982
- if(ObjectUtils.hasEquivalentProps(currentValue, typedValue)){
997
+ if(!(newValue instanceof Promise) && ObjectUtils.hasEquivalentProps(currentValue, typedValue)){
983
998
  return Promise.resolve(false);
984
999
  }
985
-
1000
+
986
1001
  if(newValue instanceof Promise){
987
1002
  const promise:Promise<boolean> = new Promise(accept => {
988
1003
  newValue.then(resolvedValue => {
@@ -1016,6 +1031,18 @@ export default class DataUnit {
1016
1031
  this.dispatchAction(Action.FIELD_INVALIDATED, { fieldName, message, recordId }, undefined);
1017
1032
  }
1018
1033
 
1034
+ /**
1035
+ *
1036
+ * Cancela o saving exibindo os campos invalidos.
1037
+ *
1038
+ * @param filds - Lista dos campos
1039
+ * @param recordId - Indica qual registro está com os campos inválido.
1040
+ *
1041
+ */
1042
+ public savingCanceled(fields: Array<{name: string, message: string}>, recordId: string): void {
1043
+ this.dispatchAction(Action.SAVING_CANCELED, { fields, recordId }, undefined);
1044
+ }
1045
+
1019
1046
  /**
1020
1047
  *
1021
1048
  * Limpa campos inválidos.
@@ -36,6 +36,7 @@ export enum Action{
36
36
  SAVING_DATA = "savingData",
37
37
  DATA_SAVED = "dataSaved",
38
38
  SAVING_ERROR = "savingError",
39
+ SAVING_CANCELED = "savingCanceled",
39
40
 
40
41
  REMOVING_RECORDS = "removingRecords",
41
42
  RECORDS_REMOVED = "recordsRemoved",
@@ -47,7 +47,7 @@ class RecordsReducerImpl implements ActionReducer {
47
47
  newRecords.push(newRecord);
48
48
  }
49
49
  });
50
- return newRecords.concat(Array.from(recordsMap.values()));
50
+ return Array.from(recordsMap.values()).concat(newRecords);
51
51
 
52
52
  case Action.RECORD_LOADED:
53
53
  action.payload.forEach((record: any) => {
@@ -0,0 +1,45 @@
1
+
2
+ import * as RecordsReducerModule from '../RecordsSlice';
3
+ import StateManager from '../../StateManager';
4
+ import { Record } from '../../../DataUnit';
5
+ import { Action } from '../../action/DataUnitAction';
6
+
7
+ jest.spyOn(RecordsReducerModule, "getRecords").mockImplementation(() => [
8
+ {
9
+ __record__id__: '0',
10
+ }, {
11
+ __record__id__: '1',
12
+ }]);
13
+
14
+ describe('RecordsSlice', () => {
15
+ let stateManager: StateManager;
16
+ let currentState: Array<Record>;
17
+
18
+ beforeEach(() => {
19
+ stateManager = new StateManager([RecordsReducerModule.RecordsReducer]);
20
+ currentState = [{
21
+ __record__id__: '0',
22
+ }, {
23
+ __record__id__: '1',
24
+ }]
25
+ });
26
+
27
+ it('should return newlines at the end', () => {
28
+ let records = RecordsReducerModule.RecordsReducer.reduce(stateManager, currentState, {
29
+ type: Action.DATA_SAVED,
30
+ payload: {
31
+ records: [
32
+ {__record__id__: '0'},
33
+ {__record__id__: '2'},
34
+ {__record__id__: '1'},
35
+ ]
36
+ }
37
+ })
38
+ expect(records).toEqual([
39
+ {__record__id__: '0'},
40
+ {__record__id__: '1'},
41
+ {__record__id__: '2'}
42
+ ]);
43
+ });
44
+
45
+ });
@@ -0,0 +1,44 @@
1
+ import DataUnit from '../DataUnit';
2
+ import { Action } from '../state/action/DataUnitAction';
3
+
4
+ describe('DataUnit', () => {
5
+
6
+ let dataUnit: DataUnit;
7
+
8
+ beforeEach(() => {
9
+ dataUnit = new DataUnit();
10
+
11
+ dataUnit['dispatchAction'] = jest.fn();
12
+ });
13
+
14
+ it('should return false when not waiting to reload', () => {
15
+ expect(dataUnit.isWaitingToReload()).toBe(false);
16
+ });
17
+
18
+ it('should set waitingToReload to true', () => {
19
+ dataUnit.setWaitingToReload(true);
20
+ expect(dataUnit.isWaitingToReload()).toBe(true);
21
+ });
22
+
23
+ it('should set waitingToReload to false', () => {
24
+ dataUnit.setWaitingToReload(true);
25
+ dataUnit.setWaitingToReload(false);
26
+ expect(dataUnit.isWaitingToReload()).toBe(false);
27
+ });
28
+
29
+ it('should dispatch SAVING_CANCELED action with correct fields and recordId', () => {
30
+ const fields = [
31
+ { name: 'field1', message: 'error1' },
32
+ { name: 'field2', message: 'error2' }
33
+ ];
34
+ const recordId = '12345';
35
+
36
+ dataUnit.savingCanceled(fields, recordId);
37
+
38
+ expect(dataUnit['dispatchAction']).toHaveBeenCalledWith(
39
+ Action.SAVING_CANCELED,
40
+ { fields, recordId },
41
+ undefined
42
+ );
43
+ });
44
+ });