genesys.tsx 2.7 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
import * as React from 'react';
import * as ReactDOM from 'react-dom';
Oleksii Savran's avatar
Oleksii Savran committed
3
import initI18n from './i18n';
Matija Obreza's avatar
Matija Obreza committed
4
5
6
7
import { log } from '@genesys/client/utilities/debug';
// import * as cookies from 'es-cookie';

import { reconfigureServiceAxios, LoginService } from '@genesys/client/service';
Oleksii Savran's avatar
Oleksii Savran committed
8
9
import App from 'ui/core/App';
import ApiAccessError from 'ui/core/ApiAccessError';
Oleksii Savran's avatar
Oleksii Savran committed
10
import { Config, DefaultConfig } from '../config/config';
Oleksii Savran's avatar
Oleksii Savran committed
11
import OverviewPage from 'accession/OverviewPage';
Matija Obreza's avatar
Matija Obreza committed
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

// declare const window: Window & { devToolsExtension: any, __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any, initialLanguage: any, initialI18nStore: any, localeMapping: any };
// const AUTH_COOKIE = 'GENESYS_AUTH';

console.log('Loading Genesys UI library');

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

  const appLogin = LoginService.loginApp(clientId, clientSecret, apiUrl)
    .then((data) => {
      console.log('loginApp token', data);
      reconfigureServiceAxios({ accessToken: data.access_token });

      // const expDate = new Date(data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
      // log(`Saving cookies to expire on ${expDate}`);
      // cookies.set('access_token', data.access_token, { path: '/', expires: expDate });

      return true;
    })
    .catch((error) => {
      log('Something went wrong', error);
      throw error;
    });

Maksym Tishchenko's avatar
Maksym Tishchenko committed
37
38
39
40
41
42
43
  // if (cookieToken) {
  //   return Promise.resolve(true);
  // } else {
  //   cookies.remove(AUTH_COOKIE);
  //   return appLogin;
  // }
  return appLogin;
Matija Obreza's avatar
Matija Obreza committed
44
45
};

Oleksii Savran's avatar
Oleksii Savran committed
46
export function showGenesysUI(holdingNode: HTMLElement, config: Config = DefaultConfig, language: string = 'en') {
47
  const { apiUrl, clientId, clientKey, filter } = config;
Matija Obreza's avatar
Matija Obreza committed
48
49
  reconfigureServiceAxios({ apiUrl });

50
51
  initI18n(language);

52
  checkAccessTokens(apiUrl, clientId, clientKey).then( (result) => {
Matija Obreza's avatar
Matija Obreza committed
53
54
55
56
57
58
59
60
61
62
63
64
65

    ReactDOM.render(
      <App filter={ filter } apiUrl={ apiUrl } />,
      holdingNode,
    );
  }).catch((err) => {
    log('Oh, oh', err);
    ReactDOM.render(
      <ApiAccessError error={ err } apiUrl={ apiUrl } />,
      holdingNode,
    );
  });
}
Matija Obreza's avatar
Matija Obreza committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


export function showOverview(holdingNode: HTMLElement, config: Config = DefaultConfig, language: string = 'en') {
  const { apiUrl, clientId, clientKey, filter } = config;
  reconfigureServiceAxios({ apiUrl });

  initI18n(language);

  checkAccessTokens(apiUrl, clientId, clientKey).then( (result) => {

    ReactDOM.render(
      <OverviewPage filter={ filter } />,
      holdingNode,
    );
  }).catch((err) => {
    log('Oh, oh', err);
    ReactDOM.render(
      <ApiAccessError error={ err } apiUrl={ apiUrl } />,
      holdingNode,
    );
  });
}