view client/src/views/Users.vue @ 406:40e7ab3df32c

feat: Basic CRUD for usermanagement etd Basic features for adding, changing, deleting users established
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 15 Aug 2018 15:14:23 +0200
parents cb233a5a2ecd
children a9440a4826aa
line wrap: on
line source

<template>
  <div class="main d-flex">
    <Sidebar v-bind:isOverlay="false"></Sidebar>
    <div class="d-flex content flex-column">
        <div>
          <h1>User Management</h1>
        </div>
        <div class="d-flex flex-row">
          <div :class="userlistStyle">
            <div class="card">
                <div class="card-header text-white bg-info mb-3">
                  users
                </div>
                <div class="card-body">
                  <table class="table table-hover">
                    <thead>
                      <tr>
                        <th scope="col">Username</th>
                        <th scope="col">Country</th>
                        <th scope="col">Email</th>
                        <th scope="col">Role</th>
                        <th scope="col"></th>
                      </tr>
                    </thead>
                    <tbody>
                    <tr v-for="user in users" :key="user.user" @click="selectUser(user.user)">
                      <td>{{ user.user }}</td>
                      <td>{{ user.country }}</td>
                      <td>{{ user.email}}</td>
                      <td>{{ user.role }}</td>
                      <td><i @click="deleteUser(user.user)" class="fa fa-trash-o"></i></td>
                      </tr>
                    </tbody>
                </table>
              </div>
                <div class="adduser">
                  <button @click="addUser" class="btn btn-info pull-right">Add User</button>
                </div>
            </div>
          </div>
          <Userdetail v-if="isUserDetailsVisible"></Userdetail>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
@import "../assets/application.scss";
.main {
  height: 100vh;
}

.content {
  margin-top: $large-offset;
  margin-left: auto;
  margin-right: auto;
}

.adduser {
  margin-right: $offset;
  padding-bottom: $offset;
}

.userlist {
  margin-top: $large-offset;
  margin-right: $offset;
  height: 100%;
}

.userlistsmall {
  width: 35vw;
}

.userlistextended {
  width: 70vw;
}

.shadow {
  box-shadow: $basic-shadow-light !important;
}

.table th,
td {
  font-size: 0.9rem;
  border-top: 0px !important;
}

.table td {
  font-size: 0.9rem;
  cursor: pointer;
}
</style>

<script>
import Sidebar from "../components/Sidebar";
import Userdetail from "../components/Userdetail";
import store from "../store";
import { mapGetters } from "vuex";
import app from "../main";

export default {
  name: "userview",
  data() {
    return {};
  },
  components: {
    Sidebar,
    Userdetail
  },
  computed: {
    ...mapGetters("usermanagement", ["users", "isUserDetailsVisible"]),
    userlistStyle() {
      return {
        userlist: true,
        shadow: true,
        userlistsmall: this.isUserDetailsVisible,
        userlistextended: !this.isUserDetailsVisible
      };
    }
  },
  methods: {
    deleteUser(name) {
      this.$store
        .dispatch("usermanagement/deleteUser", { name: name })
        .then(() => {
          this.submitted = false;
          this.$store.dispatch("usermanagement/loadUsers").catch(error => {
            const { status, data } = error.response;
            app.$toast.error({
              title: "Backend Error",
              message: `${status}: ${data.message}`
            });
          });
        })
        .catch(error => {
          const { status, data } = error.response;
          app.$toast.error({
            title: "Backend Error",
            message: `${status}: ${data.message}`
          });
        });
    },
    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);
    }
  },
  beforeRouteEnter(to, from, next) {
    store
      .dispatch("usermanagement/loadUsers")
      .then(next)
      .catch(error => {
        const { status, data } = error.response;
        app.$toast.error({
          title: "Backend Error",
          message: `${status}: ${data.message}`
        });
      });
  },
  beforeRouteLeave(to, from, next) {
    store.commit("usermanagement/clearCurrentUser");
    store.commit("usermanagement/setUserDetailsInvisible");
    next();
  }
};
</script>