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

0% Statements 0/90
0% Branches 0/1
0% Functions 0/1
0% Lines 0/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                                     
<template>
  <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.purchaseDate">
      Purchased {{ ago(meal.purchaseDate) }}
    </p>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { ago } from '@/dateformats'
import type { Meal } from '@/domain/types'

const props = defineProps<{ meal: Meal }>()

function englishSeperator(index: number, list: Array<unknown>) {
  switch (index) {
    case list.length - 1:
      return ''
    case list.length - 2:
      return ' and '
    default:
      return ', '
  }
}

function englishList(list: string[]) {
  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)}`
  }
}

const date = computed(() =>
  props.meal.suggestedDate
    ? props.meal.suggestedDate.toLocaleDateString('en-au', { month: 'numeric', day: 'numeric' })
    : ''
)

const dayOfWeek = computed(() =>
  props.meal.suggestedDate ? props.meal.suggestedDate.toLocaleDateString('en-au', { weekday: 'long' }) : ''
)

const mealTitle = computed(() => {
  const recipes = props.meal.recipes ?? []
  const extras = props.meal.extraIngredients ?? []
  const recipeNames = recipes
    .map((mr) => mr.recipe?.name)
    .filter((n): n is string => typeof n === 'string' && n.length > 0)
  const recipesText = englishList(recipeNames)
  const ingredientsText = englishList(extras.map((i) => i.name))

  if (recipesText && ingredientsText) return `${recipesText} with ${ingredientsText}`
  if (recipesText || ingredientsText) return recipesText || ingredientsText
  return 'Nothing planned'
})
</script>

<style></style>