All files / components/Admin/Articles/AdminArticle index.vue

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

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                                                          1x   1x 1x   2x                                                                                                                                                                                                               1x                                    
<template>
  <div class="row w-100 justify-content-center m-0 mt-3">
    <div class="d-flex flex-column col-10">
      <div class="d-flex align-items-center justify-content-between">
        <button v-on:click="clearSelectedArticles()" class="button">
          <BIconArrowLeft />
        </button>
        <p class="text-ellipsis m-0 ml-2">{{ selected.name }}</p>
        <div>
          <button
            class="border-left"
            v-on:click="setPreviousArticle"
            :disabled="!previous"
          >
            <BIconChevronLeft />
          </button>
          <button
            class="border-right"
            v-on:click="setNextArticle"
            :disabled="!next"
          >
            <BIconChevronRight />
          </button>
        </div>
      </div>
      <ArticleForm :article="selected" />
    </div>
  </div>
</template>
 
<script>
import { mapMutations, mapGetters, mapActions } from "vuex";
import { prevCurrNextItems } from "~/helpers";
 
import ArticleForm from "../ArticleForm";
 
export default {
  name: "AdminArticle",
  components: { ArticleForm },
  computed: {
    ...mapGetters({
      articles: "admin_articles/articles",
      previous: "admin_articles/previous",
      selected: "admin_articles/selected",
      next: "admin_articles/next",
      params: "admin_articles/params",
      total: "admin_articles/total",
    }),
  },
  methods: {
    prevCurrNextItems,
    ...mapActions({
      updateArticle: "admin_articles/updateArticle",
      deleteArticles: "admin_articles/deleteArticles",
      getArticles: "admin_articles/getArticles",
    }),
    ...mapMutations({
      setParams: "admin_articles/setParams",
      clearSelectedArticles: "admin_articles/clearSelectedArticles",
      setSelectedArticles: "admin_articles/setSelectedArticles",
      setImages: "cool_light_box/setImages",
      setImageIndex: "cool_light_box/setImageIndex",
    }),
    setNextArticle: async function () {
      const index = this.findIndex();
 
      const { page, currentPerPage } = this.params;
 
      let isMax = false;
 
      if (
        this.total / currentPerPage - page <= 0 &&
        index === this.articles.length - 2
      ) {
        isMax = true;
      }
 
      const { selected, next, previous } = this.prevCurrNextItems(
        this.articles[index + 1],
        this.articles
      );
 
      if (!isMax && !next) {
        const { page } = this.params;
 
        this.setParams({ ...this.params, page: page + 1 });
 
        await this.getArticles();
 
        const result = this.prevCurrNextItems(this.articles[0], [
          selected,
          ...this.articles,
        ]);
 
        this.setSelectedArticles(result);
      } else {
        this.setSelectedArticles({ selected, next, previous });
      }
    },
    setPreviousArticle: async function () {
      const index = this.findIndex();
 
      const { page } = this.params;
 
      const { selected, next, previous } = this.prevCurrNextItems(
        this.articles[index - 1],
        this.articles
      );
 
      let isMin = false;
 
      if (page >= 1 && index === 1) {
        isMin = true;
      }
 
      if (!isMin && !previous) {
        const { page } = this.params;
        this.setParams({ ...this.params, page: page - 1 });
 
        await this.getArticles();
 
        const result = this.prevCurrNextItems(
          this.articles[this.articles.length - 1],
          [...this.articles, selected]
        );
        this.setSelectedArticles(result);
      } else {
        this.setSelectedArticles({ selected, next, previous });
      }
    },
    findIndex: function () {
      return this.articles.findIndex((item) => item.id === this.selected.id);
    },
  },
  async destroyed() {
    this.setParams({ ...this.params, page: 1 });
    await this.getArticles();
  },
};
</script>
 
<style scoped>
.button {
  border-radius: 5px;
  background: none;
}
 
.border-left {
  border-radius: 5px 0 0 5px;
  border: 1px solid #000;
}
 
.border-right {
  border-radius: 0 5px 5px 0;
  border: 1px solid #000;
}
</style>