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

128 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-03-16 21:02:44 +01:00
import useVuelidate from '@vuelidate/core'
import { required, requiredIf, sameAs } from '@vuelidate/validators'
import { mapActions, mapState } from 'vuex'
import InterfaceLanguageSwitcher from '../interface_language_switcher/interface_language_switcher.vue'
import localeService from '../../services/locale/locale.service.js'
2023-01-22 17:15:52 +01:00
import { DAY } from 'src/services/date_utils/date_utils.js'
2017-04-15 18:12:23 +02:00
const registration = {
2022-03-16 21:02:44 +01:00
setup () { return { v$: useVuelidate() } },
2017-04-15 18:12:23 +02:00
data: () => ({
user: {
email: '',
fullname: '',
username: '',
password: '',
confirm: '',
2023-01-22 17:15:52 +01:00
birthday: '',
reason: '',
language: ''
2018-12-15 01:05:47 +01:00
},
captcha: {}
2017-04-15 18:12:23 +02:00
}),
components: {
InterfaceLanguageSwitcher
},
2020-05-04 11:56:39 +02:00
validations () {
return {
user: {
email: { required: requiredIf(() => this.accountActivationRequired) },
username: { required },
fullname: { required },
password: { required },
confirm: {
required,
2022-03-29 15:08:57 +02:00
sameAs: sameAs(this.user.password)
},
2023-01-22 17:15:52 +01:00
birthday: {
required: requiredIf(() => this.birthdayRequired),
maxValue: value => {
return !this.birthdayRequired || new Date(value).getTime() <= this.birthdayMin.getTime()
}
},
reason: { required: requiredIf(() => this.accountApprovalRequired) },
language: {}
}
}
},
created () {
if ((!this.registrationOpen && !this.token) || this.signedIn) {
2019-07-05 09:02:14 +02:00
this.$router.push({ name: 'root' })
}
2018-12-15 01:05:47 +01:00
2018-12-16 20:47:52 +01:00
this.setCaptcha()
},
computed: {
token () { return this.$route.params.token },
2019-03-18 15:35:13 +01:00
bioPlaceholder () {
return this.replaceNewlines(this.$t('registration.bio_placeholder'))
},
reasonPlaceholder () {
return this.replaceNewlines(this.$t('registration.reason_placeholder'))
2019-03-18 15:35:13 +01:00
},
2023-01-22 17:15:52 +01:00
birthdayMin () {
const minAge = this.birthdayMinAge
const today = new Date()
today.setUTCMilliseconds(0)
today.setUTCSeconds(0)
today.setUTCMinutes(0)
today.setUTCHours(0)
const minDate = new Date()
minDate.setTime(today.getTime() - minAge * DAY)
return minDate
},
birthdayMinAttr () {
return this.birthdayMin.toJSON().replace(/T.+$/, '')
},
2023-01-22 17:15:52 +01:00
birthdayMinFormatted () {
const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
return this.user.birthday && new Date(Date.parse(this.birthdayMin)).toLocaleDateString(browserLocale, { timeZone: 'UTC', day: 'numeric', month: 'long', year: 'numeric' })
},
...mapState({
registrationOpen: (state) => state.instance.registrationOpen,
signedIn: (state) => !!state.users.currentUser,
isPending: (state) => state.users.signUpPending,
serverValidationErrors: (state) => state.users.signUpErrors,
2020-05-04 11:56:39 +02:00
termsOfService: (state) => state.instance.tos,
accountActivationRequired: (state) => state.instance.accountActivationRequired,
2023-01-22 17:15:52 +01:00
accountApprovalRequired: (state) => state.instance.accountApprovalRequired,
birthdayRequired: (state) => state.instance.birthdayRequired,
birthdayMinAge: (state) => state.instance.birthdayMinAge
})
},
2017-04-15 18:12:23 +02:00
methods: {
...mapActions(['signUp', 'getCaptcha']),
async submit () {
2017-04-15 18:12:23 +02:00
this.user.nickname = this.user.username
2018-08-05 09:01:38 +02:00
this.user.token = this.token
2018-12-16 18:55:09 +01:00
this.user.captcha_solution = this.captcha.solution
this.user.captcha_token = this.captcha.token
this.user.captcha_answer_data = this.captcha.answer_data
if (this.user.language) {
this.user.language = localeService.internalToBackendLocale(this.user.language)
}
2022-03-29 15:08:57 +02:00
this.v$.$touch()
2022-03-29 15:08:57 +02:00
if (!this.v$.$invalid) {
try {
await this.signUp(this.user)
2019-07-05 09:02:14 +02:00
this.$router.push({ name: 'friends' })
} catch (error) {
console.warn('Registration failed: ', error)
this.setCaptcha()
}
}
2018-12-16 20:47:52 +01:00
},
2018-12-16 20:55:11 +01:00
setCaptcha () {
2018-12-16 20:47:52 +01:00
this.getCaptcha().then(cpt => { this.captcha = cpt })
},
replaceNewlines (str) {
return str.replace(/\s*\n\s*/g, ' \n')
2017-04-15 18:12:23 +02:00
}
}
}
export default registration