Skip to content

Commit f6fd6cb

Browse files
committed
remember tags before updating
1 parent 7668c2a commit f6fd6cb

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

frontend/src/ts/collections/results.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ resultsCollection.createIndex((row) => row.timestamp, {
240240
type ActionType = {
241241
updateTags: {
242242
resultId: string;
243-
tagIds: string[];
243+
currentTagIds: string[];
244+
newTagIds: string[];
244245
//TODO: remove when result page is migrated to solidjs
245246
afterUpdate?: (params: { tagPbs: string[] }) => void;
246247
};
@@ -251,14 +252,14 @@ type ActionType = {
251252

252253
const actions = {
253254
updateTags: createOptimisticAction<ActionType["updateTags"]>({
254-
onMutate: ({ resultId, tagIds }) => {
255+
onMutate: ({ resultId, newTagIds }) => {
255256
resultsCollection.update(resultId, (result) => {
256-
result.tags = tagIds;
257+
result.tags = newTagIds;
257258
});
258259
},
259-
mutationFn: async ({ resultId, tagIds, afterUpdate }) => {
260+
mutationFn: async ({ resultId, currentTagIds, newTagIds, afterUpdate }) => {
260261
const response = await Ape.results.updateTags({
261-
body: { resultId, tagIds },
262+
body: { resultId, tagIds: newTagIds },
262263
});
263264
if (response.status !== 200) {
264265
throw new Error(
@@ -273,8 +274,8 @@ const actions = {
273274
}
274275

275276
const tagsToUpdate = [
276-
...result.tags.filter((tag) => !tagIds.includes(tag)),
277-
...tagIds.filter((tag) => !result.tags.includes(tag)),
277+
...currentTagIds.filter((tag) => !newTagIds.includes(tag)),
278+
...newTagIds.filter((tag) => !currentTagIds.includes(tag)),
278279
];
279280
tagsToUpdate.forEach((tag) => {
280281
updateLocalTagPB(
@@ -290,6 +291,11 @@ const actions = {
290291
);
291292
});
292293

294+
resultsCollection.utils.writeUpdate({
295+
_id: resultId,
296+
tags: newTagIds,
297+
});
298+
293299
afterUpdate?.({ tagPbs: response.body.data.tagPbs });
294300
},
295301
}),

frontend/src/ts/modals/edit-result-tags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ async function save(): Promise<void> {
111111
try {
112112
await updateTags({
113113
resultId: state.resultId,
114-
tagIds: state.tags,
114+
currentTagIds: state.startingTags,
115+
newTagIds: state.tags,
115116
afterUpdate: ({ tagPbs }) => {
116117
if (state.source === "resultPage") {
117118
TestResult.updateTagsAfterEdit(state.tags, tagPbs);

0 commit comments

Comments
 (0)