packlist/frontend/src/api.ts

79 lines
2.3 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 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, tagName: string): Promise<Item> {
const res = await fetch(`${API_BASE}/items/${itemId}/tags`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: tagName }),
});
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();
}