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

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

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                                                                                                                                  1x   1x 1x   1x 4x                                                                                                                                                               1x        
<template>
  <form class="d-flex flex-column" v-on:submit.stop.prevent="submit">
    <div class="block d-flex flex-column p-2 mt-2">
      <label class="d-flex font-weight-bold" for="name">Name</label>
      <input
        id="name"
        type="text"
        required
        v-model.trim="currentArticle.name"
      />
    </div>
    <div class="block d-flex flex-column p-2 mt-2">
      <label class="d-flex font-weight-bold" for="title">Title</label>
      <input
        id="title"
        type="text"
        required
        v-model.trim="currentArticle.title"
      />
    </div>
    <div class="block d-flex flex-column p-2 mt-2">
      <label class="d-flex font-weight-bold" for="article">Article</label>
      <textarea
        id="article"
        type="text"
        required
        v-model.trim="currentArticle.article"
      />
    </div>
    <div class="block d-flex flex-column p-2 mt-2">
      <label class="d-flex font-weight-bold" for="published_at"
        >Article status</label
      >
      <select id="published_at" required v-model="status">
        <option value="published">published</option>
        <option value="null">draft</option>
      </select>
    </div>
    <div class="block mt-2 p-2 d-flex flex-column">
      <label class="d-flex font-weight-bold" for="media">Media</label>
      <ArticleMedia
        v-if="article && currentArticle.image && currentArticle.image.id"
        id="media"
      />
      <UploadImages v-else v-on:changed="handleImages" :max="1" id="media" />
    </div>
    <div class="d-flex">
      <button
        class="w-100 m-1 btn btn-success text-uppercase my-2"
        type="submit"
      >
        {{ article ? "update" : "create" }}
      </button>
      <button
        v-if="article"
        class="col-3 m-1 btn btn-danger text-uppercase my-2"
        v-on:click="deleteArticle"
        type="button"
      >
        delete
      </button>
    </div>
    <ConfirmModal :id="'confirm-delete-article'" v-on:confirm="confirm" />
  </form>
</template>
 
<script>
import { mapActions } from "vuex";
import UploadImages from "vue-upload-drop-images";
 
import ArticleMedia from "./ArticleMedia.vue";
import ConfirmModal from "~/components/Admin/common/ConfirmModal.vue";
 
export default {
  name: "ArticleForm",
  props: {
    article: Object | null,
  },
  components: { UploadImages, ConfirmModal, ArticleMedia },
  data: () => ({
    status: "null",
    currentArticle: {
      name: "",
      title: "",
      article: "",
      image: null,
      published_at: null,
    },
  }),
  watch: {
    article: function () {
      if (this.article) {
        this.currentArticle = JSON.parse(JSON.stringify(this.article));
        const { published_at } = this.currentArticle;
 
        if (published_at) {
          this.status = "published";
        }
      } else {
        this.currentArticle = {
          name: "",
          title: "",
          article: "",
          image: null,
          published_at: null,
        };
      }
    },
  },
  methods: {
    ...mapActions({
      updateArticle: "admin_articles/updateArticle",
      deleteArticles: "admin_articles/deleteArticles",
      createArticle: "admin_articles/createArticle",
    }),
    submit: function () {
      const { published_at } = this.currentArticle;
 
      if (this.status === "null") {
        this.currentArticle.published_at = null;
      } else if (this.status !== null && published_at === null) {
        this.currentArticle.published_at = new Date();
      }
 
      if (this.article) {
        this.updateArticle(this.currentArticle);
      } else {
        this.createArticle(this.currentArticle);
      }
    },
    handleImages(files) {
      this.currentArticle.image = files;
    },
    deleteArticle: function () {
      this.$root.$emit("bv::show::modal", "confirm-delete-article");
    },
    confirm: async function () {
      const { id } = this.currentArticle;
      await this.deleteArticles([id]);
    },
  },
  mounted() {
    if (this.article) {
      this.currentArticle = JSON.parse(JSON.stringify(this.article));
      const { published_at } = this.currentArticle;
 
      if (published_at) {
        this.status = "published";
      }
    }
  },
};
</script>
 
<style scoped>
</style>