AddGroupModal.vue 1.85 KB
Newer Older
1
<template>
Sonia Zorba's avatar
Sonia Zorba committed
2
<b-modal id="add-group-modal" title="Add group" @show="resetModal" @shown="afterShow" ok-title="Add" @ok="addGroup">
3
4
  <b-form inline>
    <label class="w-25" for="new-group-name-input">Group name:</label>
Sonia Zorba's avatar
Sonia Zorba committed
5
    <b-form-input v-model="newGroupName" id="new-group-name-input" ref="newGroupNameInput" class="w-75" aria-describedby="new-group-name-input-feedback" :state="newGroupNameState" v-on:input="resetError" @keydown.native.enter="addGroup">
6
    </b-form-input>
7
    <b-form-invalid-feedback id="new-group-name-input-feedback" class="text-right">{{newGroupNameError}}</b-form-invalid-feedback>
Sonia Zorba's avatar
Sonia Zorba committed
8
    <b-form-checkbox class="mt-3 ml-3" v-model="leaf">is leaf</b-form-checkbox>
9
10
  </b-form>
</b-modal>
11
12
13
14
15
16
</template>

<script>
import client from 'api-client';

export default {
17
  name: 'AddGroupModal',
18
19
  computed: {
    newGroupNameState() {
20
      if (this.newGroupNameError) {
21
        return false;
22
      }
23
      return null;
24
25
26
27
    }
  },
  data: function() {
    return {
28
29
      newGroupName: '',
      newGroupNameError: '',
Sonia Zorba's avatar
Sonia Zorba committed
30
      leaf: true
31
32
33
34
35
    };
  },
  methods: {
    resetModal: function() {
      this.newGroupName = null;
36
      this.leaf = true;
37
38
      this.resetError();
    },
Sonia Zorba's avatar
Sonia Zorba committed
39
40
41
    afterShow: function() {
      this.$refs.newGroupNameInput.focus();
    },
42
43
44
45
46
47
48
    resetError: function() {
      this.newGroupNameError = null;
    },
    addGroup: function(event) {
      // Prevent modal from closing
      event.preventDefault();

49
      if (!this.newGroupName) {
50
        this.newGroupNameError = "Group name is required";
Sonia Zorba's avatar
Sonia Zorba committed
51
      } else {
Sonia Zorba's avatar
Sonia Zorba committed
52
        client.addGroup(this.newGroupName, this.leaf, this.$store.state.input)
Sonia Zorba's avatar
Sonia Zorba committed
53
          .then(res => {
54
55
56
57
58
            this.$store.commit('updateGroupsPanel', res);
            this.$bvModal.hide('add-group-modal');
          })
          .catch(res => {
            this.newGroupNameError = res.message;
Sonia Zorba's avatar
Sonia Zorba committed
59
          });
60
61
62
63
64
      }
    }
  }
}
</script>