-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathSignupForm.vue
More file actions
235 lines (221 loc) · 7.04 KB
/
SignupForm.vue
File metadata and controls
235 lines (221 loc) · 7.04 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<template lang='pug'>
form(data-test='signup' @submit.prevent='')
label.field
i18n.label Username
input.input(
:class='{error: $v.form.username.$error}'
autocapitalize='off'
name='username'
ref='username'
v-model.trim='form.username'
@input='debounceField("username")'
@blur='updateField("username")'
data-test='signName'
v-error:username='{ attrs: { "data-test": "badUsername" } }'
)
.c-auto-password-field-container
password-form(
mode='auto'
:label='L("This is your password. Save it now.")'
name='password'
:$v='$v'
)
.c-confirm-check-container
label.checkbox
input.input(
type='checkbox'
name='savedPassword'
v-model='form.savedPassword'
data-test='savedPassword'
@click.stop=''
)
i18n I have saved the password
label.checkbox
input.input(
type='checkbox'
name='terms'
v-model='form.terms'
data-test='signTerms'
@click.stop=''
)
i18n(
:args='{ a_: `<a class="link" target="_blank" href="${linkToTerms}">`, _a: "</a>"}'
) I agree to the {a_}terms and conditions{_a}
banner-scoped(ref='formMsg' allow-a)
.buttons.is-centered
button-submit(
@click='signup'
data-test='signSubmit'
:disabled='$v.form.$invalid'
) {{ L('Create an account') }}
</template>
<script>
import sbp from '@sbp/sbp'
import { L } from '@common/common.js'
import { maxLength, minLength, required } from 'vuelidate/lib/validators'
import { validationMixin } from 'vuelidate'
import PasswordForm from '@containers/access/PasswordForm.vue'
import BannerScoped from '@components/banners/BannerScoped.vue'
import ButtonSubmit from '@components/ButtonSubmit.vue'
import {
IDENTITY_PASSWORD_MIN_CHARS as passwordMinChars,
IDENTITY_USERNAME_MAX_CHARS as usernameMaxChars
} from '@model/contracts/shared/constants.js'
import { requestNotificationPermission } from '@model/notifications/nativeNotification.js'
import validationsDebouncedMixins from '@view-utils/validationsDebouncedMixins.js'
import {
allowedUsernameCharacters,
noConsecutiveHyphensOrUnderscores,
noLeadingOrTrailingHyphen,
noLeadingOrTrailingUnderscore,
noUppercase,
noWhitespace
} from '@model/contracts/shared/validators.js'
import { Secret } from '@chelonia/lib/Secret'
import ALLOWED_URLS from '@view-utils/allowedUrls.js'
export const usernameValidations = {
[L('A username is required.')]: required,
[L('A username cannot contain whitespace.')]: noWhitespace,
[L('A username can only contain letters, digits, hyphens or underscores.')]: allowedUsernameCharacters,
[L('A username cannot exceed {maxChars} characters.', { maxChars: usernameMaxChars })]: maxLength(usernameMaxChars),
[L('A username cannot contain uppercase letters.')]: noUppercase,
[L('A username cannot start or end with a hyphen.')]: noLeadingOrTrailingHyphen,
[L('A username cannot start or end with an underscore.')]: noLeadingOrTrailingUnderscore,
[L('A username cannot contain two consecutive hyphens or underscores.')]: noConsecutiveHyphensOrUnderscores
}
export default ({
name: 'SignupForm',
mixins: [
validationMixin,
validationsDebouncedMixins
],
props: {
// ButtonSubmit component waits until the `click` listener (which is `signup` function) is finished
// This prop is something we could add to wait for it to be finished in `signup` process
postSubmit: {
type: Function,
default: () => {}
}
},
components: {
PasswordForm,
BannerScoped,
ButtonSubmit
},
mounted () {
// NOTE: nextTick is needed because debounceField is called once after the form is mounted
this.$nextTick(() => {
this.$refs.username.focus()
})
},
data () {
return {
form: {
username: '',
password: '',
savedPassword: false,
terms: false,
pictureBase64: ''
},
linkToTerms: ALLOWED_URLS.TERMS_PAGE,
usernameAsyncValidation: {
timer: null,
resolveFn: null
}
}
},
methods: {
async signup () {
if (this.$v.form.$invalid) {
this.$refs.formMsg.danger(L('The form is invalid.'))
return
}
try {
this.$emit('signup-status', 'submitting')
await sbp('gi.app/identity/signupAndLogin', {
username: this.form.username,
password: new Secret(this.form.password)
})
await this.postSubmit()
this.$emit('signup-status', 'success')
// Request notification permissions now (within short time window of user action:
// https://github.com/whatwg/notifications/issues/108 )
requestNotificationPermission({ enableIfGranted: true })
} catch (e) {
console.error('Signup.vue submit() error:', e)
this.$refs.formMsg?.danger(e.message)
this.$emit('signup-status', 'error')
}
}
},
// we use dynamic validation schema to support accessing this.usernameAsyncValidation
// https://vuelidate.js.org/#sub-dynamic-validation-schema
validations () {
return {
form: {
username: {
...usernameValidations,
[L('This username is already being used.')]: (value) => {
if (!value) return true
if (this.usernameAsyncValidation.timer) {
clearTimeout(this.usernameAsyncValidation.timer)
}
if (this.usernameAsyncValidation.resolveFn) {
this.usernameAsyncValidation.resolveFn(true)
this.usernameAsyncValidation.resolveFn = null
}
return new Promise((resolve) => {
this.usernameAsyncValidation.resolveFn = resolve
this.usernameAsyncValidation.timer = setTimeout(async () => {
try {
resolve(!await sbp('namespace/lookup', value, { skipCache: true }))
} catch (e) {
console.warn('unexpected exception in SignupForm validation:', e)
resolve(true)
}
}, 1000)
})
}
},
password: {
[L('A password is required.')]: required,
[L('Your password must be at least {minChars} characters long.', { minChars: passwordMinChars })]: minLength(passwordMinChars)
},
savedPassword: {
[L('Please save the password.')]: (value) => Boolean(value)
},
terms: {
[L('You need to agree to the terms and conditions.')]: (value) => Boolean(value)
}
}
}
}
}: Object)
</script>
<style lang="scss" scoped>
@import "@assets/style/_variables.scss";
.c-password-fields-container {
display: flex;
flex-direction: column;
align-items: stretch;
margin-top: 1.5rem;
@include tablet {
flex-direction: row;
align-items: flex-start;
gap: 1.5rem;
}
@include phone {
margin-bottom: 1.5rem;
}
}
.c-auto-password-field-container {
margin: 1.5rem 0;
}
.c-confirm-check-container {
position: relative;
display: flex;
flex-direction: column;
row-gap: 0.5rem;
margin-bottom: 2rem;
}
</style>