1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-24 15:49:42 +02:00

feat: Set default number of days on meal planner (#3650)

This commit is contained in:
boc-the-git 2024-05-27 07:30:15 +10:00 committed by GitHub
parent af9e0f27a3
commit e3c642debf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 7 deletions

View file

@ -23,6 +23,13 @@
:first-day-of-week="firstDayOfWeek"
:local="$i18n.locale"
>
<v-text-field
v-model="numberOfDays"
type="number"
:label="$t('meal-plan.numberOfDays-label')"
:hint="$t('meal-plan.numberOfDays-hint')"
persistent-hint
/>
<v-spacer></v-spacer>
<v-btn text color="primary" @click="state.picker = false">
{{ $t("general.ok") }}
@ -47,10 +54,11 @@
</template>
<script lang="ts">
import { computed, defineComponent, ref, useRoute, useRouter } from "@nuxtjs/composition-api";
import { computed, defineComponent, ref, useRoute, useRouter, watch } from "@nuxtjs/composition-api";
import { isSameDay, addDays, parseISO } from "date-fns";
import { useGroupSelf } from "~/composables/use-groups";
import { useMealplans } from "~/composables/use-group-mealplan";
import { useUserMealPlanPreferences } from "~/composables/use-users/preferences";
export default defineComponent({
middleware: ["auth"],
@ -59,6 +67,12 @@ export default defineComponent({
const router = useRouter();
const { group } = useGroupSelf();
const mealPlanPreferences = useUserMealPlanPreferences();
const numberOfDays = ref<number>(mealPlanPreferences.value.numberOfDays || 7);
watch(numberOfDays, (val) => {
mealPlanPreferences.value.numberOfDays = Number(val);
});
// Force to /view if current route is /planner
if (route.value.path === "/group/mealplan/planner") {
router.push("/group/mealplan/planner/view");
@ -74,18 +88,16 @@ export default defineComponent({
}
const state = ref({
range: [fmtYYYYMMDD(new Date()), fmtYYYYMMDD(addDays(new Date(), 6))] as [string, string],
range: [fmtYYYYMMDD(new Date()), fmtYYYYMMDD(addDays(new Date(), adjustForToday(numberOfDays.value)))] as [string, string],
start: new Date(),
picker: false,
end: addDays(new Date(), 6),
end: addDays(new Date(), adjustForToday(numberOfDays.value)),
});
const firstDayOfWeek = computed(() => {
return group.value?.preferences?.firstDayOfWeek || 0;
});
const recipeSearchTerm = ref("");
const weekRange = computed(() => {
const sorted = state.value.range.sort((a, b) => {
return parseYYYYMMDD(a).getTime() - parseYYYYMMDD(b).getTime();
@ -99,7 +111,7 @@ export default defineComponent({
}
return {
start: new Date(),
end: addDays(new Date(), 6),
end: addDays(new Date(), adjustForToday(numberOfDays.value)),
};
});
@ -113,6 +125,12 @@ export default defineComponent({
});
}
function adjustForToday(days: number) {
// The use case for this function is "how many days are we adding to 'today'?"
// e.g. If the user wants 7 days, we substract one to do "today + 6"
return days > 0 ? days - 1 : days + 1
}
const days = computed(() => {
const numDays =
Math.floor((weekRange.value.end.getTime() - weekRange.value.start.getTime()) / (1000 * 60 * 60 * 24)) + 1;
@ -141,7 +159,7 @@ export default defineComponent({
mealsByDate,
weekRange,
firstDayOfWeek,
recipeSearchTerm,
numberOfDays,
};
},
head() {