Files
wlan-cloud-ui/app/containers/QueryWrapper/index.js
2020-12-07 17:50:28 -05:00

26 lines
766 B
JavaScript

import React from 'react';
import { Alert } from 'antd';
import { Loading } from '@tip-wlan/wlan-cloud-ui-library';
import { useQuery } from '@apollo/client';
import { Redirect } from 'react-router-dom';
export const withQuery = (Comp, query, getVariables) => props => {
const { loading, error, data, refetch, fetchMore } = useQuery(query, {
variables: getVariables(),
});
if (loading) {
return <Loading />;
}
if (error) {
if (error.message === '403: Forbidden' || error.message === '401: Unauthorized') {
return <Redirect to="/login" />;
}
return <Alert message="Error" description="Failed to load profiles." type="error" showIcon />;
}
return <Comp {...props} data={data} fetchMore={fetchMore} refetch={refetch} />;
};