genesys.tsx 1.99 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
import * as React from 'react';
import * as ReactDOM from 'react-dom';
Maksym Tishchenko's avatar
Maksym Tishchenko committed
3
import "./i18n";
Matija Obreza's avatar
Matija Obreza committed
4
5
6
7
8
9
10
11
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
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
import { log } from '@genesys/client/utilities/debug';
// import * as cookies from 'es-cookie';

import { reconfigureServiceAxios, LoginService } from '@genesys/client/service';
import App from './ui/App';
import ApiAccessError from './ui/ApiAccessError';

// 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;
    });

    // if (cookieToken) {
    //   return Promise.resolve(true);
    // } else {
    //   cookies.remove(AUTH_COOKIE);
    //   return appLogin;
    // }
    return appLogin;
};


export function showGenesysUI(apiUrl: string, clientId: string, clientSecret: string, holdingNode: HTMLElement, filter: object = {}) {
  reconfigureServiceAxios({ apiUrl });

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

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