import React from 'react';
import { Spinner } from 'app/shared/components/spinner/spinner';
import { isEmpty } from 'lodash';
import { ErrorsList } from 'app/shared/components/errorMessage/errorsList';

export interface LoadingProps {
  isLoading: boolean;
  isError: any;
}

const withLoading = (Component) => ({ isLoading, isError: errors, ...props }: LoadingProps) => {
  if (!isEmpty(errors)) {
    return <ErrorsList errors={errors} />;
  }
  return isLoading ? <Spinner /> : <Component {...props} />;
};

export { withLoading };
