@unitedstatespowersquadrons/components 1.2.6 → 1.2.7-pre.2

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.
@@ -1,6 +1,7 @@
1
1
  import React, { createContext, useContext, useMemo } from "react";
2
2
  import { Draft, produce } from "immer";
3
3
  import { useImmerReducer } from "use-immer";
4
+ import ActionCable, { Cable } from "actioncable";
4
5
  import { ReducerHandler } from "./types";
5
6
 
6
7
  interface InitialAppProps<AppState> {
@@ -10,10 +11,14 @@ interface InitialAppProps<AppState> {
10
11
 
11
12
  export default function buildReducer<AppState, AppAction>(
12
13
  reducer: (draft: Draft<AppState>, action: AppAction) => void,
13
- handlers: ReducerHandler<AppAction>[] = []
14
+ handlers: ReducerHandler<AppAction>[] = [],
15
+ actionCableUrl?: string
14
16
  ) {
15
17
  const StateContext = createContext<AppState | null>(null);
16
18
  const DispatchContext = createContext<React.Dispatch<AppAction> | null>(null);
19
+ const CableContext = createContext<Cable | null>(null);
20
+
21
+ const cable = actionCableUrl ? ActionCable.createConsumer(actionCableUrl) : null;
17
22
 
18
23
  function AppProvider(props: InitialAppProps<AppState>) {
19
24
  const [providerState, dispatch] = useImmerReducer(reducer, props.initialState, (state: AppState) => {
@@ -30,9 +35,11 @@ export default function buildReducer<AppState, AppAction>(
30
35
 
31
36
  return (
32
37
  <StateContext.Provider value={providerState}>
33
- <DispatchContext.Provider value={wrappedDispatch}>
34
- {props.children}
35
- </DispatchContext.Provider>
38
+ <CableContext.Provider value={cable}>
39
+ <DispatchContext.Provider value={wrappedDispatch}>
40
+ {props.children}
41
+ </DispatchContext.Provider>
42
+ </CableContext.Provider>
36
43
  </StateContext.Provider>
37
44
  );
38
45
  }
@@ -45,6 +52,14 @@ export default function buildReducer<AppState, AppAction>(
45
52
  return context;
46
53
  }
47
54
 
55
+ function useAppCable() {
56
+ const context = useContext(CableContext);
57
+ if (!context) {
58
+ throw new Error("useAppCable has to be used within AppProvider");
59
+ }
60
+ return context;
61
+ }
62
+
48
63
  function useAppDispatch() {
49
64
  const context = useContext(DispatchContext);
50
65
  if (!context) {
@@ -55,6 +70,7 @@ export default function buildReducer<AppState, AppAction>(
55
70
 
56
71
  return {
57
72
  AppProvider,
73
+ useAppCable,
58
74
  useAppDispatch,
59
75
  useAppState,
60
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unitedstatespowersquadrons/components",
3
- "version": "1.2.6",
3
+ "version": "1.2.7-pre.2",
4
4
  "description": "USPS shared React components library",
5
5
  "main": "index.tsx",
6
6
  "scripts": {
@@ -17,6 +17,9 @@
17
17
  },
18
18
  "homepage": "https://github.com/unitedstatespowersquadrons/components#readme",
19
19
  "dependencies": {
20
+ "@rails/actioncable": "^8.0.200",
21
+ "@types/actioncable": "^5.2.11",
22
+ "actioncable": "^5.2.8-1",
20
23
  "classnames": "^2.5.1",
21
24
  "immer": "^10.1.1",
22
25
  "react": "^19.1.0",