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

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

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
<template>
  <div :class="{ editing: editing }">
    <button
      v-if="editing"
      @click="emit('on-add')"
    >
      <img
        class="icon"
        :src="addCart"
      > <br>
      Add Ingredient
    </button>
    <button
      v-if="!editOnly"
      @click="toggleEditing"
    >
      <span v-if="editing">
        <img
          class="icon"
          :src="editOff"
        > <br>
        Done Editing
      </span>
      <span v-else>
        <img
          class="icon"
          :src="editOn"
        > <br>
        Edit My List
      </span>
    </button>
    <ul>
      <li
        v-for="ingredient in ingredients"
        :key="ingredient"
      >
        <div v-if="editing">
          <p class="ingredient-line">
            <ingredient-line
              :ingredient="ingredient"
              @update-ingredient="updateIngredient"
              @update-product-link="updateProduct"
            />
          </p>
          <button @click="emit('on-delete', ingredient)">
            <img
              class="icon"
              :src="trash"
            >
          </button>
        </div>
        <div v-else>
          <compact-parsed-ingredient :ingredient="ingredient" />
        </div>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { parseProduct, parseIngredients } from '@/api/sdk'
import IngredientLine from './IngredientLine.vue'
import CompactParsedIngredient from './CompactParsedIngredient.vue'
const addCart = new URL('@/assets/add-cart.svg', import.meta.url).toString()
const editOff = new URL('@/assets/edit-off.svg', import.meta.url).toString()
const editOn = new URL('@/assets/edit.svg', import.meta.url).toString()
const trash = new URL('@/assets/trash.svg', import.meta.url).toString()

const props = defineProps({
  ingredients: { type: Array, required: true },
  editOnly: { type: Boolean, default: false },
})
const emit = defineEmits(['on-add', 'on-delete', 'on-update-ingredient', 'on-editing'])

const editing = ref(props.editOnly ?? false)

async function updateProduct(ingredient, product_link) {
  const product = await parseProduct(ingredient, product_link)
  emit('on-update-ingredient', ingredient, { ...ingredient, product })
}

async function updateIngredient(ingredient, line) {
  const newIngredients = await parseIngredients([line])
  emit('on-update-ingredient', ingredient, newIngredients[0])
}

function toggleEditing() {
  editing.value = !editing.value
  emit('on-editing', editing.value)
}
</script>

<style scoped>
.icon {
  width: 2em;
  height: 2em;
}

ul {
  padding: 0;
}

li {
  list-style: none;
}

li > div {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  width: 100%;
}

.editing li {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 5px;
}

.ingredient-line {
  flex: 1;
  margin: 0;
  margin-right: 1em;
}
</style>