packlist/frontend/src/api.ts

130 lines
3.8 KiB
TypeScript

export interface Tag {
id: string;
name: string;
}
export interface Item {
id: string;
name: string;
tags: Tag[];
}
const API_BASE = "http://localhost:8000"; // ggf. anpassen
export async function getSeed() {
return fetch(`${API_BASE}/dev/seed`).then(res => res.json());
}
export async function getTrips() {
return fetch(`${API_BASE}/trips/`).then(res => res.json());
}
export async function getTripItems(tripId: string) {
return fetch(`${API_BASE}/trip-items/by-trip/${tripId}`).then(res => res.json());
}
export async function toggleTripItem(tripItemId: string) {
return fetch(`${API_BASE}/trip-items/${tripItemId}/toggle`, { method: 'POST' }).then(res => res.json());
}
export async function getItems(): Promise<Item[]> {
const res = await fetch(`${API_BASE}/items/`);
if (!res.ok) throw new Error("Failed to fetch items");
return res.json();
}
export async function getTags(): Promise<Tag[]> {
const res = await fetch(`${API_BASE}/tags/`);
if (!res.ok) throw new Error("Failed to fetch tags");
return res.json();
}
export async function updateItemName(itemId: string, name: string): Promise<Item> {
const res = await fetch(`${API_BASE}/items/${itemId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
if (!res.ok) throw new Error("Failed to update item");
return res.json();
}
export async function deleteItem(itemId: string): Promise<void> {
const res = await fetch(`${API_BASE}/items/${itemId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete item");
}
export async function deleteItemTag(itemId: string, tagId: string): Promise<Item> {
const res = await fetch(`${API_BASE}/items/${itemId}/tags/${tagId}`, {
method: "DELETE",
});
if (!res.ok) throw new Error("Failed to delete tag from item");
return res.json();
}
export async function addItemTag(itemId: string, tagId: string): Promise<Item> {
const res = await fetch(`${API_BASE}/items/${itemId}/tags`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tag_id: tagId }),
});
if (!res.ok) throw new Error("Failed to add tag to item");
return res.json();
}
export async function createItem(name: string, tags: string[]): Promise<Item> {
const res = await fetch(`${API_BASE}/items/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, tags }),
});
if (!res.ok) throw new Error("Failed to create item");
return res.json();
}
export async function getNextTripId(): Promise<string> {
const res = await fetch(`${API_BASE}/trips/next-id`);
if (!res.ok) throw new Error("No upcoming trip found");
return res.json();
}
export async function deleteTrip(tripId: string): Promise<void> {
const res = await fetch(`${API_BASE}/trips/${tripId}`, { method: "DELETE" });
if (!res.ok) throw new Error("Failed to delete trip");
}
export async function createTrip(data: {
name: string;
start_date: string;
end_date: string;
selected_tag_ids?: string[];
marked_tag_ids?: string[];
}): Promise<any> {
const res = await fetch(`${API_BASE}/trips/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) throw new Error("Failed to create trip");
return res.json();
}
export async function updateTrip(
tripId: string,
data: {
name: string;
start_date: string;
end_date: string;
selected_tag_ids: string[];
marked_tag_ids: string[];
}
): Promise<any> {
const res = await fetch(`${API_BASE}/trips/${tripId}/reconfigure`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) throw new Error("Failed to update trip");
return res.json();
}