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
@@ -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