fix searching unchanged query on search tab switch

This commit is contained in:
geoffrey45
2022-06-01 20:12:32 +03:00
parent d51c5e46d2
commit 253de52376
+25 -19
View File
@@ -14,11 +14,10 @@ import useDebouncedRef from "../composables/useDebouncedRef";
import useTabStore from "./tabs"; import useTabStore from "./tabs";
/** /**
* *
* @param id The id of the element of the div to scroll
* Scrolls on clicking the loadmore button * Scrolls on clicking the loadmore button
*/ */
function scrollOnLoad(id: string) { function scrollOnLoad() {
const elem = document.getElementById(id); const elem = document.getElementById("tab-content");
elem.scroll({ elem.scroll({
top: elem.scrollHeight, top: elem.scrollHeight,
@@ -32,28 +31,32 @@ export default defineStore("search", () => {
const currentTab = ref("tracks"); const currentTab = ref("tracks");
const tracks = reactive({ const tracks = reactive({
query: "",
value: <Track[]>[], value: <Track[]>[],
more: false, more: false,
}); });
const albums = reactive({ const albums = reactive({
query: "",
value: <AlbumInfo[]>[], value: <AlbumInfo[]>[],
more: false, more: false,
}); });
const artists = reactive({ const artists = reactive({
query: "",
value: <Artist[]>[], value: <Artist[]>[],
more: false, more: false,
}); });
/** /**
* Searches for tracks, albums and artists * Searches for tracks, albums and artists
* @param query query to search for * @param newquery query to search for
*/ */
function fetchTracks(query: string) { function fetchTracks(newquery: string) {
searchTracks(query).then((res) => { searchTracks(newquery).then((res) => {
tracks.value = res.tracks; tracks.value = res.tracks;
tracks.more = res.more; tracks.more = res.more;
tracks.query = newquery;
}); });
} }
@@ -61,6 +64,7 @@ export default defineStore("search", () => {
searchAlbums(query).then((res) => { searchAlbums(query).then((res) => {
albums.value = res.albums; albums.value = res.albums;
albums.more = res.more; albums.more = res.more;
albums.query = query;
}); });
} }
@@ -68,6 +72,7 @@ export default defineStore("search", () => {
searchArtists(query).then((res) => { searchArtists(query).then((res) => {
artists.value = res.artists; artists.value = res.artists;
artists.more = res.more; artists.more = res.more;
artists.query = query;
}); });
} }
@@ -82,9 +87,7 @@ export default defineStore("search", () => {
tracks.value = [...tracks.value, ...res.tracks]; tracks.value = [...tracks.value, ...res.tracks];
tracks.more = res.more; tracks.more = res.more;
}) })
.then(() => { .then(() => scrollOnLoad());
scrollOnLoad("tab-content");
});
} }
/** /**
@@ -98,9 +101,7 @@ export default defineStore("search", () => {
albums.value = [...albums.value, ...res.albums]; albums.value = [...albums.value, ...res.albums];
albums.more = res.more; albums.more = res.more;
}) })
.then(() => { .then(() => scrollOnLoad());
scrollOnLoad("tab-content");
});
} }
/** /**
@@ -114,9 +115,7 @@ export default defineStore("search", () => {
artists.value = [...artists.value, ...res.artists]; artists.value = [...artists.value, ...res.artists];
artists.more = res.more; artists.more = res.more;
}) })
.then(() => { .then(() => scrollOnLoad());
scrollOnLoad("tab-content");
});
} }
watch( watch(
@@ -148,18 +147,25 @@ export default defineStore("search", () => {
watch( watch(
() => currentTab.value, () => currentTab.value,
(newTab) => { (newTab) => {
const current_query: string = query.value;
switch (newTab) { switch (newTab) {
case "tracks": case "tracks":
fetchTracks(query.value); if (tracks.query == current_query) break;
fetchTracks(current_query);
break; break;
case "albums": case "albums":
fetchAlbums(query.value); if (albums.query == current_query) break;
fetchAlbums(current_query);
break; break;
case "artists": case "artists":
fetchArtists(query.value); if (artists.query == current_query) break;
fetchArtists(current_query);
break; break;
default: default:
fetchTracks(query.value); fetchTracks(current_query);
break; break;
} }
} }