-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move hook reducer into hook factory file
- Loading branch information
1 parent
c666519
commit b06e831
Showing
4 changed files
with
128 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import * as React from "react"; | ||
import * as prismic from "@prismicio/client"; | ||
|
||
import { usePrismicClient } from "./usePrismicClient"; | ||
|
||
type PrismicClientError = | ||
| prismic.PrismicError | ||
| prismic.ParsingError | ||
| prismic.ForbiddenError; | ||
|
||
const enum StateMachineStateType { | ||
IDLE, | ||
PENDING, | ||
SUCCEEDED, | ||
FAILED, | ||
} | ||
|
||
export type StateMachineState<TData> = { | ||
state: StateMachineStateType; | ||
data?: TData; | ||
error?: PrismicClientError; | ||
}; | ||
|
||
type StateMachineAction<TData> = | ||
| [type: "start"] | ||
| [type: "succeed", payload: TData] | ||
| [type: "fail", payload: PrismicClientError]; | ||
|
||
const reducer = <TData>( | ||
state: StateMachineState<TData>, | ||
action: StateMachineAction<TData>, | ||
): StateMachineState<TData> => { | ||
if (action[0] === "start") { | ||
return { state: StateMachineStateType.PENDING }; | ||
} else if (action[0] === "succeed") { | ||
return { state: StateMachineStateType.SUCCEEDED, data: action[1] }; | ||
} else if (action[0] === "fail") { | ||
return { ...state, state: StateMachineStateType.FAILED, error: action[1] }; | ||
} | ||
|
||
return state; | ||
}; | ||
|
||
const initialState = { state: StateMachineStateType.IDLE } as const; | ||
|
||
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T; | ||
|
||
type ClientPrototype = typeof prismic.Client.prototype; | ||
|
||
export type ClientMethodParameters<MethodName extends keyof ClientPrototype> = | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ClientPrototype[MethodName] extends (...args: any[]) => any | ||
? Parameters<ClientPrototype[MethodName]> | ||
: never; | ||
|
||
export type HookOnlyParameters = { | ||
client?: prismic.Client; | ||
}; | ||
|
||
const getParamHookDependencies = ( | ||
params: ClientMethodParameters<"get">[0] = {}, | ||
) => { | ||
return [ | ||
params.ref, | ||
params.lang, | ||
params.page, | ||
params.after, | ||
params.fetch, | ||
params.pageSize, | ||
params.orderings, | ||
params.fetchLinks, | ||
params.graphQuery, | ||
params.predicates, | ||
params.accessToken, | ||
]; | ||
}; | ||
|
||
export const createClientHook = < | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
TMethod extends (...args: any[]) => Promise<any>, | ||
TArgs extends Parameters<TMethod>, | ||
>( | ||
method: TMethod, | ||
) => { | ||
return ( | ||
...args: TArgs | ||
): [ | ||
data: StateMachineState<UnwrapPromise<ReturnType<TMethod>>>["data"], | ||
state: Pick< | ||
StateMachineState<UnwrapPromise<ReturnType<TMethod>>>, | ||
"state" | "error" | ||
>, | ||
] => { | ||
const params: | ||
| (ClientMethodParameters<"get">[0] & HookOnlyParameters) | ||
| undefined = args[args.length - 1]; | ||
const client = usePrismicClient(params?.client); | ||
|
||
const [state, dispatch] = React.useReducer< | ||
React.Reducer< | ||
StateMachineState<UnwrapPromise<ReturnType<TMethod>>>, | ||
StateMachineAction<UnwrapPromise<ReturnType<TMethod>>> | ||
> | ||
>(reducer, initialState); | ||
|
||
React.useEffect( | ||
() => { | ||
dispatch(["start"]); | ||
method | ||
.apply(client, args) | ||
.then((result) => dispatch(["succeed", result])) | ||
.catch((error) => dispatch(["fail", error])); | ||
}, | ||
// We must disable exhaustive-deps to optimize providing `params` deps. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[client, ...args.slice(-1), ...getParamHookDependencies(params)], | ||
); | ||
|
||
return React.useMemo( | ||
() => [state.data, { state: state.state, error: state.error }], | ||
[state], | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.