view client/src/components/usermanagement/Usermanagement.vue @ 2384:c06b001dc26b

client: improved popup implementation For deleting users and templates there was a more or less quick n' dirty implementation of a confirmation dialog/popup. Since we need this kind of dialog in several more places I generalized the implementation a bit and made it more robust.
author Markus Kottlaender <markus@intevation.de>
date Mon, 25 Feb 2019 13:11:30 +0100
parents 7b79d4966a87
children f185503ef35a
line wrap: on
line source

<template>
  <div class="main d-flex flex-row" style="position: relative;">
    <Spacer></Spacer>
    <div class="d-flex content py-3">
      <div :class="userlistStyle">
        <div class="card shadow-xs">
          <h6
            class="mb-0 py-2 px-3 border-bottom d-flex text-info align-items-center"
          >
            <font-awesome-icon
              icon="users-cog"
              class="mr-2 fa-fw"
            ></font-awesome-icon>
            <translate class="headline">Users</translate>
          </h6>
          <div class="card-body">
            <table id="datatable" :class="tableStyle">
              <thead>
                <tr>
                  <th scope="col" @click="sortBy('role')">
                    <span
                      >Role&nbsp;
                      <font-awesome-icon
                        v-if="sortCriterion == 'role'"
                        icon="angle-down"
                      ></font-awesome-icon>
                    </span>
                  </th>
                  <th scope="col" @click="sortBy('user')">
                    <span
                      >Username&nbsp;
                      <font-awesome-icon
                        v-if="sortCriterion == 'user'"
                        icon="angle-down"
                      ></font-awesome-icon>
                    </span>
                  </th>
                  <th scope="col" @click="sortBy('country')">
                    <span
                      >Country&nbsp;
                      <font-awesome-icon
                        v-if="sortCriterion == 'country'"
                        icon="angle-down"
                      ></font-awesome-icon>
                    </span>
                  </th>
                  <th scope="col" @click="sortBy('email')">
                    <span
                      >Email&nbsp;
                      <font-awesome-icon
                        v-if="sortCriterion == 'email'"
                        icon="angle-down"
                      ></font-awesome-icon>
                    </span>
                  </th>
                  <th scope="col"></th>
                </tr>
              </thead>
              <transition-group name="fade" tag="tbody">
                <tr v-for="user in users" :key="user.user">
                  <td @click="selectUser(user.user)">
                    <font-awesome-icon
                      v-tooltip="roleLabel(user.role)"
                      :icon="roleIcon(user.role)"
                      class="fa-lg"
                    ></font-awesome-icon>
                  </td>
                  <td @click="selectUser(user.user)">{{ user.user }}</td>
                  <td @click="selectUser(user.user)">{{ user.country }}</td>
                  <td @click="selectUser(user.user)">{{ user.email }}</td>
                  <td class="text-right">
                    <button
                      @click="sendTestMail(user.user)"
                      class="btn btn-sm btn-dark mr-1"
                      v-tooltip="$gettext('Send testmail')"
                      v-if="user.email"
                    >
                      <font-awesome-icon icon="paper-plane"></font-awesome-icon>
                    </button>
                    <button
                      @click="deleteUser(user.user)"
                      class="btn btn-sm btn-dark"
                      v-tooltip="$gettext('Delete user')"
                    >
                      <font-awesome-icon icon="trash" />
                    </button>
                  </td>
                </tr>
              </transition-group>
            </table>
          </div>
          <div class="d-flex mx-auto align-items-center">
            <button
              @click="prevPage"
              v-if="this.currentPage !== 1"
              class="mr-2 btn btn-sm btn-light align-self-center"
            >
              <font-awesome-icon icon="angle-left"></font-awesome-icon>
            </button>
            {{ this.currentPage }} / {{ this.pages }}
            <button
              @click="nextPage"
              v-if="this.currentPage !== this.pages"
              class="ml-2 btn btn-sm btn-light align-self-center"
            >
              <font-awesome-icon icon="angle-right"></font-awesome-icon>
            </button>
          </div>
          <div class="mr-3 py-3 text-right">
            <button @click="addUser" class="btn btn-info addbutton shadow-sm">
              <translate>Add User</translate>
            </button>
          </div>
        </div>
      </div>
      <Userdetail v-if="isUserDetailsVisible"></Userdetail>
    </div>

    <div
      :class="[
        'box popup ui-element rounded bg-white',
        { show: showDeleteUserPrompt }
      ]"
    >
      <div>
        <h6 class="mb-0 py-2 px-3 border-bottom d-flex align-items-center">
          <font-awesome-icon icon="trash" class="mr-2"></font-awesome-icon>
          <translate>Delete user</translate>
          <font-awesome-icon
            icon="times"
            class="ml-auto text-muted pointer"
            @click="showDeleteUserPrompt = false"
          ></font-awesome-icon>
        </h6>
        <div class="p-3 text-left">
          <translate class="text-center d-block"
            >Do you really want to delete the following user account:</translate
          >
          <h5 class="mt-3 text-center">{{ userToDelete }}</h5>
        </div>
        <div
          class="py-2 px-3 border-top d-flex align-items-center justify-content-between"
        >
          <button
            class="btn btn-sm btn-warning"
            @click="showDeleteUserPrompt = false"
          >
            no
          </button>
          <button class="btn btn-sm btn-info" @click="deleteUser(userToDelete)">
            yes
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.addbutton {
  position: absolute;
  bottom: $offset;
  right: $offset;
}

.content {
  width: 100%;
}

.userdetails {
  width: 50%;
}

.main {
  height: 100%;
}

.icon {
  font-size: large;
}

.userlist {
  min-width: 520px;
  height: 100%;
}

.userlistsmall {
  width: 100%;
}

.userlistextended {
  width: 100%;
}

.table {
  margin: auto;
}

.table th {
  cursor: pointer;
}

.table th:first-child {
  width: 50px;
}

.table th,
td {
  font-size: $smaller;
  border-top: 0px !important;
  text-align: left;
  padding: $small-offset !important;
}

.table td {
  font-size: $smaller;
  cursor: pointer;
}

tr span {
  display: flex;
}
</style>

<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 store from "@/store";
import { mapGetters, mapState } from "vuex";
import { displayError, displayInfo } from "@/lib/errors.js";
import { HTTP } from "@/lib/http";
import Vue from "vue";
import { VTooltip, VPopover, VClosePopover } from "v-tooltip";

Vue.directive("tooltip", VTooltip);
Vue.directive("close-popover", VClosePopover);
Vue.component("v-popover", VPopover);

export default {
  name: "userview",
  data() {
    return {
      sortCriterion: "user",
      pageSize: 20,
      currentPage: 1,
      userToDelete: "",
      showDeleteUserPrompt: false
    };
  },
  components: {
    Userdetail: () => import("./Userdetail"),
    Spacer: () => import("@/components/Spacer")
  },
  computed: {
    ...mapGetters("usermanagement", ["isUserDetailsVisible"]),
    ...mapState("application", ["showSidebar"]),
    users() {
      let users = [...this.$store.getters["usermanagement/users"]];
      users.sort((a, b) => {
        if (
          a[this.sortCriterion].toLowerCase() <
          b[this.sortCriterion].toLowerCase()
        )
          return -1;
        if (
          a[this.sortCriterion].toLowerCase() >
          b[this.sortCriterion].toLowerCase()
        )
          return 1;
        return 0;
      });
      const start = (this.currentPage - 1) * this.pageSize;
      return users.slice(start, start + this.pageSize);
    },
    pages() {
      let users = [...this.$store.getters["usermanagement/users"]];
      return Math.ceil(users.length / this.pageSize);
    },
    tableStyle() {
      return {
        table: true,
        "table-hover": true,
        "table-sm": this.isUserDetailsVisible,
        fadeIn: true,
        animated: true
      };
    },
    userlistStyle() {
      return [
        "userlist mr-3",
        {
          userlistsmall: this.isUserDetailsVisible,
          userlistextended: !this.isUserDetailsVisible
        }
      ];
    }
  },
  methods: {
    sendTestMail(user) {
      HTTP.get("/testmail/" + user, {
        headers: {
          "X-Gemma-Auth": localStorage.getItem("token"),
          "Content-type": "text/xml; charset=UTF-8"
        }
      })
        .then(() => {
          displayInfo({
            message: this.$gettext("Testmail sent")
          });
        })
        .catch(error => {
          this.loginFailed = true;
          const { status, data } = error.response;
          displayError({
            title: this.$gettext("Backend Error"),
            message: `${status}: ${data.message || data}`
          });
        });
    },
    tween() {},
    nextPage() {
      if (this.currentPage < this.pages) {
        document.querySelector("#datatable").classList.add("fadeOut");
        setTimeout(() => {
          document.querySelector("#datatable").classList.remove("fadeOut");
          this.currentPage += 1;
        }, 10);
      }
      return;
    },
    prevPage() {
      if (this.currentPage > 0) {
        document.querySelector("#datatable").classList.add("fadeOut");
        setTimeout(() => {
          document.querySelector("#datatable").classList.remove("fadeOut");
          this.currentPage -= 1;
        }, 10);
      }
      return;
    },
    sortBy(criterion) {
      this.sortCriterion = criterion;
    },
    deleteUser(name) {
      this.$store.commit("application/popup", {
        icon: "trash",
        title: this.$gettext("Delete User"),
        content:
          this.$gettext(
            "Do you really want to delete the following user account:"
          ) +
          `<br>
        <b>${name}</b>`,
        confirm: {
          label: this.$gettext("Delete"),
          icon: "trash",
          callback: () => {
            this.$store
              .dispatch("usermanagement/deleteUser", { name })
              .then(() => {
                this.$store
                  .dispatch("usermanagement/loadUsers")
                  .catch(error => {
                    const { status, data } = error.response;
                    displayError({
                      title: this.$gettext("Backend Error"),
                      message: `${status}: ${data.message || data}`
                    });
                  });
              })
              .catch(error => {
                const { status, data } = error.response;
                displayError({
                  title: this.$gettext("Backend Error"),
                  message: `${status}: ${data.message || data}`
                });
              });
          }
        },
        cancel: {
          label: this.$gettext("Cancel"),
          icon: "times"
        }
      });
    },
    addUser() {
      this.$store.commit("usermanagement/clearCurrentUser");
      this.$store.commit("usermanagement/setUserDetailsVisible");
    },
    selectUser(name) {
      const user = this.$store.getters["usermanagement/getUserByName"](name);
      this.$store.commit("usermanagement/setCurrentUser", user);
    },
    roleIcon(role) {
      if (role === "sys_admin") return "star";
      if (role === "waterway_admin") return ["fab", "adn"];
      return "user";
    },
    roleLabel(role) {
      const labels = {
        sys_admin: this.$gettext("System-Administrator"),
        waterway_admin: this.$gettext("Waterway Admin"),
        waterway_user: this.$gettext("Waterway User")
      };
      return labels[role];
    }
  },
  beforeRouteEnter(to, from, next) {
    store
      .dispatch("usermanagement/loadUsers")
      .then(next)
      .catch(error => {
        const { status, data } = error.response;
        displayError({
          title: this.$gettext("Backend Error"),
          message: `${status}: ${data}`
        });
      });
  },
  beforeRouteLeave(to, from, next) {
    store.commit("usermanagement/clearCurrentUser");
    store.commit("usermanagement/setUserDetailsInvisible");
    next();
  }
};
</script>