summaryrefslogtreecommitdiff
path: root/src/lib/ServiceList.tsx
blob: be9a31118326d4f55ff978a19a9ce34d00e36f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { useContext } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import _ from 'lodash';
import Page from '../containers/Page';
import ListTable from '../components/ListTable';
import hooks from '../hooks/useAPIClient';
import ServiceContext from './ServiceContext';
import ServiceFilters from './ServiceFilters';
import ServiceSearch from './ServiceSearch';

const ServiceList: React.FC = () => {
  const service = useContext(ServiceContext);
  const history = useHistory();
  const location = useLocation();
  const { data } = hooks[service.route].useList(location.search);

  const actions = service.actions || [{
    name: 'Добавить',
    route: `/${service.route}/add${location.search}`,
  }];

  const handleRowClick = (item: any) => {
    const route = service.rowLink
      ? service.rowLink(item)
      : `/${service.route}/${item?._id}`;

    history.push(route);
  };

  const filters = (
    <>
      {service.filters && <ServiceFilters />}
      {service.searchBy && <ServiceSearch />}
    </>
  );

  return (
    <Page
      title={service.name}
      actions={actions}
      filters={filters}
    >
      <ListTable items={data} fields={service.tableFields} handleRowClick={handleRowClick} />
    </Page>
  );
};

export default ServiceList;