Files
mealie/frontend/app/components/Domain/Household/GroupMealPlanEntryDialog.vue
T

293 lines
8.7 KiB
Vue

<template>
<BaseDialog
v-model="dialog"
:title="entry ? $t('meal-plan.update-this-meal-plan') : $t('meal-plan.create-a-new-meal-plan')"
:submit-text="entry ? $t('general.update') : $t('general.create')"
:icon="$globals.icons.foods"
:submit-disabled="submitDisabled"
color="primary"
width="1000"
can-submit
disable-submit-on-enter
@submit="submit"
>
<v-card-text>
<v-row>
<v-col
cols="12"
md="5"
>
<v-btn-toggle
v-model="entryMode"
mandatory
divided
variant="outlined"
color="primary"
class="w-100 mb-4"
>
<v-btn
value="recipe"
class="flex-grow-1"
>
<v-icon start>
{{ $globals.icons.silverwareForkKnife }}
</v-icon>
{{ $t("general.recipe") }}
</v-btn>
<v-btn
value="note"
class="flex-grow-1"
>
<v-icon start>
{{ $globals.icons.textBox }}
</v-icon>
{{ $t("meal-plan.note") }}
</v-btn>
</v-btn-toggle>
<MealPlanDatePicker v-model="pickerModel" :multiple="pickerMultiple" :entry-type="entryType" />
<!-- the dates are part of the label, so they stay with the switch instead of floating above the next field -->
<v-switch
v-if="showWeekOption"
v-model="isWeekEntry"
color="primary"
hide-details
>
<template #label>
<div>
<div>{{ $t("meal-plan.plan-for-the-whole-week") }}</div>
<div v-if="isWeekEntry" class="text-caption text-medium-emphasis">
{{ $d(weekRange.start, "short") }} - {{ $d(weekRange.end, "short") }}
</div>
</div>
</template>
</v-switch>
<v-select
v-model="entryType"
class="mt-4"
:items="planTypeOptions"
:label="$t('recipe.entry-type')"
item-title="text"
item-value="value"
:return-object="false"
hide-details
/>
<RecipeScaleEditButton
v-if="isRecipe && recipe"
v-model="recipeScale"
class="mt-4"
:recipe-servings="recipeServings"
edit-scale
/>
</v-col>
<!-- both modes fill the same pane, so the dialog doesn't resize when switching between them -->
<v-col
cols="12"
md="7"
class="entry-detail"
>
<RecipeSelector
v-if="isRecipe"
ref="selector"
v-model="recipe"
height="auto"
:query-filter="ruleQueryFilter"
>
<template #filters>
<v-switch
v-model="ignoreRules"
class="ignore-rules-switch flex-grow-0 ms-auto"
color="primary"
density="compact"
hide-details
:disabled="!applicableRuleFilter"
:label="$t('meal-plan.ignore-rules')"
/>
</template>
<template #no-results>
<v-alert
v-if="ruleQueryFilter"
type="info"
variant="tonal"
>
<div>{{ $t("meal-plan.no-recipes-match-your-rules") }}</div>
<v-btn
class="mt-2"
size="small"
color="info"
variant="tonal"
@click="ignoreRules = true"
>
{{ $t("meal-plan.ignore-rules") }}
</v-btn>
</v-alert>
<v-alert
v-else
type="info"
variant="tonal"
:text="$t('search.no-results')"
/>
</template>
</RecipeSelector>
<div v-else>
<v-text-field
v-model="title"
:label="$t('meal-plan.meal-title')"
:rules="[validators.required]"
/>
<v-textarea
v-model="text"
:label="$t('meal-plan.meal-note')"
rows="6"
/>
</div>
</v-col>
</v-row>
</v-card-text>
</BaseDialog>
</template>
<script setup lang="ts">
import RecipeScaleEditButton from "~/components/Domain/Recipe/RecipeScaleEditButton.vue";
import RecipeSelector from "~/components/Domain/Recipe/RecipeSelector.vue";
import { useMealPlanEntryDates, usePlanTypeOptions } from "~/composables/use-group-mealplan";
import { buildRuleQueryFilter, useMealplanRules } from "~/composables/use-mealplan-rules";
import { useUserMealPlanPreferences } from "~/composables/use-users/preferences";
import { validators } from "~/composables/use-validators";
import type { PlanEntryCreate, PlanEntryType, PlanEntryOut, PlanEntryUpdate } from "~/lib/api/types/meal-plan";
import type { RecipeSummary } from "~/lib/api/types/recipe";
interface Props {
entry?: PlanEntryOut | null;
date?: Date | null;
/** Set when creating an entry for a week rather than a specific day */
endDate?: Date | null;
}
const props = withDefaults(defineProps<Props>(), {
entry: null,
date: null,
endDate: null,
});
const emit = defineEmits<{
create: [payload: PlanEntryCreate];
update: [payload: PlanEntryUpdate];
}>();
const dialog = defineModel<boolean>({ required: true });
const { rules } = useMealplanRules();
const planTypeOptions = usePlanTypeOptions();
const selector = ref<InstanceType<typeof RecipeSelector> | null>(null);
const entryMode = ref<"recipe" | "note">("recipe");
const { selectedDate, isWeekEntry, weekRange, entryDates, entryDatesPayload, pickerModel, pickerMultiple }
= useMealPlanEntryDates();
const entryType = ref<PlanEntryType>("dinner");
const recipe = ref<RecipeSummary | null>(null);
const recipeScale = ref(1);
const title = ref("");
const text = ref("");
const ignoreRules = ref(false);
const isRecipe = computed(() => entryMode.value === "recipe");
const mealPlanPreferences = useUserMealPlanPreferences();
const showWeekOption = computed(() => mealPlanPreferences.value.mode === "weeks" || isWeekEntry.value);
const applicableRuleFilter = computed(() => buildRuleQueryFilter(
rules.value,
entryDates.value.date,
entryType.value,
entryDates.value.endDate,
));
const ruleQueryFilter = computed(() => ignoreRules.value ? null : applicableRuleFilter.value);
const recipeServings = computed(() => recipe.value?.recipeServings || recipe.value?.recipeYieldQuantity || 1);
// a scale is only meaningful for the recipe it was chosen for
watch(() => recipe.value?.id, () => {
recipeScale.value = 1;
}, { flush: "sync" });
const submitDisabled = computed(() => isRecipe.value ? !recipe.value : !title.value.trim());
function parseEntryDate(date: string) {
const [year, month, day] = date.split("-").map(Number);
return new Date(year, month - 1, day);
}
function initialize() {
entryMode.value = props.entry && !props.entry.recipeId ? "note" : "recipe";
selectedDate.value = props.entry ? parseEntryDate(props.entry.date) : props.date ?? new Date();
isWeekEntry.value = props.entry ? !!props.entry.endDate : !!props.endDate;
entryType.value = props.entry?.entryType ?? "dinner";
recipe.value = props.entry?.recipe ?? null;
recipeScale.value = props.entry?.recipeScale ?? 1;
title.value = props.entry?.title ?? "";
text.value = props.entry?.text ?? "";
ignoreRules.value = false;
selector.value?.reset();
}
function submit() {
const payload = {
...entryDatesPayload.value,
entryType: entryType.value,
title: isRecipe.value ? "" : title.value,
text: isRecipe.value ? "" : text.value,
recipeId: isRecipe.value ? recipe.value?.id : null,
recipeScale: isRecipe.value ? recipeScale.value : 1,
};
if (props.entry) {
emit("update", {
...payload,
id: props.entry.id,
groupId: props.entry.groupId,
userId: props.entry.userId,
});
}
else {
emit("create", payload);
}
}
watch(dialog, (isOpen) => {
if (isOpen) {
initialize();
}
});
</script>
<style scoped>
.entry-detail {
position: relative;
min-height: clamp(320px, 45vh, 520px);
}
/*
Take the pane out of flow so a long result list scrolls inside it instead of growing the
dialog, while it still stretches to the height of the settings column beside it.
The inset matches the v-col gutter padding.
*/
.entry-detail > * {
position: absolute;
inset: 12px;
overflow-y: auto;
}
/* v-switch reserves a taller control than the filter buttons next to it */
.ignore-rules-switch {
--v-input-control-height: 28px;
align-self: flex-start;
}
</style>