From 8d85800e2f8ff90b618858644dd54ef793418143 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 18 Feb 2023 21:55:24 +0100 Subject: [PATCH] updated store --- vue/src/stores/MealPlanStore.js | 44 ++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/vue/src/stores/MealPlanStore.js b/vue/src/stores/MealPlanStore.js index af58fe04..171d7222 100644 --- a/vue/src/stores/MealPlanStore.js +++ b/vue/src/stores/MealPlanStore.js @@ -3,6 +3,7 @@ import {ApiApiFactory} from "@/utils/openapi/api"; const _STORE_ID = 'meal_plan_store' 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 * 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]); } 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: { @@ -41,7 +56,34 @@ export const useMealPlanStore = defineStore(_STORE_ID, { 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) + }) + } }, }) \ No newline at end of file