genesys.tsx 2.21 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
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
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;
};

44
45
46
47
48
49
class Config {
  public apiUrl: string = 'https://api.sandbox.genesys-pgr.org';
  public clientId: string = 'clientid@genesys';
  public clientKey: string = 'changeme';
  public filter: object = {};
}
Matija Obreza's avatar
Matija Obreza committed
50

51
export function showGenesysUI(holdingNode: HTMLElement, { apiUrl, clientId, clientKey, filter }: Config, language: string = "en") {
Matija Obreza's avatar
Matija Obreza committed
52
53
  reconfigureServiceAxios({ apiUrl });

54
55
  initI18n(language);

56
  checkAccessTokens(apiUrl, clientId, clientKey).then( (result) => {
Matija Obreza's avatar
Matija Obreza committed
57
58
59
60
61
62
63
64
65
66
67
68
69

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