munch-ease-frontend/src/components/meals/MealCard.vue

85 lines
2.3 KiB
Vue
Raw Normal View History

2024-01-13 02:00:15 +00:00
<template>
<div class="meal-card">
2024-10-16 07:30:45 +00:00
<h3>{{ mealTitle }}</h3>
<h4>{{ dayOfWeek }} <small>{{ date }}</small></h4>
2024-01-17 11:31:45 +00:00
<p>
Cooked by
2024-05-02 11:25:01 +00:00
<span v-for="(chef, index) in meal.chefs" :key="chef.id">
2024-10-16 07:30:45 +00:00
{{ chef.name }}{{ englishSeperator(index, meal.chefs) }}
2024-01-17 11:31:45 +00:00
</span>
<span v-if="!meal.chefs.length">somebody?</span>
</p>
<p>
For
2024-05-02 11:25:01 +00:00
<span v-for="(consumer, index) in meal.consumers" :key="consumer.id">
2024-10-16 07:30:45 +00:00
{{ consumer.name }}{{ englishSeperator(index, meal.consumers) }}
2024-01-17 11:31:45 +00:00
</span>
<span v-if="!meal.consumers.length">somebody?</span>
</p>
2024-05-20 23:57:49 +00:00
<p v-if="meal.purchase_date">
2024-10-15 08:40:06 +00:00
Purchased {{ ago(meal.purchase_date) }}
2024-05-20 23:57:49 +00:00
</p>
2024-01-13 02:00:15 +00:00
</div>
</template>
<style>
</style>
<script>
2024-10-15 08:40:06 +00:00
import { ago } from '@/dateformats.js'
2024-10-16 07:30:45 +00:00
function englishSeperator(index, list) {
switch (index) {
case list.length - 1:
return '';
case list.length - 2:
return ' and ';
default:
return ', ';
}
}
function englishList(list) {
switch (list.length) {
case 0:
return '';
case 1:
return list[0];
case 2:
return `${list[0]} and ${list[1]}`;
default:
return `${list.slice(0, -1).join(', ')}, and ${list.slice(-1)}`;
}
}
2024-01-13 02:00:15 +00:00
export default {
name: 'MealCard',
props: ['meal'],
computed: {
date() {
2024-05-25 02:09:27 +00:00
return this.meal.suggested_date.toLocaleDateString('en-au', { month: 'numeric', day: 'numeric' })
2024-01-13 02:00:15 +00:00
},
dayOfWeek() {
2024-05-25 02:09:27 +00:00
return this.meal.suggested_date.toLocaleDateString('en-au', { weekday: 'long' })
2024-01-17 11:57:44 +00:00
},
2024-10-16 07:30:45 +00:00
mealTitle() {
const recipesText = englishList(this.meal.recipes.map(mealRecipe => mealRecipe.recipe.name));
const ingredientsText = englishList(this.meal.extra_ingredients.map(ingredient => ingredient.name));
if (recipesText && ingredientsText) {
return `${recipesText} with ${ingredientsText}`;
} else if (recipesText || ingredientsText) {
return recipesText || ingredientsText;
} else {
return 'Nothing planned';
}
}
2024-01-17 11:57:44 +00:00
},
methods: {
2024-10-16 07:30:45 +00:00
englishSeperator,
2024-10-15 08:40:06 +00:00
ago
2024-01-13 02:00:15 +00:00
}
}
</script>