CartPage.tsx 5.71 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
1
2
3
4
5
6
7
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';
Oleksii Savran's avatar
Oleksii Savran committed
8
import { Link, RouteComponentProps } from 'react-router-dom';
Oleksii Savran's avatar
Oleksii Savran committed
9

Oleksii Savran's avatar
Oleksii Savran committed
10
interface ICartPageState {
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
  isAllSelected: boolean;
}

Oleksii Savran's avatar
Oleksii Savran committed
17
interface ICartPageProps extends WithTranslation, RouteComponentProps {
Oleksii Savran's avatar
Oleksii Savran committed
18
19
20
  filter?: AccessionFilter;
}

Oleksii Savran's avatar
Oleksii Savran committed
21
class CartPage extends React.Component<ICartPageProps, ICartPageState> {
Oleksii Savran's avatar
Oleksii Savran committed
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
  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
    }));
  };

Oleksii Savran's avatar
Oleksii Savran committed
103
104
105
106
107
  private onRequest = () => {
    const { history } = this.props;
    history.push('/request');
  };

Oleksii Savran's avatar
Oleksii Savran committed
108
109
110
  public render() {
    const { accessions, selected, isEmpty, isAllSelected } = this.state;
    const { t } = this.props;
Oleksii Savran's avatar
Oleksii Savran committed
111
112
    const selectedUUIDs = new Set();
    selected.forEach((uuid) => selectedUUIDs.add(uuid));
Oleksii Savran's avatar
Oleksii Savran committed
113
114
115
116
117

    return (
      <>
        <h1 className="d-flex justify-content-between align-items-center">
          { t('cart.title') }
Oleksii Savran's avatar
Oleksii Savran committed
118
119
120
121
122
          { selected.length === 0 ? (
            <button type="button" className="btn btn-primary" onClick={ this.onRequest }>
              { t('cart.request') }
            </button>
          ) : (
Oleksii Savran's avatar
Oleksii Savran committed
123
124
125
            <button type="button" className="btn btn-primary" onClick={ this.removeItems }>
              { t('cart.removeFromCart') }
            </button>
Oleksii Savran's avatar
Oleksii Savran committed
126
          ) }
Oleksii Savran's avatar
Oleksii Savran committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        </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
148
149
              { accessions.map((a, i) => (
                <tr key={ a.uuid } className={ selectedUUIDs.has(a.uuid) ? 'table-primary' : '' }>
Oleksii Savran's avatar
Oleksii Savran committed
150
151
152
                  <td>
                    <input
                      type="checkbox"
Oleksii Savran's avatar
Oleksii Savran committed
153
154
155
                      name={ `checkbox-${a.uuid}-${i}` }
                      data-uuid={ a.uuid }
                      checked={ selectedUUIDs.has(a.uuid) }
Oleksii Savran's avatar
Oleksii Savran committed
156
157
158
159
                      onChange={ this.toggleRowSelect }
                      className="align-middle"
                    />
                  </td>
Matija Obreza's avatar
Matija Obreza committed
160
                  <td><Link to={ `/a/${a.uuid}` }>{ a.accessionNumber }</Link></td>
Oleksii Savran's avatar
Oleksii Savran committed
161
162
163
164
165
166
167
168
169
170
171
172
173
                  <td>{ a.accessionName }</td>
                  <td>{ a.countryOfOrigin && a.countryOfOrigin.name }</td>
                </tr>
              )) }
            </tbody>
          </table>
        ) }
        { !isEmpty && !accessions && <div>{t('common.loading')}...</div> }
      </>
    );
  };
}

Oleksii Savran's avatar
Oleksii Savran committed
174
export default withTranslation()(CartPage)