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

89 lines
2.1 KiB
Vue
Raw Normal View History

2024-01-13 02:00:15 +00:00
<template>
2025-10-18 02:08:22 +00:00
<div class="meal-card">
<h3>{{ mealTitle }}</h3>
<h4>
{{ dayOfWeek }} <small>{{ date }}</small>
</h4>
<p>
Cooked by
<span v-for="(chef, index) in meal.chefs" :key="chef.id">
{{ chef.name }}{{ englishSeperator(index, meal.chefs) }}
</span>
<span v-if="!meal.chefs.length">somebody?</span>
</p>
<p>
For
<span v-for="(consumer, index) in meal.consumers" :key="consumer.id">
{{ consumer.name }}{{ englishSeperator(index, meal.consumers) }}
</span>
<span v-if="!meal.consumers.length">somebody?</span>
</p>
<p v-if="meal.purchase_date">Purchased {{ ago(meal.purchase_date) }}</p>
</div>
2024-01-13 02:00:15 +00:00
</template>
2025-10-18 02:08:22 +00:00
<style></style>
2024-01-13 02:00:15 +00:00
<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) {
2025-10-18 02:08:22 +00:00
switch (index) {
case list.length - 1:
return ''
case list.length - 2:
return ' and '
default:
return ', '
}
2024-10-16 07:30:45 +00:00
}
function englishList(list) {
2025-10-18 02:08:22 +00:00
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-10-16 07:30:45 +00:00
}
2024-01-13 02:00:15 +00:00
export default {
2025-10-18 02:08:22 +00:00
name: 'MealCard',
props: ['meal'],
computed: {
date() {
return this.meal.suggested_date.toLocaleDateString('en-au', {
month: 'numeric',
day: 'numeric',
})
},
dayOfWeek() {
return this.meal.suggested_date.toLocaleDateString('en-au', { weekday: 'long' })
},
mealTitle() {
const recipesText = englishList(this.meal.recipes.map((mealRecipe) => mealRecipe.recipe.name))
const ingredientsText = englishList(
this.meal.extra_ingredients.map((ingredient) => ingredient.name)
)
2024-10-16 07:30:45 +00:00
2025-10-18 02:08:22 +00:00
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
},
2025-10-18 02:08:22 +00:00
},
methods: {
englishSeperator,
ago,
},
2024-01-13 02:00:15 +00:00
}
2025-10-18 02:08:22 +00:00
</script>