index.ts 2.76 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
4
import * as cookies from 'es-cookie';
import { LoginService, reconfigureServiceAxios } from '@genesys/client/service';
import { clearCookies, saveCookies } from '@genesys/client/utilities';
import { log } from '@genesys/client/utilities/debug';
Matija Obreza's avatar
Matija Obreza committed
5
import Accession from '@genesys/client/model/accession/Accession';
Matija Obreza's avatar
Matija Obreza committed
6
7
8
9
10
11
12
13
14
15


export function checkAccessTokens(apiUrl: string, clientId: string, clientSecret: string) {
  const cookieToken: string = typeof window !== 'undefined' && cookies.get('access_token');

  const applicationLogin = () => {
    LoginService.loginApp(clientId, clientSecret, apiUrl)
      .then((data) => {
        console.log('loginApp token', data);
        saveCookies(
Oleksii Savran's avatar
Oleksii Savran committed
16
          { access_token: data.access_token, authorities: 'ROLE_CLIENT' },
Matija Obreza's avatar
Matija Obreza committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
          data.exp * 1000 || new Date().getTime() + data.expires_in * 1000,
          apiUrl,
        );
        return reconfigureServiceAxios({ accessToken: data.access_token });
      })
      .catch((error) => {
        log('Something went wrong', error);
      });
  };

  if (cookieToken) {
    return Promise.resolve();
  } else {
    clearCookies();
  }

  return applicationLogin();
}
Oleksii Savran's avatar
Oleksii Savran committed
35
36
37
38

export class LocalStorageCart {
  public static LS_KEY = 'ui-embedded-cart';

Oleksii Savran's avatar
Oleksii Savran committed
39
40
41
  public static getCartItemsLS = () => {
    const items = JSON.parse(localStorage.getItem(LocalStorageCart.LS_KEY));
    return items && items.length ? items : [];
Oleksii Savran's avatar
Oleksii Savran committed
42
43
  };

Maksym Tishchenko's avatar
Maksym Tishchenko committed
44
45
  public static addToCart = (what: string | string[]) => {
    // console.log('add to cart: ', what);
Oleksii Savran's avatar
Oleksii Savran committed
46
47
    const cartUuids = LocalStorageCart.getCartItemsLS();
    if (cartUuids.length === 0) {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
48
      return localStorage.setItem(LocalStorageCart.LS_KEY, JSON.stringify(Array.isArray(what) ? what : [what]));
Oleksii Savran's avatar
Oleksii Savran committed
49
    }
Oleksii Savran's avatar
Oleksii Savran committed
50
    const uuidsSet = new Set(cartUuids);
Oleksii Savran's avatar
Oleksii Savran committed
51

Maksym Tishchenko's avatar
Maksym Tishchenko committed
52
53
54
55
56
57
58
59
60
61
62
    if (typeof what === 'string') {
      uuidsSet.add(what);
    }
    if (Array.isArray(what)) {
      what.forEach((id) => {
        if (uuidsSet.has(id)) {
          return;
        }
        uuidsSet.add(id);
      });
    }
Oleksii Savran's avatar
Oleksii Savran committed
63
    const cart = [];
Oleksii Savran's avatar
Oleksii Savran committed
64
    uuidsSet.forEach((id) => cart.push(id));
Oleksii Savran's avatar
Oleksii Savran committed
65
66
67
    localStorage.setItem(LocalStorageCart.LS_KEY, JSON.stringify(cart));
  };

Maksym Tishchenko's avatar
Maksym Tishchenko committed
68
  public static removeFromCart = (what: string | string[]) => {
Oleksii Savran's avatar
Oleksii Savran committed
69
    const cartUuids = LocalStorageCart.getCartItemsLS();
Maksym Tishchenko's avatar
Maksym Tishchenko committed
70
71
72
73
74
75
76
    let updatedCartItems;
    if (typeof what === 'string') {
      updatedCartItems = cartUuids.filter((cartUuid) => cartUuid !== what);
    }
    if (typeof what === 'object') {
      updatedCartItems = cartUuids.filter((cartUuid) => !what.some((uuidToRemove) => cartUuid === uuidToRemove));
    }
Oleksii Savran's avatar
Oleksii Savran committed
77
78
79
    localStorage.setItem(LocalStorageCart.LS_KEY, JSON.stringify(updatedCartItems));
  }
}
Matija Obreza's avatar
Matija Obreza committed
80
81
82
83

export const canAddToCart = (accession: Accession) => {
  return accession.historic !== true && accession.available !== false && accession.institute.allowMaterialRequests === true;
}