|
| 1 | +<script lang="ts" setup> |
| 2 | +/** |
| 3 | + * |
| 4 | + * Component to preview and select videos base on the user related search terms |
| 5 | + * |
| 6 | + * @author Ismael García <[email protected]> |
| 7 | + * @version 0.0.1 |
| 8 | + * |
| 9 | + * @todo [ ] Test the component |
| 10 | + * @todo [ ] Integration test. |
| 11 | + * @todo [✔] Update the typescript. |
| 12 | + */ |
| 13 | +interface VideoResult { |
| 14 | + url: string; |
| 15 | + image: string; |
| 16 | + video_files: { |
| 17 | + fileType: string; |
| 18 | + link: string; |
| 19 | + quality: string; |
| 20 | + }[]; |
| 21 | +} |
| 22 | +
|
| 23 | +const { video } = useVideoSettings(); |
| 24 | +
|
| 25 | +const searchResults = ref<VideoResultFormat[]>([]); |
| 26 | +
|
| 27 | +const $URL_SEARCH = `https://api.pexels.com/videos/search`; |
| 28 | +
|
| 29 | +const { |
| 30 | + public: { pexelsApiKey }, |
| 31 | +} = useRuntimeConfig(); |
| 32 | +const HandleSearch = async () => { |
| 33 | + const termsToSearch: string[] = video.value.search.split(","); |
| 34 | + termsToSearch.forEach(async (term) => { |
| 35 | + // Fetch result from pexels |
| 36 | + console.log("Key", pexelsApiKey); |
| 37 | +
|
| 38 | + const { data } = await useFetch<{ videos: VideoResult[] }>( |
| 39 | + `${$URL_SEARCH}?query=${term}&per_page=20`, |
| 40 | + { |
| 41 | + headers: { |
| 42 | + Authorization: `${pexelsApiKey}`, |
| 43 | + }, |
| 44 | + } |
| 45 | + ); |
| 46 | +
|
| 47 | + //Get the video of the results |
| 48 | + if (!data.value?.videos) return; |
| 49 | + const formattedVideos = data.value.videos.map((video) => { |
| 50 | + return { |
| 51 | + url: video.url, |
| 52 | + image: video.image, |
| 53 | + videoUrl: video.video_files.find( |
| 54 | + (videoFile) => videoFile.link && videoFile.quality === "hd" |
| 55 | + ), |
| 56 | + }; |
| 57 | + }); |
| 58 | + searchResults.value = [...searchResults.value, ...formattedVideos]; |
| 59 | + }); |
| 60 | +}; |
| 61 | +
|
| 62 | +const HandleSelectVideo = (v: VideoResultFormat) => { |
| 63 | + if (selectedUrls.value.includes(v.url)) { |
| 64 | + video.value.selectedVideoUrls = video.value.selectedVideoUrls.filter( |
| 65 | + (video) => video.url !== v.url |
| 66 | + ); |
| 67 | + } else { |
| 68 | + if (!video.value.selectedVideoUrls) { |
| 69 | + video.value.selectedVideoUrls = []; |
| 70 | + } |
| 71 | + video.value.selectedVideoUrls.push(v); |
| 72 | + } |
| 73 | +}; |
| 74 | +
|
| 75 | +const selectedUrls = computed(() => { |
| 76 | + return video.value.selectedVideoUrls?.map((video) => video.url) || []; |
| 77 | +}); |
| 78 | +</script> |
| 79 | + |
| 80 | +<template> |
| 81 | + <div> |
| 82 | + <div class="max-w-5xl mx-auto"> |
| 83 | + <n-input-group> |
| 84 | + <n-input v-model:value="video.search" /> |
| 85 | + <n-button type="success" ghost @click="HandleSearch"> Search </n-button> |
| 86 | + </n-input-group> |
| 87 | + </div> |
| 88 | + <div class="max-w-5xl mx-auto mt-10"> |
| 89 | + <section class="grid grid-cols-3 gap-10"> |
| 90 | + <div |
| 91 | + v-for="result in searchResults" |
| 92 | + :key="result.url" |
| 93 | + :value="result.url" |
| 94 | + class="relative" |
| 95 | + > |
| 96 | + <video |
| 97 | + v-if="result.videoUrl" |
| 98 | + :src="result.videoUrl?.link" |
| 99 | + controls |
| 100 | + :poster="result.image" |
| 101 | + ></video> |
| 102 | + <n-button |
| 103 | + :type="selectedUrls.includes(result.url) ? 'success' : 'primary'" |
| 104 | + @click="HandleSelectVideo(result)" |
| 105 | + circle |
| 106 | + size="small" |
| 107 | + class="absolute top-2 right-2" |
| 108 | + > |
| 109 | + <template #icon> |
| 110 | + <Icon |
| 111 | + :name=" |
| 112 | + selectedUrls.includes(result.url) ? 'mdi:check' : 'mdi:plus' |
| 113 | + " |
| 114 | + /> |
| 115 | + </template> |
| 116 | + </n-button> |
| 117 | + </div> |
| 118 | + </section> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | +</template> |
| 122 | +<style scoped></style> |
0 commit comments