import React from "react";
import VehiclesListComponent from "./vehicles-list.component";
import { useQuery } from "react-apollo";
import AlertComponent from "../alert/alert.component";
import { QUERY_ALL_VEHICLES } from "../../graphql/vehicles.queries";

export default function VehiclesListContainer() {
  const { loading, error, data, refetch } = useQuery(QUERY_ALL_VEHICLES, {
    fetchPolicy: "network-only"
  });

  if (error) return <AlertComponent message={error.message} type="error" />;
  return (
    <VehiclesListComponent
      loading={loading}
      vehicles={data ? data.vehicles : null}
      refetch={refetch}
    />
  );
}
