memorium/islands/KMenu/commands/add_series_infos.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-08 11:03:20 +02:00
import { MenuEntry } from "@islands/KMenu/types.ts";
2023-08-08 21:50:23 +02:00
import { TMDBSeries } from "@lib/types.ts";
2023-08-08 11:03:20 +02:00
import { getCookie } from "@lib/string.ts";
import { Series } from "@lib/resource/series.ts";
2023-08-08 21:50:23 +02:00
export const addSeriesInfo: MenuEntry = {
title: "Add Series infos",
2023-08-08 11:03:20 +02:00
meta: "",
icon: "IconReportSearch",
cb: async (state, context) => {
2023-08-08 21:50:23 +02:00
console.log({ state, context });
2023-08-08 11:03:20 +02:00
state.activeState.value = "loading";
2023-08-08 21:50:23 +02:00
const series = context as Series;
2023-08-08 11:03:20 +02:00
2023-08-08 21:50:23 +02:00
const query = series.name;
2023-08-08 11:03:20 +02:00
const response = await fetch(
`/api/tmdb/query?q=${encodeURIComponent(query)}&type=serie`,
);
2023-08-08 21:50:23 +02:00
const json = await response.json() as TMDBSeries[];
2023-08-08 11:03:20 +02:00
2023-08-08 21:50:23 +02:00
console.log({ json });
const menuID = `result/${series.name}`;
2023-08-08 11:03:20 +02:00
state.menus[menuID] = {
title: "Select",
entries: json.map((m) => ({
2023-08-08 21:50:23 +02:00
title: `${m.name || m.original_name} released ${m.first_air_date}`,
2023-08-08 11:03:20 +02:00
cb: async () => {
state.activeState.value = "loading";
2023-08-08 21:50:23 +02:00
console.log({ m });
await fetch(`/api/series/enhance/${series.name}/`, {
2023-08-08 11:03:20 +02:00
method: "POST",
body: JSON.stringify({ tmdbId: m.id }),
});
state.visible.value = false;
state.activeState.value = "normal";
2023-08-08 21:50:23 +02:00
//window.location.reload();
2023-08-08 11:03:20 +02:00
},
})),
};
state.activeMenu.value = menuID;
state.activeState.value = "normal";
},
visible: () => {
const loc = globalThis["location"];
if (!getCookie("session_cookie")) return false;
2023-08-08 21:50:23 +02:00
return (loc?.pathname?.includes("series") &&
!loc.pathname.endsWith("series"));
2023-08-08 11:03:20 +02:00
},
};