CartPage.tsx 5.46 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
1
2
3
4
5
6
7
8
9
10
import React from 'react';

import { AccessionService } from '@genesys/client/service';
import AccessionFilter from '@genesys/client/model/accession/AccessionFilter';
import Accession from '@genesys/client/model/accession/Accession';
import { withTranslation, WithTranslation } from 'react-i18next';
import { LocalStorageCart } from 'utilities';
import { Link } from 'react-router-dom';

interface IAccessionListPageState {
Oleksii Savran's avatar
Oleksii Savran committed
11
  accessions: Array<Accession & Record<string, any>>;
Oleksii Savran's avatar
Oleksii Savran committed
12
  isEmpty: boolean;
Oleksii Savran's avatar
Oleksii Savran committed
13
  selected: string[];
Oleksii Savran's avatar
Oleksii Savran committed
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
49
50
51
52
  isAllSelected: boolean;
}

interface IAccessionListPageProps extends WithTranslation {
  filter?: AccessionFilter;
}

export class AccessionListPage extends React.Component<IAccessionListPageProps, IAccessionListPageState> {
  public constructor(props) {
    super(props);

    this.state = {
      accessions: null,
      isEmpty: false,
      selected: [],
      isAllSelected: false,
    }
  }

  public componentDidMount(): void {
    if (typeof document !== 'undefined') {
      window.addEventListener('storage', this.handleLocalStorageUpdate);
      this.receiveData();
    }
  }

  public componentWillUnmount() {
    if (typeof document !== 'undefined') {
      window.removeEventListener('storage', this.handleLocalStorageUpdate);
    }
  }

  private handleLocalStorageUpdate = (e: StorageEvent) => {
    if (e.key === LocalStorageCart.LS_KEY) {
      this.receiveData();
    }
  };

  private receiveData = () => {
Oleksii Savran's avatar
Oleksii Savran committed
53
54
    const accessionsUUIDds = LocalStorageCart.getCartItemsLS();
    if (accessionsUUIDds.length === 0) {
Oleksii Savran's avatar
Oleksii Savran committed
55
56
      this.setState({ isEmpty: true, accessions: null });
    } else {
Oleksii Savran's avatar
Oleksii Savran committed
57
      this.loadData(accessionsUUIDds);
Oleksii Savran's avatar
Oleksii Savran committed
58
59
60
    }
  };

Oleksii Savran's avatar
Oleksii Savran committed
61
62
63
64
  private loadData = (UUIDs: string[]): Promise<void> => {
    // const cartItems = LocalStorageCart.getCartItemsLS();
    // const map = new Map();
    // cartItems.forEach((item) => map.set(+item.id, item.distributionFormCode));
Oleksii Savran's avatar
Oleksii Savran committed
65
66

    return AccessionService
Oleksii Savran's avatar
Oleksii Savran committed
67
68
69
70
      .listAllByUuid(UUIDs)
      .then((data: Array<Accession & any>) => {
        // console.log('accessions: ', data);
        // data.forEach((accession) => accession.distributionFormCode = map.get(accession.id));
Oleksii Savran's avatar
Oleksii Savran committed
71
72
73
74
75
76
77
78
79
        this.setState({ accessions: data, isEmpty: false });
      })
      .catch((e) => {
        console.log('Api call failed: ', e);
      });
  };

  private toggleRowSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { selected } = this.state;
Oleksii Savran's avatar
Oleksii Savran committed
80
81
    const { uuid } = (e.currentTarget as HTMLElement).dataset;
    const updated = selected.filter((selectedUuid) => selectedUuid !== uuid);
Oleksii Savran's avatar
Oleksii Savran committed
82
    if (updated.length === selected.length) {
Oleksii Savran's avatar
Oleksii Savran committed
83
      updated.push(uuid);
Oleksii Savran's avatar
Oleksii Savran committed
84
85
86
87
88
89
90
    }
    this.setState({ selected: updated });
  };

  private removeItems = () => {
    const { selected, accessions } = this.state;
    LocalStorageCart.removeFromCart(selected);
Oleksii Savran's avatar
Oleksii Savran committed
91
92
    const accessionsUpdated = accessions.filter((acc) => !selected.some((uuid) => uuid === acc.uuid));
    const isEmpty = accessionsUpdated.length === 0;
Oleksii Savran's avatar
Oleksii Savran committed
93
94
95
96
97
98
    this.setState({ selected: [], accessions: isEmpty ? null : accessionsUpdated, isEmpty, isAllSelected: false });
  };

  private onToggleAll = () => {
    this.setState((prevState) => ({
      isAllSelected: !prevState.isAllSelected,
Oleksii Savran's avatar
Oleksii Savran committed
99
      selected: !prevState.isAllSelected ? prevState.accessions.map((acc) => acc.uuid) : [],
Oleksii Savran's avatar
Oleksii Savran committed
100
101
102
103
104
105
    }));
  };

  public render() {
    const { accessions, selected, isEmpty, isAllSelected } = this.state;
    const { t } = this.props;
Oleksii Savran's avatar
Oleksii Savran committed
106
107
    const selectedUUIDs = new Set();
    selected.forEach((uuid) => selectedUUIDs.add(uuid));
Oleksii Savran's avatar
Oleksii Savran committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    return (
      <>
        <h1 className="d-flex justify-content-between align-items-center">
          { t('cart.title') }
          { selected.length !== 0 &&
            <button type="button" className="btn btn-primary" onClick={ this.removeItems }>
              { t('cart.removeFromCart') }
            </button>
          }
        </h1>
        { isEmpty && <div>{ t('cart.isEmpty') }</div> }
        { accessions && (
          <table className="table table-striped">
            <thead className="thead-dark">
              <tr>
                <th>
                  <input
                    type="checkbox"
                    name="select-all"
                    checked={ isAllSelected }
                    onChange={ this.onToggleAll }
                    className="align-middle"
                  />
                </th>
                <th>{ t('accession.model') }</th>
                <th>{ t('accession.accessionName') }</th>
                <th>{ t('accession.countryOfOrigin') }</th>
              </tr>
            </thead>
            <tbody>
Oleksii Savran's avatar
Oleksii Savran committed
139
140
              { accessions.map((a, i) => (
                <tr key={ a.uuid } className={ selectedUUIDs.has(a.uuid) ? 'table-primary' : '' }>
Oleksii Savran's avatar
Oleksii Savran committed
141
142
143
                  <td>
                    <input
                      type="checkbox"
Oleksii Savran's avatar
Oleksii Savran committed
144
145
146
                      name={ `checkbox-${a.uuid}-${i}` }
                      data-uuid={ a.uuid }
                      checked={ selectedUUIDs.has(a.uuid) }
Oleksii Savran's avatar
Oleksii Savran committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                      onChange={ this.toggleRowSelect }
                      className="align-middle"
                    />
                  </td>
                  <td><Link to={ `/a/${a.id}` }>{ a.accessionNumber }</Link></td>
                  <td>{ a.accessionName }</td>
                  <td>{ a.countryOfOrigin && a.countryOfOrigin.name }</td>
                </tr>
              )) }
            </tbody>
          </table>
        ) }
        { !isEmpty && !accessions && <div>{t('common.loading')}...</div> }
      </>
    );
  };
}

export default withTranslation()(AccessionListPage)