+
+
+
+
+
+
+ {{ meal_type.icon }} {{
+ meal_type.name
+ }}
+
-
-
- {{ $t("Servings") }}
-
-
-
- {{ $t("Share") }}
-
-
-
- {{
- $t("AddToShopping")
- }}
-
-
-
-
-
-
{{ $t("Start Day") }}
-
-
{{ $t("End Day") }}
-
-
+
+
+
+
+
+
+
+
+ {{ $t("Servings") }}
+
+
+
+ {{ $t("Share") }}
+
+
+
+ {{
+ $t("AddToShopping")
+ }}
+
-
-
-
{{ $t("Create Meal Plan") }}
-
{{ $t("Exit") }}
+
+
+
+
{{ $t("Start Day") }}
+
+
{{ $t("End Day") }}
+
+
+
+
+
+
+
+
+ {{ $t("Create Meal Plan") }}
+
+ {{ $t("Exit") }}
+
-
@@ -84,9 +87,11 @@ import GenericMultiselect from "@/components/GenericMultiselect"
import {ApiMixin} from "@/utils/utils"
import {useUserPreferenceStore} from "@/stores/UserPreferenceStore";
import VueCookies from "vue-cookies";
+import moment from "moment/moment";
+import {useMealPlanStore} from "@/stores/MealPlanStore";
-const { ApiApiFactory } = require("@/utils/openapi/api")
-const { StandardToasts } = require("@/utils/utils")
+const {ApiApiFactory} = require("@/utils/openapi/api")
+const {StandardToasts} = require("@/utils/utils")
Vue.use(BootstrapVue)
Vue.use(VueCookies)
@@ -94,8 +99,8 @@ let MEALPLAN_COOKIE_NAME = "mealplan_settings"
export default {
name: "AutoMealPlanModal",
- components: {
- GenericMultiselect
+ components: {
+ GenericMultiselect
},
props: {
modal_title: String,
@@ -108,22 +113,23 @@ export default {
mixins: [ApiMixin],
data() {
return {
- AutoPlan: {
- meal_types: [],
- keywords: [[]],
- servings: 1,
- date: Date.now(),
- startDay: null,
- endDay: null,
- shared: [],
- addshopping: false
- },
- mealplan_settings: {
+ AutoPlan: {
+ meal_types: [],
+ keywords: [[]],
+ servings: 1,
+ date: Date.now(),
+ startDay: null,
+ endDay: null,
+ shared: [],
+ addshopping: false
+ },
+ mealplan_settings: {
addshopping: false,
- }
+ },
+ loading: false,
}
},
- watch: {
+ watch: {
mealplan_settings: {
handler(newVal) {
this.$cookies.set(MEALPLAN_COOKIE_NAME, this.mealplan_settings)
@@ -141,22 +147,25 @@ export default {
},
methods: {
genericSelectChanged: function (obj) {
- this.AutoPlan.keywords[obj.var] = obj.val
+ this.AutoPlan.keywords[obj.var] = obj.val
},
showModal() {
- if (this.$cookies.isKey(MEALPLAN_COOKIE_NAME)) {
+ if (this.$cookies.isKey(MEALPLAN_COOKIE_NAME)) {
this.mealplan_settings = Object.assign({}, this.mealplan_settings, this.$cookies.get(MEALPLAN_COOKIE_NAME))
- }
- this.refreshMealTypes()
+ }
+ this.refreshMealTypes()
- this.AutoPlan.servings = 1
- this.AutoPlan.startDay = new Date()
- this.AutoPlan.endDay = this.current_period.periodEnd
- useUserPreferenceStore().getData().then(userPreference => {
- this.AutoPlan.shared = userPreference.plan_share
+ this.AutoPlan.servings = 1
+ this.AutoPlan.startDay = new Date()
+ this.AutoPlan.endDay = this.current_period.periodEnd
+ useUserPreferenceStore().getData().then(userPreference => {
+ this.AutoPlan.shared = userPreference.plan_share
})
+ this.AutoPlan.addshopping = this.mealplan_settings.addshopping
+
+ this.loading = false
},
- sortMealTypes() {
+ sortMealTypes() {
this.meal_types.forEach(function (element, index) {
element.order = index
})
@@ -178,7 +187,7 @@ export default {
})
})
},
- refreshMealTypes() {
+ refreshMealTypes() {
let apiClient = new ApiApiFactory()
Promise.resolve(apiClient.listMealTypes().then((result) => {
@@ -186,30 +195,57 @@ export default {
meal_type.editing = false
})
this.AutoPlan.meal_types = result.data
- })).then( () => {
- let mealArray = this.AutoPlan.meal_types
- for (let i = 0; i < mealArray.length; i++) {
- this.AutoPlan.keywords[i] = [];
- }}
+ })).then(() => {
+ let mealArray = this.AutoPlan.meal_types
+ for (let i = 0; i < mealArray.length; i++) {
+ this.AutoPlan.keywords[i] = [];
+ }
+ }
)
},
- createPlan() {
- this.$bvModal.hide(`autoplan-modal`)
- this.AutoPlan.addshopping = this.mealplan_settings.addshopping
- this.$emit("create-plan", this.AutoPlan)
+ createPlan() {
+ if (!this.loading) {
+ this.loading = true
+
+ let requests = []
+ for (let i = 0; i < this.AutoPlan.meal_types.length; i++) {
+ if (this.AutoPlan.keywords[i].length === 0) continue
+ requests.push(this.autoPlanThread(this.AutoPlan, i))
+ }
+
+ Promise.allSettled(requests).then(r => {
+ this.refreshEntries()
+ this.loading = false
+ this.$bvModal.hide(`autoplan-modal`)
+ }).catch(err => {
+ StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)
+ this.loading = false
+ })
+ }
+
},
- updateStartDay(date){
- this.AutoPlan.startDay = date
- },
- updateEndDay(date){
- this.AutoPlan.endDay = date
- },
- updateServings(numberOfServings) {
- this.AutoPlan.servings = numberOfServings
- },
- exitPlan() {
- this.$bvModal.hide(`autoplan-modal`)
- }
+
+ async autoPlanThread(autoPlan, mealTypeIndex) {
+ let apiClient = new ApiApiFactory()
+ let data = {
+ "start_date": moment(autoPlan.startDay).format("YYYY-MM-DD"),
+ "end_date": moment(autoPlan.endDay).format("YYYY-MM-DD"),
+ "meal_type_id": autoPlan.meal_types[mealTypeIndex].id,
+ "keywords": autoPlan.keywords[mealTypeIndex],
+ "servings": autoPlan.servings,
+ "shared": autoPlan.shared,
+ "addshopping": autoPlan.addshopping
+ }
+ return apiClient.createAutoPlanViewSet(data)
+
+ },
+ refreshEntries() { //TODO move properly to MealPLanStore (save period for default refresh)
+ let date = this.current_period
+ useMealPlanStore().refreshFromAPI(moment(date.periodStart).format("YYYY-MM-DD"), moment(date.periodEnd).format("YYYY-MM-DD"))
+ },
+ exitPlan() {
+ this.$bvModal.hide(`autoplan-modal`)
+ }
},