Skip to content

Commit

Permalink
fix: use string union over enum for hook states
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Oct 9, 2021
1 parent 91a9381 commit fef36bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ export {
useSinglePrismicDocument,
} from "./hooks";

export { PrismicClientHookState } from "./types";
export type { JSXMapSerializer, JSXFunctionSerializer } from "./types";
export type {
JSXMapSerializer,
JSXFunctionSerializer,
PrismicClientHookState,
} from "./types";
22 changes: 1 addition & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,4 @@ export type JSXMapSerializer = prismicR.RichTextMapSerializer<JSX.Element>;
/**
* States of a `@prismicio/client` hook.
*/
export const enum PrismicClientHookState {
/**
* The hook has not started fetching.
*/
IDLE,

/**
* The hook is fetching data.
*/
PENDING,

/**
* The hook sucessfully fetched data.
*/
SUCCEEDED,

/**
* The hook failed to fetch data.
*/
FAILED,
}
export type PrismicClientHookState = "idle" | "loading" | "loaded" | "failed";
12 changes: 7 additions & 5 deletions src/useStatefulPrismicClientMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ const reducer = <TData>(
): StateMachineState<TData> => {
switch (action[0]) {
case "start": {
return { state: PrismicClientHookState.PENDING };
return { state: "loading" };
}

case "succeed": {
return { state: PrismicClientHookState.SUCCEEDED, data: action[1] };
return { state: "loaded", data: action[1] };
}

case "fail": {
return {
...state,
state: PrismicClientHookState.FAILED,
state: "failed",
error: action[1],
};
}
}
};

const initialState = { state: PrismicClientHookState.IDLE } as const;
const initialState: StateMachineState<never> = {
state: "idle",
};

type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;

Expand Down Expand Up @@ -148,7 +150,7 @@ export const useStatefulPrismicClientMethod = <

React.useEffect(
() => {
if (state.state === PrismicClientHookState.IDLE && !skip) {
if (state.state === "idle" && !skip) {
dispatch(["start"]);
method
.call(client, ...argsWithoutParams, params)
Expand Down

0 comments on commit fef36bc

Please sign in to comment.