Files
ad/molodetz/static/js/utils/TermsGate.js
T

35 lines
991 B
JavaScript

// retoor <retoor@molodetz.nl>
export class TermsGate {
constructor(dialog) {
this.dialog = dialog;
this.pending = null;
}
async intercept(response) {
const payload = await response.json().catch(() => ({}));
if (payload.error?.code !== "terms_required") {
return false;
}
if (!this.pending) {
this.pending = this.dialog
.confirm(`The terms have changed (version ${payload.error.terms_version}). Accept and continue?`)
.then(async (accepted) => {
if (!accepted) {
return false;
}
const result = await fetch("/terms/accept", {
method: "POST",
credentials: "same-origin",
headers: { Accept: "application/json", "Content-Type": "application/json" },
body: "{}",
});
return result.ok;
})
.finally(() => {
setTimeout(() => { this.pending = null; }, 0);
});
}
return this.pending;
}
}