All files / munch-ease-frontend/src/components/ingredients IngredientLine.vue

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

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                                                                                                                                                         
<template>
  <div class="ingredient-item">
    <p>
      <input
        v-model="ingredientText"
        placeholder="Enter an ingredient"
        @keyup.enter="updateIngredient"
        @blur="updateIngredient"
      >
      <input
        v-if="ingredient.line"
        v-model="productLink"
        class="product-link-input"
        placeholder="Enter product link"
        @keyup.enter="updateProductLink"
        @blur="updateProductLink"
      >
    </p>
    <p v-if="ingredient.line">
      <!-- Single line parse results -->
      <compact-parsed-ingredient :ingredient="ingredient" />
    </p>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import CompactParsedIngredient from './CompactParsedIngredient.vue'
import type { Ingredient } from '@/domain/types'

const props = defineProps<{ ingredient: Ingredient }>()
const emit = defineEmits<{
  (e: 'update-ingredient', ingredient: Ingredient, newLine: string): void
  (e: 'update-product-link', ingredient: Ingredient, link: string): void
}>()

const ingredientText = ref<string>(props.ingredient?.line ?? '')
const productLink = ref<string>(props.ingredient.product?.link ?? '')

watch(
  () => props.ingredient,
  (newIngredient) => {
    ingredientText.value = newIngredient?.line ?? ''
    productLink.value = newIngredient?.product?.link ?? ''
  },
  { deep: true }
)

function updateIngredient() {
  if (ingredientText.value != props.ingredient.line) {
    emit('update-ingredient', props.ingredient, ingredientText.value)
  }
}

function updateProductLink() {
  if (productLink.value && productLink.value != props.ingredient.product?.link) {
    emit('update-product-link', props.ingredient, productLink.value)
  }
}
</script>

<style scoped>
input {
  border: 0;
  font-size: larger;
  border-bottom: 1px solid #ccc;
  border-left: 1px solid #ccc;
  width: 100%;
  padding: 0.5vh;
}

.product-link-input {
  color: #777;
  margin-top: 0.5vh;
}
</style>