Updates to Tasks
This commit is contained in:
@@ -1,23 +1,27 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { AppwriteIds, databases, ID } from 'src/boot/appwrite';
|
||||
import type { Models } from 'appwrite';
|
||||
import { Boat } from './boat';
|
||||
|
||||
export const TASKSTATUS = ['ready', 'complete', 'waiting', 'archived'];
|
||||
|
||||
export interface Task extends Partial<Models.Document> {
|
||||
title: string;
|
||||
description: string;
|
||||
required_skills: SkillTag[];
|
||||
tags: TaskTag[];
|
||||
/* Array of Appwrite Document IDs */
|
||||
required_skills: string[];
|
||||
/* Array of Appwrite Document IDs */
|
||||
tags: string[];
|
||||
due_date: string;
|
||||
duration: number;
|
||||
/* Array of Appwrite Document IDs */
|
||||
volunteers: string[];
|
||||
volunteers_required: number;
|
||||
status: string;
|
||||
depends_on: Task[];
|
||||
boat?: Boat;
|
||||
} // TODO: convert some of these strings into objects.
|
||||
/* Array of Appwrite Document IDs */
|
||||
depends_on: string[];
|
||||
/* Appwrite ID of a Boat resource */
|
||||
boat?: string[];
|
||||
}
|
||||
|
||||
export interface TaskTag extends Models.Document {
|
||||
name: string;
|
||||
@@ -46,24 +50,7 @@ export const useTaskStore = defineStore('tasks', {
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collectionIdTask
|
||||
);
|
||||
this.tasks = response.documents.map((document) => {
|
||||
// TODO: Should this be a GraphQL query, instead?
|
||||
// Map over `required_skills` and replace each skill ID with the corresponding skill object from `this.skillTags`
|
||||
const updatedRequiredSkills = document.required_skills.map(
|
||||
(skillId: string) =>
|
||||
this.skillTags.find((skillTag) => skillTag.$id === skillId) || {}
|
||||
);
|
||||
const updatedTaskTags = document.tags.map((tagid: string) =>
|
||||
this.taskTags.find((taskTag) => taskTag.$id === tagid)
|
||||
);
|
||||
|
||||
// Update the `required_skills` property of the document with the new array of skill objects
|
||||
return {
|
||||
...document,
|
||||
required_skills: updatedRequiredSkills,
|
||||
tags: updatedTaskTags,
|
||||
};
|
||||
}) as Task[];
|
||||
this.tasks = response.documents as Task[];
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch tasks', error);
|
||||
}
|
||||
@@ -112,10 +99,6 @@ export const useTaskStore = defineStore('tasks', {
|
||||
},
|
||||
async addTask(task: Task) {
|
||||
const newTask = <Models.Document>{ ...task };
|
||||
newTask.required_skills = task.required_skills.map((s) => s['$id']);
|
||||
newTask.tags = task.tags.map((s) => s['$id']);
|
||||
newTask.depends_on = task.depends_on.map((d) => d['$id']);
|
||||
newTask.boat = task.boat?.$id;
|
||||
try {
|
||||
const response = await databases.createDocument(
|
||||
AppwriteIds.databaseId,
|
||||
@@ -128,9 +111,32 @@ export const useTaskStore = defineStore('tasks', {
|
||||
console.error('Failed to add task:', error);
|
||||
}
|
||||
},
|
||||
async updateTask(task: Task) {
|
||||
const newTask = <Partial<Models.Document>>{
|
||||
...task,
|
||||
id: undefined,
|
||||
$databaseId: undefined,
|
||||
$collectionId: undefined,
|
||||
};
|
||||
if (!task.$id) {
|
||||
console.error('No Task ID!');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const response = await databases.updateDocument(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collectionIdTask,
|
||||
task.$id,
|
||||
newTask
|
||||
);
|
||||
this.tasks.push(response as Task);
|
||||
} catch (error) {
|
||||
console.error('Failed to update task:', error);
|
||||
}
|
||||
},
|
||||
// TODO: Enhance this store to include offline caching, and subscription notification when items change on the server.
|
||||
|
||||
filterTasks(searchQuery: string) {
|
||||
filterTasksByTitle(searchQuery: string) {
|
||||
const result = this.tasks.filter((task) =>
|
||||
task.title.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
);
|
||||
@@ -140,20 +146,14 @@ export const useTaskStore = defineStore('tasks', {
|
||||
},
|
||||
// Add more actions as needed (e.g., updateTask, deleteTask)
|
||||
getters: {
|
||||
// A getter to reconstruct the hierarchical structure from flat task data
|
||||
taskHierarchy: (state) => {
|
||||
function buildHierarchy(
|
||||
tasks: Task[],
|
||||
parentId: string | null = null
|
||||
): Task[] {
|
||||
return tasks
|
||||
.filter((task) => task.parentId === parentId)
|
||||
.map((task) => ({
|
||||
...task,
|
||||
subtasks: buildHierarchy(tasks, task.$id), // Assuming $id is the task ID field
|
||||
}));
|
||||
}
|
||||
return buildHierarchy(state.tasks);
|
||||
getTaskById: (state) => (id: string) => {
|
||||
return state.tasks.find((task) => task.$id === id) || null;
|
||||
},
|
||||
getTaskTagById: (state) => (id: string) => {
|
||||
return state.taskTags.find((tag) => tag.$id === id) || null;
|
||||
},
|
||||
getSkillById: (state) => (id: string) => {
|
||||
return state.skillTags.find((tag) => tag.$id === id) || null;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user