mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-05-31 20:25:14 -04:00
feat: Set default number of days on meal planner (#3650)
This commit is contained in:
parent
af9e0f27a3
commit
e3c642debf
@ -18,6 +18,10 @@ export enum ImagePosition {
|
|||||||
right = "right",
|
right = "right",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UserMealPlanPreferences {
|
||||||
|
numberOfDays: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface UserRecipePreferences {
|
export interface UserRecipePreferences {
|
||||||
orderBy: string;
|
orderBy: string;
|
||||||
orderDirection: string;
|
orderDirection: string;
|
||||||
@ -40,6 +44,20 @@ export interface UserParsingPreferences {
|
|||||||
parser: RegisteredParser;
|
parser: RegisteredParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useUserMealPlanPreferences(): Ref<UserMealPlanPreferences> {
|
||||||
|
const fromStorage = useLocalStorage(
|
||||||
|
"meal-planner-preferences",
|
||||||
|
{
|
||||||
|
numberOfDays: 7,
|
||||||
|
},
|
||||||
|
{ mergeDefaults: true }
|
||||||
|
// we cast to a Ref because by default it will return an optional type ref
|
||||||
|
// but since we pass defaults we know all properties are set.
|
||||||
|
) as unknown as Ref<UserMealPlanPreferences>;
|
||||||
|
|
||||||
|
return fromStorage;
|
||||||
|
}
|
||||||
|
|
||||||
export function useUserPrintPreferences(): Ref<UserPrintPreferences> {
|
export function useUserPrintPreferences(): Ref<UserPrintPreferences> {
|
||||||
const fromStorage = useLocalStorage(
|
const fromStorage = useLocalStorage(
|
||||||
"recipe-print-preferences",
|
"recipe-print-preferences",
|
||||||
|
@ -291,6 +291,8 @@
|
|||||||
"mealplan-updated": "Mealplan Updated",
|
"mealplan-updated": "Mealplan Updated",
|
||||||
"no-meal-plan-defined-yet": "No meal plan defined yet",
|
"no-meal-plan-defined-yet": "No meal plan defined yet",
|
||||||
"no-meal-planned-for-today": "No meal planned for today",
|
"no-meal-planned-for-today": "No meal planned for today",
|
||||||
|
"numberOfDays-hint": "Number of days on page load",
|
||||||
|
"numberOfDays-label": "Default Days",
|
||||||
"only-recipes-with-these-categories-will-be-used-in-meal-plans": "Only recipes with these categories will be used in Meal Plans",
|
"only-recipes-with-these-categories-will-be-used-in-meal-plans": "Only recipes with these categories will be used in Meal Plans",
|
||||||
"planner": "Planner",
|
"planner": "Planner",
|
||||||
"quick-week": "Quick Week",
|
"quick-week": "Quick Week",
|
||||||
|
@ -23,6 +23,13 @@
|
|||||||
:first-day-of-week="firstDayOfWeek"
|
:first-day-of-week="firstDayOfWeek"
|
||||||
:local="$i18n.locale"
|
: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-spacer></v-spacer>
|
||||||
<v-btn text color="primary" @click="state.picker = false">
|
<v-btn text color="primary" @click="state.picker = false">
|
||||||
{{ $t("general.ok") }}
|
{{ $t("general.ok") }}
|
||||||
@ -47,10 +54,11 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<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 { isSameDay, addDays, parseISO } from "date-fns";
|
||||||
import { useGroupSelf } from "~/composables/use-groups";
|
import { useGroupSelf } from "~/composables/use-groups";
|
||||||
import { useMealplans } from "~/composables/use-group-mealplan";
|
import { useMealplans } from "~/composables/use-group-mealplan";
|
||||||
|
import { useUserMealPlanPreferences } from "~/composables/use-users/preferences";
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
middleware: ["auth"],
|
middleware: ["auth"],
|
||||||
@ -59,6 +67,12 @@ export default defineComponent({
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { group } = useGroupSelf();
|
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
|
// Force to /view if current route is /planner
|
||||||
if (route.value.path === "/group/mealplan/planner") {
|
if (route.value.path === "/group/mealplan/planner") {
|
||||||
router.push("/group/mealplan/planner/view");
|
router.push("/group/mealplan/planner/view");
|
||||||
@ -74,18 +88,16 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const state = ref({
|
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(),
|
start: new Date(),
|
||||||
picker: false,
|
picker: false,
|
||||||
end: addDays(new Date(), 6),
|
end: addDays(new Date(), adjustForToday(numberOfDays.value)),
|
||||||
});
|
});
|
||||||
|
|
||||||
const firstDayOfWeek = computed(() => {
|
const firstDayOfWeek = computed(() => {
|
||||||
return group.value?.preferences?.firstDayOfWeek || 0;
|
return group.value?.preferences?.firstDayOfWeek || 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
const recipeSearchTerm = ref("");
|
|
||||||
|
|
||||||
const weekRange = computed(() => {
|
const weekRange = computed(() => {
|
||||||
const sorted = state.value.range.sort((a, b) => {
|
const sorted = state.value.range.sort((a, b) => {
|
||||||
return parseYYYYMMDD(a).getTime() - parseYYYYMMDD(b).getTime();
|
return parseYYYYMMDD(a).getTime() - parseYYYYMMDD(b).getTime();
|
||||||
@ -99,7 +111,7 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
start: new Date(),
|
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 days = computed(() => {
|
||||||
const numDays =
|
const numDays =
|
||||||
Math.floor((weekRange.value.end.getTime() - weekRange.value.start.getTime()) / (1000 * 60 * 60 * 24)) + 1;
|
Math.floor((weekRange.value.end.getTime() - weekRange.value.start.getTime()) / (1000 * 60 * 60 * 24)) + 1;
|
||||||
@ -141,7 +159,7 @@ export default defineComponent({
|
|||||||
mealsByDate,
|
mealsByDate,
|
||||||
weekRange,
|
weekRange,
|
||||||
firstDayOfWeek,
|
firstDayOfWeek,
|
||||||
recipeSearchTerm,
|
numberOfDays,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
head() {
|
head() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user