memorium/islands/KMenu/commands/add_movie_infos.ts

52 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2023-08-28 22:45:20 +02:00
2023-08-04 13:48:12 +02:00
import { Movie } from "@lib/resource/movies.ts";
import { TMDBMovie } from "@lib/types.ts";
2023-08-04 22:35:25 +02:00
import { getCookie } from "@lib/string.ts";
2023-08-04 13:48:12 +02:00
export const addMovieInfos: MenuEntry = {
title: "Add Movie infos",
meta: "",
icon: "IconReportSearch",
cb: async (state, context) => {
state.activeState.value = "loading";
const movie = context as Movie;
const query = movie.name;
const response = await fetch(
`/api/tmdb/query?q=${encodeURIComponent(query)}`,
);
const json = await response.json() as TMDBMovie[];
const menuID = `result/${movie.name}`;
state.menus[menuID] = {
title: "Select",
entries: json.map((m) => ({
title: `${m.title} released ${m.release_date}`,
cb: async () => {
state.activeState.value = "loading";
await fetch(`/api/movies/enhance/${movie.name}/`, {
method: "POST",
body: JSON.stringify({ tmdbId: m.id }),
});
state.visible.value = false;
state.activeState.value = "normal";
window.location.reload();
},
})),
};
state.activeMenu.value = menuID;
2023-08-09 23:51:40 +02:00
state.commandInput.value = "";
2023-08-04 13:48:12 +02:00
state.activeState.value = "normal";
},
visible: () => {
const loc = globalThis["location"];
2023-08-04 22:35:25 +02:00
if (!getCookie("session_cookie")) return false;
2023-08-07 14:44:04 +02:00
return (loc?.pathname?.includes("movie") &&
2023-08-08 21:50:23 +02:00
!loc.pathname.endsWith("movies"));
2023-08-04 13:48:12 +02:00
},
};