-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJS_GetFeedbackStorageObject.js
More file actions
101 lines (99 loc) · 3.51 KB
/
JS_GetFeedbackStorageObject.js
File metadata and controls
101 lines (99 loc) · 3.51 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";
import AsyncStorage from '@react-native-async-storage/async-storage';
// BEGIN EXTRA CODE
// END EXTRA CODE
/**
* @param {string} key
* @param {string} entity
* @returns {Promise.<MxObject>}
*/
export async function JS_GetFeedbackStorageObject(key, entity) {
// BEGIN USER CODE
if (!key) {
return Promise.reject(new Error("Input parameter 'Key' is required"));
}
if (!entity) {
return Promise.reject(new Error("Input parameter 'Entity' is required"));
}
return getItem(key).then(result => {
if (result === null) {
return Promise.reject(new Error(`Storage item '${key}' does not exist`));
}
const value = JSON.parse(result);
return getOrCreateMxObject(entity, value).then(newObject => {
const newValue = serializeMxObject(newObject);
return setItem(key, JSON.stringify(newValue)).then(() => newObject);
});
});
function getItem(key) {
if (navigator && navigator.product === "ReactNative") {
return AsyncStorage.getItem(key);
}
if (window) {
const value = window.localStorage.getItem(key);
return Promise.resolve(value);
}
return Promise.reject(new Error("No storage API available"));
}
function setItem(key, value) {
if (navigator && navigator.product === "ReactNative") {
return AsyncStorage.setItem(key, value);
}
if (window) {
window.localStorage.setItem(key, value);
return Promise.resolve();
}
return Promise.reject(new Error("No storage API available"));
}
function getOrCreateMxObject(entity, value) {
return getMxObject(value.guid).then(existingObject => {
if (existingObject) {
return existingObject;
}
else {
return createMxObject(entity, value);
}
});
}
function getMxObject(guid) {
return new Promise((resolve, reject) => {
mx.data.get({
guid,
callback: mxObject => resolve(mxObject),
error: error => reject(error)
});
});
}
function createMxObject(entity, value) {
return new Promise((resolve, reject) => {
mx.data.create({
entity,
callback: mxObject => {
Object.keys(value)
.filter(attribute => attribute !== "guid")
.forEach(attributeName => {
const attributeValue = value[attributeName];
mxObject.set(attributeName, attributeValue);
});
resolve(mxObject);
},
error: () => reject(new Error(`Could not create '${entity}' object`))
});
});
}
function serializeMxObject(object) {
return object.getAttributes().reduce((accumulator, attributeName) => {
accumulator[attributeName] = object.get(attributeName);
return accumulator;
}, { guid: object.getGuid() });
}
// END USER CODE
}