fixed admin search loading issues and improved error handling

This commit is contained in:
Edward Tirado Jr 2025-06-30 15:32:02 -05:00
parent 23769de95c
commit 270bc4767a

View file

@ -19,6 +19,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { Movie } from "~/types/movie"; import type { Movie } from "~/types/movie";
import "lazysizes";
const loading = ref(false); const loading = ref(false);
@ -42,29 +43,27 @@ const findMovies = async function (e: Event) {
return; return;
} }
const { data, error } = await useFetch<Movie[]>( $fetch<Movie[]>(`${config.public.apiURL}/movies/search?q=${searchTerm}`, {
`${config.public.apiURL}/movies/search?q=${searchTerm}`,
{
method: "GET", method: "GET",
headers: { headers: {
"Content-type": "application/json", "Content-type": "application/json",
Authorization: `Token ${useCookie("token").value}`, Authorization: `Token ${useCookie("token").value}`,
}, },
}, })
); .then((data) => {
movies.value = data;
if (error.value) {
if (error.value.statusCode === 401) {
alert("Unauthorized");
}
} else {
if (!data.value) {
alert("No movies found.");
} else {
movies.value = data.value || [];
}
}
loading.value = false; loading.value = false;
})
.catch((err) => {
if (err.statusCode === 401) {
navigateTo("/login");
} else if (err.statusCode === 404) {
alert("No movies found");
loading.value = false;
} else {
alert("An error occurred. Please try again later.");
}
});
}; };
</script> </script>