@carbonorm/carbonreact 3.6.2 → 4.0.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.
- package/README.md +1 -1
- package/dist/CarbonReact.d.ts +17 -8
- package/dist/components/Alert/Alert.d.ts +5 -1
- package/dist/components/Errors/BackendThrowable.d.ts +4 -1
- package/dist/components/WebSocket/CarbonWebSocket.d.ts +3 -1
- package/dist/hoc/KeysMatching.d.ts +1 -1
- package/dist/hoc/SubsetMatching.d.ts +4 -0
- package/dist/hoc/deleteRestfulObjectArrays.d.ts +17 -3
- package/dist/hoc/updateRestfulObjectArrays.d.ts +25 -11
- package/dist/index.cjs.js +156 -169
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +157 -170
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/CarbonReact.tsx +36 -21
- package/src/components/Alert/Alert.tsx +32 -29
- package/src/components/Errors/BackendThrowable.tsx +11 -8
- package/src/components/WebSocket/CarbonWebSocket.tsx +21 -15
- package/src/hoc/KeysMatching.ts +4 -2
- package/src/hoc/SubsetMatching.ts +6 -0
- package/src/hoc/deleteRestfulObjectArrays.tsx +51 -52
- package/src/hoc/updateRestfulObjectArrays.tsx +97 -97
- package/src/index.ts +1 -0
- package/src/variables/C6.tsx +1 -1
- package/dist/variables/C6.d.ts +0 -842
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import CarbonReact, { iCarbonReactState, tStatefulApiData } from "CarbonReact";
|
|
2
|
+
import { KeysMatching } from "./KeysMatching";
|
|
3
|
+
import { SubsetMatching } from "./SubsetMatching";
|
|
5
4
|
|
|
6
5
|
export enum eUpdateInsertMethod {
|
|
7
6
|
REPLACE,
|
|
@@ -9,123 +8,124 @@ export enum eUpdateInsertMethod {
|
|
|
9
8
|
LAST,
|
|
10
9
|
}
|
|
11
10
|
|
|
11
|
+
export interface iUpdateRestfulObjectArrays<
|
|
12
|
+
ObjectType extends {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
} = {},
|
|
15
|
+
S extends { [key: string]: any; } = CarbonReact['state'],
|
|
16
|
+
P = CarbonReact['props']
|
|
17
|
+
> {
|
|
18
|
+
instance: CarbonReact<P, S>;
|
|
19
|
+
dataOrCallback: ObjectType[] | ((state: Readonly<S>, props: Readonly<P>) => ObjectType[] | null);
|
|
20
|
+
stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>;
|
|
21
|
+
uniqueObjectId: keyof ObjectType | (keyof ObjectType)[];
|
|
22
|
+
insertUpdateOrder: eUpdateInsertMethod;
|
|
23
|
+
callback?: () => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
12
26
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @param dataOrCallback
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
18
|
-
* @param insertUpdateOrder
|
|
19
|
-
* @param callback -
|
|
20
|
-
* parameter of setState.
|
|
27
|
+
* Updates or inserts objects in a stateful array, merging new data with existing objects.
|
|
28
|
+
* @param instance - The React component instance.
|
|
29
|
+
* @param dataOrCallback - Array of objects or a callback function returning an array of objects.
|
|
30
|
+
* @param stateKey - The key in the state where the data array is stored.
|
|
31
|
+
* @param uniqueObjectId - The unique identifier(s) for objects, typically the primary key of the table.
|
|
32
|
+
* @param insertUpdateOrder - The order in which new data should be inserted/updated.
|
|
33
|
+
* @param callback - Optional callback function to run after state update.
|
|
21
34
|
*/
|
|
22
|
-
export default function updateRestfulObjectArrays<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return bootstrap.setState((previousBootstrapState, props): {} => {
|
|
35
|
+
export default function updateRestfulObjectArrays<
|
|
36
|
+
ObjectType extends {
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
} = {},
|
|
39
|
+
S extends { [key: string]: any; } = CarbonReact['state'],
|
|
40
|
+
P = CarbonReact['props']
|
|
41
|
+
>({
|
|
42
|
+
instance,
|
|
43
|
+
dataOrCallback,
|
|
44
|
+
stateKey,
|
|
45
|
+
uniqueObjectId,
|
|
46
|
+
insertUpdateOrder = eUpdateInsertMethod.LAST,
|
|
47
|
+
callback,
|
|
48
|
+
}: iUpdateRestfulObjectArrays<ObjectType, S, P>): void {
|
|
37
49
|
|
|
38
|
-
|
|
50
|
+
const uniqueObjectIds = Array.isArray(uniqueObjectId) ? uniqueObjectId : [uniqueObjectId];
|
|
39
51
|
|
|
40
|
-
|
|
52
|
+
type ValidObject = SubsetMatching<S & iCarbonReactState, tStatefulApiData<ObjectType>>;
|
|
41
53
|
|
|
42
|
-
|
|
54
|
+
instance.setState((
|
|
55
|
+
previousBootstrapState: Readonly<S & iCarbonReactState>,
|
|
56
|
+
props: Readonly<P>
|
|
57
|
+
): Pick<S & iCarbonReactState, keyof S> | null => {
|
|
43
58
|
|
|
44
|
-
|
|
59
|
+
let newOrReplacementData: ObjectType[] | null = [];
|
|
45
60
|
|
|
46
|
-
|
|
61
|
+
if (Array.isArray(dataOrCallback)) {
|
|
47
62
|
|
|
48
|
-
|
|
63
|
+
newOrReplacementData = dataOrCallback;
|
|
49
64
|
|
|
50
|
-
|
|
65
|
+
} else if (typeof dataOrCallback === "function") {
|
|
51
66
|
|
|
52
|
-
|
|
67
|
+
newOrReplacementData = dataOrCallback(previousBootstrapState, props);
|
|
53
68
|
|
|
54
|
-
|
|
69
|
+
} else {
|
|
55
70
|
|
|
56
|
-
|
|
71
|
+
throw new Error("The dataOrCallback parameter must be an array or function");
|
|
57
72
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (value[uniqueObjectId] !== item[uniqueObjectId]) {
|
|
61
|
-
|
|
62
|
-
isMatch = false;
|
|
63
|
-
|
|
64
|
-
return true;
|
|
73
|
+
}
|
|
65
74
|
|
|
66
|
-
|
|
75
|
+
if (newOrReplacementData === null) {
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
67
78
|
|
|
68
|
-
|
|
79
|
+
const findUniqueObjectIds = (item: ObjectType, value: ObjectType) => {
|
|
80
|
+
return uniqueObjectIds.every((id) => item[id] === value[id]);
|
|
81
|
+
};
|
|
69
82
|
|
|
70
|
-
|
|
83
|
+
const previousStateProperty = previousBootstrapState[stateKey] as tStatefulApiData<ObjectType> ?? [];
|
|
71
84
|
|
|
72
|
-
|
|
85
|
+
let updatedData: tStatefulApiData<ObjectType> = newOrReplacementData.map((value) => {
|
|
86
|
+
const existingObject = previousStateProperty?.find((item) =>
|
|
87
|
+
findUniqueObjectIds(item, value)
|
|
88
|
+
) || {};
|
|
89
|
+
return { ...existingObject, ...value };
|
|
90
|
+
});
|
|
73
91
|
|
|
74
|
-
|
|
92
|
+
const filterOutUpdated = (array: tStatefulApiData<ObjectType>) => {
|
|
93
|
+
return array?.filter((item) => !updatedData.some((value) => findUniqueObjectIds(item, value))) ?? [];
|
|
94
|
+
};
|
|
75
95
|
|
|
76
|
-
|
|
96
|
+
let newState: Partial<ValidObject> = {};
|
|
77
97
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
}) ?? [];
|
|
84
|
-
|
|
85
|
-
switch (insertUpdateOrder) {
|
|
86
|
-
default:
|
|
87
|
-
throw Error('The insertUpdateOrder (eUpdateInsertMethod) was not implemented')
|
|
88
|
-
case eUpdateInsertMethod.LAST:
|
|
89
|
-
return {
|
|
90
|
-
[stateKey]: null === newOrReplacementData ? null : [
|
|
98
|
+
switch (insertUpdateOrder) {
|
|
99
|
+
case eUpdateInsertMethod.LAST:
|
|
100
|
+
newState[stateKey as keyof ValidObject] = [
|
|
101
|
+
...filterOutUpdated(previousStateProperty),
|
|
91
102
|
...updatedData,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
case eUpdateInsertMethod.FIRST:
|
|
97
|
-
return {
|
|
98
|
-
[stateKey]: null === newOrReplacementData ? null : [
|
|
99
|
-
...(previousStateProperty?.filter(item => false === (updatedData?.find(value => findUniqueObjectIds(item, value)) || false)) ?? []),
|
|
103
|
+
] as any;
|
|
104
|
+
break;
|
|
105
|
+
case eUpdateInsertMethod.FIRST:
|
|
106
|
+
newState[stateKey as keyof ValidObject] = [
|
|
100
107
|
...updatedData,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (-1 === index) {
|
|
112
|
-
|
|
113
|
-
return oldObject
|
|
114
|
-
|
|
108
|
+
...filterOutUpdated(previousStateProperty),
|
|
109
|
+
] as any;
|
|
110
|
+
break;
|
|
111
|
+
case eUpdateInsertMethod.REPLACE:
|
|
112
|
+
newState[stateKey as keyof ValidObject] = [
|
|
113
|
+
...(previousStateProperty?.map((oldObject) => {
|
|
114
|
+
const index = updatedData.findIndex((item) => findUniqueObjectIds(item, oldObject));
|
|
115
|
+
if (index !== -1) {
|
|
116
|
+
return updatedData.splice(index, 1)[0];
|
|
115
117
|
}
|
|
116
|
-
|
|
117
|
-
return updatedData.splice(index, 1).pop()
|
|
118
|
-
|
|
118
|
+
return oldObject;
|
|
119
119
|
}) ?? []),
|
|
120
|
-
...updatedData
|
|
121
|
-
]
|
|
122
|
-
|
|
120
|
+
...updatedData,
|
|
121
|
+
] as any;
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
throw new Error("The insertUpdateOrder (eUpdateInsertMethod) was not implemented");
|
|
123
125
|
}
|
|
124
126
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
return newState as Pick<S & iCarbonReactState, keyof S>;
|
|
128
|
+
},
|
|
129
|
+
callback
|
|
130
|
+
);
|
|
129
131
|
}
|
|
130
|
-
|
|
131
|
-
|
package/src/index.ts
CHANGED
|
@@ -29,6 +29,7 @@ export { default as CarbonWebSocket } from "./components/WebSocket/CarbonWebSock
|
|
|
29
29
|
export * from "./components/WebSocket/CarbonWebSocket";
|
|
30
30
|
export * from "./hoc/GlobalHistory";
|
|
31
31
|
export * from "./hoc/KeysMatching";
|
|
32
|
+
export * from "./hoc/SubsetMatching";
|
|
32
33
|
export { default as addValidSQL } from "./hoc/addValidSQL";
|
|
33
34
|
export * from "./hoc/addValidSQL";
|
|
34
35
|
export { default as changed } from "./hoc/changed";
|
package/src/variables/C6.tsx
CHANGED