summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/UserModuleEdit.vue
blob: 29dd55c61e0aa8a9d1fadc7ad3a8b03b75175922 (plain) (blame)
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
<i18n>
{
  "en": {
    "titleExisting": "Edit user",
    "titleNew": "Create user",
    "userCreated": "User was successfully created.",
    "userUpdated": "User was successfully updated."
  },
  "de": {
    "titleExisting": "Benutzer bearbeiten",
    "titleNew": "Benutzer erstellen",
    "userCreated": "Benutzer wurde erfolgreich erstellt.",
    "userUpdated": "Benutzer wurde erfolgreich aktualisiert."
  }
}
</i18n>

<template>
  <v-card>
    <v-card-title class="dialog-title elevation-3">
      <div class="headline">{{ dialog.info.id ? $t('titleExisting') : $t('titleNew') }}</div>
    </v-card-title>
    <v-card-text style="height: 100%;">
      <div class="edit-user">
        <signup v-model="userForm" ref="editComponent" :id="dialog.info.id"></signup>
      </div>
    </v-card-text>
    <v-divider></v-divider>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn text="flat" @click="setDialog({ show: false })">{{ $t('cancel') }}</v-btn>
      <v-btn :color="dialog.info.id ? 'primary' : 'success'" @click="saveUser">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
import signup from '@/components/UserCreateForm'

export default {
  name: 'UserModuleEdit',
  components: {
    signup
  },
  data () {
    return {
      userForm: { }
    }
  },
  computed: {
    ...mapState('users', ['dialog']),
    ...mapGetters(['user'])
  },
  methods: {
    ...mapMutations('setUserFullName', 'setUserName'),
    setDialog (data) {
      this.$store.commit('users/setDialog', data)
    },
    async saveUser () {
      if (this.$refs.editComponent.validate()) {
        var url = '/api/users'
        if (this.dialog.info.id) {
          url += '/' + this.dialog.info.id
        }
        this.$http.post(url, {
          username: this.userForm.username,
          password: this.userForm.password,
          name: this.userForm.name,
          email: this.userForm.email
        }).then(response => {
          if (this.dialog.info.id === this.user.id) this.$store.dispatch('loadUser')
          this.$store.dispatch('users/loadData')
          this.setDialog({ show: false })
          this.$snackbar({ color: 'success', text: this.dialog.info.id ? this.$t('userUpdated') : this.$t('userCreated') })
        }).catch(error => {
          if (error.response.data.error === 'USER_ALREADY_EXISTS') {
            this.$refs.editComponent.setUsernameTakenError()
          }
        })
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.dialog-title {
  z-index: 1;
}
.selected-list {
  padding: 30px;
}
.edit-user {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>