update meal-plan API on MealPlanStore

This commit is contained in:
smilerz
2023-12-17 15:02:20 -06:00
parent 205bf5253d
commit a59a78f44c

View File

@ -1,14 +1,14 @@
import {defineStore} from 'pinia' import { ApiApiFactory } from "@/utils/openapi/api"
import {ApiApiFactory} from "@/utils/openapi/api"; import { StandardToasts } from "@/utils/utils"
import { defineStore } from "pinia"
const _STORE_ID = 'meal_plan_store'
const _LOCAL_STORAGE_KEY = 'MEAL_PLAN_CLIENT_SETTINGS'
import Vue from "vue" import Vue from "vue"
import {StandardToasts} from "@/utils/utils";
const _STORE_ID = "meal_plan_store"
const _LOCAL_STORAGE_KEY = "MEAL_PLAN_CLIENT_SETTINGS"
/* /*
* 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
* */ * */
export const useMealPlanStore = defineStore(_STORE_ID, { export const useMealPlanStore = defineStore(_STORE_ID, {
state: () => ({ state: () => ({
plans: {}, plans: {},
@ -19,7 +19,7 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
plan_list: function () { plan_list: function () {
let plan_list = [] let plan_list = []
for (let key in this.plans) { for (let key in this.plans) {
plan_list.push(this.plans[key]); plan_list.push(this.plans[key])
} }
return plan_list return plan_list
}, },
@ -35,7 +35,7 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
servings: 1, servings: 1,
shared: [], shared: [],
title: "", title: "",
title_placeholder: 'Title', // meal plan edit modal should be improved to not need this title_placeholder: "Title", // meal plan edit modal should be improved to not need this
} }
}, },
client_settings: function () { client_settings: function () {
@ -43,7 +43,7 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
this.settings = this.loadClientSettings() this.settings = this.loadClientSettings()
} }
return this.settings return this.settings
} },
}, },
actions: { actions: {
refreshFromAPI(from_date, to_date) { refreshFromAPI(from_date, to_date) {
@ -51,14 +51,12 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
this.currently_updating = [from_date, to_date] // certainly no perfect check but better than nothing this.currently_updating = [from_date, to_date] // certainly no perfect check but better than nothing
let options = { let options = {
query: { from_date: from_date,
from_date: from_date, to_date: to_date,
to_date: to_date,
},
} }
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient.listMealPlans(options).then(r => { apiClient.listMealPlans(options).then((r) => {
r.data.forEach((p) => { r.data.forEach((p) => {
Vue.set(this.plans, p.id, p) Vue.set(this.plans, p.id, p)
}) })
@ -68,31 +66,40 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
}, },
createObject(object) { createObject(object) {
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
return apiClient.createMealPlan(object).then(r => { return apiClient
//StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE) .createMealPlan(object)
Vue.set(this.plans, r.data.id, r.data) .then((r) => {
return r //StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE)
}).catch(err => { Vue.set(this.plans, r.data.id, r.data)
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err) return r
}) })
.catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)
})
}, },
updateObject(object) { updateObject(object) {
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
return apiClient.updateMealPlan(object.id, object).then(r => { return apiClient
//StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_UPDATE) .updateMealPlan(object.id, object)
Vue.set(this.plans, object.id, object) .then((r) => {
}).catch(err => { //StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_UPDATE)
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_UPDATE, err) Vue.set(this.plans, object.id, object)
}) })
.catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_UPDATE, err)
})
}, },
deleteObject(object) { deleteObject(object) {
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
return apiClient.destroyMealPlan(object.id).then(r => { return apiClient
//StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_DELETE) .destroyMealPlan(object.id)
Vue.delete(this.plans, object.id) .then((r) => {
}).catch(err => { //StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_DELETE)
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_DELETE, err) Vue.delete(this.plans, object.id)
}) })
.catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_DELETE, err)
})
}, },
updateClientSettings(settings) { updateClientSettings(settings) {
this.settings = settings this.settings = settings
@ -110,6 +117,6 @@ export const useMealPlanStore = defineStore(_STORE_ID, {
} else { } else {
return JSON.parse(s) return JSON.parse(s)
} }
} },
}, },
}) })