genesys.tsx 2.11 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
import * as React from 'react';
import * as ReactDOM from 'react-dom';
3
import initI18n from "./i18n";
Matija Obreza's avatar
Matija Obreza committed
4
5
6
7
8
9
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';
10
import { Config, DefaultConfig } from "../config/config";
Matija Obreza's avatar
Matija Obreza committed
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

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

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

49
50
  initI18n(language);

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

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