1
- /* eslint ember/no-actions-hash: 0 */
2
- /* eslint ember/no-classic-classes: 0 */
3
- /* eslint ember/no-classic-components: 0 */
4
- /* eslint ember/require-tagless-components: 0 */
5
-
1
+ import Component from '@glimmer/component' ;
2
+ import { action } from '@ember/object' ;
6
3
import { inject as service } from '@ember/service' ;
7
- import Component from '@ember/component ' ;
4
+ import { tracked } from '@glimmer/tracking ' ;
8
5
import get from 'lodash/get' ;
9
- import isEmailValid from 'mon-pix /utils/email-validator' ;
6
+ import isEmailValid from '.. /utils/email-validator' ;
10
7
import isPasswordValid from '../utils/password-validator' ;
11
8
import ENV from 'mon-pix/config/environment' ;
12
9
@@ -17,156 +14,141 @@ const ERROR_INPUT_MESSAGE_MAP = {
17
14
password : 'pages.sign-up.fields.password.error' ,
18
15
} ;
19
16
20
- export default Component . extend ( {
21
-
22
- session : service ( ) ,
23
- intl : service ( ) ,
24
- url : service ( ) ,
25
-
26
- _notificationMessage : null ,
27
- validation : null ,
28
- _tokenHasBeenUsed : null ,
29
- isLoading : false ,
30
- errorMessage : null ,
31
-
32
- init ( ) {
33
- this . _super ( ...arguments ) ;
34
- this . _resetValidationFields ( ) ;
35
- } ,
17
+ class SignupFormValidation {
18
+ lastName = {
19
+ @tracked status : 'default' ,
20
+ @tracked message : null ,
21
+ }
22
+ firstName = {
23
+ @tracked status : 'default' ,
24
+ @tracked message : null ,
25
+ }
26
+ email = {
27
+ @tracked status : 'default' ,
28
+ @tracked message : null ,
29
+ }
30
+ password = {
31
+ @tracked status : 'default' ,
32
+ @tracked message : null ,
33
+ }
34
+ cgu = {
35
+ @tracked status : 'default' ,
36
+ @tracked message : null ,
37
+ }
38
+ }
39
+
40
+ export default class SignupForm extends Component {
41
+ @service session ;
42
+ @service intl ;
43
+ @service url ;
44
+
45
+ @tracked errorMessage = null ;
46
+ @tracked isLoading = false ;
47
+ @tracked notificationMessage = null ;
48
+ @tracked validation = new SignupFormValidation ( ) ;
49
+ _tokenHasBeenUsed = null ;
36
50
37
51
get homeUrl ( ) {
38
52
return this . url . homeUrl ;
39
- } ,
53
+ }
40
54
41
55
get cguUrl ( ) {
42
56
return this . url . cguUrl ;
43
- } ,
57
+ }
44
58
45
59
_getErrorMessage ( status , key ) {
46
60
return ( status === 'error' ) ? this . intl . t ( ERROR_INPUT_MESSAGE_MAP [ key ] ) : null ;
47
- } ,
61
+ }
48
62
49
63
_getValidationStatus ( isValidField ) {
50
64
return ( isValidField ) ? 'error' : 'success' ;
51
- } ,
65
+ }
52
66
53
67
_isValuePresent ( value ) {
54
68
return value . trim ( ) ? true : false ;
55
- } ,
69
+ }
56
70
57
71
_updateValidationStatus ( key , status , message ) {
58
- const statusObject = 'validation.' + key + '.status' ;
59
- const messageObject = 'validation.' + key + '.message' ;
60
- this . set ( statusObject , status ) ;
61
- this . set ( messageObject , message ) ;
62
- } ,
63
-
64
- _getModelAttributeValueFromKey ( key ) {
65
- const userModel = this . user ;
66
- return userModel . get ( key ) ;
67
- } ,
68
-
69
- _resetValidationFields ( ) {
70
- const defaultValidationObject = {
71
- lastName : {
72
- status : 'default' ,
73
- message : null ,
74
- } ,
75
- firstName : {
76
- status : 'default' ,
77
- message : null ,
78
- } ,
79
- email : {
80
- status : 'default' ,
81
- message : null ,
82
- } ,
83
- password : {
84
- status : 'default' ,
85
- message : null ,
86
- } ,
87
- cgu : {
88
- status : 'default' ,
89
- message : null ,
90
- } ,
91
- } ;
92
-
93
- this . set ( 'validation' , defaultValidationObject ) ;
94
- } ,
72
+ this . validation [ key ] . status = status ;
73
+ this . validation [ key ] . message = message ;
74
+ }
95
75
96
76
_updateInputsStatus ( ) {
97
- const errors = this . user . errors ;
77
+ const errors = this . args . user . errors ;
98
78
errors . forEach ( ( { attribute, message } ) => {
99
79
this . _updateValidationStatus ( attribute , 'error' , message ) ;
100
80
} ) ;
101
- } ,
81
+ }
102
82
103
83
_executeFieldValidation ( key , isValid ) {
104
- const modelAttrValue = this . _getModelAttributeValueFromKey ( key ) ;
84
+ const modelAttrValue = this . args . user [ key ] ;
105
85
const isValidInput = ! isValid ( modelAttrValue ) ;
106
86
const status = this . _getValidationStatus ( isValidInput ) ;
107
87
const message = this . _getErrorMessage ( status , key ) ;
108
88
this . _updateValidationStatus ( key , status , message ) ;
109
- } ,
89
+ }
110
90
111
91
_trimNamesAndEmailOfUser ( ) {
112
- const { firstName, lastName, email } = this . user ;
113
- this . set ( 'user.firstName' , firstName . trim ( ) ) ;
114
- this . set ( 'user.lastName' , lastName . trim ( ) ) ;
115
- this . set ( 'user.email' , email . trim ( ) ) ;
116
- } ,
117
-
118
- actions : {
119
-
120
- resetTokenHasBeenUsed ( ) {
121
- this . set ( '_tokenHasBeenUsed' , false ) ;
122
- } ,
123
-
124
- validateInput ( key ) {
125
- this . _executeFieldValidation ( key , this . _isValuePresent ) ;
126
- } ,
127
-
128
- validateInputEmail ( key ) {
129
- this . _executeFieldValidation ( key , isEmailValid ) ;
130
- } ,
131
-
132
- validateInputPassword ( key ) {
133
- this . _executeFieldValidation ( key , isPasswordValid ) ;
134
- } ,
135
-
136
- signup ( ) {
137
- this . set ( '_notificationMessage' , null ) ;
138
- this . set ( 'isLoading' , true ) ;
139
-
140
- this . _trimNamesAndEmailOfUser ( ) ;
141
- this . set ( 'user.lang' , this . intl . t ( 'current-lang' ) ) ;
142
-
143
- const campaignCode = get ( this . session , 'attemptedTransition.from.parent.params.code' ) ;
144
- this . user . save ( { adapterOptions : { campaignCode } } ) . then ( ( ) => {
145
- const credentials = { login : this . user . email , password : this . user . password } ;
146
- this . authenticateUser ( credentials ) ;
147
- this . set ( '_tokenHasBeenUsed' , true ) ;
148
- this . set ( 'user.password' , null ) ;
149
- } ) . catch ( ( response ) => {
150
- const error = get ( response , 'errors[0]' ) ;
151
- if ( error ) {
152
- this . _manageErrorsApi ( error ) ;
153
- }
154
- else {
155
- this . set ( 'errorMessage' , this . intl . t ( ENV . APP . API_ERROR_MESSAGES . INTERNAL_SERVER_ERROR . MESSAGE ) ) ;
156
- }
157
- this . set ( '_tokenHasBeenUsed' , true ) ;
158
- this . set ( 'isLoading' , false ) ;
159
- } ) ;
160
- } ,
161
- } ,
92
+ const { firstName, lastName, email } = this . args . user ;
93
+ this . args . user . firstName = firstName . trim ( ) ;
94
+ this . args . user . lastName = lastName . trim ( ) ;
95
+ this . args . user . email = email . trim ( ) ;
96
+ }
97
+
98
+ @action
99
+ resetTokenHasBeenUsed ( ) {
100
+ this . _tokenHasBeenUsed = false ;
101
+ }
102
+
103
+ @action
104
+ validateInput ( key ) {
105
+ this . _executeFieldValidation ( key , this . _isValuePresent ) ;
106
+ }
107
+
108
+ @action
109
+ validateInputEmail ( key ) {
110
+ this . _executeFieldValidation ( key , isEmailValid ) ;
111
+ }
112
+
113
+ @action
114
+ validateInputPassword ( key ) {
115
+ this . _executeFieldValidation ( key , isPasswordValid ) ;
116
+ }
117
+
118
+ @action
119
+ signup ( event ) {
120
+ event && event . preventDefault ( ) ;
121
+ this . notificationMessage = null ;
122
+ this . isLoading = true ;
123
+
124
+ this . _trimNamesAndEmailOfUser ( ) ;
125
+ this . args . user . lang = this . intl . t ( 'current-lang' ) ;
126
+
127
+ const campaignCode = get ( this . session , 'attemptedTransition.from.parent.params.code' ) ;
128
+ this . args . user . save ( { adapterOptions : { campaignCode } } ) . then ( ( ) => {
129
+ const credentials = { login : this . args . user . email , password : this . args . user . password } ;
130
+ this . args . authenticateUser ( credentials ) ;
131
+ this . _tokenHasBeenUsed = true ;
132
+ this . args . user . password = null ;
133
+ } ) . catch ( ( response ) => {
134
+ const error = get ( response , 'errors[0]' ) ;
135
+ if ( error ) {
136
+ this . _manageErrorsApi ( error ) ;
137
+ } else {
138
+ this . errorMessage = this . intl . t ( ENV . APP . API_ERROR_MESSAGES . INTERNAL_SERVER_ERROR . MESSAGE ) ;
139
+ }
140
+ this . _tokenHasBeenUsed = true ;
141
+ this . isLoading = false ;
142
+ } ) ;
143
+ }
162
144
163
145
_manageErrorsApi ( firstError ) {
164
146
const statusCode = get ( firstError , 'status' ) ;
165
147
if ( statusCode === '422' ) {
166
148
return this . _updateInputsStatus ( ) ;
167
149
}
168
- this . set ( ' errorMessage' , this . _showErrorMessages ( statusCode ) ) ;
169
- } ,
150
+ this . errorMessage = this . _showErrorMessages ( statusCode ) ;
151
+ }
170
152
171
153
_showErrorMessages ( statusCode ) {
172
154
const httpStatusCodeMessages = {
@@ -177,5 +159,5 @@ export default Component.extend({
177
159
'default' : ENV . APP . API_ERROR_MESSAGES . INTERNAL_SERVER_ERROR . MESSAGE ,
178
160
} ;
179
161
return this . intl . t ( httpStatusCodeMessages [ statusCode ] || httpStatusCodeMessages [ 'default' ] ) ;
180
- } ,
181
- } ) ;
162
+ }
163
+ }
0 commit comments