Keep put-form open on error

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2018-12-27 01:21:18 -08:00
1 parent cae54613a2
commit 14c629689f
5 files changed
+167 -59

No files matched your search

+15 -6
View File
@@ -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,14 +278,22 @@ function renderTable(
Object.assign(
{
actionHandler: (action, object) => {
return new Promise(resolve => {
putActionHandler(action, object)
.then(() => {
.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);
overlay.close(cb);
m.redraw();
resolve();
});
});
}
},
+50 -16
View File
@@ -37,26 +37,31 @@ const unpackSmartQuery = memoize(query => {
});
});
function putActionHandler(action, object, isNew) {
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 = {};
if (!id) errors["_id"] = "ID can not be empty";
if (!object.provision) errors.provision = "Provision not selected";
if (Object.keys(errors).length) return void resolve(errors);
store
.resourceExists("presets", id)
.then(exists => {
if (exists && isNew) {
notifications.push("error", "Preset already exists");
store.fulfill(0, Date.now());
return;
return void resolve({ _id: "Preset already exists" });
}
if (!exists && !isNew) {
notifications.push("error", "Preset already deleted");
store.fulfill(0, Date.now());
return;
return void resolve({ _id: "Preset does not exist" });
}
store
@@ -67,28 +72,27 @@ function putActionHandler(action, object, isNew) {
`Preset ${exists ? "updated" : "created"}`
);
store.fulfill(0, Date.now());
resolve();
})
.catch(err => {
notifications.push("error", err.message);
});
.catch(reject);
})
.catch(err => {
notifications.push("error", err.message);
});
.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 => {
notifications.push("error", err.message);
reject(err);
store.fulfill(0, Date.now());
});
} else {
throw new Error("Undefined action");
reject(new Error("Undefined action"));
}
});
}
const formData = {
@@ -220,8 +224,23 @@ function renderTable(
{
base: preset,
actionHandler: (action, object) => {
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);
putActionHandler(action, object, false);
}
resolve();
})
.catch(err => {
notifications.push("error", err.message);
resolve();
});
});
}
},
formData
@@ -323,9 +342,24 @@ function renderTable(
Object.assign(
{
actionHandler: (action, object) => {
putActionHandler(action, object, true);
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
)
+45 -16
View File
@@ -32,26 +32,26 @@ const unpackSmartQuery = memoize(query => {
});
});
function putActionHandler(action, object, isNew) {
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;
return void resolve({ _id: "Provision already exists" });
}
if (!exists && !isNew) {
notifications.push("error", "Provision already deleted");
store.fulfill(0, Date.now());
return;
return void resolve({ _id: "Provision does not exist" });
}
store
@@ -62,28 +62,27 @@ function putActionHandler(action, object, isNew) {
`Provision ${exists ? "updated" : "created"}`
);
store.fulfill(0, Date.now());
resolve();
})
.catch(err => {
notifications.push("error", err.message);
});
.catch(reject);
})
.catch(err => {
notifications.push("error", err.message);
});
.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 => {
notifications.push("error", err.message);
store.fulfill(0, Date.now());
reject(err);
});
} else {
throw new Error("Undefined action");
reject(new Error("Undefined action"));
}
});
}
const formData = {
@@ -207,8 +206,23 @@ function renderTable(
{
base: provision,
actionHandler: (action, object) => {
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);
putActionHandler(action, object, false);
}
resolve();
})
.catch(err => {
notifications.push("error", err.message);
resolve();
});
});
}
},
formData
@@ -316,9 +330,24 @@ function renderTable(
Object.assign(
{
actionHandler: (action, object) => {
putActionHandler(action, object, true);
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
)
+12 -5
View File
@@ -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
+45 -16
View File
@@ -32,26 +32,26 @@ const unpackSmartQuery = memoize(query => {
});
});
function putActionHandler(action, object, isNew) {
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;
return void resolve({ _id: "Virtual parameter already exists" });
}
if (!exists && !isNew) {
notifications.push("error", "Virtual parameter already deleted");
store.fulfill(0, Date.now());
return;
return void resolve({ _id: "Virtual parameter does not exist" });
}
store
@@ -62,28 +62,27 @@ function putActionHandler(action, object, isNew) {
`Virtual parameter ${exists ? "updated" : "created"}`
);
store.fulfill(0, Date.now());
resolve();
})
.catch(err => {
notifications.push("error", err.message);
});
.catch(reject);
})
.catch(err => {
notifications.push("error", err.message);
});
.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 => {
notifications.push("error", err.message);
store.fulfill(0, Date.now());
reject(err);
});
} else {
throw new Error("Undefined action");
reject(new Error("Undefined action"));
}
});
}
const formData = {
@@ -208,8 +207,23 @@ function renderTable(
{
base: virtualParameter,
actionHandler: (action, object) => {
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);
putActionHandler(action, object, false);
}
resolve();
})
.catch(err => {
notifications.push("error", err.message);
resolve();
});
});
}
},
formData
@@ -326,9 +340,24 @@ function renderTable(
Object.assign(
{
actionHandler: (action, object) => {
putActionHandler(action, object, true);
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
)