-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-lib.js
More file actions
60 lines (51 loc) · 1.97 KB
/
common-lib.js
File metadata and controls
60 lines (51 loc) · 1.97 KB
1
2
3
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
import { CookieUtils } from './CookieUtils.js';
let model;
// following the APP GUIDELINES: https://api.pryv.com/guides/app-guidelines/
export const serviceInfoUrl = HDSLib.pryv.Browser.serviceInfoFromUrl() || "https://demo.datasafe.dev/reg/service/info";
// ---- state management to be replaced by framework logic
const apps = {
client: null,
managing: null
};
/**
*
* @param {*} app - AppClientAccount or AppManagingAccount (null to delete)
* @param {string} type 'client' or 'managing'
*/
export function stateSaveApp (type, app) {
console.log('## stateSaveApp ', app);
const navData = app ? {
apiEndpoint: app.connection.apiEndpoint,
streamId: app.baseStreamId,
name: app.appName
} : null;
stateSetData(navData, 'hds-app-' + type + '-app');
apps[type] = app;
}
export async function stateGetApp (type) {
if (apps[type]) return apps[type];
const navData = stateGetData('hds-app-' + type + '-app');
if (navData !== null) {
if (type === 'client') {
apps.client = await HDSLib.appTemplates.AppClientAccount.newFromApiEndpoint(navData.streamId, navData.apiEndpoint, navData.name);
} else {
apps.managing = await HDSLib.appTemplates.AppManagingAccount.newFromApiEndpoint(navData.streamId, navData.apiEndpoint, navData.name);
}
}
console.log('## stateGetApp ', type, apps[type]);
return apps[type];
}
const COOKIE_KEY = 'hds-app-client-de';
export function stateSetData(data, cookieKey = COOKIE_KEY) {
if (data == null) return CookieUtils.del(cookieKey, '/');
CookieUtils.set(cookieKey, data, 365, '/');
// debug
const debugData = stateGetData(cookieKey);
console.log('## stateSetData ', {cookieKey, data, debugData});
}
export function stateGetData(cookieKey = COOKIE_KEY) {
const cookieContent = CookieUtils.get(cookieKey);
console.log('## stateGetData ', { cookieKey, cookieContent });
const formKey = (new URLSearchParams(window.location.search)).get('formKey');
return Object.assign({ formKey }, cookieContent);
}