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
+282 -174

No files matched your search

+82 -53
View File
@@ -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