Files
romm/frontend/src/components/common/Collection/Dialog/AddRoms.vue
Spinnich 2ecefa3d3f Fix race condition in collection and favorite rom membership updates
Replace full rom_ids list replacement with atomic POST/DELETE endpoints
that add or remove individual ROMs from a collection. This prevents
concurrent rapid clicks from overwriting each other (last-write-wins).

Also fix missing session.flush() in add_rom_user() and add collection
endpoint tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 15:08:53 -04:00

147 lines
4.3 KiB
Vue

<script setup lang="ts">
import type { Emitter } from "mitt";
import { inject, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useDisplay } from "vuetify";
import CollectionListItem from "@/components/common/Collection/ListItem.vue";
import RAvatarCollection from "@/components/common/Collection/RAvatar.vue";
import RomListItem from "@/components/common/Game/ListItem.vue";
import RDialog from "@/components/common/RDialog.vue";
import collectionApi from "@/services/api/collection";
import storeCollections from "@/stores/collections";
import storeRoms, { type SimpleRom } from "@/stores/roms";
import type { Events } from "@/types/emitter";
const { t } = useI18n();
const { mdAndUp } = useDisplay();
const show = ref(false);
const romsStore = storeRoms();
const collectionsStore = storeCollections();
const selectedCollection = ref<ReturnType<typeof collectionsStore.getCollection>>();
const roms = ref<SimpleRom[]>([]);
const emitter = inject<Emitter<Events>>("emitter");
emitter?.on("showAddToCollectionDialog", (romsToAdd) => {
roms.value = romsToAdd;
show.value = true;
});
const HEADERS = [
{
title: "Name",
align: "start",
sortable: true,
key: "name",
},
] as const;
async function addRomsToCollection() {
if (!selectedCollection.value) return;
const romIds = roms.value.map((r) => r.id);
await collectionApi
.addRomsToCollection(selectedCollection.value.id, romIds)
.then(({ data }) => {
emitter?.emit("snackbarShow", {
msg: `Roms added to ${selectedCollection.value?.name} successfully!`,
icon: "mdi-check-bold",
color: "green",
timeout: 2000,
});
emitter?.emit("refreshDrawer", null);
collectionsStore.updateCollection(data);
})
.catch((error) => {
console.error(error);
emitter?.emit("snackbarShow", {
msg: error.response.data.detail,
icon: "mdi-close-circle",
color: "red",
});
return;
})
.finally(() => {
emitter?.emit("showLoadingDialog", { loading: false, scrim: false });
romsStore.resetSelection();
closeDialog();
});
}
function closeDialog() {
roms.value = [];
show.value = false;
selectedCollection.value = undefined;
}
</script>
<template>
<RDialog
v-model="show"
icon="mdi-bookmark-plus-outline"
scroll-content
:width="mdAndUp ? '45vw' : '95vw'"
@close="closeDialog"
>
<template #header>
<v-row no-gutters class="justify-center">
<span>{{ t("rom.adding-to-collection", roms.length) }}</span>
</v-row>
</template>
<template #prepend>
<v-autocomplete
v-model="selectedCollection"
class="pa-3"
density="default"
:label="t('common.collection')"
item-title="name"
:items="collectionsStore.ownedCollections"
variant="outlined"
hide-details
return-object
clearable
>
<template #item="{ props, item }">
<CollectionListItem
:collection="item.raw"
v-bind="props"
:with-title="false"
/>
</template>
<template #chip="{ item }">
<v-chip class="pl-0" label>
<RAvatarCollection :collection="item.raw" :size="35" class="mr-2" />
{{ item.raw.name }}
</v-chip>
</template>
</v-autocomplete>
</template>
<template #content>
<v-data-table-virtual
:item-value="(item) => item.id"
:items="roms"
:width="mdAndUp ? '60vw' : '95vw'"
:headers="HEADERS"
hide-default-header
>
<template #item.name="{ item }">
<RomListItem :rom="item" with-filename with-size />
</template>
</v-data-table-virtual>
</template>
<template #footer>
<v-row class="justify-center my-2">
<v-btn-group divided density="compact">
<v-btn class="bg-toplayer" @click="closeDialog">
{{ t("common.cancel") }}
</v-btn>
<v-btn
class="bg-toplayer text-romm-green"
:disabled="!selectedCollection"
:variant="!selectedCollection ? 'plain' : 'flat'"
@click="addRomsToCollection"
>
{{ t("common.confirm") }}
</v-btn>
</v-btn-group>
</v-row>
</template>
</RDialog>
</template>