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

Add Updating to Cells #2641

Merged
merged 5 commits into from
Jun 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions packages/web/src/components/createCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export type CellLoadingProps = Omit<
'error' | 'loading' | 'data'
>
// @MARK not sure about this partial, but we need to do this for tests and storybook
// `updating` is just `loading` renamed; since Cells default to stale-while-refetch,
// this prop lets users render something like a spinner to show that a request is in-flight
export type CellSuccessProps<TData = any> = Partial<
Omit<QueryOperationResult<TData>, 'error' | 'loading' | 'data'>
> &
A.Compute<TData> // pre-computing makes the types more readable on hover
Omit<QueryOperationResult<TData>, 'error' | 'data'>
> & { updating: boolean } & A.Compute<TData> // pre-computing makes the types more readable on hover

export interface CreateCellProps<CellProps> {
beforeQuery?: <TProps>(props: TProps) => { variables: TProps }
Expand Down Expand Up @@ -96,7 +97,7 @@ export function createCell<CellProps = any>({
beforeQuery = (props) => ({
variables: props,
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
notifyOnNetworkStatusChange: true,
}),
QUERY,
afterQuery = (data) => ({ ...data }),
Expand Down Expand Up @@ -131,14 +132,22 @@ export function createCell<CellProps = any>({
} else {
throw error
}
} else if (loading) {
return <Loading {...queryRest} {...props} />
} else if (data) {
if (typeof Empty !== 'undefined' && isEmpty(data)) {
return <Empty {...queryRest} {...props} />
return (
<Empty {...{ updating: loading, ...queryRest }} {...props} />
)
} else {
return <Success {...afterQuery(data)} {...queryRest} {...props} />
return (
<Success
{...afterQuery(data)}
{...{ updating: loading, ...queryRest }}
{...props}
/>
)
}
} else if (loading) {
return <Loading {...queryRest} {...props} />
} else {
throw new Error(
'Cannot render cell: GraphQL success but `data` is null'
Expand Down