@unitedstatespowersquadrons/components 1.2.0 → 1.2.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.
package/Readme.md
CHANGED
|
@@ -55,13 +55,10 @@ If you only need access to app state, but no dispatchable events, you can use
|
|
|
55
55
|
the simplified generator function:
|
|
56
56
|
|
|
57
57
|
```ts
|
|
58
|
-
import { createContext } from "react";
|
|
59
58
|
import { buildUnreducableState } from "@unitedstatespowersquadrons/components";
|
|
60
59
|
import { AppState } from "./types";
|
|
61
60
|
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
export const { AppProvider, useAppState } = buildUnreducableState<AppState>(StateContext);
|
|
61
|
+
export const { AppProvider, useAppState } = buildUnreducableState<AppState>();
|
|
65
62
|
```
|
|
66
63
|
|
|
67
64
|
## Automated Testing
|
|
@@ -8,7 +8,7 @@ export const doSomething = (
|
|
|
8
8
|
const actionType = "doSomething";
|
|
9
9
|
|
|
10
10
|
const handler: DispatchHandler<AppAction> = (action: AppAction) => {
|
|
11
|
-
if (action.type !== actionType) return
|
|
11
|
+
if (action.type !== actionType) return;
|
|
12
12
|
|
|
13
13
|
const { something } = action;
|
|
14
14
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import React, { useContext } from "react";
|
|
1
|
+
import React, { createContext, useContext } from "react";
|
|
2
2
|
|
|
3
3
|
interface InitialAppProps<AppState> {
|
|
4
4
|
initialState: AppState;
|
|
5
5
|
children: React.ReactNode;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export default function buildUnreducableState<AppState>(
|
|
9
|
-
StateContext
|
|
10
|
-
|
|
8
|
+
export default function buildUnreducableState<AppState>() {
|
|
9
|
+
const StateContext = createContext<AppState | null>(null);
|
|
10
|
+
|
|
11
11
|
const AppProvider = (props: InitialAppProps<AppState>) => {
|
|
12
12
|
return (
|
|
13
13
|
<StateContext.Provider value={props.initialState}>
|
package/Reducer/types.ts
CHANGED
|
@@ -7,13 +7,13 @@ export interface RailsResponse {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export type DispatchFunc<T> = (action: T) => void;
|
|
10
|
-
|
|
11
|
-
body?: string;
|
|
10
|
+
interface DispatchData {
|
|
12
11
|
bodyJson?: object;
|
|
13
12
|
headers?: { [key: string]: string };
|
|
14
|
-
href
|
|
13
|
+
href: string;
|
|
15
14
|
method?: string;
|
|
16
|
-
}
|
|
15
|
+
}
|
|
16
|
+
export type DispatchHandler<T> = (action: T) => DispatchData | undefined;
|
|
17
17
|
|
|
18
18
|
export type ReducerHandler<AppAction> = (dispatch: DispatchFunc<AppAction>) => DispatchFunc<AppAction>;
|
|
19
19
|
|
package/Reducer/wrapDispatch.tsx
CHANGED
|
@@ -23,17 +23,20 @@ export default function wrapDispatch<
|
|
|
23
23
|
const updateDebounceTime = (("testEnv" in window) && window.testEnv) ? 50 : 250;
|
|
24
24
|
|
|
25
25
|
const updateHandler = async (action: A, signal: AbortSignal) => {
|
|
26
|
-
const
|
|
26
|
+
const handlerData = handler(action);
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// If the handler returns nothing, this action shouldn't be processed
|
|
29
|
+
if (!handlerData) return;
|
|
29
30
|
|
|
30
|
-
const
|
|
31
|
+
const {
|
|
32
|
+
bodyJson = {},
|
|
33
|
+
headers = {},
|
|
34
|
+
href,
|
|
35
|
+
method = "POST",
|
|
36
|
+
} = handlerData;
|
|
31
37
|
|
|
32
38
|
try {
|
|
33
|
-
const res = await railsFetchJson<ServerResponse>(
|
|
34
|
-
href,
|
|
35
|
-
{ bodyJson, headers: requestHeaders, method: method || "POST", signal }
|
|
36
|
-
);
|
|
39
|
+
const res = await railsFetchJson<ServerResponse>(href, { bodyJson, headers, method, signal });
|
|
37
40
|
|
|
38
41
|
dispatch({ res, type: "saveComplete" } as A);
|
|
39
42
|
} catch(e) {
|