Bruno Predot

Début de la logique, ajout de refs + interface + de la méthode fetchMovies.

@@ -2,10 +2,55 @@ @@ -2,10 +2,55 @@
2 //#region --import--. 2 //#region --import--.
3 import SearchBar from "~/components/SearchBar.vue"; 3 import SearchBar from "~/components/SearchBar.vue";
4 import { ref } from "vue"; 4 import { ref } from "vue";
  5 +import { useTMDB } from "~/composables/tMDB";
  6 +//#endregion
  7 +
  8 +//#region --Declaration--.
  9 +const { fetchPopularMovies } = useTMDB();
  10 +//#endregion
  11 +
  12 +//#region --Type--.
  13 +interface Movie {
  14 + id: number;
  15 + title: string;
  16 + poster_path: string | null;
  17 + vote_average: number;
  18 + release_date: string;
  19 +}
5 //#endregion 20 //#endregion
6 21
7 //#region --Data/refs--. 22 //#region --Data/refs--.
8 const isInitialLoading = ref(true); 23 const isInitialLoading = ref(true);
  24 +const isLoadingMore = ref(false);
  25 +const movies = ref<Movie[]>([]);
  26 +const currentPage = ref(1);
  27 +const totalPages = ref(0);
  28 +//#endregion
  29 +
  30 +//#region --Function--.
  31 +// Fetch popular movies
  32 +const fetchMovies = async (page: number) => {
  33 + try {
  34 + isLoadingMore.value = true;
  35 + const data = await fetchPopularMovies(page);
  36 + console.log("data in fetchMovies", data);
  37 +
  38 + if (page === 1) {
  39 + movies.value = data.results;
  40 + } else {
  41 + movies.value = [...movies.value, ...data.results];
  42 + }
  43 +
  44 + totalPages.value = data.total_pages;
  45 + currentPage.value = page;
  46 + } catch (error) {
  47 + console.error("Error fetching popular movies:", error);
  48 + } finally {
  49 + isInitialLoading.value = false;
  50 + isLoadingMore.value = false;
  51 + }
  52 +};
  53 +fetchMovies(1)
9 //#endregion 54 //#endregion
10 </script> 55 </script>
11 56
@@ -16,6 +61,8 @@ const isInitialLoading = ref(true); @@ -16,6 +61,8 @@ const isInitialLoading = ref(true);
16 <search-bar placeholder="Rechercher un film..."/> 61 <search-bar placeholder="Rechercher un film..."/>
17 <!-- Loading Skeleton --> 62 <!-- Loading Skeleton -->
18 <skeleton-movies-loader :is-initial-loading="isInitialLoading" :skeleton-number="20" /> 63 <skeleton-movies-loader :is-initial-loading="isInitialLoading" :skeleton-number="20" />
  64 + <!-- Liste des films -->
  65 + <pre>{{ movies }}</pre>
19 </section> 66 </section>
20 </template> 67 </template>
21 68