All files / components/Admin/Categories index.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                                                                                1x   1x   2x                                                                                                                                       1x                                                                          
<template>
  <div class="w-100 h-100">
    <div
      v-if="loading"
      class="w-100 h-100 d-flex align-items-center justify-content-center"
    >
      <Loader />
    </div>
    <div v-else class="w-100">
      <h6 class="w-100 text-left p-3 m-0">Categories</h6>
      <div class="block mx-5 tags-input-container p-2">
        <div
          class="tag p-1 m-1"
          :class="index === activeTag && 'active'"
          v-for="(category, index) in currentCategories"
          :key="category.name"
        >
          <span v-if="activeTag !== index" @click="activeTag = index">{{
            category.name
          }}</span>
          <input
            v-else
            v-model.lazy="currentCategories[index].name"
            v-focus
            :style="{ width: category.name.length + 'ch' }"
            @blur="update(category)"
            @keyup.enter="update(category)"
          />
          <span @click="remove(category)"><BIconX /></span>
        </div>
        <input
          v-model="tagValue"
          @keyup.enter="add"
          class="m-2"
          placeholder="Add category..."
        />
      </div>
    </div>
  </div>
</template>
 
<script>
import { mapActions, mapGetters } from "vuex";
 
import Loader from "~/components/common/Loader";
 
export default {
  name: "Categories",
  components: { Loader },
  data: () => ({
    tagValue: "",
    activeTag: null,
    loading: true,
    currentCategories: [],
  }),
  computed: {
    ...mapGetters({ categories: "categories/categories" }),
  },
  watch: {
    categories: function () {
      this.currentCategories = JSON.parse(JSON.stringify(this.categories));
    },
  },
  methods: {
    ...mapActions({
      getCategories: "categories/getCategories",
      createCategory: "categories/createCategory",
      updateCategory: "categories/updateCategory",
      deleteCategory: "categories/deleteCategory",
    }),
    add: async function () {
      await this.createCategory({
        name: this.tagValue[0].toUpperCase() + this.tagValue.slice(1),
        slug: this.tagValue.toLowerCase(),
      });
      this.tagValue = "";
    },
    update: async function (category) {
      if (this.activeTag === null) return;
      this.activeTag = null;
 
      const { id, name } = category;
 
      const cat = this.categories.filter((c) => c.id === id)[0];
 
      if (cat && cat.name === name) return;
 
      const body = {
        name: name[0].toUpperCase() + name.slice(1),
        slug: name.toLowerCase(),
      };
 
      await this.updateCategory({ id, body });
    },
    remove: async function (category) {
      const { id } = category;
      await this.deleteCategory(id);
    },
  },
  directives: {
    focus: {
      inserted: (el) => {
        el.focus();
      },
    },
  },
  async mounted() {
    this.loading = true;
    await this.getCategories();
    this.currentCategories = JSON.parse(JSON.stringify(this.categories));
    this.loading = false;
  },
};
</script>
 
<style lang="scss" scoped>
.tags-input-container {
  background-color: rgba($color: #ffffff, $alpha: 0.7);
 
  input {
    width: 100%;
    padding: 0;
    margin: 0;
    border: 0;
    outline: none;
    background-color: transparent;
    font-size: 1rem;
  }
  .tag {
    float: left;
    display: flex;
    justify-content: center;
    cursor: pointer;
    &:hover,
    &.active {
      background-color: #57c340;
      border-radius: 5px;
    }
    span:first-child {
      margin-right: 8px;
    }
    svg {
      color: #666;
      &:hover {
        color: #333;
      }
    }
  }
}
</style>