Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix v2.16.6 #1965

Merged
merged 9 commits into from
Mar 4, 2024
Prev Previous commit
Next Next commit
feat(ui-common): add keepLastResolvedOnReload prop in usePromiseCon…
…d component
skamril committed Mar 4, 2024
commit fa0d3824d086fbb5523543d1506c2010c17ca520
22 changes: 21 additions & 1 deletion webapp/src/components/common/utils/UsePromiseCond.tsx
Original file line number Diff line number Diff line change
@@ -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>) {
@@ -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)}
</>
);