updated useFetch requests in onMounted to use

This commit is contained in:
Edward Tirado Jr 2025-04-19 14:48:48 -05:00
parent 592bbb1dd3
commit f7af5501da
4 changed files with 62 additions and 75 deletions

View file

@ -44,7 +44,7 @@
</div> </div>
<div id="lists" class="hidden"> <div id="lists" class="hidden">
<lists></lists> <lists />
</div> </div>
</div> </div>
</template> </template>
@ -54,15 +54,14 @@ import AddMovie from "~/components/modal-content/AddMovie.vue";
import Search from "~/components/admin/search.vue"; import Search from "~/components/admin/search.vue";
import Showings from "~/components/admin/showings.vue"; import Showings from "~/components/admin/showings.vue";
import Lists from "~/components/admin/lists.vue"; import Lists from "~/components/admin/lists.vue";
import type { MovieList } from "~/types/movielist";
import { useCookie } from "#app"; import { useCookie } from "#app";
import type { Movie } from "~/types/movie"; import type { Movie } from "~/types/movie";
import Modal from "~/components/Modal.vue"; import Modal from "~/components/Modal.vue";
const lists = defineModel<MovieList>("movie-lists", { default: [] });
const modal_movie = defineModel<Movie>("#movie-modal"); const modal_movie = defineModel<Movie>("#movie-modal");
const movie_modal = ref<InstanceType<typeof Modal> | null>(null); const movie_modal = ref<InstanceType<typeof Modal> | null>(null);
const current_view = ref("search");
const closeModal = function () { const closeModal = function () {
movie_modal?.value?.toggleModal(); movie_modal?.value?.toggleModal();
@ -72,6 +71,7 @@ const showModal = function (movie: Movie) {
movie_modal?.value?.toggleModal(); movie_modal?.value?.toggleModal();
}; };
const toggleDisplay = function (element_id: string) { const toggleDisplay = function (element_id: string) {
if (element_id === current_view.value) return;
let tabs = ["search", "showings", "lists"]; let tabs = ["search", "showings", "lists"];
tabs.forEach((value) => { tabs.forEach((value) => {
@ -80,6 +80,7 @@ const toggleDisplay = function (element_id: string) {
document document
.getElementById(element_id + "-tab") .getElementById(element_id + "-tab")
?.classList.toggle("underline"); ?.classList.toggle("underline");
current_view.value = element_id;
} else if (!document.getElementById(value)?.classList.contains("hidden")) { } else if (!document.getElementById(value)?.classList.contains("hidden")) {
document.getElementById(value)?.classList.toggle("hidden"); document.getElementById(value)?.classList.toggle("hidden");
document.getElementById(value + "-tab")?.classList.toggle("underline"); document.getElementById(value + "-tab")?.classList.toggle("underline");

View file

@ -53,7 +53,7 @@
<span <span
v-if="logged_in" v-if="logged_in"
class="text-center hover-pointer" class="text-center hover-pointer"
@click="removeMovie(movie.id)" @click="removeMovie(movie.imdb_id)"
> >
X X
</span> </span>
@ -83,35 +83,27 @@ const hide_scheduled = ref(false);
const getList = async function (list_id: number) { const getList = async function (list_id: number) {
let config = useRuntimeConfig(); let config = useRuntimeConfig();
const { data, error } = await useFetch<MovieList>( $fetch<MovieList>(`${config.public.apiURL}/lists/${list_id}`, {
`${config.public.apiURL}/lists/${list_id}`,
{
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) => {
list.value = data;
if (error.value) { movies.value = data?.movies || [];
if (error.value.statusCode === 401) { filtered_movies.value = movies.value;
})
.catch((err) => {
if (err.statusCode === 401) {
navigateTo("/"); navigateTo("/");
} }
if (error.value.statusCode === 404) { if (err.statusCode === 404) {
alert("List not found"); alert("List not found");
navigateTo("/lists"); navigateTo("/lists");
} }
} else { });
if (!data.value) {
alert("List not found");
navigateTo("/lists");
} else {
list.value = data.value;
movies.value = data.value?.movies || [];
filtered_movies.value = movies.value;
}
}
}; };
const hideScheduled = function () { const hideScheduled = function () {
@ -127,7 +119,7 @@ const hideScheduled = function () {
} }
}; };
const removeMovie = async function (movie_id: number) { const removeMovie = async function (movie_id: string) {
let config = useRuntimeConfig(); let config = useRuntimeConfig();
let confirmed = confirm("Remove movie from list?"); let confirmed = confirm("Remove movie from list?");

View file

@ -31,21 +31,19 @@ const updateLists = async function () {
headers["Authorization"] = `Token ${useCookie("token").value}`; headers["Authorization"] = `Token ${useCookie("token").value}`;
} }
const { data, error } = await useFetch<MovieList[]>( await $fetch<MovieList[]>(`${config.public.apiURL}/lists`, {
`${config.public.apiURL}/lists`,
{
method: "GET", method: "GET",
headers: headers, headers: headers,
}, })
); .then((data) => {
lists.value = data || [];
if (error.value) { })
if (error.value.statusCode === 401) { .catch((err) => {
if (err.statusCode === 401) {
useCookie("token").value = null;
navigateTo("/"); navigateTo("/");
} }
} else { });
lists.value = data.value || [];
}
}; };
onMounted(() => { onMounted(() => {

View file

@ -61,6 +61,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { Showing } from "~/types/showing"; import type { Showing } from "~/types/showing";
import type { Schedule } from "~/types/schedule"; import type { Schedule } from "~/types/schedule";
import { $fetch } from "ofetch";
import { useCookie } from "#app";
const schedule = defineModel<Schedule>("schedule"); const schedule = defineModel<Schedule>("schedule");
const past_showings = defineModel<Showing[]>("past_showings", { const past_showings = defineModel<Showing[]>("past_showings", {
@ -100,39 +102,33 @@ const getSchedule = async function (previous = false) {
let params = ""; let params = "";
if (previous) params = "?past_showings=true"; if (previous) params = "?past_showings=true";
const { data, error } = await useFetch<Schedule>( await $fetch(`${config.public.apiURL}/schedules/1${params}`, {
`${config.public.apiURL}/schedules/1${params}`,
{
method: "GET", method: "GET",
headers: { headers: {
Accept: "application/json", Accept: "application/json",
"Content-type": "application/json", "Content-type": "application/json",
Authorization: `Token ${useCookie("token").value}`, Authorization: `Token ${useCookie("token").value}`,
}, },
}, })
); .then((data) => {
if (error.value) {
if (error.value.statusCode === 401) {
navigateTo("/");
}
if (error.value.statusCode === 404) {
alert("Schedule not found");
navigateTo("/");
}
} else {
if (!data.value) {
alert("Schedule not found");
navigateTo("/");
} else {
if (previous) { if (previous) {
past_showings.value = data.value.past_showings; past_showings.value = data.past_showings;
} else { } else {
schedule.value = data.value; schedule.value = data;
} }
document.getElementById("loader")?.classList.toggle("hidden"); document.getElementById("loader")?.classList.toggle("hidden");
})
.catch((err) => {
switch (err.statusCode) {
case 401:
useCookie("token").value = null;
navigateTo("/");
break;
case 404:
alert("Unable to find schedule");
break;
} }
} });
return schedule; return schedule;
}; };