MoviesList.vue
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script lang="ts" setup>
//#region --import--.
import SearchBar from "~/components/SearchBar.vue";
import { ref } from "vue";
import { useTMDB } from "~/composables/tMDB";
import { Movie } from "~/models/movie";
import type { MovieInterface } from "~/interfaces/movie";
//#endregion
//#region --Declaration--.
const { fetchPopularMovies } = useTMDB();
//#endregion
//#region --Data/refs--.
const isInitialLoading = ref(true);
const isLoadingMore = ref(false);
const currentPage = ref(1);
const totalPages = ref(0);
//#endregion
//#region --Computed--.
const movies = computed(() => {
return useRepo(Movie).all() as unknown as MovieInterface[];
});
//#endregion
//#region --Function--.
/**
* Fetch popular movies
* @param page
*/
const fetchMovies = async (page: number) => {
try {
isLoadingMore.value = true;
const data = await fetchPopularMovies(page);
// Save in Movie model.
useRepo(Movie).save(data.results);
totalPages.value = data.total_pages;
currentPage.value = page;
} catch (error) {
console.error("Error fetching popular movies:", error);
} finally {
isInitialLoading.value = false;
isLoadingMore.value = false;
}
};
//#endregion
//#region --Global event--.
onMounted(() => {
// Chargement des premiers films.
fetchMovies(1);
});
//#endregion
</script>
<template>
<section>
<h1 class="text-4xl font-bold mb-8 text-center">Découvrez les films populaires</h1>
<!-- Barre de recherche -->
<search-bar placeholder="Rechercher un film..." />
<!-- Loading Skeleton -->
<skeleton-movies-loader :is-initial-loading="isInitialLoading" :skeleton-number="20" />
<!-- Liste des films -->
<!-- <pre>{{ movies }}</pre>-->
</section>
</template>
<style scoped></style>