Skip to content

Commit

Permalink
add another command to delete all tasks and labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiss committed Jul 18, 2024
1 parent 969c449 commit d421aca
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
7 changes: 7 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export default class VikunjaPlugin extends Plugin {
await this.commands.moveAllTasksToDefaultProject();
}
})
this.addCommand({
id: "vikunja-reset-tasks",
name: "Reset vikunja instance, delete all tasks and labels",
callback: async () => {
await this.commands.resetTasksInVikunja();
}
})
}

private async handleEditorChange() {
Expand Down
37 changes: 37 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {App, Notice} from "obsidian";
import {getAPI} from "obsidian-dataview";
import VikunjaPlugin from "../../main";
import {PluginTask} from "../vaultSearcher/vaultSearcher";
import {ConfirmModal} from "../modals/confirmModal";

export default class Commands {
private plugin: VikunjaPlugin;
Expand Down Expand Up @@ -85,4 +86,40 @@ export default class Commands {

return await vaultSearcher.getTasks(taskParser);
}

async resetTasksInVikunja() {
if (this.plugin.settings.debugging) console.log("Reset tasks in Vikunja");

new ConfirmModal(this.app, async (result) => {
if (result !== "yes") {
if (this.plugin.settings.debugging) console.log("Reset tasks in Vikunja cancelled");
return;
}

if (this.plugin.settings.debugging) console.log("Resetting tasks in Vikunja confirmed");
new Notice("Deleting tasks and labels in Vikunja");

while (true) {
const tasks = await this.plugin.tasksApi.getAllTasks();
const labels = await this.plugin.labelsApi.getLabels()

if (tasks.length === 0 && labels.length === 0) {
if (this.plugin.settings.debugging) console.log("No tasks and labels found in Vikunja");
break;
}

// It is important to handle both tasks and labels in one go, because tasks throws an error, if deleted in one go, so there need some time between execution.
if (this.plugin.settings.debugging) console.log("Deleting tasks", tasks);
await this.plugin.tasksApi.deleteTasks(tasks);

if (this.plugin.settings.debugging) console.log("Deleting labels", labels);
await this.plugin.labelsApi.deleteLabels(labels);
}


if (this.plugin.settings.debugging) console.log("Resetting tasks in Vikunja done");
new Notice("Resetting tasks and labels in Vikunja done");
}
).open();
}
}
39 changes: 39 additions & 0 deletions src/modals/confirmModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {App, Modal, Setting} from "obsidian";

export class ConfirmModal extends Modal {
result: string;
onSubmit: (result: string) => void;

constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.onSubmit = onSubmit;
}

onOpen() {
const {contentEl} = this;

contentEl.createEl("h1", {text: "Are you sure to reset your vikunja instance?"});

new Setting(contentEl)
.setName("Enter 'yes' to confirm:")
.addText((text) =>
text.onChange((value) => {
this.result = value
}));

new Setting(contentEl)
.addButton((btn) =>
btn
.setButtonText("Submit")
.setCta()
.onClick(() => {
this.close();
this.onSubmit(this.result);
}));
}

onClose() {
let {contentEl} = this;
contentEl.empty();
}
}
7 changes: 7 additions & 0 deletions src/vikunja/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class Label {
if (this.plugin.settings.debugging) console.log("LabelsAPI: Created labels", createdLabels);
return createdLabels;
}

async deleteLabels(labels: ModelsLabel[]) {
for (const label of labels) {
if (!label.id) throw new Error("Label id is required to delete label");
await this.deleteLabel(label.id);
}
}
}

export {Label};
12 changes: 9 additions & 3 deletions src/vikunja/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ class Tasks {
}

async deleteTask(task: ModelsTask) {
if (this.plugin.settings.debugging) console.log("TasksApi: Deleting task", task);
if (!task.id) throw new Error("TasksApi: Task id is not defined");
const param: TasksIdDeleteRequest = {id: task.id};
return this.tasksApi.tasksIdDelete(param);
const taskId = task.id;
if (this.plugin.settings.debugging) console.log("TasksApi: Deleting task", taskId);
const param: TasksIdDeleteRequest = {id: taskId};
try {
const result = await this.tasksApi.tasksIdDelete(param);
if (this.plugin.settings.debugging) console.log("TasksApi: Deleted task", taskId, result);
} catch (error) {
console.error("Error deleting task", error);
}
}

async deleteTasks(tasksToDeleteInVikunja: ModelsTask[]) {
Expand Down

0 comments on commit d421aca

Please sign in to comment.