react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
145 lines (144 loc) • 3.61 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
class Formula {
constructor(post) {
__publicField(this, "_values", /* @__PURE__ */ new Map());
__publicField(this, "_post");
__publicField(this, "_action", null);
__publicField(this, "_callbacks", {});
__publicField(this, "_isSubmitting", false);
this._post = post;
}
/**
* Register or update a value in the formula
* @param key - Field key
* @param value - Field value
* @param reset - Optional reset callback
*/
value(key, value, reset) {
this._values.set(key, { value, reset });
return this;
}
/**
* Get a single value by key
*/
get(key) {
var _a;
return (_a = this._values.get(key)) == null ? void 0 : _a.value;
}
/**
* Check if a value is registered
*/
has(key) {
return this._values.has(key);
}
/**
* Remove a value from the formula
*/
remove(key) {
this._values.delete(key);
return this;
}
/**
* Get all collected values as an object
*/
params() {
const result = {};
this._values.forEach((entry, key) => {
result[key] = entry.value;
});
return result;
}
/**
* Reset all registered values
*/
reset() {
this._values.forEach((entry) => {
var _a;
(_a = entry.reset) == null ? void 0 : _a.call(entry);
});
this._values.clear();
return this;
}
/**
* Link an action to this formula
*/
action(action) {
this._action = action;
return this;
}
/**
* Get the linked action
*/
getAction() {
return this._action;
}
/**
* Get the Post model
*/
getPost() {
return this._post;
}
/**
* Set callback for successful completion
*/
onComplete(callback) {
this._callbacks.onComplete = callback;
return this;
}
/**
* Set callback for errors
*/
onError(callback) {
this._callbacks.onError = callback;
return this;
}
/**
* Set callback for finally (always runs)
*/
onFinally(callback) {
this._callbacks.onFinally = callback;
return this;
}
/**
* Check if formula is currently submitting
*/
isSubmitting() {
return this._isSubmitting;
}
/**
* Submit the formula via Post
* @param additionalData - Additional data to merge with form values
*/
async submit(additionalData) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
if (this._isSubmitting) {
return void 0;
}
this._isSubmitting = true;
try {
const body = {
...this.params(),
...additionalData
};
this._post.body(body);
const result = await this._post.execute();
(_b = (_a = this._callbacks).onComplete) == null ? void 0 : _b.call(_a, result);
(_d = (_c = this._action) == null ? void 0 : _c.callComplete) == null ? void 0 : _d.call(_c, result);
return result;
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
(_f = (_e = this._callbacks).onError) == null ? void 0 : _f.call(_e, err);
(_h = (_g = this._action) == null ? void 0 : _g.callError) == null ? void 0 : _h.call(_g, err);
throw err;
} finally {
this._isSubmitting = false;
(_j = (_i = this._callbacks).onFinally) == null ? void 0 : _j.call(_i);
}
}
}
export {
Formula
};
//# sourceMappingURL=index.js.map