akkoma-fe/src/components/registration/registration.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

import { validationMixin } from 'vuelidate'
import { required, sameAs } from 'vuelidate/lib/validators'
import { mapActions, mapState } from 'vuex'
2018-12-05 09:47:42 +00:00
import { SIGN_UP } from '../../mutation_types'
2017-04-15 16:12:23 +00:00
const registration = {
mixins: [validationMixin],
2017-04-15 16:12:23 +00:00
data: () => ({
user: {
email: '',
fullname: '',
username: '',
password: '',
confirm: ''
},
clientValidationFailed: false
2017-04-15 16:12:23 +00:00
}),
validations: {
user: {
email: { required },
username: { required },
fullname: { required },
password: { required },
confirm: {
required,
sameAsPassword: sameAs('password')
}
}
},
created () {
if ((!this.registrationOpen && !this.token) || this.signedIn) {
this.$router.push('/main/all')
}
// // Seems like this doesn't work at first page open for some reason
// if (this.$store.state.instance.registrationOpen && this.token) {
// this.$router.push('/registration')
// }
},
computed: {
token () { return this.$route.params.token },
...mapState({
registrationOpen: (state) => state.instance.registrationOpen,
signedIn: (state) => !!state.users.currentUser,
isPending: (state) => state.users[SIGN_UP.isPending],
serverValidationErrors: (state) => state.users[SIGN_UP.errors],
2018-12-05 09:47:42 +00:00
termsofservice: (state) => state.instance.tos
})
},
2017-04-15 16:12:23 +00:00
methods: {
...mapActions(['signUp']),
async submit () {
2017-04-15 16:12:23 +00:00
this.user.nickname = this.user.username
2018-08-05 07:01:38 +00:00
this.user.token = this.token
this.$v.$touch()
if (!this.$v.$invalid) {
try {
await this.signUp(this.user)
this.$router.push('/main/friends')
} catch (error) {
console.log('Registration failed: ' + error)
}
}
2017-04-15 16:12:23 +00:00
}
}
}
export default registration