2022-03-02 01:08:32 +00:00
|
|
|
import { OperationVariables, QueryHookOptions, useQuery } from '@apollo/client';
|
|
|
|
import classNames from 'classnames';
|
2022-03-02 01:01:10 +00:00
|
|
|
import { DocumentNode } from 'graphql';
|
|
|
|
import { ReactNode } from 'react';
|
|
|
|
|
|
|
|
interface PageQueryContainerProps<TData, TVariables> {
|
|
|
|
query: DocumentNode;
|
|
|
|
options?: QueryHookOptions<TData, TVariables>;
|
|
|
|
children: (data: TData) => ReactNode;
|
|
|
|
}
|
|
|
|
|
2022-03-02 01:08:32 +00:00
|
|
|
export const PageQueryContainer = <TData, TVariables = OperationVariables>({
|
2022-03-02 01:01:10 +00:00
|
|
|
query,
|
|
|
|
options,
|
|
|
|
children,
|
|
|
|
}: PageQueryContainerProps<TData, TVariables>) => {
|
|
|
|
const { data, loading, error } = useQuery<TData, TVariables>(query, options);
|
2022-03-02 01:08:32 +00:00
|
|
|
const splashClasses = classNames(
|
|
|
|
'w-full h-full',
|
|
|
|
'flex items-center justify-center'
|
|
|
|
);
|
2022-03-02 01:01:10 +00:00
|
|
|
|
|
|
|
if (loading || !data) {
|
2022-03-02 01:08:32 +00:00
|
|
|
return <div className={splashClasses}>Loading...</div>;
|
2022-03-02 01:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2022-03-02 01:08:32 +00:00
|
|
|
return (
|
|
|
|
<div className={splashClasses}>Something went wrong: {error.message}</div>
|
|
|
|
);
|
2022-03-02 01:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return <>{children(data)}</>;
|
|
|
|
};
|