updated store

This commit is contained in:
vabene1111 2023-02-18 21:55:24 +01:00
parent c08c1d30ad
commit 8d85800e2f

View File

@ -3,6 +3,7 @@ import {ApiApiFactory} from "@/utils/openapi/api";
const _STORE_ID = 'meal_plan_store' const _STORE_ID = 'meal_plan_store'
import Vue from "vue" import Vue from "vue"
import {StandardToasts} from "@/utils/utils";
/* /*
* test store to play around with pinia and see if it can work for my usecases * test store to play around with pinia and see if it can work for my usecases
* dont trust that all mealplans are in store as there is no cache validation logic, its just a shared data holder * dont trust that all mealplans are in store as there is no cache validation logic, its just a shared data holder
@ -19,6 +20,20 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
plan_list.push(this.plans[key]); plan_list.push(this.plans[key]);
} }
return plan_list return plan_list
},
empty_meal_plan: function () {
return {
date: null,
id: -1,
meal_type: null,
note: "",
note_markdown: "",
recipe: null,
servings: 1,
shared: [],
title: "",
title_placeholder: 'Title', // meal plan edit modal should be improved to not need this
}
} }
}, },
actions: { actions: {
@ -41,7 +56,34 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
this.currently_updating = null this.currently_updating = null
}) })
} }
}, },
createObject(object) {
let apiClient = new ApiApiFactory()
return apiClient.createMealPlan(object).then(r => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE)
Vue.set(this.plans, r.data.id, r.data)
return r
}).catch(err => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)
})
},
updateObject(object) {
let apiClient = new ApiApiFactory()
return apiClient.updateMealPlan(object.id, object).then(r => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_UPDATE)
Vue.set(this.plans, object.id, object)
}).catch(err => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_UPDATE, err)
})
},
deleteObject(object) {
let apiClient = new ApiApiFactory()
return apiClient.destroyMealPlan(object.id).then(r => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_DELETE)
Vue.delete(this.plans, object.id)
}).catch(err => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_DELETE, err)
})
}
}, },
}) })