@unitedstatespowersquadrons/components 1.2.7-pre.6 → 1.2.8-pre.1
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/Reducer/buildReducer.tsx +13 -1
- package/Reducer/types.ts +5 -0
- package/package.json +1 -1
package/Reducer/buildReducer.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import React, { createContext, useContext, useMemo } from "react";
|
|
|
2
2
|
import { Draft, produce } from "immer";
|
|
3
3
|
import { useImmerReducer } from "use-immer";
|
|
4
4
|
import { Cable, createConsumer } from "@rails/actioncable";
|
|
5
|
-
import { ReducerHandler } from "./types";
|
|
5
|
+
import { CableAction, DispatchFunc, ReducerHandler } from "./types";
|
|
6
6
|
|
|
7
7
|
interface InitialAppProps<AppState> {
|
|
8
8
|
initialState: AppState;
|
|
@@ -60,6 +60,17 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
60
60
|
return context;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
function useAppCableSubscription(dispatch: DispatchFunc<CableAction>, channel: string) {
|
|
64
|
+
const appCable = useAppCable();
|
|
65
|
+
|
|
66
|
+
return(
|
|
67
|
+
appCable.subscriptions.create(
|
|
68
|
+
{ channel },
|
|
69
|
+
{ received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
63
74
|
function useAppDispatch() {
|
|
64
75
|
const context = useContext(DispatchContext);
|
|
65
76
|
if (!context) {
|
|
@@ -71,6 +82,7 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
71
82
|
return {
|
|
72
83
|
AppProvider,
|
|
73
84
|
useAppCable,
|
|
85
|
+
useAppCableSubscription,
|
|
74
86
|
useAppDispatch,
|
|
75
87
|
useAppState,
|
|
76
88
|
};
|
package/Reducer/types.ts
CHANGED
|
@@ -21,6 +21,11 @@ export interface GenericAppAction {
|
|
|
21
21
|
type: string;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
export interface CableAction {
|
|
25
|
+
data: object;
|
|
26
|
+
type: "receivedCable";
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
export type RailsAppAction<R extends RailsResponse = RailsResponse> =
|
|
25
30
|
| { type: "saveComplete"; res: R }
|
|
26
31
|
| { type: "saveFailed" };
|