react-antd-admin-panel
Version:
Easy prototyping admin panel using React and Antd
100 lines • 4.26 kB
JavaScript
import Axios from "./Axios";
import axios from 'axios';
export default class Post extends Axios {
constructor() {
var _a;
super();
this._formatData = (v) => v;
this._formatParams = (v) => v;
this.env = JSON.parse((_a = window.localStorage.getItem('env')) !== null && _a !== void 0 ? _a : '{}');
}
key(v) { this._key = v; return this; }
main(v) { this._main = v; return this; }
parent(v) { this._parent = v; if (this._parent._main)
this._main = this._parent._main; return this; }
formatData(v) { this._formatData = v; return this; }
formatParams(v) { this._formatParams = v; return this; }
copy() {
let post = new Post();
post.main(this._main);
post.target(this._target);
return post;
}
finalize(r, args, action, formula) {
this._success = true;
this._onThen(r, args);
action === null || action === void 0 ? void 0 : action._onComplete(r, args);
formula === null || formula === void 0 ? void 0 : formula.onComplete();
}
submit(args, action, formula) {
if (this._doIf && !this._doIf())
return;
this._onInit();
let target = this.targetBuild(args);
let header = typeof this._header === 'function' ? this._header(args) : this._header;
let params = this._formatParams(formula === null || formula === void 0 ? void 0 : formula.params());
let method = 'post';
if (target.method)
method = target.method.toLowerCase();
if (target.params)
params = Object.assign(Object.assign({}, target.params), params);
if (target.target)
target = target.target;
let settings = {
data: this._formatData(params),
headers: header !== null && header !== void 0 ? header : {},
};
if (this.env.cachePost) {
let store = window.localStorage.getItem(target);
if (store) {
this.finalize(JSON.parse(store), args, action, formula);
return;
}
}
if (method === 'delete') {
// @ts-ignore
axios
.delete(target, settings)
.then((response) => {
try {
if (this.env.cachePost)
window.localStorage.setItem(target, JSON.stringify(response));
}
catch (e) { }
this.finalize(response, args, action, formula);
})
.catch((r) => {
var _a;
if (((_a = r.response) === null || _a === void 0 ? void 0 : _a.status) === 403 && this._main)
this._main.$config.config.access.accessViolationApi(this._main);
this._error = true;
this._onCatch(r, args);
action === null || action === void 0 ? void 0 : action._onError(r, args);
formula === null || formula === void 0 ? void 0 : formula.onError();
});
}
else {
// @ts-ignore
axios
.post(target, params !== null && params !== void 0 ? params : formula === null || formula === void 0 ? void 0 : formula.params(), settings)
.then((response) => {
try {
if (this.env.cachePost)
window.localStorage.setItem(target, JSON.stringify(response));
}
catch (e) { }
this.finalize(response, args, action, formula);
})
.catch((r) => {
var _a;
if (((_a = r.response) === null || _a === void 0 ? void 0 : _a.status) === 403 && this._main)
this._main.$config.config.access.accessViolationApi(this._main);
this._error = true;
this._onCatch(r);
action === null || action === void 0 ? void 0 : action._onError();
formula === null || formula === void 0 ? void 0 : formula.onError();
});
}
}
}
//# sourceMappingURL=Post.js.map