-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathclient-framework.js
More file actions
98 lines (85 loc) · 2.78 KB
/
client-framework.js
File metadata and controls
98 lines (85 loc) · 2.78 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
import { Component } from "react";
import {redirect} from './client-entry';
import {getSettings} from './client-entry';
import {bindState, validateState, resetState, whitelistState, bindProp, queryString} from './validate-state.js';
import errorMessages from './error-messages';
export {route, ready, redirect, config} from './client-entry';
import dateParser from './date-parser';
export function server(target, key, descriptor) {
const original = descriptor.value;
descriptor.value = function(...args) {
this.setState(function (state, props) {
const loadingState = {...state};
loadingState.loading = {...loadingState.loading, [key]: true}
return loadingState;
});
const body = new FormData();
body.append('method', key);
body.append('state', JSON.stringify(this.state));
body.append('settings', JSON.stringify(this.settings));
args.forEach((arg, index) => {
if(arg instanceof File) {
body.append(`param${index}`, arg);
} else {
body.append(`param${index}`, JSON.stringify(arg));
}
})
body.append('params', args.length);
return new Promise((resolve, reject) => {
fetch(location.href, {
method: 'post',
body: body
}).then(r=>r.text()).then((response) => {
response = JSON.parse(response, dateParser);
this.schema = response.schema;
this.setState(response.state);
this.setState(function (state, props) {
const loadingState = {...state};
loadingState.loading = {...loadingState.loading}
delete loadingState.loading[key];
return loadingState;
});
this.set(response.settings);
resolve(response.returned);
if(response.redirect) {
this.redirect(response.redirect);
}
});
});
};
return descriptor;
}
export class Page extends Component {
settings = getSettings();
state = {};
schema = {};
constructor(props) {
super(props);
this.errorMessages = errorMessages[this.settings.locale];
this.queryString = queryString.bind(this);
this.bindProp = bindProp.bind(this);
this.bindState = bindState.bind(this);
this.validateState = validateState.bind(this);
this.resetState = resetState.bind(this);
this.whitelistState = whitelistState.bind(this);
}
setSession(updates) {
console.error('you cannot set sessions in the client!');
}
redirect(target) {
redirect(target);
}
set(updates) {
this.settings = Object.assign(this.settings, updates);
document.title = `${this.settings.title}`;
}
@server
async uploadImage(recordKey, propertyKey, file) {
}
@server
async uploadFile(recordKey, propertyKey, file) {
}
}
export function ObjectID() {
console.error('you cannot generate an object id in the client!');
}