All files / components/Admin/AdminBundles BundleForm.vue

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                                                                                                                                                                1x   1x 3x                                                                                                                                                                 1x        
<template>
  <div class="mb-3 p-3">
    <div class="mb-2">
      <label class="d-flex" for="first-name"> Title </label>
      <input
        id="title"
        type="text"
        placeholder="Enter title"
        v-model.trim="currentBundle.title"
        required
        autofocus="true"
        class="w-100"
      />
    </div>
    <div class="mb-2">
      <label class="d-flex" for="first-name"> Description </label>
      <input
        id="description"
        type="text"
        placeholder="Enter description"
        v-model.trim="currentBundle.description"
        required
        autofocus="true"
        class="w-100"
      />
    </div>
    <div class="mb-2">
      <label class="d-flex" for="first-name"> Price </label>
      <input
        id="price"
        type="number"
        placeholder="Enter price"
        v-model="currentBundle.price"
        required
        autofocus="true"
        class="w-100"
      />
    </div>
    <div class="d-flex flex-column mb-2">
      <label class="d-flex" for="published_at">Status</label>
      <select id="published_at" required v-model="status">
        <option value="published">published</option>
        <option value="null">draft</option>
      </select>
    </div>
    <div class="mb-2">
      <label class="d-flex"> Products </label>
      <SelectWithSearch
        :data="products"
        :filter="filterProducts"
        :clickItem="addProduct"
        :placeholder="'Serch products'"
        class="mb-3"
      >
        <template v-slot:item="products">
          <ProductCard class="w-100 m-0 mb-2" :product="products.item" />
        </template>
      </SelectWithSearch>
      <div v-if="currentBundle.products.length">
        <ul class="p-0">
          <li
            v-for="product in currentBundle.products"
            :key="product.id"
            class="row w-100 mb-2 m-0 justify-content-between"
          >
            <div class="col-11 p-0">
              <ProductCard class="w-100 m-0" :product="product" />
            </div>
            <div
              class="col-1 d-flex justify-content-center align-items-start p-0"
              v-on:click="deleteProduct(product.id)"
            >
              <BIconX />
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
 
<script>
import SelectWithSearch from "~/components/common/SelectWithSearch.vue";
import ProductCard from "~/components/Admin/common/ProductCard.vue";
 
export default {
  name: "BundleForm",
  components: {
    SelectWithSearch,
    ProductCard,
    ProductCard,
  },
  props: {
    bundle: Object,
  },
  data: () => ({
    status: "null",
    currentBundle: {
      products: [],
      title: "",
      desription: "",
      price: 0,
    },
    products: [],
  }),
  watch: {
    status: function () {
      const { published_at } = this.currentBundle;
 
      if (this.status === "null") {
        this.currentBundle.published_at = null;
      } else if (this.status !== null && published_at === null) {
        this.currentBundle.published_at = new Date();
      }
 
      this.$emit("getBundle", this.currentBundle);
    },
    currentBundle: {
      handler() {
        const { published_at } = this.currentBundle;
 
        if (this.status === "null") {
          this.currentBundle.published_at = null;
        } else if (this.status !== null && published_at === null) {
          this.currentBundle.published_at = new Date();
        }
 
        this.$emit("getBundle", this.currentBundle);
      },
      deep: true,
    },
    bundle: function () {
      this.currentBundle = this.bundle;
    },
  },
  methods: {
    filterProducts: function (text, data) {
      return data.filter((item) =>
        item.title.toLowerCase().includes(text.toLowerCase())
      );
    },
    addProduct: function (item) {
      const index = this.currentBundle.products.findIndex(
        (p) => p.id === item.id
      );
      if (index === -1) {
        this.currentBundle.products.push(item);
      }
    },
    deleteProduct: function (id) {
      this.currentBundle.products = this.currentBundle.products.filter(
        (product) => product.id != id
      );
    },
  },
  async beforeMount() {
    if (this.bundle) {
      this.currentBundle = this.bundle;
      if (this.currentBundle.published_at) {
        this.status = "published";
      }
    }
    this.products = await this.$strapi.find("products");
  },
};
</script>
 
<style scoped>
</style>