All files / components/Admin/AdminProducts/AdminProduct ProductMedia.vue

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

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                                                                                                    1x   1x   1x 1x   1x 3x                                                                                                                                                                           1x                                          
<template>
  <div class="block d-flex flex-column">
    <label class="d-flex font-weight-bold p-2" for="media">Media</label>
    <div class="row w-100 m-0 justify-content-center" id="media">
      <div class="col-12 mb-2">
        <label class="btn btn-primary w-100">
          <i class="fa fa-image"></i> Add<input
            type="file"
            style="display: none"
            name="image"
            accept="image/*"
            multiple
            v-on:change="addFile"
          />
        </label>
      </div>
      <div
        class="image-wrap col-5 position-relative m-2"
        v-for="(image, index) in images"
        :key="image.id"
      >
        <div
          class="
            image-buttons
            position-absolute
            align-items-center
            justify-content-center
          "
        >
          <button
            class="btn btn-primary m-1"
            type="button"
            v-on:click="setImageIndex(index)"
          >
            show
          </button>
          <button
            class="btn btn-danger m-1"
            type="button"
            v-on:click="deleteFromImages(index)"
          >
            delete
          </button>
        </div>
        <PreloaderImage :image="image.url" rounded />
      </div>
    </div>
    <ConfirmModal :id="'confirm-delete-image'" v-on:confirm="confirm" />
  </div>
</template>
 
<script>
import { mapMutations, mapActions, mapGetters } from "vuex";
 
import { getStrapiMedia } from "~/utils/medias";
import { warn } from "~/utils/warn";
 
import PreloaderImage from "~/components/common/PreloaderImage.vue";
import ConfirmModal from "~/components/Admin/common/ConfirmModal.vue";
 
export default {
  name: "ProductMedia",
  components: { PreloaderImage, ConfirmModal },
  data: () => ({
    images: [],
    removeImageIndex: null,
  }),
  computed: {
    ...mapGetters({
      selected: "admin_products/selected",
    }),
  },
  watch: {
    selected: {
      handler() {
        this.images = [...this.selected.image];
        const images = this.images.map((item) => this.getStrapiMedia(item.url));
        this.setImages(images);
      },
      deep: true,
    },
  },
  methods: {
    getStrapiMedia,
    ...mapActions({
      updateProduct: "admin_products/updateProduct",
    }),
    ...mapMutations({
      updateSelectedImages: "admin_products/updateSelectedImages",
      setImages: "cool_light_box/setImages",
      setImageIndex: "cool_light_box/setImageIndex",
    }),
    addFile: async function (e) {
      const { files } = e.target;
 
      for (let i = 0; i < files.length; i++) {
        const isImage = [
          "image/png",
          "image/jpeg",
          "image/jpg",
          "image/svg",
        ].includes(files[i]["type"]);
        if (!isImage) {
          warn("Only jpg/jpeg/jpg/png files are allowed!");
          return;
        }
      }
 
      const formData = new FormData();
 
      for (let i = 0; i < files.length; i++) {
        formData.append("files", files[i], files[i].name);
      }
 
      const product = {
        ...this.selected,
        image: this.images,
        files: formData,
      };
 
      await this.updateProduct(product);
    },
    deleteFromImages: async function (index) {
      if (this.images.length <= 1) {
        warn("There is must be at least one image");
        return;
      }
      this.removeImageIndex = index;
      this.$root.$emit("bv::show::modal", "confirm-delete-image");
    },
 
    confirm: async function () {
      this.images.splice(this.removeImageIndex, 1);
      this.removeImageIndex = null;
 
      const product = { ...this.selected, image: this.images };
      await this.updateProduct(product);
    },
  },
  mounted() {
    this.images = [...this.selected.image];
    const images = this.images.map((item) => this.getStrapiMedia(item.url));
    this.setImages(images);
  },
};
</script>
 
<style scoped>
.image-wrap > .image-buttons {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: none;
  border-radius: 10px;
  background-color: rgba(0, 0, 0, 0.663);
}
 
.image-wrap:hover > .image-buttons {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
}
</style>