forked from retoor/devplacepy
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
// retoor <retoor@molodetz.nl>
|
|
|
|
import { Http } from "./Http.js";
|
|
import { Toast } from "./Toast.js";
|
|
|
|
export class OptimisticAction {
|
|
async submit(url, params, errorTarget, render) {
|
|
try {
|
|
const result = await Http.sendForm(url, params, { silent: true });
|
|
if (render) render(result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error("optimistic action failed", error);
|
|
if (errorTarget) Toast.flash(errorTarget, "Error", 1500);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async submitOptimistic(url, params, errorTarget, apply, revert, reconcile) {
|
|
apply();
|
|
try {
|
|
const result = await Http.sendForm(url, params, { silent: true });
|
|
if (reconcile) reconcile(result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error("optimistic action failed", error);
|
|
revert();
|
|
if (errorTarget) Toast.flash(errorTarget, "Error", 1500);
|
|
return null;
|
|
}
|
|
}
|
|
}
|