Skip to content

Commit

Permalink
feat(ui-common): add keepLastResolvedOnReload prop in usePromiseCon…
Browse files Browse the repository at this point in the history
…d component
  • Loading branch information
skamril committed Mar 4, 2024
1 parent 7721747 commit fa0d382
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion webapp/src/components/common/utils/UsePromiseCond.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface UsePromiseCondProps<T> {
ifPending?: () => React.ReactNode;
ifRejected?: (error: Response["error"]) => React.ReactNode;
ifResolved?: (data: T) => React.ReactNode;
keepLastResolvedOnReload?: boolean;
}

function UsePromiseCond<T>(props: UsePromiseCondProps<T>) {
Expand All @@ -49,21 +50,40 @@ function UsePromiseCond<T>(props: UsePromiseCondProps<T>) {
ifPending = () => <SimpleLoader />,
ifRejected = (error) => <SimpleContent title={error?.toString()} />,
ifResolved,
keepLastResolvedOnReload = false,
} = props;
const { status, data, error } = response;

////////////////////////////////////////////////////////////////
// Utils
////////////////////////////////////////////////////////////////

const hasToKeepLastResolved = () => {
return data !== undefined && keepLastResolvedOnReload;
};

////////////////////////////////////////////////////////////////
// JSX
////////////////////////////////////////////////////////////////

return (
<>
{R.cond([
// Resolved
[
R.either(R.equals(PromiseStatus.Resolved), hasToKeepLastResolved),
() => ifResolved?.(data as T),
],
// Pending
[
R.either(
R.equals(PromiseStatus.Idle),
R.equals(PromiseStatus.Pending),
),
() => ifPending(),
],
// Rejected
[R.equals(PromiseStatus.Rejected), () => ifRejected(error)],
[R.equals(PromiseStatus.Resolved), () => ifResolved?.(data as T)],
])(status)}
</>
);
Expand Down

0 comments on commit fa0d382

Please sign in to comment.