2020-06-06 00:03:37 +02:00
|
|
|
"use strict";
|
2020-05-04 04:53:28 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const router = require("../router.js");
|
|
|
|
const api = require("../api.js");
|
|
|
|
const misc = require("../util/misc.js");
|
|
|
|
const uri = require("../util/uri.js");
|
|
|
|
const PoolCategoryList = require("../models/pool_category_list.js");
|
|
|
|
const PoolCreateView = require("../views/pool_create_view.js");
|
|
|
|
const EmptyView = require("../views/empty_view.js");
|
2020-05-04 04:53:28 +02:00
|
|
|
|
|
|
|
class PoolCreateController {
|
|
|
|
constructor(ctx) {
|
2020-06-06 00:03:37 +02:00
|
|
|
if (!api.hasPrivilege("pools:create")) {
|
2020-05-04 04:53:28 +02:00
|
|
|
this._view = new EmptyView();
|
2020-06-06 00:03:37 +02:00
|
|
|
this._view.showError("You don't have privileges to create pools.");
|
2020-05-04 04:53:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
PoolCategoryList.get().then(
|
|
|
|
(poolCategoriesResponse) => {
|
|
|
|
const categories = {};
|
|
|
|
for (let category of poolCategoriesResponse.results) {
|
|
|
|
categories[category.name] = category.name;
|
|
|
|
}
|
2020-05-04 04:53:28 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
this._view = new PoolCreateView({
|
|
|
|
canCreate: api.hasPrivilege("pools:create"),
|
|
|
|
categories: categories,
|
|
|
|
escapeColons: uri.escapeColons,
|
|
|
|
});
|
2020-05-04 04:53:28 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
this._view.addEventListener("submit", (e) =>
|
|
|
|
this._evtCreate(e)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(error.message);
|
|
|
|
}
|
|
|
|
);
|
2020-05-04 04:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtCreate(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
2020-06-06 00:03:37 +02:00
|
|
|
api.post(uri.formatApiLink("pool"), e.detail).then(
|
|
|
|
() => {
|
2020-05-04 04:53:28 +02:00
|
|
|
this._view.clearMessages();
|
|
|
|
misc.disableExitConfirmation();
|
2020-06-06 00:03:37 +02:00
|
|
|
const ctx = router.show(uri.formatClientLink("pools"));
|
|
|
|
ctx.controller.showSuccess("Pool created.");
|
|
|
|
},
|
|
|
|
(error) => {
|
2020-05-04 04:53:28 +02:00
|
|
|
this._view.showError(error.message);
|
|
|
|
this._view.enableForm();
|
2020-06-06 00:03:37 +02:00
|
|
|
}
|
|
|
|
);
|
2020-05-04 04:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
module.exports = (router) => {
|
|
|
|
router.enter(["pool", "create"], (ctx, next) => {
|
|
|
|
ctx.controller = new PoolCreateController(ctx, "create");
|
2020-05-04 04:53:28 +02:00
|
|
|
});
|
|
|
|
};
|