view client/src/components/importoverview/ImportOverviewAlt.vue @ 2626:50cc5bffd787

client: importoverview2: implemented Finish Review (confirm) dialog
author Markus Kottlaender <markus@intevation.de>
date Wed, 13 Mar 2019 16:26:14 +0100
parents eb1ec926ff97
children 0b14de0bb85f
line wrap: on
line source

<template>
  <div class="overview">
    <UIBoxHeader
      icon="clipboard-check"
      title="Staging Area"
      :closeCallback="$parent.close"
    />
    <div class="p-2 d-flex flex-row flex-fill justify-content-between">
      <Filters></Filters>
      <div>
        <button class="btn btn-sm btn-info mr-1" @click="loadLogs()">
          <font-awesome-icon icon="spinner" spin v-if="loading" />
          <font-awesome-icon icon="redo" v-else />
        </button>
        <button
          class="btn btn-sm btn-info"
          :disabled="!reviewed.length"
          @click="save"
        >
          <translate>Commit</translate> {{ reviewed.length }}
        </button>
      </div>
    </div>
    <LogEntry
      class="border-top d-flex-flex-column w-100"
      :entry="entry"
      v-for="entry in imports"
      :key="entry.id"
    ></LogEntry>
  </div>
</template>

<script>
/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 */

import { mapState } from "vuex";
import { displayError, displayInfo } from "@/lib/errors.js";
import { STATES } from "@/store/imports.js";

export default {
  name: "importoverviewalt",
  components: {
    Filters: () => import("./Filters.vue"),
    LogEntry: () => import("./LogEntry.vue")
  },
  data() {
    return {
      loading: false
    };
  },
  computed: {
    ...mapState("imports", ["imports", "filters", "reviewed"])
  },
  methods: {
    loadLogs() {
      this.loading = true;
      this.$store
        .dispatch("imports/getImports")
        .then(() => {
          this.loading = false;
        })
        .catch(error => {
          const { status, data } = error.response;
          displayError({
            title: this.$gettext("Backend Error"),
            message: `${status}: ${data.message || data}`
          });
        });
    },
    save() {
      if (!this.reviewed.length) return;

      let popupContent = `<table class="table table-sm small mb-0 border-0" style="margin-top: -1px;">`;
      this.reviewed.forEach(r => {
        let imp = this.imports.find(i => i.id === r.id);
        let approved = STATES.APPROVED === r.status;
        popupContent += `<tr>
          <td>${imp.id}</td>
          <td>${imp.kind.toUpperCase()}</td>
          <td>${this.$options.filters.dateTime(imp.enqueued)}</td>
          <td class="text-${approved ? "success" : "danger"}">
            ${this.$gettext(approved ? "approved" : "declined")}
          </td>
        </tr>`;
      });
      popupContent += "</table>";

      this.$store.commit("application/popup", {
        icon: "clipboard-check",
        title: this.$gettext("Finish Review"),
        padding: false,
        big: true,
        content: popupContent,
        confirm: {
          icon: "check",
          callback: () => {
            let data = this.reviewed.map(r => ({
              id: r.id,
              state: r.status
            }));
            this.$store
              .dispatch("imports/confirmReview", data)
              .then(response => {
                this.loadLogs();
                const messages = response.data
                  .map(x => {
                    if (x.message) return x.message;
                    if (x.error) return x.error;
                  })
                  .join("\n\n");
                displayInfo({
                  title: "Staging Area",
                  message: messages,
                  options: {
                    timeout: 0,
                    buttons: [{ text: "Ok", action: null, bold: true }]
                  }
                });
              })
              .catch(error => {
                const { status, data } = error.response;
                displayError({
                  title: "Backend Error",
                  message: `${status}: ${data.message || data}`
                });
              });
          }
        },
        cancel: {
          label: this.$gettext("Cancel"),
          icon: "times"
        }
      });
    }
  },
  watch: {
    filters() {
      this.$store.dispatch("imports/getImports", this.filters);
    }
  },
  mounted() {
    this.loadLogs();
  }
};
</script>