@carbonorm/carbonreact 4.0.13 → 4.0.14

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,14 +1,14 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "4.0.13",
4
+ "version": "4.0.14",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "type": "module",
10
10
  "dependencies": {
11
- "@carbonorm/carbonnode": "^2.0.0",
11
+ "@carbonorm/carbonnode": "^2.0.8",
12
12
  "@fortawesome/fontawesome-svg-core": "^6.4.0",
13
13
  "@fortawesome/free-solid-svg-icons": "^6.4.0",
14
14
  "@fortawesome/react-fontawesome": "^0.2.0",
@@ -1,40 +1,44 @@
1
- import CarbonReact, {iCarbonReactState, isJsonString} from "CarbonReact";
1
+ import CarbonReact, {iCarbonReactState, isJsonString, tStatefulApiData} from "CarbonReact";
2
2
  import {addAlert} from "../Alert/Alert";
3
3
  import {useEffectOnce} from "../../api/hoc/useEffectOnce";
4
- import {tC6Tables, tC6RestApi} from "@carbonorm/carbonnode";
4
+ import {iC6Object} from "@carbonorm/carbonnode";
5
5
 
6
6
 
7
- export interface iCarbonWebSocketProps<P,S extends iCarbonReactState> {
7
+ export interface iCarbonWebSocketProps<P, S extends iCarbonReactState> {
8
8
  url?: string,
9
9
  timeoutSeconds?: number,
10
10
  heartbeatSeconds?: number,
11
- instance: CarbonReact<P,S>,
12
- TABLES?: tC6Tables,
13
- WsLiveUpdates?: tC6RestApi,
11
+ instance: CarbonReact<P, S>,
12
+ C6?: iC6Object,
14
13
  }
15
14
 
16
15
  /**
17
16
  * @function connect
18
17
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
19
18
  **/
20
- export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonWebSocketProps<P,S>) {
19
+ export function initiateWebsocket<P, S extends iCarbonReactState>(props: iCarbonWebSocketProps<P, S>) {
21
20
 
22
21
  let {
23
22
  instance,
24
- TABLES = undefined,
25
- WsLiveUpdates = undefined,
26
23
  url = 'ws' + (window.location.protocol === 'https:' ? 's' : '') + '://' + window.location.host + '/carbonorm/websocket',
27
24
  timeoutSeconds = 250,
28
- heartbeatSeconds = 60
25
+ heartbeatSeconds = 60,
26
+ C6
29
27
  } = props;
30
28
 
29
+ const {
30
+ TABLES = undefined,
31
+ IMPORT = undefined,
32
+ } = C6 ?? {};
33
+
34
+
31
35
  const {websocket} = instance.state;
32
36
 
33
37
 
34
38
  if (!("WebSocket" in window)) {
35
39
 
36
40
  // todo - store that this has been shown in the state
37
- addAlert<P,S>({
41
+ addAlert<P, S>({
38
42
  title: 'Browser does not support websockets, live updates will fail. You may need to refresh the page to see the newest content.',
39
43
  text: 'Please use a modern browser.',
40
44
  icon: 'warning',
@@ -105,14 +109,6 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
105
109
 
106
110
  }
107
111
 
108
- if (undefined === WsLiveUpdates) {
109
-
110
- console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.')
111
-
112
- return;
113
-
114
- }
115
-
116
112
  if (parsedData?.REST) {
117
113
 
118
114
  const TABLE_NAME: string = parsedData?.REST?.TABLE_NAME;
@@ -139,7 +135,7 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
139
135
 
140
136
  const TABLE_NAME_SHORT = TABLE_NAME.substring(TABLE_PREFIX.length);
141
137
 
142
- const currentCache: [] = instance.state[TABLE_NAME_SHORT]
138
+ const currentCache: tStatefulApiData<{ [key: string]: any }> = instance.state[TABLE_NAME_SHORT]
143
139
 
144
140
  // just because we have a websocket update, doesn't mean we need the update
145
141
  // check to see if the primary key is in the current cache
@@ -155,7 +151,8 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
155
151
 
156
152
  const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY)
157
153
 
158
- const elementsToUpdate = currentCache.filter((row: any) => {
154
+ // todo - which direction should we filter
155
+ const elementsToUpdate = currentCache?.filter((row: any) => {
159
156
 
160
157
  for (const element of primaryKeyKeys) {
161
158
 
@@ -174,7 +171,7 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
174
171
 
175
172
  return true
176
173
 
177
- })
174
+ }) ?? []
178
175
 
179
176
  console.log('elementsToUpdate', elementsToUpdate)
180
177
 
@@ -192,8 +189,30 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
192
189
 
193
190
  })
194
191
 
195
- updatedElements.forEach((row: any) => {
196
- WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, row)
192
+ updatedElements.forEach(async (row: any) => {
193
+
194
+ const RestRequests = await IMPORT?.(TABLE_NAME_SHORT)
195
+
196
+ const {
197
+ postState,
198
+ deleteState,
199
+ putState,
200
+ } = RestRequests;
201
+
202
+ switch (METHOD) {
203
+ case 'POST':
204
+ postState({}, row)
205
+ break;
206
+ case 'DELETE':
207
+ deleteState({}, row)
208
+ break;
209
+ case 'PUT':
210
+ putState({}, row)
211
+ break;
212
+ default:
213
+ console.error('Method not supported', METHOD)
214
+ }
215
+
197
216
  })
198
217
 
199
218
  }
@@ -286,7 +305,7 @@ export function initiateWebsocket<P,S extends iCarbonReactState>(props: iCarbonW
286
305
 
287
306
  }
288
307
 
289
- export default function <P,S extends iCarbonReactState>(props: iCarbonWebSocketProps<P,S>) {
308
+ export default function <P, S extends iCarbonReactState>(props: iCarbonWebSocketProps<P, S>) {
290
309
 
291
310
  useEffectOnce(() => {
292
311