diff --git a/src/composables/useApi.ts b/src/composables/useApi.ts index 6c033ee..e2b1c79 100644 --- a/src/composables/useApi.ts +++ b/src/composables/useApi.ts @@ -1,12 +1,8 @@ +import type { Settings } from './useSettings.ts' import { useCrypto } from './useCrypto.ts' import { useStore } from './useStore.ts' -const BASE_URL = 'https://automation.deep-node.de/webhook' - -interface Settings { - username: string - password: string -} +const BASE_URL = import.meta.env.VITE_API_URL function isTauri() { return typeof window !== 'undefined' && Boolean((window as typeof window & { __TAURI__?: unknown }).__TAURI__) @@ -18,19 +14,18 @@ async function buildAuthHeader(): Promise { const settings = await getValue('settings') if (!settings) return undefined - let { username, password } = settings - password = decrypt(password) as string + let { accessKey } = settings + accessKey = decrypt(accessKey) as string - if (username && password) { - const token = btoa(`${username}:${password}`) - return `Basic ${token}` + if (accessKey) { + return `Bearer ${accessKey}` } return undefined } export function useApi() { const apiFetch = async (endpoint: string, options: RequestInit = {}) => { - const url = endpoint.startsWith('http') ? endpoint : `${BASE_URL}/${endpoint}` + const url = endpoint.startsWith('http') ? endpoint : `${BASE_URL}${endpoint}` const authHeader = await buildAuthHeader() const headers = { @@ -62,13 +57,13 @@ export function useApi() { const post = (endpoint: string, body: unknown, options: RequestInit = {}) => apiFetch(endpoint, { ...options, method: 'POST', body: JSON.stringify(body) }) - const put = (endpoint: string, body: unknown, options: RequestInit = {}) => - apiFetch(endpoint, { ...options, method: 'PUT', body: JSON.stringify(body) }) + const patch = (endpoint: string, body: unknown, options: RequestInit = {}) => + apiFetch(endpoint, { ...options, method: 'PATCH', body: JSON.stringify(body) }) return { get, post, - put, + patch, fetch: apiFetch, } } diff --git a/src/composables/useSettings.ts b/src/composables/useSettings.ts index d9898bc..cf278fa 100644 --- a/src/composables/useSettings.ts +++ b/src/composables/useSettings.ts @@ -3,14 +3,12 @@ import { useCrypto } from './useCrypto.ts' import { useStore } from './useStore.ts' export interface Settings { - username: string - password: string + accessKey: string todayShown: boolean } const settingsDefault: Settings = { - username: '', - password: '', + accessKey: '', todayShown: false, } const settings = ref({ ...settingsDefault }) @@ -26,31 +24,31 @@ export function useSettings() { return } - let password = readSettings.password ?? '' - if (password) { + let accessKey = readSettings.accessKey ?? '' + if (accessKey) { try { - password = decrypt(password) as string + accessKey = decrypt(accessKey) as string } catch (error) { - console.warn('Failed to decrypt stored password:', error) + console.warn('Failed to decrypt stored accessKey:', error) } } settings.value = { ...settingsDefault, ...readSettings, - password, + accessKey, } } const saveSettings = async () => { - const encryptedPassword = settings.value.password - ? encrypt(settings.value.password) as string + const encryptedPassword = settings.value.accessKey + ? encrypt(settings.value.accessKey) as string : '' await setValue('settings', { ...settings.value, - password: encryptedPassword, + accessKey: encryptedPassword, }) } diff --git a/src/composables/useTasks.ts b/src/composables/useTasks.ts index 124ad04..69e0f15 100644 --- a/src/composables/useTasks.ts +++ b/src/composables/useTasks.ts @@ -1,12 +1,12 @@ import type { Task } from '../types.ts' import { useArrayUnique } from '@vueuse/core' import { ref } from 'vue' -import { TaskStatus } from '../types.ts' import { useApi } from './useApi.ts' const tasks = ref([]) const isLoading = ref(false) const error = ref(null) +const endpoint = '/items/pomodays' export function useTasks() { const api = useApi() @@ -17,7 +17,7 @@ export function useTasks() { isLoading.value = true error.value = null try { - const data = await api.get('e5880167-9322-4d7b-8a38-e06bae8a7734/list').then(res => res.json()) + const data = await api.get(`${endpoint}?limit=-1`).then(res => res.json()) tasks.value = data.tasks ?? [] } catch (e: any) { @@ -29,29 +29,12 @@ export function useTasks() { } } - const createTask = async (taskData: Pick) => { - // Get next ID as per current logic in CreateScreen.vue - const nextId = () => tasks.value.sort((a, b) => a.id_ - b.id_).reduce((acc, task) => { - if (task.id_ === acc + 1) - return acc + 1 - return acc - }, 0) + 1 + const createTask = async (taskData: Pick) => { isLoading.value = true error.value = null try { - const newTask: Omit = { - id_: nextId(), - status: TaskStatus.WAIT, - logs: [], - lastaction: Date.now(), - archived: false, - ...taskData, - } - - const data = await api.put('e5880167-9322-4d7b-8a38-e06bae8a7734/list', { tasks: [newTask] }).then(res => res.json()) - if (data.tasks) { - tasks.value = data.tasks - } + await api.post(endpoint, { ...taskData }).then(res => res.json()) + await fetchTasks() } catch (e: any) { error.value = e.message || 'Failed to create task' @@ -63,15 +46,29 @@ export function useTasks() { } } - const updateTask = async (task: Task | Task[]) => { + const updateTask = async (task: Task) => { isLoading.value = true error.value = null - const tasksToUpdate = (Array.isArray(task) ? task : [task]).map(t => ({ ...t, lastaction: Date.now() } as Task)) try { - const data = await api.put('e5880167-9322-4d7b-8a38-e06bae8a7734/list', { tasks: tasksToUpdate }).then(res => res.json()) - if (data.tasks) { - tasks.value = data.tasks - } + await api.patch(`${endpoint}/${task.id}`, task).then(res => res.json()) + await fetchTasks() + } + catch (e: any) { + error.value = e.message || 'Failed to update task' + console.error('Error updating task:', e) + throw e + } + finally { + isLoading.value = false + } + } + + const updateTasks = async (data: Partial, keys: Task['id'][]) => { + isLoading.value = true + error.value = null + try { + await api.patch(`${endpoint}`, { data, keys }).then(res => res.json()) + await fetchTasks() } catch (e: any) { error.value = e.message || 'Failed to update task' @@ -92,6 +89,7 @@ export function useTasks() { fetchTasks, createTask, updateTask, + updateTasks, categories, } } diff --git a/src/types.ts b/src/types.ts index 526d4a9..ff1cdff 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,24 +1,23 @@ export enum TaskStatus { - NONE, - DONE, - WIP, - WAIT, - FLAG, + ARCHIVE = 'archive', + DONE = 'done', + WIP = 'wip', + WAIT = 'wait', + FLAG = 'flag', } export interface Worklog { - start: number + start: string end: number } export interface Task { - archived: boolean tag: string title: string status: TaskStatus - lastaction: number | null - logs: Worklog[] - dueDate: number | null + lastaction: string | null + logs: Worklog[] | null + due_date: string | null id_: number id: number }