half-refactor of dialogs as components

This commit is contained in:
zurdi
2023-08-08 15:21:17 +02:00
parent 874dff9018
commit 80fb047275
10 changed files with 170 additions and 192 deletions

View File

@@ -18,25 +18,18 @@ async def search_rom_igdb(
log.info(emoji.emojize(":magnifying_glass_tilted_right: IGDB Searching"))
matched_roms: list = []
if search_term:
log.info(f"Searching by {search_by}: {search_term}")
if search_by.lower() == "id":
matched_roms = igdbh.get_matched_roms_by_id(search_term)
elif search_by.lower() == "name":
matched_roms = igdbh.get_matched_roms_by_name(search_term, rom["p_igdb_id"])
else:
log.info(
emoji.emojize(
f":video_game: {rom['p_slug']}: {rom['file_name']}"
)
log.info(f"Searching by {search_by}: {search_term}")
if search_by.lower() == "id":
matched_roms = igdbh.get_matched_roms_by_id(search_term)
elif search_by.lower() == "name":
matched_roms = (
[]
if not rom["p_igdb_id"]
else igdbh.get_matched_roms_by_name(search_term, rom["p_igdb_id"])
)
matched_roms = igdbh.get_matched_roms(rom["file_name"], rom["p_igdb_id"])
log.info("Results:")
[
log.info(f"\t - {rom['r_name']}")
for rom in matched_roms
]
[log.info(f"\t - {rom['r_name']}") for rom in matched_roms]
return {"roms": matched_roms, "msg": "success"}

View File

@@ -183,34 +183,6 @@ class IGDBHandler:
for rom in matched_roms
]
@check_twitch_token
def get_matched_roms(self, file_name: str, p_igdb_id: int):
if not p_igdb_id:
return []
matched_roms = self._request(
self.games_url,
data=f"""
search "{uc(get_search_term(file_name))}";
fields id, slug, name, summary;
where platforms=[{p_igdb_id}];
""",
)
return [
dict(
rom,
url_cover=self._search_cover(rom["id"]).replace(
"t_thumb", "t_cover_big"
),
url_screenshots=self._search_screenshots(rom["id"]),
r_igdb_id=rom.pop("id"),
r_slug=rom.pop("slug"),
r_name=rom.pop("name"),
)
for rom in matched_roms
]
class TwitchAuth:
def __init__(self) -> None:

View File

@@ -78,22 +78,3 @@ def test_get_matched_roms_by_name():
roms = igdbh.get_matched_roms_by_name("Notarealgametitle", 4)
assert roms == []
@pytest.mark.vcr()
def test_get_matched_roms():
roms = igdbh.get_matched_roms("Paper Mario (USA).n64", 4)
assert len(roms) == 9
assert roms[0]["r_igdb_id"] == 3340
assert roms[0]["r_slug"] == "paper-mario"
assert roms[0]["r_name"] == "Paper Mario"
assert roms[0]["summary"]
assert urlparse(roms[0]["url_cover"]).hostname == "images.igdb.com"
assert urlparse(roms[0]["url_screenshots"][0]).hostname == "images.igdb.com"
roms = igdbh.get_matched_roms("Paper Mario (USA).n64", None)
assert roms == []
roms = igdbh.get_matched_roms("Notarealgametitle", 4)
assert roms == []

View File

@@ -4,22 +4,26 @@ import { useRouter } from "vue-router";
import { useDisplay } from "vuetify";
import { deleteRomApi } from "@/services/api.js";
const router = useRouter();
const deleteFromFs = ref(false);
const { xs, mdAndDown, lgAndUp } = useDisplay();
const props = defineProps(["show", "rom"]);
const router = useRouter();
const show = ref(false);
const rom = ref();
const deleteFromFs = ref(false);
const emitter = inject("emitter");
emitter.on("showDeleteDialog", (romToDelete) => {
rom.value = romToDelete;
show.value = true;
});
async function deleteRom() {
await deleteRomApi(props.rom, deleteFromFs.value)
await deleteRomApi(rom.value, deleteFromFs.value)
.then((response) => {
emitter.emit("refreshPlatforms");
emitter.emit("snackbarShow", {
msg: response.data.msg,
icon: "mdi-check-bold",
color: "green",
});
router.push(`/platform/${props.rom.p_slug}`);
})
.catch((error) => {
console.log(error);
@@ -28,11 +32,12 @@ async function deleteRom() {
icon: "mdi-close-circle",
color: "red",
});
if (error.response.status == 404) {
router.push(`/platform/${props.rom.p_slug}`);
}
return;
});
emitter.emit("close-delete-dialog");
await router.push(`/platform/${rom.value.p_slug}`);
emitter.emit("refreshGallery");
emitter.emit("refreshPlatforms");
show.value = false;
}
</script>
@@ -40,7 +45,10 @@ async function deleteRom() {
<v-dialog
:modelValue="show"
width="auto"
@click:outside="emitter.emit('close-delete-dialog')"
@click:outside="show = false"
@keydown.esc="show = false"
no-click-animation
persistent
>
<v-card
rounded="0"
@@ -57,7 +65,7 @@ async function deleteRom() {
</v-col>
<v-col>
<v-btn
@click="emitter.emit('close-delete-dialog')"
@click="show = false"
class="bg-primary"
rounded="0"
variant="text"
@@ -74,7 +82,7 @@ async function deleteRom() {
<span>Deleting {{ rom.file_name }}. Do you confirm?</span>
</v-row>
<v-row class="justify-center pa-2" no-gutters>
<v-btn @click="emitter.emit('close-delete-dialog')">Cancel</v-btn>
<v-btn @click="show = false">Cancel</v-btn>
<v-btn @click="deleteRom()" class="text-red ml-5">Confirm</v-btn>
</v-row>
</v-card-text>

View File

@@ -1,20 +1,23 @@
<script setup>
import { ref, inject } from "vue";
import { useRouter } from "vue-router";
import { useDisplay } from "vuetify";
import { deleteRomApi } from "@/services/api.js";
import { updateRomApi } from "@/services/api.js";
const { xs, mdAndDown, lgAndUp } = useDisplay();
const props = defineProps(["show", "rom", "updatedRom"]);
const updatedRom = ref();
const show = ref(false);
const rom = ref();
const emitter = inject("emitter");
const updatedRom = ref(undefined);
const saveFiles = ref(false);
const updating = ref(false);
const loading = ref(true);
emitter.on("showEditDialog", (romToEdit, updatedRomToEdit) => {
show.value = true;
rom.value = romToEdit;
updatedRom.value = updatedRomToEdit;
});
async function updateRom(updatedData = { ...updatedRom.value }) {
emitter.emit("close-edit-dialog");
emitter.emit("updating-rom-start");
show.value = false;
emitter.emit("showLoadingDialog", true, true);
await updateRomApi(rom.value, {
r_igdb_id: updatedData.r_igdb_id,
@@ -47,7 +50,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
color: "red",
});
});
emitter.emit("updating-rom-end");
emitter.emit("showLoadingDialog", false, false);
}
</script>
@@ -57,8 +60,10 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
scroll-strategy="none"
width="auto"
:scrim="false"
@click:outside="emitter.emit('close-edit-dialog')"
v-if="rom !== undefined"
@click:outside="show = false"
@keydown.esc="show = false"
no-click-animation
persistent
>
<v-card
rounded="0"
@@ -75,7 +80,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
</v-col>
<v-col>
<v-btn
@click="emitter.emit('close-edit-dialog')"
@click="show = false"
class="bg-primary"
rounded="0"
variant="text"
@@ -91,7 +96,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
<v-row class="justify-center pa-2" no-gutters>
<v-text-field
@keyup.enter="updateRom()"
v-model="props.updatedRom.r_name"
v-model="updatedRom.r_name"
label="Name"
variant="outlined"
required
@@ -101,7 +106,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
<v-row class="justify-center pa-2" no-gutters>
<v-text-field
@keyup.enter="updateRom()"
v-model="props.updatedRom.file_name"
v-model="updatedRom.file_name"
label="File name"
variant="outlined"
required
@@ -111,7 +116,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
<v-row class="justify-center pa-2" no-gutters>
<v-textarea
@keyup.enter="updateRom()"
v-model="props.updatedRom.summary"
v-model="updatedRom.summary"
label="Summary"
variant="outlined"
required
@@ -130,7 +135,7 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
/>
</v-row>
<v-row class="justify-center pa-2" no-gutters>
<v-btn @click="emitter.emit('close-edit-dialog')">Cancel</v-btn>
<v-btn @click="show = false">Cancel</v-btn>
<v-btn @click="updateRom()" class="text-rommGreen ml-5">Apply</v-btn>
</v-row>
</v-card-text>

View File

@@ -0,0 +1,29 @@
<script setup>
import { ref, inject } from "vue";
const show = ref(false);
const scrim = ref(false);
const emitter = inject("emitter");
emitter.on("showLoadingDialog", (loading, setScrim) => {
show.value = loading;
scrim.value = setScrim;
});
</script>
<template>
<v-dialog
:modelValue="show"
scroll-strategy="none"
width="auto"
:scrim="false"
persistent
>
<v-progress-circular
:width="3"
:size="70"
color="rommAccent1"
indeterminate
/>
</v-dialog>
</template>

View File

@@ -1,36 +1,48 @@
<script setup>
import { ref, inject, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
import { ref, inject, onBeforeUnmount } from "vue";
import { useDisplay } from "vuetify";
import { updateRomApi, searchRomIGDBApi } from "@/services/api.js";
const emitter = inject("emitter");
const { xs, mdAndDown, lgAndUp } = useDisplay();
const props = defineProps(["show", "rom"]);
const show = ref(false);
const rom = ref();
const updatedRom = ref();
const searching = ref(false);
const searchTerm = ref("");
const searchBy = ref("Name");
const matchedRoms = ref([]);
const renameAsIGDB = ref(false);
const updating = ref(false);
const emitter = inject("emitter");
emitter.on("showSearchDialog", (romToSearch, updatedRomToSearch) => {
rom.value = romToSearch;
searchTerm.value = romToSearch["file_name_no_tags"];
updatedRom.value = updatedRomToSearch;
show.value = true;
searchRomIGDB();
});
async function searchRomIGDB() {
searching.value = true;
await searchRomIGDBApi(searchTerm.value, searchBy.value, props.rom)
.then((response) => {
matchedRoms.value = response.data.roms;
})
.catch((error) => {
console.log(error);
});
searching.value = false;
if (!searching.value) {
searching.value = true;
await searchRomIGDBApi(searchTerm.value, searchBy.value, rom.value)
.then((response) => {
matchedRoms.value = response.data.roms;
})
.catch((error) => {
console.log(error);
})
.finally(() => {
searching.value = false;
});
}
}
async function updateRom(updatedData = { ...updatedRom.value }) {
emitter.emit("close-search-dialog");
emitter.emit("updating-rom-start");
show.value = false;
emitter.emit("showLoadingDialog", true, true);
await updateRomApi(props.rom, {
await updateRomApi(rom, {
r_igdb_id: updatedData.r_igdb_id,
r_slug: updatedData.r_slug,
summary: updatedData.summary,
@@ -60,12 +72,15 @@ async function updateRom(updatedData = { ...updatedRom.value }) {
icon: "mdi-close-circle",
color: "red",
});
})
.finally(() => {
emitter.emit("showLoadingDialog", false, false);
});
emitter.emit("updating-rom-end");
}
defineExpose({ searchRomIGDB });
onBeforeUnmount(() => {
emitter.off("showSearchDialog");
});
</script>
<template>
@@ -74,7 +89,10 @@ defineExpose({ searchRomIGDB });
scroll-strategy="none"
width="auto"
:scrim="false"
@click:outside="emitter.emit('close-search-dialog')"
@click:outside="show = false"
@keydown.esc="show = false"
no-click-animation
persistent
>
<v-card
:class="{
@@ -94,7 +112,7 @@ defineExpose({ searchRomIGDB });
</v-col>
<v-col>
<v-btn
@click="emitter.emit('close-search-dialog')"
@click="show = false"
class="bg-primary"
rounded="0"
variant="text"
@@ -135,6 +153,7 @@ defineExpose({ searchRomIGDB });
variant="text"
icon="mdi-search-web"
block
:disabled="searching"
/>
</v-col>
</v-row>
@@ -207,6 +226,9 @@ defineExpose({ searchRomIGDB });
</template>
<style scoped>
.scroll {
overflow-y: scroll;
}
.loader-searching,
.no-results-searching {
margin-top: 200px;

View File

@@ -1,11 +1,12 @@
<script setup>
import { ref } from "vue";
import { ref, inject } from "vue";
import { downloadRomApi } from "@/services/api.js";
import useDownloadStore from "@/stores/download.js";
const downloadStore = useDownloadStore();
// Props
const emitter = inject("emitter");
const props = defineProps(["rom"]);
const saveFiles = ref(false);
const downloadUrl = `${window.location.origin}${props.rom.download_path}`;
@@ -49,18 +50,23 @@ const downloadUrl = `${window.location.origin}${props.rom.download_path}`;
icon="mdi-dots-vertical"
size="x-small"
variant="text"
disabled
/>
</template>
<v-list rounded="0" class="pa-0">
<v-list-item @click="searchRomIGDB()" class="pt-4 pb-4 pr-5">
<v-list-item
@click="emitter.emit('showSearchDialog', rom)"
class="pt-4 pb-4 pr-5"
>
<v-list-item-title class="d-flex"
><v-icon icon="mdi-search-web" class="mr-2" />Search
IGDB</v-list-item-title
>
</v-list-item>
<v-divider class="border-opacity-25" />
<v-list-item @click="dialogEditRom = true" class="pt-4 pb-4 pr-5">
<v-list-item
@click="emitter.emit('showEditDialog', rom)"
class="pt-4 pb-4 pr-5"
>
<v-list-item-title class="d-flex"
><v-icon
icon="mdi-pencil-box"
@@ -70,7 +76,7 @@ const downloadUrl = `${window.location.origin}${props.rom.download_path}`;
</v-list-item>
<v-divider class="border-opacity-25" />
<v-list-item
@click="dialogDeleteRom = true"
@click="emitter.emit('showDeleteDialog', rom)"
class="pt-4 pb-4 pr-5 text-red"
>
<v-list-item-title class="d-flex"

View File

@@ -8,64 +8,50 @@ import BackgroundHeader from "@/components/Game/Details/BackgroundHeader.vue";
import SearchRomDialog from "@/components/Dialog/SearchRom.vue";
import EditRomDialog from "@/components/Dialog/EditRom.vue";
import DeleteRomDialog from "@/components/Dialog/DeleteRom.vue";
import LoadingDialog from "@/components/Dialog/Loading.vue";
// Props
const route = useRoute();
const downloadStore = useDownloadStore();
const rom = ref(undefined);
const updatedRom = ref(undefined);
const rom = ref();
const updatedRom = ref();
const saveFiles = ref(false);
const updating = ref(false);
const loading = ref(true);
const searchRomDialog = ref();
const searchRomDialogShow = ref(false);
const editRomDialogShow = ref(false);
const deleteRomDialogShow = ref(false);
const filesToDownload = ref(undefined);
const filesToDownload = ref();
const tab = ref("details");
const downloadUrl = ref(undefined);
const downloadUrl = ref();
const { xs, mdAndDown, lgAndUp } = useDisplay();
// Event listeners bus
const emitter = inject("emitter");
emitter.on("close-search-dialog", () => {
searchRomDialogShow.value = false;
});
emitter.on("close-edit-dialog", () => {
editRomDialogShow.value = false;
});
emitter.on("close-delete-dialog", () => {
deleteRomDialogShow.value = false;
});
// Functions
onBeforeMount(() => {
fetchRomApi(route.params.platform, route.params.rom)
onBeforeMount(async () => {
emitter.emit("showLoadingDialog", true, false);
await fetchRomApi(route.params.platform, route.params.rom)
.then((response) => {
rom.value = response.data;
downloadUrl.value = `${window.location.origin}${rom.value.download_path}`;
updatedRom.value = response.data;
loading.value = false;
})
.catch((error) => {
console.log(error);
loading.value = false;
})
.finally(() => {
emitter.emit("showLoadingDialog", false, false);
});
});
</script>
<template>
<template v-if="rom !== undefined">
<background-header :image="rom.path_cover_s" />
</template>
<background-header v-if="rom" :image="rom.path_cover_s" />
<div
v-if="rom"
:class="{
content: lgAndUp,
'content-tablet': mdAndDown,
'content-mobile': xs,
}"
v-if="rom !== undefined"
>
<v-row class="pt-8 justify-center">
<v-col
@@ -143,7 +129,7 @@ onBeforeMount(() => {
</template>
<v-list rounded="0" class="pa-0">
<v-list-item
@click="searchRomDialogShow = true; searchRomDialog.searchRomIGDB()"
@click="emitter.emit('showSearchDialog', rom)"
class="pt-4 pb-4 pr-5"
>
<v-list-item-title class="d-flex"
@@ -153,7 +139,7 @@ onBeforeMount(() => {
</v-list-item>
<v-divider class="border-opacity-25" />
<v-list-item
@click="editRomDialogShow = true"
@click="emitter.emit('showEditDialog', rom, updatedRom)"
class="pt-4 pb-4 pr-5"
>
<v-list-item-title class="d-flex"
@@ -165,7 +151,7 @@ onBeforeMount(() => {
</v-list-item>
<v-divider class="border-opacity-25" />
<v-list-item
@click="deleteRomDialogShow = true"
@click="emitter.emit('showDeleteDialog', rom)"
class="pt-4 pb-4 pr-5 text-red"
>
<v-list-item-title class="d-flex"
@@ -391,30 +377,10 @@ onBeforeMount(() => {
</v-row>
</div>
<search-rom-dialog :show="searchRomDialogShow" :rom="rom" ref="searchRomDialog"/>
<edit-rom-dialog
:show="editRomDialogShow"
:rom="rom"
:updatedRom="updatedRom"
/>
<delete-rom-dialog :show="deleteRomDialogShow" :rom="rom" />
<v-dialog
:model-value="updating || loading"
scroll-strategy="none"
width="auto"
:scrim="updating"
persistent
>
<v-progress-circular
:width="3"
:size="70"
color="rommAccent1"
indeterminate
/>
</v-dialog>
<search-rom-dialog />
<edit-rom-dialog />
<delete-rom-dialog />
<loading-dialog />
</template>
<style scoped>

View File

@@ -12,6 +12,10 @@ import GalleryViewBtn from "@/components/GalleryAppBar/GalleryViewBtn.vue";
import GameCard from "@/components/Game/Card/Base.vue";
import GameListHeader from "@/components/Game/ListItem/Header.vue";
import GameListItem from "@/components/Game/ListItem/Item.vue";
import SearchRomDialog from "@/components/Dialog/SearchRom.vue";
import EditRomDialog from "@/components/Dialog/EditRom.vue";
import DeleteRomDialog from "@/components/Dialog/DeleteRom.vue";
import LoadingDialog from "@/components/Dialog/Loading.vue";
// Props
const route = useRoute();
@@ -31,14 +35,14 @@ emitter.on("filter", onFilterChange);
socket.on("scan:done", () => {
scanning.set(false);
emitter.emit("refreshPlatforms");
emitter.emit("refreshGallery");
emitter.emit("snackbarShow", {
msg: "Scan completed successfully!",
icon: "mdi-check-bold",
color: "green",
});
socket.disconnect();
emitter.emit("refreshPlatforms");
emitter.emit("refreshGallery");
});
socket.on("scan:done_ko", (msg) => {
@@ -68,6 +72,7 @@ async function fetchMoreSearch() {
if (searchCursor.value === null || gettingRoms.value) return;
gettingRoms.value = true;
emitter.emit("showLoadingDialog", gettingRoms.value, false);
await fetchRomsApi({
platform: route.params.platform,
cursor: searchCursor.value,
@@ -85,6 +90,7 @@ async function fetchMoreSearch() {
})
.finally(() => {
gettingRoms.value = false;
emitter.emit("showLoadingDialog", gettingRoms.value, false);
});
}
@@ -92,6 +98,7 @@ async function fetchMoreRoms(platform) {
if (cursor.value === null || gettingRoms.value) return;
gettingRoms.value = true;
emitter.emit("showLoadingDialog", gettingRoms.value, false);
await fetchRomsApi({ platform, cursor: cursor.value })
.then((response) => {
roms.value = [...roms.value, ...response.data.items];
@@ -103,6 +110,7 @@ async function fetchMoreRoms(platform) {
})
.finally(() => {
gettingRoms.value = false;
emitter.emit("showLoadingDialog", gettingRoms.value, false);
});
}
@@ -224,22 +232,10 @@ onBeforeRouteUpdate(async (to, _) => {
</v-row>
</template>
<template v-if="gettingRoms">
<v-dialog
:model-value="gettingRoms"
scroll-strategy="none"
width="auto"
:scrim="false"
persistent
>
<v-progress-circular
:width="3"
:size="70"
color="rommAccent1"
indeterminate
/>
</v-dialog>
</template>
<search-rom-dialog />
<edit-rom-dialog />
<delete-rom-dialog />
<loading-dialog />
</template>
<style scoped>