AccessionListPage.tsx 6.39 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
4
5
6
7
8
9
10
import React from 'react';
import { Link } from 'react-router-dom';
import { parse } from 'query-string';

import { AccessionService } from '@genesys/client/service';
import AccessionFilter from '@genesys/client/model/accession/AccessionFilter';
import Accession from '@genesys/client/model/accession/Accession';
import FilteredPage, { IPageRequest } from '@genesys/client/model/FilteredPage';
import Pagination from './Pagination';
import { AccessionFilters } from './AccessionFilters';
Matija Obreza's avatar
Matija Obreza committed
11
import { withTranslation, WithTranslation } from 'react-i18next';
Oleksii Savran's avatar
Oleksii Savran committed
12
import { LocalStorageCart } from 'utilities';
Matija Obreza's avatar
Matija Obreza committed
13
14
15
16

interface IAccessionListPageState {
  filter: AccessionFilter;
  accessions: FilteredPage<Accession>;
Oleksii Savran's avatar
Oleksii Savran committed
17
18
  selected: number[];
  isAllSelected: boolean;
Matija Obreza's avatar
Matija Obreza committed
19
20
21
22
23
24
25
}

interface IAccessionListPageProps {
  filter: AccessionFilter;
  location: any;
}

Matija Obreza's avatar
Matija Obreza committed
26
class AccessionListPage extends React.Component<IAccessionListPageProps & WithTranslation, IAccessionListPageState> {
Matija Obreza's avatar
Matija Obreza committed
27
28
29
30
31
32
  public constructor(props) {
    super(props);

    this.state = {
      filter: this.props.filter,
      accessions: null,
Oleksii Savran's avatar
Oleksii Savran committed
33
34
      selected: [],
      isAllSelected: false,
Matija Obreza's avatar
Matija Obreza committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    }
  }

  public componentDidMount() {
    const { current, filterCode } = this.parseLocationSearch(this.props.location);
    this.loadData(filterCode || this.state.filter, current ? { page: current } : {});
  }

  public componentDidUpdate(prevProps) {
    const { current: currentPage, filterCode } = this.parseLocationSearch(this.props.location);
    const { current: prevPage, filterCode: prevFilterCode } = this.parseLocationSearch(prevProps.location);

    if (prevPage !== undefined && currentPage === 0 && prevFilterCode !== undefined && filterCode === undefined) {
      // console.log('did update, reset filter);
      this.loadData(this.props.filter, {});
    }
  }

  private parseLocationSearch = (location) => {
    const current = +parse(location.search).p || 0;
    const filterCode = parse(location.search).filter && parse(location.search).filter.toString();
    // const size = +parse(location.search).l || 20;
    // const sort = parse(location.search).s;
    // const dir = parse(location.search).d;

    return { current, filterCode /* , size, sort, dir, */ };
  };

  private loadData = (filter: string | AccessionFilter, pageR: IPageRequest): Promise<any> => {
    return AccessionService
      .list(filter, pageR)
      .then((data) => {
        // console.log('accessions: ', data);
        this.setState({ accessions: data, filter: data.filter });
        return data;
      })
      .catch((e) => {
        console.log('Api call failed: ', e);
      });
  };

  private applyFilter = (filter: AccessionFilter) => {
    this.loadData({ ...this.state.filter, ...filter }, {});
  };

Oleksii Savran's avatar
Oleksii Savran committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  private toggleRowSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { selected } = this.state;
    const { id } = (e.currentTarget as HTMLElement).dataset;
    const updated = selected.filter((selectedId) => selectedId !== +id);
    if (updated.length === selected.length) {
      updated.push(+id);
    }
    this.setState({ selected: updated });
  };

  private addSelectedToCart = () => {
    LocalStorageCart.addToCart(this.state.selected);
    this.setState({ selected: [], isAllSelected: false });
  };

  private onToggleAll = () => {
    this.setState((prevState) => ({
      isAllSelected: !prevState.isAllSelected,
      selected: !prevState.isAllSelected ? prevState.accessions.content.map((acc) => acc.id) : [],
    }));
  };

Matija Obreza's avatar
Matija Obreza committed
102
  public render() {
Oleksii Savran's avatar
Oleksii Savran committed
103
    const { accessions, selected, isAllSelected } = this.state;
Matija Obreza's avatar
Matija Obreza committed
104
    const { t } = this.props;
Oleksii Savran's avatar
Oleksii Savran committed
105
106
107
    const selectedIds = new Set();
    selected.forEach((id) => selectedIds.add(+id));

Matija Obreza's avatar
Matija Obreza committed
108
109
    if (accessions === null) {
      return (
Matija Obreza's avatar
Matija Obreza committed
110
        <div>{ t('loading') }</div>
Matija Obreza's avatar
Matija Obreza committed
111
112
113
114
      );
    } else {
      return (
        <>
Oleksii Savran's avatar
Oleksii Savran committed
115
116
117
118
119
120
121
122
          <h1 className="d-flex justify-content-between align-items-center">
            { t('estimatedNumberOfItems', { count: accessions.totalElements, what: t('accession.model', { count: accessions.totalElements }) }) }
            { selected.length !== 0 &&
              <button type="button" className="btn btn-primary" onClick={ this.addSelectedToCart }>
                { t('cart.addToCart') }
              </button>
            }
          </h1>
Matija Obreza's avatar
Matija Obreza committed
123
124
125
126
          <AccessionFilters filter={ this.state.filter } applyFilter={ this.applyFilter } key={ `filters-${accessions.filterCode}` }/>
          <Pagination loadData={ this.loadData } paged={ accessions }>
            <table className="table table-striped">
              <thead className="thead-dark">
Oleksii Savran's avatar
Oleksii Savran committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
                <tr>
                  <th>
                    <input
                      type="checkbox"
                      name="select-all"
                      checked={ isAllSelected }
                      onChange={ this.onToggleAll }
                      className="align-middle"
                    />
                  </th>
                  <th>{ t('accession.crop') }</th>
                  <th>{ t('accession.acceNumb') }</th>
                  <th>{ t('accession.accessionName') }</th>
                  <th>{ t('accession.taxonomy') }</th>
                  <th>{ t('accession.countryOfOrigin') }</th>
                  <th>{ t('accession.sampStat') }</th>
                </tr>
Matija Obreza's avatar
Matija Obreza committed
144
145
              </thead>
              <tbody>
Oleksii Savran's avatar
Oleksii Savran committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                { accessions.content.map((a, i) => (
                  <tr key={ a.id } className={ selectedIds.has(a.id) ? 'table-primary' : '' }>
                    <td>
                      <input
                        type="checkbox"
                        name={ `checkbox-${a.id}-${i}` }
                        data-id={ a.id }
                        checked={ selectedIds.has(a.id) }
                        onChange={ this.toggleRowSelect }
                        className="align-middle"
                      />
                    </td>
                    <td>{ a.cropName }</td>
                    <td><Link to={ `/a/${a.uuid}` }>{ a.accessionNumber }</Link></td>
                    <td>{ a.accessionName }</td>
                    <td><span dangerouslySetInnerHTML={ { __html: a.taxonomy.taxonNameHtml } } /></td>
                    <td>{ a.countryOfOrigin && a.countryOfOrigin.name }</td>
                    <td>{ a.sampStat && t(`accession.sampleStatus.${a.sampStat}`) }</td>
                  </tr>
                )) }
Matija Obreza's avatar
Matija Obreza committed
166
167
168
169
170
171
172
173
              </tbody>
            </table>
          </Pagination>
        </>
      );
    }
  };
}
Matija Obreza's avatar
Matija Obreza committed
174
175

export default withTranslation()(AccessionListPage);