From a3ab682575ec1783ee5d412ae3f8ae0f3a98dbe1 Mon Sep 17 00:00:00 2001 From: DCsunset Date: Mon, 6 Jul 2020 22:32:59 -0700 Subject: [PATCH] feat(frontend): add vuex --- frontend/store/index.ts | 36 ++++++++++++++++++++++++++++++++++++ frontend/types/index.d.ts | 13 +++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 frontend/store/index.ts create mode 100644 frontend/types/index.d.ts diff --git a/frontend/store/index.ts b/frontend/store/index.ts new file mode 100644 index 0000000..7584f19 --- /dev/null +++ b/frontend/store/index.ts @@ -0,0 +1,36 @@ +import { ActionTree, MutationTree } from 'vuex'; +import { Task } from 'taskwarrior-lib'; +import { getAccessorType } from 'typed-vuex'; + +export const state = () => ({ + tasks: [] as Task[] +}); + +export type RootState = ReturnType; + +export const mutations: MutationTree = { + setTasks(state, tasks: Task[]) { + state.tasks = tasks; + } +}; + +export const actions: ActionTree = { + async fetchTasks(context) { + const tasks: Task[] = await this.$axios.$get('/api/tasks'); + context.commit('setTasks', tasks); + }, + + async deleteTasks(context, tasks: Task[]) { + await this.$axios.$delete('/api/tasks', { + params: { tasks: tasks.map(task => task.uuid) } + }); + const newTasks = context.state.tasks.filter(task => tasks.findIndex(t => t.uuid === task.uuid) === -1); + context.commit('setTasks', newTasks); + } +}; + +export const accessorType = getAccessorType({ + state, + mutations, + actions +}); diff --git a/frontend/types/index.d.ts b/frontend/types/index.d.ts new file mode 100644 index 0000000..79bbd82 --- /dev/null +++ b/frontend/types/index.d.ts @@ -0,0 +1,13 @@ +import { accessorType } from '~/store'; + +declare module 'vue/types/vue' { + interface Vue { + $accessor: typeof accessorType + } +} + +declare module '@nuxt/types' { + interface NuxtAppOptions { + $accessor: typeof accessorType + } +}