From bcec03bbc679cfa58bc7a4dfd5edbfc939ae888b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:23:40 +0800 Subject: [PATCH] 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. --- app/composables/useGroupRoutes.ts | 35 +++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/app/composables/useGroupRoutes.ts b/app/composables/useGroupRoutes.ts index e623213..e8df845 100644 --- a/app/composables/useGroupRoutes.ts +++ b/app/composables/useGroupRoutes.ts @@ -9,6 +9,12 @@ export function useGroupRoutesApi() { const groupId = ref(null); /** Group whose routes were last loaded (or saved) successfully. */ const loadedId = ref(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. let request = 0; @@ -24,17 +30,19 @@ export function useGroupRoutesApi() { if (groupId.value !== id) { routes.value = []; loadedId.value = null; + revision = undefined; } groupId.value = id; loading.value = true; error.value = ""; needLogin.value = false; try { - const data = await apiFetch<{ routes?: Route[] }>( + const data = await apiFetch<{ routes?: Route[]; revision?: string }>( `/admin/api/groups/${encodeURIComponent(id)}/routes`, ); if (mine !== request) return; routes.value = data.routes ?? []; + revision = data.revision; loadedId.value = id; } catch (err) { if (mine === request && !needLogin.value) @@ -50,15 +58,29 @@ export function useGroupRoutesApi() { groupId.value = null; loadedId.value = null; routes.value = []; + revision = undefined; loading.value = false; error.value = ""; } async function save(id: string, next: Route[]): Promise { - await apiFetch(`/admin/api/groups/${encodeURIComponent(id)}/routes`, { - method: "PUT", - body: JSON.stringify({ routes: next }), - }); + let data: { routes?: Route[]; revision?: string }; + try { + data = await apiFetch<{ routes?: Route[]; revision?: string }>( + `/admin/api/groups/${encodeURIComponent(id)}/routes`, + { + method: "PUT", + 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. if (groupId.value !== id) return; // What was just written is the newest state: a load of this group that @@ -66,7 +88,8 @@ export function useGroupRoutesApi() { request++; loading.value = false; error.value = ""; - routes.value = next; + routes.value = data.routes ?? next; + revision = data.revision; loadedId.value = id; }