Keep put-form open on error
This commit is contained in:
1 parent
cae54613a2
commit
14c629689f
5 files changed
+282
-174
No files matched your search
+21
-12
@@ -52,13 +52,14 @@ const unpackSmartQuery = memoize(query => {
|
||||
});
|
||||
});
|
||||
|
||||
function putActionHandler(action, object) {
|
||||
function putActionHandler(action, _object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const object = Object.assign({}, _object);
|
||||
if (action === "save") {
|
||||
const file = object["file"] ? object["file"][0] : null;
|
||||
delete object["file"];
|
||||
|
||||
if (!file) return void reject(new Error("File not selected"));
|
||||
if (!file) return void resolve({ file: "File not selected" });
|
||||
|
||||
const id = file.name;
|
||||
|
||||
@@ -67,7 +68,7 @@ function putActionHandler(action, object) {
|
||||
.then(exists => {
|
||||
if (exists) {
|
||||
store.fulfill(0, Date.now());
|
||||
return void reject(new Error("File already exists"));
|
||||
return void resolve({ file: "File already exists" });
|
||||
}
|
||||
const headers = Object.assign(
|
||||
{
|
||||
@@ -277,15 +278,23 @@ function renderTable(
|
||||
Object.assign(
|
||||
{
|
||||
actionHandler: (action, object) => {
|
||||
putActionHandler(action, object)
|
||||
.then(() => {
|
||||
overlay.close(cb);
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
overlay.close(cb);
|
||||
m.redraw();
|
||||
});
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
|
||||
+85
-51
@@ -37,58 +37,62 @@ const unpackSmartQuery = memoize(query => {
|
||||
});
|
||||
});
|
||||
|
||||
function putActionHandler(action, object, isNew) {
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
function putActionHandler(action, _object, isNew) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const object = Object.assign({}, _object);
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
|
||||
if (!id) return void notifications.push("error", "ID can not be empty");
|
||||
const errors = {};
|
||||
|
||||
store
|
||||
.resourceExists("presets", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
notifications.push("error", "Preset already exists");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
if (!id) errors["_id"] = "ID can not be empty";
|
||||
if (!object.provision) errors.provision = "Provision not selected";
|
||||
|
||||
if (!exists && !isNew) {
|
||||
notifications.push("error", "Preset already deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
if (Object.keys(errors).length) return void resolve(errors);
|
||||
|
||||
store
|
||||
.putResource("presets", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Preset ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store
|
||||
.resourceExists("presets", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("presets", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Preset deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
store.fulfill(0, Date.now());
|
||||
});
|
||||
} else {
|
||||
throw new Error("Undefined action");
|
||||
}
|
||||
return void resolve({ _id: "Preset already exists" });
|
||||
}
|
||||
|
||||
if (!exists && !isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
return void resolve({ _id: "Preset does not exist" });
|
||||
}
|
||||
|
||||
store
|
||||
.putResource("presets", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Preset ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
})
|
||||
.catch(reject);
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("presets", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Preset deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
store.fulfill(0, Date.now());
|
||||
});
|
||||
} else {
|
||||
reject(new Error("Undefined action"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const formData = {
|
||||
@@ -220,8 +224,23 @@ function renderTable(
|
||||
{
|
||||
base: preset,
|
||||
actionHandler: (action, object) => {
|
||||
overlay.close(cb);
|
||||
putActionHandler(action, object, false);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, false)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
@@ -323,8 +342,23 @@ function renderTable(
|
||||
Object.assign(
|
||||
{
|
||||
actionHandler: (action, object) => {
|
||||
putActionHandler(action, object, true);
|
||||
overlay.close(cb);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, true)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
|
||||
+82
-53
@@ -32,58 +32,57 @@ const unpackSmartQuery = memoize(query => {
|
||||
});
|
||||
});
|
||||
|
||||
function putActionHandler(action, object, isNew) {
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
function putActionHandler(action, _object, isNew) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const object = Object.assign({}, _object);
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
|
||||
if (!id) return void notifications.push("error", "ID can not be empty");
|
||||
if (!id) return void resolve({ _id: "ID can not be empty" });
|
||||
|
||||
store
|
||||
.resourceExists("provisions", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
notifications.push("error", "Provision already exists");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!exists && !isNew) {
|
||||
notifications.push("error", "Provision already deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
|
||||
store
|
||||
.putResource("provisions", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Provision ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store
|
||||
.resourceExists("provisions", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("provisions", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Provision deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
store.fulfill(0, Date.now());
|
||||
});
|
||||
} else {
|
||||
throw new Error("Undefined action");
|
||||
}
|
||||
return void resolve({ _id: "Provision already exists" });
|
||||
}
|
||||
|
||||
if (!exists && !isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
return void resolve({ _id: "Provision does not exist" });
|
||||
}
|
||||
|
||||
store
|
||||
.putResource("provisions", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Provision ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
})
|
||||
.catch(reject);
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("provisions", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Provision deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
store.fulfill(0, Date.now());
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
reject(new Error("Undefined action"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const formData = {
|
||||
@@ -207,8 +206,23 @@ function renderTable(
|
||||
{
|
||||
base: provision,
|
||||
actionHandler: (action, object) => {
|
||||
overlay.close(cb);
|
||||
putActionHandler(action, object, false);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, false)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
@@ -316,8 +330,23 @@ function renderTable(
|
||||
Object.assign(
|
||||
{
|
||||
actionHandler: (action, object) => {
|
||||
putActionHandler(action, object, true);
|
||||
overlay.close(cb);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, true)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
|
||||
@@ -138,7 +138,9 @@ const component = {
|
||||
onclick: e => {
|
||||
e.redraw = false;
|
||||
e.target.disabled = true;
|
||||
actionHandler("delete", current.object);
|
||||
actionHandler("delete", current.object).then(() => {
|
||||
e.target.disabled = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
"Delete"
|
||||
@@ -155,11 +157,16 @@ const component = {
|
||||
{
|
||||
onsubmit: e => {
|
||||
e.redraw = false;
|
||||
e.target.onsubmit = null;
|
||||
// const onsubmit = e.target.onsubmit;
|
||||
e.preventDefault();
|
||||
for (const elem of e.target.elements) elem.disabled = true;
|
||||
submit.dom.textContent = "Loading ...";
|
||||
actionHandler("save", current.object);
|
||||
// e.target.onsubmit = null;
|
||||
submit.dom.disabled = true;
|
||||
// submit.dom.textContent = "Loading ...";
|
||||
actionHandler("save", current.object).then(() => {
|
||||
// submit.dom.textContent = "Save";
|
||||
// e.target.onsubmit = onsubmit;
|
||||
submit.dom.disabled = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
form
|
||||
|
||||
@@ -32,58 +32,57 @@ const unpackSmartQuery = memoize(query => {
|
||||
});
|
||||
});
|
||||
|
||||
function putActionHandler(action, object, isNew) {
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
function putActionHandler(action, _object, isNew) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const object = Object.assign({}, _object);
|
||||
if (action === "save") {
|
||||
const id = object["_id"];
|
||||
delete object["_id"];
|
||||
|
||||
if (!id) return void notifications.push("error", "ID can not be empty");
|
||||
if (!id) return void resolve({ _id: "ID can not be empty" });
|
||||
|
||||
store
|
||||
.resourceExists("virtualParameters", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
notifications.push("error", "Virtual parameter already exists");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!exists && !isNew) {
|
||||
notifications.push("error", "Virtual parameter already deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
return;
|
||||
}
|
||||
|
||||
store
|
||||
.putResource("virtualParameters", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Virtual parameter ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store
|
||||
.resourceExists("virtualParameters", id)
|
||||
.then(exists => {
|
||||
if (exists && isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("virtualParameters", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Virtual parameter deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
store.fulfill(0, Date.now());
|
||||
});
|
||||
} else {
|
||||
throw new Error("Undefined action");
|
||||
}
|
||||
return void resolve({ _id: "Virtual parameter already exists" });
|
||||
}
|
||||
|
||||
if (!exists && !isNew) {
|
||||
store.fulfill(0, Date.now());
|
||||
return void resolve({ _id: "Virtual parameter does not exist" });
|
||||
}
|
||||
|
||||
store
|
||||
.putResource("virtualParameters", id, object)
|
||||
.then(() => {
|
||||
notifications.push(
|
||||
"success",
|
||||
`Virtual parameter ${exists ? "updated" : "created"}`
|
||||
);
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
})
|
||||
.catch(reject);
|
||||
} else if (action === "delete") {
|
||||
store
|
||||
.deleteResource("virtualParameters", object["_id"])
|
||||
.then(() => {
|
||||
notifications.push("success", "Virtual parameter deleted");
|
||||
store.fulfill(0, Date.now());
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
store.fulfill(0, Date.now());
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
reject(new Error("Undefined action"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const formData = {
|
||||
@@ -208,8 +207,23 @@ function renderTable(
|
||||
{
|
||||
base: virtualParameter,
|
||||
actionHandler: (action, object) => {
|
||||
overlay.close(cb);
|
||||
putActionHandler(action, object, false);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, false)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
@@ -326,8 +340,23 @@ function renderTable(
|
||||
Object.assign(
|
||||
{
|
||||
actionHandler: (action, object) => {
|
||||
putActionHandler(action, object, true);
|
||||
overlay.close(cb);
|
||||
return new Promise(resolve => {
|
||||
putActionHandler(action, object, true)
|
||||
.then(errors => {
|
||||
errors = errors ? Object.values(errors) : [];
|
||||
if (errors.length) {
|
||||
for (const err of errors)
|
||||
notifications.push("error", err);
|
||||
} else {
|
||||
overlay.close(cb);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.catch(err => {
|
||||
notifications.push("error", err.message);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
formData
|
||||
|
||||
Reference in new issue
Block a user