import React, { useEffect } from "react";
import CourtesyCarsPageComponent from "./courtesy-cars.page.component";
import { useQuery } from "@apollo/react-hooks";
import AlertComponent from "../../components/alert/alert.component";
import { QUERY_ALL_CC } from "../../graphql/courtesy-car.queries";
import { setBreadcrumbs } from "../../redux/application/application.actions";
import { connect } from "react-redux";
import { useTranslation } from "react-i18next";

const mapDispatchToProps = (dispatch) => ({
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs)),
});

export function CourtesyCarsPageContainer({ setBreadcrumbs }) {
  const { loading, error, data, refetch } = useQuery(QUERY_ALL_CC);
  const { t } = useTranslation();
  useEffect(() => {
    document.title = t("titles.courtesycars");

    setBreadcrumbs([
      { link: "/manage/courtesycars", label: t("titles.bc.courtesycars") },
    ]);
  }, [setBreadcrumbs, t]);
  if (error) return <AlertComponent message={error.message} type='error' />;
  return (
    <CourtesyCarsPageComponent
      loading={loading}
      data={(data && data.courtesycars) || []}
      refetch={refetch}
    />
  );
}

export default connect(null, mapDispatchToProps)(CourtesyCarsPageContainer);
