2025-03-30 20:51:11 -05:00
|
|
|
<template>
|
|
|
|
<form class="py-3 p-sm-0 align-items-center" @submit="findMovies">
|
2025-04-13 01:39:23 -05:00
|
|
|
<label class="px-0" for="search-field">Search</label>
|
2025-03-30 20:51:11 -05:00
|
|
|
<div class="px-0 mx-0">
|
2025-04-13 01:39:23 -05:00
|
|
|
<input id="search-field" class="p-1" name="search-field" type="text" />
|
2025-03-30 20:51:11 -05:00
|
|
|
<button class="btn p-1" type="button" @click="findMovies">Submit</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<ul class="grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 gap-4">
|
|
|
|
<li v-for="movie in movies" class="p-1 movie-card">
|
2025-06-18 23:14:20 -05:00
|
|
|
<MoviePoster :image="movie.poster" @click="showModal(movie)" />
|
2025-03-30 20:51:11 -05:00
|
|
|
<div class="p-2">
|
2025-04-13 01:39:23 -05:00
|
|
|
<h5 class="text-center">{{ movie.title }} ({{ movie.year }})</h5>
|
2025-03-30 20:51:11 -05:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</template>
|
|
|
|
|
2025-04-13 01:39:23 -05:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import type { Movie } from "~/types/movie";
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(e: "show-modal", movie: Movie): void;
|
|
|
|
}>();
|
|
|
|
const movies = defineModel<Movie[]>("movie_list", { default: [] });
|
2025-03-30 20:51:11 -05:00
|
|
|
|
2025-04-13 01:39:23 -05:00
|
|
|
const showModal = (movie: Movie) => {
|
|
|
|
emit("show-modal", movie);
|
|
|
|
};
|
2025-04-18 18:47:57 -05:00
|
|
|
|
2025-04-13 01:39:23 -05:00
|
|
|
const findMovies = async function (e: Event) {
|
|
|
|
let config = useRuntimeConfig();
|
|
|
|
e.preventDefault();
|
|
|
|
let searchTerm = (document.getElementById("search-field") as HTMLInputElement)
|
|
|
|
?.value;
|
|
|
|
|
|
|
|
if (!searchTerm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { data, error } = await useFetch<Movie[]>(
|
|
|
|
`${config.public.apiURL}/movies/search?q=${searchTerm}`,
|
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json",
|
|
|
|
Authorization: `Token ${useCookie("token").value}`,
|
|
|
|
},
|
2025-03-30 20:51:11 -05:00
|
|
|
},
|
2025-04-13 01:39:23 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (error.value) {
|
|
|
|
if (error.value.statusCode === 401) {
|
|
|
|
alert("Unauthorized");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!data.value) {
|
|
|
|
alert("No movies found.");
|
|
|
|
} else {
|
|
|
|
movies.value = data.value || [];
|
|
|
|
}
|
2025-03-30 20:51:11 -05:00
|
|
|
}
|
2025-04-13 01:39:23 -05:00
|
|
|
};
|
2025-03-30 20:51:11 -05:00
|
|
|
</script>
|
|
|
|
|
2025-04-13 01:39:23 -05:00
|
|
|
<style scoped></style>
|