-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react-core): update useDataState - add hasError property, resol…
…veMaybeAsync util (#5400)
- Loading branch information
1 parent
f9d1ddd
commit 4fed60c
Showing
3 changed files
with
82 additions
and
21 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
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 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 |
---|---|---|
@@ -1,42 +1,50 @@ | ||
import React from 'react'; | ||
|
||
interface ActionState<T> { | ||
export interface DataState<T> { | ||
data: T; | ||
hasError: boolean; | ||
isLoading: boolean; | ||
message: string | undefined; | ||
} | ||
|
||
const getActionState = <T>(data: T): ActionState<T> => ({ | ||
data, | ||
isLoading: false, | ||
message: undefined, | ||
}); | ||
// default state | ||
const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined }; | ||
const LOADING_STATE = { hasError: false, isLoading: true, message: undefined }; | ||
const ERROR_STATE = { hasError: true, isLoading: false }; | ||
|
||
const resolveMaybeAsync = async <T>( | ||
value: T | Promise<T> | ||
): Promise<Awaited<T>> => { | ||
const awaited = await value; | ||
return awaited; | ||
}; | ||
|
||
export default function useDataState<T, K>( | ||
action: (prevData: Awaited<T>, ...input: K[]) => T | Promise<T>, | ||
initialData: Awaited<T> | ||
): [state: ActionState<Awaited<T>>, handleAction: (...input: K[]) => void] { | ||
const [actionState, setActionState] = React.useState<ActionState<Awaited<T>>>( | ||
() => getActionState(initialData) | ||
); | ||
action: (prevData: T, ...input: K[]) => T | Promise<T>, | ||
initialData: T | ||
): [state: DataState<T>, handleAction: (...input: K[]) => void] { | ||
const [dataState, setDataState] = React.useState<DataState<T>>(() => ({ | ||
...INITIAL_STATE, | ||
data: initialData, | ||
})); | ||
|
||
const prevData = React.useRef(initialData); | ||
|
||
const handleAction: (...input: K[]) => void = React.useCallback( | ||
(...input) => { | ||
setActionState((prev) => ({ ...prev, isLoading: true })); | ||
setDataState(({ data }) => ({ ...LOADING_STATE, data })); | ||
|
||
Promise.resolve(action(prevData.current, ...input)) | ||
.then((data) => { | ||
resolveMaybeAsync(action(prevData.current, ...input)) | ||
.then((data: T) => { | ||
prevData.current = data; | ||
setActionState(getActionState(data)); | ||
setDataState({ ...INITIAL_STATE, data }); | ||
}) | ||
.catch(({ message }: Error) => { | ||
setActionState((prev) => ({ ...prev, isLoading: false, message })); | ||
setDataState(({ data }) => ({ ...ERROR_STATE, data, message })); | ||
}); | ||
}, | ||
[action] | ||
); | ||
|
||
return [actionState, handleAction]; | ||
return [dataState, handleAction]; | ||
} |