Refactor settings and task management, update API, and enhance type handling
- Replaced `username` and `password` fields with `accessKey` in `useSettings` for improved security. - Simplified task management in `useTasks`, adding `updateTasks` for bulk operations and refining API endpoint usage. - Updated `useApi` to dynamically use `VITE_API_URL` and switched to `Bearer` authentication. - Improved `Task` type definitions with renamed fields, nullable properties, and string-based timestamps. - Replaced `TaskStatus` enum values with string-based representations for better readability.
This commit is contained in:
@@ -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<string | undefined> {
|
||||
const settings = await getValue<Settings>('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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Settings>({ ...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', {
|
||||
...settings.value,
|
||||
password: encryptedPassword,
|
||||
accessKey: encryptedPassword,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Task[]>([])
|
||||
const isLoading = ref(false)
|
||||
const error = ref<string | null>(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<Task, 'title' | 'dueDate' | 'tag'>) => {
|
||||
// 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<Task, 'title' | 'due_date' | 'tag'>) => {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const newTask: Omit<Task, 'id'> = {
|
||||
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<Task>, 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,
|
||||
}
|
||||
}
|
||||
|
||||
19
src/types.ts
19
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user