Split category and category-group stores

This commit is contained in:
2022-09-23 19:43:36 +00:00
parent 5658f31457
commit b55744aad7
4 changed files with 50 additions and 38 deletions

View File

@@ -0,0 +1,39 @@
import { defineStore } from "pinia";
import { POST } from "../api";
import { useBudgetsStore } from "./budget";
interface State {
CategoryGroups: Map<string, CategoryGroup>;
}
export interface CategoryGroup {
ID: string;
Name: string;
CategoryIDs: string[];
}
export const useCategoryGroupStore = defineStore("category-group", {
state: (): State => ({
CategoryGroups: new Map<string, CategoryGroup>(),
}),
getters: {
},
actions: {
async CreateCategoryGroup(
group: string,
) {
const result = await POST(
"/category-group/new",
JSON.stringify({
budgetId: useBudgetsStore().CurrentBudgetID,
group: group,
})
);
const response = await result.json();
this.AddCategoryGroup(response);
},
async AddCategoryGroup(categoryGroup : CategoryGroup){
this.CategoryGroups.set(categoryGroup.ID, categoryGroup);
}
},
});

View File

@@ -1,12 +1,9 @@
import { defineStore } from "pinia";
import { GET, POST } from "../api";
import { POST } from "../api";
import { useBudgetsStore } from "./budget";
import { useAccountStore } from "./budget-account";
import { Budget, useSessionStore } from "./session";
interface State {
Categories: Map<string, Category>;
CategoryGroups: Map<string, CategoryGroup>;
}
export interface Category {
@@ -18,32 +15,13 @@ export interface Category {
Activity: number;
}
export interface CategoryGroup {
ID: string;
Name: string;
}
export const useCategoryStore = defineStore("category", {
state: (): State => ({
Categories: new Map<string, Category>(),
CategoryGroups: new Map<string, CategoryGroup>(),
}),
getters: {
},
actions: {
async CreateCategoryGroup(
group: string,
) {
const result = await POST(
"/category-group/new",
JSON.stringify({
budgetId: useBudgetsStore().CurrentBudgetID,
group: group,
})
);
const response = await result.json();
useCategoryStore().AddCategoryGroup(response);
},
async CreateCategory(
group: string,
name: string,
@@ -57,13 +35,10 @@ export const useCategoryStore = defineStore("category", {
})
);
const response = await result.json();
useCategoryStore().AddCategory(response);
this.AddCategory(response);
},
async AddCategory(category : Category){
this.Categories.set(category.ID, category);
},
async AddCategoryGroup(categoryGroup : CategoryGroup){
this.CategoryGroups.set(categoryGroup.ID, categoryGroup);
}
},
});