link loader to all request that use the useAxios hook

This commit is contained in:
geoffrey45
2022-08-28 16:32:06 +03:00
parent ca211b6dcf
commit 8d92ddec56
4 changed files with 66 additions and 50 deletions
+1 -11
View File
@@ -11,12 +11,7 @@
separator: menu.separator, separator: menu.separator,
}" }"
></div> ></div>
<div <div class="nav-button" id="home-button" v-else>
class="nav-button"
id="home-button"
:class="{ active: menu.route_name === $route.name }"
v-else
>
<div class="in"> <div class="in">
<component :is="menu.icon"></component> <component :is="menu.icon"></component>
<span>{{ menu.name }}</span> <span>{{ menu.name }}</span>
@@ -75,11 +70,6 @@ const menus = [
.side-nav-container { .side-nav-container {
text-transform: capitalize; text-transform: capitalize;
margin-top: 1rem; margin-top: 1rem;
font-size: 0.85rem;
.active {
background-color: $gray4;
}
.nav-button { .nav-button {
border-radius: $small; border-radius: $small;
+22 -21
View File
@@ -1,5 +1,6 @@
import state from "../state"; import state from "../state";
import axios from "axios"; import axios from "axios";
import useAxios from "./useAxios";
const base_url = `${state.settings.uri}/search`; const base_url = `${state.settings.uri}/search`;
@@ -9,39 +10,39 @@ const uris = {
artists: `${base_url}/artists?q=`, artists: `${base_url}/artists?q=`,
}; };
async function searchTracks(query: string) { /**
const url = uris.tracks + encodeURIComponent(query.trim()); * Fetch data from url
* @param url url to fetch json from
const res = await fetch(url); * @returns promise that resolves to the JSON
*/
if (!res.ok) { async function fetchData(url: string) {
const message = `An error has occured: ${res.status}`; const { data } = await useAxios({
throw new Error(message); url: url,
} get: true,
});
const data = await res.json();
return data; return data;
} }
async function searchTracks(query: string) {
const url = uris.tracks + encodeURIComponent(query.trim());
return await fetchData(url);
}
async function searchAlbums(query: string) { async function searchAlbums(query: string) {
const url = uris.albums + encodeURIComponent(query.trim()); const url = uris.albums + encodeURIComponent(query.trim());
return await fetchData(url);
const res = await axios.get(url);
return res.data;
} }
async function searchArtists(query: string) { async function searchArtists(query: string) {
const url = uris.artists + encodeURIComponent(query.trim()); const url = uris.artists + encodeURIComponent(query.trim());
return await fetchData(url);
const res = await axios.get(url);
return res.data;
} }
const url = state.settings.uri + "/search/loadmore"; const loadmore_url = state.settings.uri + "/search/loadmore";
async function loadMoreTracks(index: number) { async function loadMoreTracks(index: number) {
const response = await axios.get(url, { const response = await axios.get(loadmore_url, {
params: { params: {
type: "tracks", type: "tracks",
index: index, index: index,
@@ -52,7 +53,7 @@ async function loadMoreTracks(index: number) {
} }
async function loadMoreAlbums(index: number) { async function loadMoreAlbums(index: number) {
const response = await axios.get(url, { const response = await axios.get(loadmore_url, {
params: { params: {
type: "albums", type: "albums",
index: index, index: index,
@@ -63,7 +64,7 @@ async function loadMoreAlbums(index: number) {
} }
async function loadMoreArtists(index: number) { async function loadMoreArtists(index: number) {
const response = await axios.get(url, { const response = await axios.get(loadmore_url, {
params: { params: {
type: "artists", type: "artists",
index: index, index: index,
+8 -4
View File
@@ -1,10 +1,11 @@
import { FetchProps } from "../../interfaces"; import { FetchProps } from "../../interfaces";
import axios, { AxiosError, AxiosResponse } from "axios"; import axios, { AxiosError, AxiosResponse } from "axios";
import useLoaderStore from "@/stores/loader";
export default async (args: FetchProps) => { export default async (args: FetchProps) => {
let data: any = null; let data: any = null;
let error: string = null; let error: string | null = null;
let status: number = null; let status: number | null = null;
function getAxios() { function getAxios() {
if (args.get) { if (args.get) {
@@ -18,6 +19,8 @@ export default async (args: FetchProps) => {
return axios.post(args.url, args.props); return axios.post(args.url, args.props);
} }
const { startLoading, stopLoading } = useLoaderStore();
startLoading();
await getAxios() await getAxios()
.then((res: AxiosResponse) => { .then((res: AxiosResponse) => {
data = res.data; data = res.data;
@@ -25,8 +28,9 @@ export default async (args: FetchProps) => {
}) })
.catch((err: AxiosError) => { .catch((err: AxiosError) => {
error = err.message as string; error = err.message as string;
status = err.response.status as number; status = err.response?.status as number;
}); })
.then(() => stopLoading());
return { data, error, status }; return { data, error, status };
}; };
+29 -8
View File
@@ -12,6 +12,7 @@ import {
import { watch } from "vue"; import { watch } from "vue";
import useDebouncedRef from "../utils/useDebouncedRef"; import useDebouncedRef from "../utils/useDebouncedRef";
import useTabStore from "./tabs"; import useTabStore from "./tabs";
import useLoaderStore from "./loader";
/** /**
* *
* Scrolls on clicking the loadmore button * Scrolls on clicking the loadmore button
@@ -28,6 +29,8 @@ function scrollOnLoad() {
export default defineStore("search", () => { export default defineStore("search", () => {
const query = useDebouncedRef(null, 600); const query = useDebouncedRef(null, 600);
const { startLoading, stopLoading } = useLoaderStore();
const currentTab = ref("tracks"); const currentTab = ref("tracks");
const loadCounter = reactive({ const loadCounter = reactive({
tracks: 0, tracks: 0,
@@ -62,51 +65,67 @@ export default defineStore("search", () => {
/** /**
* Searches for tracks, albums and artists * Searches for tracks, albums and artists
* @param newquery query to search for * @param query query to search for
*/ */
function fetchTracks(newquery: string) { function fetchTracks(query: string) {
searchTracks(newquery).then((res) => { if (!query) return;
searchTracks(query).then((res) => {
tracks.value = res.tracks; tracks.value = res.tracks;
tracks.more = res.more; tracks.more = res.more;
tracks.query = newquery; tracks.query = query;
}); });
} }
function fetchAlbums(query: string) { function fetchAlbums(query: string) {
searchAlbums(query).then((res) => { if (!query) return;
startLoading();
searchAlbums(query)
.then((res) => {
albums.value = res.albums; albums.value = res.albums;
albums.more = res.more; albums.more = res.more;
albums.query = query; albums.query = query;
}); })
.then(() => stopLoading());
} }
function fetchArtists(query: string) { function fetchArtists(query: string) {
searchArtists(query).then((res) => { if (!query) return;
startLoading();
searchArtists(query)
.then((res) => {
artists.value = res.artists; artists.value = res.artists;
artists.more = res.more; artists.more = res.more;
artists.query = query; artists.query = query;
}); })
.then(() => stopLoading());
} }
function loadTracks() { function loadTracks() {
loadCounter.tracks += 12; loadCounter.tracks += 12;
startLoading();
loadMoreTracks(loadCounter.tracks) loadMoreTracks(loadCounter.tracks)
.then((res) => { .then((res) => {
tracks.value = [...tracks.value, ...res.tracks]; tracks.value = [...tracks.value, ...res.tracks];
tracks.more = res.more; tracks.more = res.more;
}) })
.then(() => stopLoading())
.then(() => scrollOnLoad()); .then(() => scrollOnLoad());
} }
function loadAlbums() { function loadAlbums() {
loadCounter.albums += 12; loadCounter.albums += 12;
startLoading();
loadMoreAlbums(loadCounter.albums) loadMoreAlbums(loadCounter.albums)
.then((res) => { .then((res) => {
albums.value = [...albums.value, ...res.albums]; albums.value = [...albums.value, ...res.albums];
albums.more = res.more; albums.more = res.more;
}) })
.then(() => stopLoading())
.then(() => { .then(() => {
setTimeout(() => { setTimeout(() => {
scrollOnLoad(); scrollOnLoad();
@@ -117,11 +136,13 @@ export default defineStore("search", () => {
function loadArtists() { function loadArtists() {
loadCounter.artists += 12; loadCounter.artists += 12;
startLoading();
loadMoreArtists(loadCounter.artists) loadMoreArtists(loadCounter.artists)
.then((res) => { .then((res) => {
artists.value = [...artists.value, ...res.artists]; artists.value = [...artists.value, ...res.artists];
artists.more = res.more; artists.more = res.more;
}) })
.then(() => stopLoading())
.then(() => scrollOnLoad()); .then(() => scrollOnLoad());
} }