diff --git a/src/App.vue b/src/App.vue index e4957c8..0e48d17 100644 --- a/src/App.vue +++ b/src/App.vue @@ -10,11 +10,7 @@ const currentPath = computed(() => router.currentRoute.value.path);
- - - - - +
diff --git a/src/composables/useApi.ts b/src/composables/useApi.ts new file mode 100644 index 0000000..ee5b127 --- /dev/null +++ b/src/composables/useApi.ts @@ -0,0 +1,43 @@ +import { fetch } from '@tauri-apps/plugin-http'; + +const BASE_URL = 'https://automation.deep-node.de/webhook'; +const AUTH_HEADER = ''; + +export function useApi() { + const apiFetch = async (endpoint: string, options: RequestInit = {}) => { + const url = endpoint.startsWith('http') ? endpoint : `${BASE_URL}/${endpoint}`; + + const headers = { + 'Content-Type': 'application/json', + 'Authorization': AUTH_HEADER, + ...options.headers, + }; + + const response = await fetch(url, { + ...options, + headers, + }); + + if (!response.ok) { + throw new Error(`API call failed: ${response.statusText}`); + } + + return response; + }; + + const get = (endpoint: string, options: RequestInit = {}) => + apiFetch(endpoint, { ...options, method: 'GET' }); + + const post = (endpoint: string, body: any, options: RequestInit = {}) => + apiFetch(endpoint, { ...options, method: 'POST', body: JSON.stringify(body) }); + + const put = (endpoint: string, body: any, options: RequestInit = {}) => + apiFetch(endpoint, { ...options, method: 'PUT', body: JSON.stringify(body) }); + + return { + get, + post, + put, + fetch: apiFetch, + }; +} diff --git a/src/composables/useTasks.ts b/src/composables/useTasks.ts new file mode 100644 index 0000000..3d1d9f9 --- /dev/null +++ b/src/composables/useTasks.ts @@ -0,0 +1,73 @@ +import { ref } from 'vue'; +import { useApi } from './useApi.ts'; +import { Task } from '../types.ts'; + +const tasks = ref([]); +const isLoading = ref(false); +const error = ref(null); + +export function useTasks() { + const api = useApi(); + + const fetchTasks = async (force = false) => { + if (tasks.value.length > 0 && !force) return; + + isLoading.value = true; + error.value = null; + try { + const response = await api.get('e5880167-9322-4d7b-8a38-e06bae8a7734/list'); + const data = await response.json(); + tasks.value = data.tasks ?? []; + } catch (e: any) { + error.value = e.message || 'Failed to fetch tasks'; + console.error('Error fetching tasks:', e); + } finally { + isLoading.value = false; + } + }; + + const createTask = async (taskData: Partial) => { + isLoading.value = true; + error.value = null; + try { + // Get next ID as per current logic in CreateScreen.vue + const nextId = await api.get('d49dde4c-530d-46ee-8205-d1357563ac16') + .then((response) => response.json()) + .then((json) => json.nextId as number) + .catch(() => null); + + if (!nextId) throw new Error('Could not get next task ID'); + + const newTask: Partial = { + ...taskData, + id_: nextId, + logs: [], + archived: false, + }; + + await api.put('e5880167-9322-4d7b-8a38-e06bae8a7734/list', { tasks: [newTask] }); + + // Update local store (optimistic update or refetch) + // Since it's a PUT to '.../list' with {tasks: [task]}, it seems to add/update? + // Based on CreateScreen.vue, it just navigates back. + // To keep store in sync without full refetch, we could add it locally if we knew the full structure + // But maybe it's safer to refetch or at least push to local state if we have the full object. + // Let's refetch to be sure it's in sync with server. + await fetchTasks(true); + } catch (e: any) { + error.value = e.message || 'Failed to create task'; + console.error('Error creating task:', e); + throw e; + } finally { + isLoading.value = false; + } + }; + + return { + tasks, + isLoading, + error, + fetchTasks, + createTask, + }; +} diff --git a/src/screens/CreateScreen.vue b/src/screens/CreateScreen.vue index be1ec14..6fa6b93 100644 --- a/src/screens/CreateScreen.vue +++ b/src/screens/CreateScreen.vue @@ -1,25 +1,15 @@ diff --git a/src/screens/ListScreen.vue b/src/screens/ListScreen.vue index 86534f1..789bd7f 100644 --- a/src/screens/ListScreen.vue +++ b/src/screens/ListScreen.vue @@ -1,19 +1,16 @@