mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
fix(console): send loaded route revisions
Send the revision actually loaded with the route list and adopt the server response after a save. Reload on conflict instead of silently overwriting another editor.
This commit is contained in:
1 parent
be300d8643
commit
bcec03bbc6
1 file changed
+28
-5
@@ -9,6 +9,12 @@ export function useGroupRoutesApi() {
|
|||||||
const groupId = ref<string | null>(null);
|
const groupId = ref<string | null>(null);
|
||||||
/** Group whose routes were last loaded (or saved) successfully. */
|
/** Group whose routes were last loaded (or saved) successfully. */
|
||||||
const loadedId = ref<string | null>(null);
|
const loadedId = ref<string | null>(null);
|
||||||
|
/**
|
||||||
|
* Server revision of the shown routes. Every save sends it back, so a save
|
||||||
|
* built on a list someone else has changed meanwhile is refused (409)
|
||||||
|
* instead of overwriting their change.
|
||||||
|
*/
|
||||||
|
let revision: string | undefined;
|
||||||
// Bumped by every load/clear, so only the latest request may land.
|
// Bumped by every load/clear, so only the latest request may land.
|
||||||
let request = 0;
|
let request = 0;
|
||||||
|
|
||||||
@@ -24,17 +30,19 @@ export function useGroupRoutesApi() {
|
|||||||
if (groupId.value !== id) {
|
if (groupId.value !== id) {
|
||||||
routes.value = [];
|
routes.value = [];
|
||||||
loadedId.value = null;
|
loadedId.value = null;
|
||||||
|
revision = undefined;
|
||||||
}
|
}
|
||||||
groupId.value = id;
|
groupId.value = id;
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
error.value = "";
|
error.value = "";
|
||||||
needLogin.value = false;
|
needLogin.value = false;
|
||||||
try {
|
try {
|
||||||
const data = await apiFetch<{ routes?: Route[] }>(
|
const data = await apiFetch<{ routes?: Route[]; revision?: string }>(
|
||||||
`/admin/api/groups/${encodeURIComponent(id)}/routes`,
|
`/admin/api/groups/${encodeURIComponent(id)}/routes`,
|
||||||
);
|
);
|
||||||
if (mine !== request) return;
|
if (mine !== request) return;
|
||||||
routes.value = data.routes ?? [];
|
routes.value = data.routes ?? [];
|
||||||
|
revision = data.revision;
|
||||||
loadedId.value = id;
|
loadedId.value = id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (mine === request && !needLogin.value)
|
if (mine === request && !needLogin.value)
|
||||||
@@ -50,15 +58,29 @@ export function useGroupRoutesApi() {
|
|||||||
groupId.value = null;
|
groupId.value = null;
|
||||||
loadedId.value = null;
|
loadedId.value = null;
|
||||||
routes.value = [];
|
routes.value = [];
|
||||||
|
revision = undefined;
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
error.value = "";
|
error.value = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save(id: string, next: Route[]): Promise<void> {
|
async function save(id: string, next: Route[]): Promise<void> {
|
||||||
await apiFetch(`/admin/api/groups/${encodeURIComponent(id)}/routes`, {
|
let data: { routes?: Route[]; revision?: string };
|
||||||
|
try {
|
||||||
|
data = await apiFetch<{ routes?: Route[]; revision?: string }>(
|
||||||
|
`/admin/api/groups/${encodeURIComponent(id)}/routes`,
|
||||||
|
{
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify({ routes: next }),
|
body: JSON.stringify({
|
||||||
});
|
routes: next,
|
||||||
|
revision: groupId.value === id ? revision : undefined,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
// Someone changed the routes meanwhile: show theirs, nothing was written.
|
||||||
|
if (isConflict(err) && groupId.value === id) void load(id);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
// The page may have moved on to another group while the request ran.
|
// The page may have moved on to another group while the request ran.
|
||||||
if (groupId.value !== id) return;
|
if (groupId.value !== id) return;
|
||||||
// What was just written is the newest state: a load of this group that
|
// What was just written is the newest state: a load of this group that
|
||||||
@@ -66,7 +88,8 @@ export function useGroupRoutesApi() {
|
|||||||
request++;
|
request++;
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
error.value = "";
|
error.value = "";
|
||||||
routes.value = next;
|
routes.value = data.routes ?? next;
|
||||||
|
revision = data.revision;
|
||||||
loadedId.value = id;
|
loadedId.value = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user