From c00a96a9873a5f43e3119508c7802f8373595b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 23 Feb 2023 23:23:44 +0100 Subject: [PATCH] deactivating refetchOnWindowFocus globally and fetching songs from /song --- front/API.ts | 26 ++++++++++++++++++++++---- front/App.tsx | 8 +++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/front/API.ts b/front/API.ts index ea25133..ec7a4d4 100644 --- a/front/API.ts +++ b/front/API.ts @@ -10,6 +10,7 @@ import Constants from "expo-constants"; import store from "./state/Store"; import { Platform } from "react-native"; import { en } from "./i18n/Translations"; +import { useQuery, QueryClient } from "react-query"; type AuthenticationInput = { username: string; password: string }; type RegistrationInput = AuthenticationInput & { email: string }; @@ -195,8 +196,26 @@ export default class API { return "11111"; } + public static async getAllSongs(): Promise { + let songs = await API.fetch({ + route: "/song", + }); + + // this is a dummy illustration, we will need to fetch the real one from the API + return songs.data.map((song: any) => ({ + id: song.id as number, + name: song.name as string, + artistId: song.artistId as number, + albumId: song.albumId as number, + genreId: song.genreId as number, + details: song.difficulties, + cover: getDummyIllustration(), + metrics: {}, + } as Song)); + } + /** - * Retrive a song + * Retrieve a song * @param songId the id to find the song */ public static async getSong(songId: number): Promise { @@ -310,9 +329,8 @@ export default class API { * Retrieve the authenticated user's recommendations */ public static async getUserRecommendations(): Promise { - // we can assume at least 5 songs - // in the future the API will return the songs directly to avoid spamming the API - return Promise.all([1, 2, 3, 4, 5].map(API.getSong)); + const queryClient = new QueryClient(); + return await queryClient.fetchQuery(["API", "allsongs"], API.getAllSongs); } /** diff --git a/front/App.tsx b/front/App.tsx index bb0ee20..49db9e3 100644 --- a/front/App.tsx +++ b/front/App.tsx @@ -9,7 +9,13 @@ import { PersistGate } from "redux-persist/integration/react"; import LanguageGate from "./i18n/LanguageGate"; import ThemeProvider, { ColorSchemeProvider } from './Theme'; -const queryClient = new QueryClient(); +const queryClient = new QueryClient({ + defaultOptions: { + queries: { + refetchOnWindowFocus: false, + }, + }, + }); export default function App() {