movie-night-web/src/components/admin/search.vue

71 lines
1.8 KiB
Vue
Raw Normal View History

2025-03-30 20:51:11 -05:00
<template>
<form class="py-3 p-sm-0 align-items-center" @submit="findMovies">
<label class="px-0" for="search-field">Search</label>
2025-03-30 20:51:11 -05:00
<div class="px-0 mx-0">
<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">
<img
:src="movie.poster"
alt="movie poster"
class="neon-border hover-pointer"
@click="showModal(movie)"
/>
2025-03-30 20:51:11 -05:00
<div class="p-2">
<h5 class="text-center">{{ movie.title }} ({{ movie.year }})</h5>
2025-03-30 20:51:11 -05:00
</div>
</li>
</ul>
</template>
<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
const showModal = (movie: Movie) => {
emit("show-modal", movie);
};
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
},
);
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-03-30 20:51:11 -05:00
</script>
<style scoped></style>