@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 CHANGED
@@ -48,7 +48,7 @@ ideally once it has mounted it never gets unmounted. In application where this i
48
48
  application even if the component is unmounted. The behavior of accessing or updating state while the component is
49
49
  unknown (undefined) and should be avoided. The example below shows a simple implementation of CarbonReact. Our user
50
50
  defined component is in `CarbonORM` which is written to extend the `CarbonReact` class. Your implementation must also
51
- extend `CarbonORM`.
51
+ extend `CarbonReact`. The name `CarbonORM` is arbitrary and can be changed to your application's name.
52
52
 
53
53
 
54
54
  [index.tsx](https://github.com/CarbonORM/CarbonORM.dev/blob/www/src/index.tsx)
@@ -2,6 +2,11 @@ import { Component, Context, ReactElement, ReactNode } from 'react';
2
2
  import 'react-toastify/dist/ReactToastify.min.css';
3
3
  import { iRestfulObjectArrayTypes } from "variables/C6";
4
4
  import { iCarbonWebSocketProps } from "./components/WebSocket/CarbonWebSocket";
5
+ import { iUpdateRestfulObjectArrays } from "./hoc/updateRestfulObjectArrays";
6
+ import { iDeleteRestfulObjectArrays } from "./hoc/deleteRestfulObjectArrays";
7
+ export type tStatefulApiData<T extends {
8
+ [key: string]: any;
9
+ } = {}> = T[] | undefined | null;
5
10
  export interface iCarbonReactState {
6
11
  alertsWaiting: any[];
7
12
  websocketEvents: MessageEvent[];
@@ -12,18 +17,22 @@ export interface iCarbonReactState {
12
17
  export declare const initialRequiredCarbonORMState: iCarbonReactState;
13
18
  export declare const initialCarbonReactState: iCarbonReactState & iRestfulObjectArrayTypes;
14
19
  export declare function isJsonString(str: string): boolean;
15
- declare abstract class CarbonReact<P = {}, S = {}> extends Component<{
20
+ declare abstract class CarbonReact<P = {}, S extends {
21
+ [key: string]: any;
22
+ } = {}> extends Component<{
16
23
  children?: ReactNode | ReactNode[];
17
24
  instanceId?: string;
18
- websocket?: iCarbonWebSocketProps | boolean;
25
+ websocket?: Omit<iCarbonWebSocketProps, "instance"> | boolean;
19
26
  } & P, S & iCarbonReactState> {
20
27
  context: Context<S & iCarbonReactState>;
21
- protected static instance: CarbonReact & Component<{
22
- children?: ReactNode | ReactNode[];
23
- } & any, any & iCarbonReactState>;
24
- protected static getState(): Readonly<iCarbonReactState> & Readonly<any>;
25
- protected static useContext(): () => iCarbonReactState;
26
- protected: any;
28
+ protected static instance: CarbonReact;
29
+ protected target: typeof CarbonReact;
30
+ protected updateRestfulObjectArrays: <ObjectType extends {
31
+ [key: string]: any;
32
+ } = {}>(rest: Omit<iUpdateRestfulObjectArrays<ObjectType, S, P>, "instance">) => void;
33
+ protected deleteRestfulObjectArrays: <ObjectType extends {
34
+ [key: string]: any;
35
+ } = {}>(rest: Omit<iDeleteRestfulObjectArrays<ObjectType, S, P>, "instance">) => void;
27
36
  static lastLocation: string;
28
37
  static whyDidYouRender: boolean;
29
38
  protected constructor(props: {
@@ -1,3 +1,4 @@
1
+ import CarbonReact from "CarbonReact";
1
2
  import { ReactNode } from "react";
2
3
  export interface iAlertButtonOptions {
3
4
  text: string;
@@ -8,6 +9,7 @@ export interface iAlertButtonOptions {
8
9
  export interface iAlert {
9
10
  title: string;
10
11
  text: string;
12
+ instance: CarbonReact;
11
13
  component?: ReactNode;
12
14
  icon?: "warning" | "error" | "success" | "info" | "question" | null;
13
15
  buttons?: (iAlertButtonOptions)[] | undefined;
@@ -21,4 +23,6 @@ export interface iAlert {
21
23
  };
22
24
  }
23
25
  export declare function addAlert(props: iAlert): void;
24
- export default function Alert(): import("react/jsx-runtime").JSX.Element | null;
26
+ export default function Alert({ instance }: {
27
+ instance: CarbonReact;
28
+ }): import("react/jsx-runtime").JSX.Element | null;
@@ -1,3 +1,6 @@
1
+ import CarbonReact from "../../CarbonReact";
1
2
  import { ReactElement } from "react";
2
- declare const _default: () => ReactElement;
3
+ declare const _default: (props: {
4
+ instance: CarbonReact;
5
+ }) => ReactElement;
3
6
  export default _default;
@@ -1,8 +1,10 @@
1
+ import CarbonReact from "CarbonReact";
1
2
  import { tC6Tables, tC6RestApi } from "@carbonorm/carbonnode";
2
3
  export interface iCarbonWebSocketProps {
3
4
  url?: string;
4
5
  timeoutSeconds?: number;
5
6
  heartbeatSeconds?: number;
7
+ instance: CarbonReact;
6
8
  TABLES?: tC6Tables;
7
9
  WsLiveUpdates?: tC6RestApi;
8
10
  }
@@ -10,5 +12,5 @@ export interface iCarbonWebSocketProps {
10
12
  * @function connect
11
13
  * This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
12
14
  **/
13
- export declare function initiateWebsocket({ TABLES, WsLiveUpdates, url, timeoutSeconds, heartbeatSeconds }?: iCarbonWebSocketProps): void;
15
+ export declare function initiateWebsocket(props: iCarbonWebSocketProps): void;
14
16
  export default function (props: iCarbonWebSocketProps): null;
@@ -1,3 +1,3 @@
1
1
  export type KeysMatching<T, V> = {
2
- [K in keyof T]-?: T[K] extends V ? K : never;
2
+ [K in keyof T]: T[K] extends V ? K : never;
3
3
  }[keyof T];
@@ -0,0 +1,4 @@
1
+ import { KeysMatching } from "./KeysMatching";
2
+ export type SubsetMatching<T extends object, V> = {
3
+ [K in KeysMatching<T, V>]: T[K];
4
+ };
@@ -1,4 +1,18 @@
1
- import CarbonReact from "CarbonReact";
2
- import { tRestfulObjectArrayValues, tStatefulApiData } from "variables/C6";
1
+ import CarbonReact, { tStatefulApiData } from "CarbonReact";
3
2
  import { KeysMatching } from "./KeysMatching";
4
- export default function deleteRestfulObjectArrays<ObjectType = tRestfulObjectArrayValues, S = typeof CarbonReact.instance.state, P = typeof CarbonReact.instance.props>(dataOrCallback: ObjectType[] | (<K extends keyof S>(state: ((prevState: Readonly<S>, props: Readonly<P>) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null), callback?: () => void) => null | (ObjectType[])), stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>, uniqueObjectId: (keyof ObjectType) | (keyof ObjectType)[], callback?: () => void): void;
3
+ export interface iDeleteRestfulObjectArrays<ObjectType extends {
4
+ [key: string]: any;
5
+ } = {}, S extends {
6
+ [key: string]: any;
7
+ } = CarbonReact['state'], P = CarbonReact['props']> {
8
+ instance: CarbonReact<P, S>;
9
+ dataOrCallback: ObjectType[] | ((state: Readonly<S>, props: Readonly<P>) => ObjectType[] | null);
10
+ stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>;
11
+ uniqueObjectId: keyof ObjectType | (keyof ObjectType)[];
12
+ callback?: () => void;
13
+ }
14
+ export default function deleteRestfulObjectArrays<ObjectType extends {
15
+ [key: string]: any;
16
+ } = {}, S extends {
17
+ [key: string]: any;
18
+ } = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, callback }: iDeleteRestfulObjectArrays<ObjectType, S, P>): void;
@@ -1,19 +1,33 @@
1
- import { tRestfulObjectArrayValues, tStatefulApiData } from "variables/C6";
2
- import CarbonReact from "CarbonReact";
1
+ import CarbonReact, { tStatefulApiData } from "CarbonReact";
3
2
  import { KeysMatching } from "./KeysMatching";
4
3
  export declare enum eUpdateInsertMethod {
5
4
  REPLACE = 0,
6
5
  FIRST = 1,
7
6
  LAST = 2
8
7
  }
8
+ export interface iUpdateRestfulObjectArrays<ObjectType extends {
9
+ [key: string]: any;
10
+ } = {}, S extends {
11
+ [key: string]: any;
12
+ } = CarbonReact['state'], P = CarbonReact['props']> {
13
+ instance: CarbonReact<P, S>;
14
+ dataOrCallback: ObjectType[] | ((state: Readonly<S>, props: Readonly<P>) => ObjectType[] | null);
15
+ stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>;
16
+ uniqueObjectId: keyof ObjectType | (keyof ObjectType)[];
17
+ insertUpdateOrder: eUpdateInsertMethod;
18
+ callback?: () => void;
19
+ }
9
20
  /**
10
- *
11
- * merged with existing objects.uniqueObjectId || {}.
12
- * @param dataOrCallback
13
- * @param uniqueObjectId - the uniqueObjectId of the object to update; typically the primary key of the table.
14
- * @param stateKey -
15
- * @param insertUpdateOrder
16
- * @param callback - if you want to do something with the updated state, you can pass a callback here. Run as the second
17
- * parameter of setState.
21
+ * Updates or inserts objects in a stateful array, merging new data with existing objects.
22
+ * @param instance - The React component instance.
23
+ * @param dataOrCallback - Array of objects or a callback function returning an array of objects.
24
+ * @param stateKey - The key in the state where the data array is stored.
25
+ * @param uniqueObjectId - The unique identifier(s) for objects, typically the primary key of the table.
26
+ * @param insertUpdateOrder - The order in which new data should be inserted/updated.
27
+ * @param callback - Optional callback function to run after state update.
18
28
  */
19
- export default function updateRestfulObjectArrays<ObjectType = tRestfulObjectArrayValues, S = typeof CarbonReact.instance.state, P = typeof CarbonReact.instance.props>(dataOrCallback: ObjectType[] | (<K extends keyof S>(state: ((prevState: Readonly<S>, props: Readonly<P>) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null), callback?: () => void) => null | (ObjectType[])), stateKey: KeysMatching<S, tStatefulApiData<ObjectType>>, uniqueObjectId: (keyof ObjectType) | (keyof ObjectType)[], insertUpdateOrder?: eUpdateInsertMethod, callback?: () => void): void;
29
+ export default function updateRestfulObjectArrays<ObjectType extends {
30
+ [key: string]: any;
31
+ } = {}, S extends {
32
+ [key: string]: any;
33
+ } = CarbonReact['state'], P = CarbonReact['props']>({ instance, dataOrCallback, stateKey, uniqueObjectId, insertUpdateOrder, callback, }: iUpdateRestfulObjectArrays<ObjectType, S, P>): void;