@unitedstatespowersquadrons/components 1.0.10 → 1.0.12
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,25 +1,19 @@
|
|
|
1
1
|
import { createContext } from "react";
|
|
2
2
|
import { Draft } from "immer";
|
|
3
|
+
import * as ReducerHandlers from "./reducerHandlers";
|
|
3
4
|
import { AppState, ServerResponse } from "./types";
|
|
4
|
-
import {
|
|
5
|
-
buildReducer,
|
|
6
|
-
DispatchFunc,
|
|
7
|
-
DispatchHandler,
|
|
8
|
-
RailsAppAction,
|
|
9
|
-
ReducerHandler,
|
|
10
|
-
wrapDispatch,
|
|
11
|
-
} from "../";
|
|
5
|
+
import { buildReducer, RailsAppAction } from "../";
|
|
12
6
|
|
|
13
7
|
// If you want to add additional data to the server response, use type `RailsAppAction<ExampleServerResponse>`.
|
|
14
8
|
// Otherwise, just use type `RailsAppAction`.
|
|
15
9
|
//
|
|
16
10
|
// If you need to access the available `type`s for your defined AppAction, use `AppActionType<AppAction>`
|
|
17
11
|
//
|
|
18
|
-
type AppAction =
|
|
12
|
+
export type AppAction =
|
|
19
13
|
| RailsAppAction<ServerResponse>
|
|
20
14
|
| { type: "doSomething"; something: string };
|
|
21
15
|
|
|
22
|
-
export const
|
|
16
|
+
export const reducer = (draft: Draft<AppState>, action: AppAction): void => {
|
|
23
17
|
const { type } = action;
|
|
24
18
|
|
|
25
19
|
switch (type) {
|
|
@@ -33,32 +27,9 @@ export const exampleReducer = (draft: Draft<AppState>, action: AppAction): void
|
|
|
33
27
|
}
|
|
34
28
|
};
|
|
35
29
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const actionType = "doSomething";
|
|
30
|
+
const StateContext = createContext<AppState | null>(null);
|
|
31
|
+
const DispatchContext = createContext<React.Dispatch<AppAction> | null>(null);
|
|
32
|
+
const handlers = Object.values(ReducerHandlers);
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const { something } = action;
|
|
45
|
-
|
|
46
|
-
const bodyJson = { something };
|
|
47
|
-
const href = "/do_something";
|
|
48
|
-
|
|
49
|
-
return { bodyJson, href };
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// If you want to add additional data to the server response, pass in type `ServerResponse`.
|
|
53
|
-
// Otherwise, just leave the second type parameter unspecified.
|
|
54
|
-
return wrapDispatch<AppAction, ServerResponse>(dispatch, [actionType], handler);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const { AppProvider, useAppDispatch, useAppState } = buildReducer(
|
|
58
|
-
exampleReducer,
|
|
59
|
-
createContext<AppState | null>(null),
|
|
60
|
-
createContext<React.Dispatch<AppAction> | null>(null),
|
|
61
|
-
[
|
|
62
|
-
doSomething,
|
|
63
|
-
]
|
|
64
|
-
);
|
|
34
|
+
export const { AppProvider, useAppDispatch, useAppState } =
|
|
35
|
+
buildReducer(reducer, StateContext, DispatchContext, handlers);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AppAction } from "./reducer";
|
|
2
|
+
import { ServerResponse } from "./types";
|
|
3
|
+
import { DispatchFunc, DispatchHandler } from "../types";
|
|
4
|
+
import wrapDispatch from "../wrapDispatch";
|
|
5
|
+
|
|
6
|
+
export const doSomething = (
|
|
7
|
+
dispatch: DispatchFunc<AppAction>
|
|
8
|
+
): DispatchFunc<AppAction> => {
|
|
9
|
+
const actionType = "doSomething";
|
|
10
|
+
|
|
11
|
+
const handler: DispatchHandler<AppAction> = (action: AppAction) => {
|
|
12
|
+
if (action.type !== actionType) return {};
|
|
13
|
+
|
|
14
|
+
const { something } = action;
|
|
15
|
+
|
|
16
|
+
const bodyJson = { something };
|
|
17
|
+
const href = "/do_something";
|
|
18
|
+
|
|
19
|
+
return { bodyJson, href };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// If you want to add additional data to the server response, pass in type `ServerResponse`.
|
|
23
|
+
// Otherwise, just leave the second type parameter unspecified.
|
|
24
|
+
return wrapDispatch<AppAction, ServerResponse>(dispatch, [actionType], handler);
|
|
25
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React, { useContext } from "react";
|
|
2
|
+
|
|
3
|
+
interface InitialAppProps<AppState> {
|
|
4
|
+
initialState: AppState;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default function buildUnreducableState<AppState>(
|
|
9
|
+
StateContext: React.Context<AppState | null>
|
|
10
|
+
) {
|
|
11
|
+
const AppProvider = (props: InitialAppProps<AppState>) => {
|
|
12
|
+
return (
|
|
13
|
+
<StateContext.Provider value={props.initialState}>
|
|
14
|
+
{props.children}
|
|
15
|
+
</StateContext.Provider>
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function useAppState() {
|
|
20
|
+
const context = useContext(StateContext);
|
|
21
|
+
if (!context) {
|
|
22
|
+
throw new Error("useAppState has to be used within AppProvider");
|
|
23
|
+
}
|
|
24
|
+
return context;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
AppProvider,
|
|
29
|
+
useAppState,
|
|
30
|
+
};
|
|
31
|
+
}
|
package/Reducer/index.tsx
CHANGED