summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/UserCreateForm.vue
blob: 8b31b95d4e4263e9bdf3b6967cbba625ff270906 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<i18n>
{
  "en": {
    "confirmPassword": "Confirm Password",
    "email": "E-Mail",
    "emailError": "E-mail must be valid.",
    "name": "Name",
    "password": "Password",
    "passwordEmptyError": "Password can not be empty.",
    "passwordLengthError": "Minimum 8 characters required.",
    "passwordMatchError": "Passwords do not match.",
    "username": "Username",
    "usernameEmptyError": "Username can not be empty.",
    "usernameExistsError": "Username already taken.",
    "usernameHasWhitespaces": "No whitespaces allowed in the username."
  },
  "de": {
    "confirmPassword": "Passwort bestätigen",
    "email": "E-Mail",
    "emailError": "Keine gültige E-Mail.",
    "name": "Name",
    "password": "Passwort",
    "passwordEmptyError": "Passwort kann nicht leer sein.",
    "passwordLengthError": "Es sind mindestens 8 Zeichen erforderlich.",
    "passwordMatchError": "Passwörter stimmen nicht überein.",
    "username": "Benutzername",
    "usernameEmptyError": "Benutzername kann nicht leer sein.",
    "usernameExistsError": "Benutzername existiert bereits.",
    "usernameHasWhitespaces": "Leerzeichen sind im Benutzername nicht erlaubt."
  }
}
</i18n>

<template>
  <v-form class="signup-form" ref="form" v-model="valid">
    <v-text-field class="required"
      validate-on-blur
      v-model="user.username"
      :rules="usernameRules"
      autocomplete="off"
      counter
    >
      <template slot="label">{{ $t('username') }} <label class="error--text">*</label></template>
    </v-text-field>
    <v-text-field
      validate-on-blur
      type="password"
      v-model="user.password"
      :rules="passwordRules"
      autocomplete="off"
    >
      <template slot="label">{{ $t('password') }} <label v-if="!id" class="error--text">*</label></template>
    </v-text-field>
    <v-text-field
      validate-on-blur
      type="password"
      v-model="user.confirmPassword"
      :rules="confirmPasswordRules"
      autocomplete="off"
    >
      <template slot="label">{{ $t('confirmPassword') }} <label v-if="!id" class="error--text">*</label></template>
    </v-text-field>
    <v-text-field
      :label="$t('name')"
      autocomplete="off"
      v-model="user.name"
    ></v-text-field>
    <v-text-field
      validate-on-blur
      :label="$t('email')"
      :rules="emailRules"
      autocomplete="off"
      v-model="user.email"
    ></v-text-field>
  </v-form>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'UserCreateForm',
  props: ['id', 'value'],
  data () {
    return {
      valid: true,
      usernameError: false,
      usernameExistsError: false,
      usernameRules: [
        v => !!v || this.$t('usernameEmptyError'),
        v => !this.usernameError || this.$t('usernameError'),
        v => !/\s/.test(v) || this.$t('usernameHasWhitespaces'),
        v => this.usernameTakenValidation(v) || this.$t('usernameExistsError')
      ],
      passwordRules: [
        v => this.pwRequired(v) || this.$t('passwordEmptyError'),
        v => this.pwLength(v) || this.$t('passwordLengthError')
      ],
      confirmPasswordRules: [
        v => this.pwRequired(v) || this.$t('passwordEmptyError'),
        v => v === this.user.password || this.$t('passwordMatchError')
      ],
      confirmPasswordError: false,
      emailRules: [
        v => this.$validateEmail(v) || this.$t('emailError')
      ],
      user: {
        username: '',
        password: '',
        confirmPassword: '',
        name: '',
        email: ''
      },
      username: ''
    }
  },
  computed: {
    ...mapState('users', ['dialog']),
    show () { return this.dialog.show },
    usernameField () { return this.user.username }
  },
  watch: {
    show (value) {
      if (value) {
        this.$refs.form.resetValidation()
        this.loadForm()
      }
    },
    value: {
      deep: true,
      handler () {
        this.user = this.value
      }
    },
    user: {
      deep: true,
      handler () {
        this.$emit('input', this.user)
      }
    },
    usernameField () {
      this.usernameExistsError = false
    }
  },
  methods: {
    validate () {
      return this.$refs.form.validate()
    },
    setUsernameTakenError () {
      this.usernameExistsError = true
      this.$refs.form.validate()
    },
    usernameTakenValidation (v) {
      if (this.user.username === this.username) return true
      else return !this.usernameExistsError
    },
    pwRequired (v) {
      if (!!v || this.id) return true
      else return false
    },
    pwLength (v) {
      if (this.id && !v) return true
      else return v.length >= 8
    },
    loadForm () {
      if (this.id) this.loadUser(this.id)
      else this.clearForm()
    },
    loadUser (id) {
      this.$http('/api/users/' + id).then(response => {
        const user = response.data
        // Username is used for username taken validation
        this.username = user.username
        this.user.username = user.username
        this.user.email = user.email
        this.user.name = user.name
        this.user.password = ''
        this.user.confirmPassword = ''
      })
    },
    clearForm () {
      this.user.username = ''
      this.user.email = ''
      this.user.name = ''
      this.user.password = ''
      this.user.confirmPassword = ''
    }
  },
  created () {
    this.loadForm()
  }
}
</script>

<style scoped>
.signup-form {
  width: 300px;
}
</style>