@carbonorm/carbonreact 3.4.7 → 3.5.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/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import react, { useRef, useState, useEffect, Component, lazy } from 'react';
2
2
  import { toastOptions, clearCache, timeout, axiosInstance, isVerbose, checkAllRequestsComplete } from '@carbonorm/carbonnode';
3
- import { useNavigate, BrowserRouter } from 'react-router-dom';
3
+ import { useNavigate } from 'react-router-dom';
4
4
  import { toast, ToastContainer } from 'react-toastify';
5
5
  import OutsideClickHandler from 'react-outside-click-handler';
6
6
  import classNames from 'classnames';
@@ -3896,12 +3896,8 @@ function getStyles(overrides = {}) {
3896
3896
 
3897
3897
  // @link https://stackoverflow.com/questions/58399637/include-modal-functionality-in-react-higher-order-component
3898
3898
  function Popup({ open = true, handleClose, children, maxWidth, }) {
3899
- if (false === open) {
3900
- // @link https://legacy.reactjs.org/docs/conditional-rendering.html#preventing-component-from-rendering
3901
- return jsxRuntime_1(jsxRuntime_3, {});
3902
- }
3903
3899
  const dig = getStyles();
3904
- return jsxRuntime_1("div", { className: classNames(dig.modal, dig.fade, dig.show, dig.dBlock), style: { backgroundColor: "rgba(0,0,0,0.8)" }, id: "exampleModalCenter", tabIndex: -1, "aria-labelledby": "exampleModalCenterTitle", "aria-modal": "true", role: "dialog", children: jsxRuntime_1("div", { style: { maxWidth: maxWidth }, className: classNames(dig.modalDialog, dig.modalDialogCentered), children: jsxRuntime_1(OutsideClickHandler, { onOutsideClick: () => handleClose(), children: jsxRuntime_1("div", { className: classNames(dig.modalContent, dig.bgTransparent, dig.modalDialogScrollable), children: children }) }) }) });
3900
+ return jsxRuntime_1("div", { className: classNames(dig.modal, dig.fade, { [dig.show]: open }, dig.dBlock), style: { backgroundColor: "rgba(0,0,0,0.8)" }, id: "exampleModalCenter", tabIndex: -1, "aria-labelledby": "exampleModalCenterTitle", "aria-modal": "true", role: "dialog", children: jsxRuntime_1("div", { style: { maxWidth: maxWidth }, className: classNames(dig.modalDialog, dig.modalDialogCentered), children: jsxRuntime_1(OutsideClickHandler, { onOutsideClick: () => handleClose(), children: jsxRuntime_1("div", { className: classNames(dig.modalContent, dig.bgTransparent, dig.modalDialogScrollable), children: children }) }) }) });
3905
3901
  }
3906
3902
 
3907
3903
  const isProduction = window.location.host.split(".")[0] === "www";
@@ -4047,7 +4043,7 @@ const useEffectOnce = (effect) => {
4047
4043
  * @function connect
4048
4044
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
4049
4045
  **/
4050
- function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
4046
+ function initiateWebsocket({ TABLES = undefined, WsLiveUpdates = undefined, url = 'ws://localhost:8080/ws', timeoutSeconds = 250, heartbeatSeconds = 60 } = {}) {
4051
4047
  const { websocket } = CarbonReact.instance.state;
4052
4048
  if (!("WebSocket" in window)) {
4053
4049
  // todo - store that this has been shown in the state
@@ -4082,17 +4078,59 @@ function initiateWebsocket({ TABLES = undefined, url = 'ws://localhost:8080/ws',
4082
4078
  };
4083
4079
  connection.onmessage = (message) => {
4084
4080
  const parsedData = isJsonString(message?.data) ? JSON.parse(message?.data) : message?.data;
4081
+ if (message.data === 'pong') {
4082
+ return;
4083
+ }
4085
4084
  CarbonReact.instance.setState((prevState) => ({
4086
4085
  websocketEvents: prevState.websocketEvents.concat(message),
4087
4086
  websocketData: prevState.websocketData.concat(parsedData), // JSON.parse no good - base64?
4088
- }));
4089
- console.info('todo - going to impl TABLES', TABLES);
4090
- /*if (undefined !== TABLES) {
4091
-
4092
- TABLES.
4093
-
4094
-
4095
- }*/
4087
+ }), () => {
4088
+ if (undefined === TABLES) {
4089
+ console.log('WebSocket updates without the TABLES property passed will not automatically update the state.');
4090
+ return;
4091
+ }
4092
+ if (undefined === WsLiveUpdates) {
4093
+ console.log('WebSocket updates without the WsLiveUpdates property passed will not automatically update the state.');
4094
+ return;
4095
+ }
4096
+ if (parsedData?.REST) {
4097
+ const TABLE_NAME = parsedData?.REST?.TABLE_NAME;
4098
+ const TABLE_PREFIX = parsedData?.REST?.TABLE_PREFIX;
4099
+ const METHOD = parsedData?.REST?.METHOD;
4100
+ const REQUEST = parsedData?.REST?.REQUEST;
4101
+ const REQUEST_PRIMARY_KEY = parsedData?.REST?.REQUEST_PRIMARY_KEY ?? null;
4102
+ if (null === REQUEST_PRIMARY_KEY) {
4103
+ console.log('WebSocket updates without a primary key are not yet supported.');
4104
+ return;
4105
+ }
4106
+ console.log('todo - going to impl REST', TABLE_NAME, METHOD, REQUEST_PRIMARY_KEY, parsedData?.REST);
4107
+ const TABLE_NAME_SHORT = TABLE_NAME.substring(TABLE_PREFIX.length);
4108
+ const currentCache = CarbonReact.instance.state[TABLE_NAME_SHORT];
4109
+ // just because we have a websocket update, doesn't mean we need the update
4110
+ // check to see if the primary key is in the current cache
4111
+ const c6Table = TABLES[TABLE_NAME_SHORT] ?? null;
4112
+ if (null === c6Table) {
4113
+ console.error('WebSocket update could not find (' + TABLE_NAME_SHORT + ') in the TABLES property passed.', TABLES);
4114
+ return;
4115
+ }
4116
+ const primaryKeyKeys = Object.keys(REQUEST_PRIMARY_KEY);
4117
+ const elementsToUpdate = currentCache.filter((row) => {
4118
+ for (const element of primaryKeyKeys) {
4119
+ if (REQUEST_PRIMARY_KEY[element] !== row[element]) {
4120
+ return false;
4121
+ }
4122
+ }
4123
+ return true;
4124
+ });
4125
+ const updatedElements = elementsToUpdate.map((row) => {
4126
+ return {
4127
+ ...row,
4128
+ ...REQUEST
4129
+ };
4130
+ });
4131
+ WsLiveUpdates[TABLE_NAME_SHORT][METHOD]({}, updatedElements);
4132
+ }
4133
+ });
4096
4134
  };
4097
4135
  window.addEventListener("focus", () => initiateWebsocket());
4098
4136
  // websocket onclose event listener
@@ -4189,16 +4227,19 @@ function isJsonString(str) {
4189
4227
  }
4190
4228
  const CarbonReact = class extends Component {
4191
4229
  static instance;
4230
+ static persistentState = undefined;
4192
4231
  static lastLocation = window.location.pathname;
4193
- static websocketUrl = (window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + ':8888/ws';
4194
- static websocketTimeoutSeconds = 250;
4195
- static websocketHeartbeatSeconds = 250;
4196
4232
  // @link https://github.com/welldone-software/why-did-you-render
4197
4233
  // noinspection JSUnusedGlobalSymbols
4198
4234
  static whyDidYouRender = true;
4199
4235
  constructor(props) {
4200
4236
  super(props);
4201
- this.state = initialCarbonReactState;
4237
+ if (CarbonReact.persistentState !== undefined && this.props.shouldStatePersist !== false) {
4238
+ this.state = CarbonReact.persistentState;
4239
+ }
4240
+ else {
4241
+ this.state = initialCarbonReactState;
4242
+ }
4202
4243
  // This should only ever be done here, when the full state is being trashed.
4203
4244
  clearCache({
4204
4245
  ignoreWarning: true
@@ -4214,6 +4255,12 @@ const CarbonReact = class extends Component {
4214
4255
  return CarbonReact.instance.state;
4215
4256
  }
4216
4257
  shouldComponentUpdate(nextProps, nextState, _nextContext) {
4258
+ if (this.props.shouldStatePersist === false) {
4259
+ CarbonReact.persistentState = undefined;
4260
+ }
4261
+ else {
4262
+ CarbonReact.persistentState = nextState;
4263
+ }
4217
4264
  changed(this.constructor.name + ' (DigApi)', 'props', this.props, nextProps);
4218
4265
  changed(this.constructor.name + ' (DigApi)', 'state', this.state, nextState);
4219
4266
  return true;
@@ -4236,7 +4283,7 @@ const CarbonReact = class extends Component {
4236
4283
  if (this.state.backendThrowable.length > 0) {
4237
4284
  return jsxRuntime_2(jsxRuntime_3, { children: [nest, jsxRuntime_1(BackendThrowable, {})] });
4238
4285
  }
4239
- return jsxRuntime_2(BrowserRouter, { children: [jsxRuntime_1(GlobalHistory, {}), jsxRuntime_1(CarbonWebSocket, {}), this.props.children, jsxRuntime_1(ToastContainer, {})] });
4286
+ return jsxRuntime_2(jsxRuntime_3, { children: [jsxRuntime_1(GlobalHistory, {}), jsxRuntime_1(CarbonWebSocket, {}), this.props.children, jsxRuntime_1(ToastContainer, {})] });
4240
4287
  }
4241
4288
  };
4242
4289