|
|
@@ -1,34 +1,25 @@
|
|
|
-import {useFetch, FetchResult} from "#app";
|
|
|
+import {FetchResult, useAsyncData} from "#app";
|
|
|
import {useImageManager} from "~/composables/data/useImageManager";
|
|
|
-import {ref, Ref} from "@vue/reactivity";
|
|
|
|
|
|
interface useImageFetchReturnType {
|
|
|
- fetch: (id: number | null, defaultImage?: string, height?: number, width?: number) => FetchResult<any>
|
|
|
+ fetch: (id: number | null, defaultImage?: string | null, height?: number, width?: number) => FetchResult<any>
|
|
|
}
|
|
|
|
|
|
-export const useImageFetch = (lazy: boolean = false): useImageFetchReturnType => {
|
|
|
+/**
|
|
|
+ * Sert d'intermédiaire entre les composants et l'ImageManager en fournissant une méthode useAsyncData toute prête.
|
|
|
+ */
|
|
|
+export const useImageFetch = (): useImageFetchReturnType => {
|
|
|
const { imageManager } = useImageManager()
|
|
|
|
|
|
const fetch = (
|
|
|
- id: number | null, // If id is null, fetch shall return the default value
|
|
|
- defaultImage: string = '',
|
|
|
+ id: number | null, // If id is null, fetch shall return the default image url
|
|
|
+ defaultImage: string | null = null,
|
|
|
height: number = 0,
|
|
|
width: number = 0
|
|
|
- ): FetchResult<any> => useFetch(
|
|
|
- // @ts-ignore
|
|
|
- async () => {
|
|
|
- const image: Ref<String> = ref(defaultImage ? `assets/images/byDefault/${defaultImage}` : '')
|
|
|
-
|
|
|
- if (id !== null) {
|
|
|
- try {
|
|
|
- image.value = await imageManager.get(id, height, width)
|
|
|
- } catch (e) {
|
|
|
- console.error(e)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return image
|
|
|
- }
|
|
|
+ ): FetchResult<string> => useAsyncData(
|
|
|
+ 'img' + (id ?? defaultImage ?? 0),
|
|
|
+ () => imageManager.get(id, defaultImage, height, width),
|
|
|
+ { lazy: true, server: false } // Always fetch images client-side
|
|
|
)
|
|
|
|
|
|
return { fetch }
|