@@ -33,7 +33,7 @@ qx.Class.define("osparc.desktop.preferences.pages.ProfilePage", {
3333 this . __userProfileData = null ;
3434 this . __userProfileModel = null ;
3535
36- this . __getValuesFromServer ( ) ;
36+ this . __getProfile ( ) ;
3737
3838 this . add ( this . __createProfileUser ( ) ) ;
3939 } ,
@@ -87,6 +87,20 @@ qx.Class.define("osparc.desktop.preferences.pages.ProfilePage", {
8787
8888 box . add ( new qx . ui . form . renderer . Single ( form ) ) ;
8989
90+ const expirationLayout = new qx . ui . container . Composite ( new qx . ui . layout . HBox ( 5 ) ) . set ( {
91+ paddingLeft : 16 ,
92+ visibility : "excluded"
93+ } ) ;
94+ expirationLayout . add ( new qx . ui . basic . Label ( this . tr ( "Expiration date:" ) ) ) ;
95+ const expirationDate = new qx . ui . basic . Label ( ) ;
96+ expirationLayout . add ( expirationDate ) ;
97+ const infoLabel = this . tr ( "Please, contact us by email:<br>" ) ;
98+ const infoExtension = new osparc . ui . hint . InfoHint ( infoLabel ) ;
99+ osparc . store . StaticInfo . getInstance ( ) . getSupportEmail ( )
100+ . then ( supportEmail => infoExtension . setHintText ( infoLabel + supportEmail ) ) ;
101+ expirationLayout . add ( infoExtension ) ;
102+ box . add ( expirationLayout ) ;
103+
90104 const img = new qx . ui . basic . Image ( ) . set ( {
91105 decorator : new qx . ui . decoration . Decorator ( ) . set ( {
92106 radius : 50
@@ -100,16 +114,16 @@ qx.Class.define("osparc.desktop.preferences.pages.ProfilePage", {
100114 "firstName" : null ,
101115 "lastName" : null ,
102116 "email" : null ,
103- "role" : null
117+ "role" : null ,
118+ "expirationDate" : null
104119 } ;
105120
106121 if ( qx . core . Environment . get ( "qx.debug" ) ) {
107- raw = {
108- "firstName" : "Bizzy" ,
109- "lastName" : "Zastrow" ,
110- 111- "role" : "Tester"
112- } ;
122+ raw . firstName = "Bizzy" ;
123+ raw . lastName = "Zastrow" ;
124+ 125+ raw . role = "User" ;
126+ raw . expirationDate = null ;
113127 }
114128 const model = this . __userProfileModel = qx . data . marshal . Json . createModel ( raw ) ;
115129 const controller = new qx . data . controller . Object ( model ) ;
@@ -122,69 +136,95 @@ qx.Class.define("osparc.desktop.preferences.pages.ProfilePage", {
122136 } ) ;
123137 controller . addTarget ( lastName , "value" , "lastName" , true ) ;
124138 controller . addTarget ( role , "value" , "role" , false ) ;
139+ controller . addTarget ( expirationDate , "value" , "expirationDate" , false , {
140+ converter : data => {
141+ if ( data ) {
142+ expirationLayout . show ( ) ;
143+ return osparc . utils . Utils . formatDateAndTime ( new Date ( data ) ) ;
144+ }
145+ return "" ;
146+ }
147+ } ) ;
125148 controller . addTarget ( img , "source" , "email" , false , {
126149 converter : function ( data ) {
127150 return osparc . utils . Avatar . getUrl ( email . getValue ( ) , 150 ) ;
128151 }
129152 } ) ;
130153
131154 // validation
132- const manager = new qx . ui . form . validation . Manager ( ) ;
133- manager . add ( email , qx . util . Validate . email ( ) ) ;
134- [ firstName , lastName ] . forEach ( field => {
135- manager . add ( field , qx . util . Validate . regExp ( / [ ^ \. \d ] + / ) , this . tr ( "Avoid dots or numbers in text" ) ) ;
136- } ) ;
155+ const emailValidator = new qx . ui . form . validation . Manager ( ) ;
156+ emailValidator . add ( email , qx . util . Validate . email ( ) ) ;
157+
158+ const namesValidator = new qx . ui . form . validation . Manager ( ) ;
159+ namesValidator . add ( firstName , qx . util . Validate . regExp ( / [ ^ \. \d ] + / ) , this . tr ( "Avoid dots or numbers in text" ) ) ;
160+ namesValidator . add ( lastName , qx . util . Validate . regExp ( / ^ $ | [ ^ \. \d ] + / ) , this . tr ( "Avoid dots or numbers in text" ) ) ; // allow also emtpy last name
137161
138162 const updateBtn = new qx . ui . form . Button ( "Update Profile" ) . set ( {
139163 allowGrowX : false
140164 } ) ;
141165 box . add ( updateBtn ) ;
142166
143- // update trigger
144167 updateBtn . addListener ( "execute" , ( ) => {
145168 if ( ! osparc . data . Permissions . getInstance ( ) . canDo ( "user.user.update" , true ) ) {
146169 this . __resetDataToModel ( ) ;
147170 return ;
148171 }
149172
150- if ( manager . validate ( ) ) {
151- const emailReq = new osparc . io . request . ApiRequest ( "/auth/change-email" , "POST" ) ;
152- emailReq . setRequestData ( {
153- "email" : model . getEmail ( )
154- } ) ;
155-
156- const profileReq = new osparc . io . request . ApiRequest ( "/me" , "PUT" ) ;
157- profileReq . setRequestData ( {
158- "first_name" : model . getFirstName ( ) ,
159- "last_name" : model . getLastName ( )
160- } ) ;
161-
162- [ emailReq , profileReq ] . forEach ( req => {
163- // requests
164- req . addListenerOnce ( "success" , e => {
165- const res = e . getTarget ( ) . getResponse ( ) ;
166- if ( res && res . data ) {
167- osparc . component . message . FlashMessenger . getInstance ( ) . log ( res . data ) ;
168- }
169- } , this ) ;
170-
171- req . addListenerOnce ( "fail" , e => {
172- // FIXME: should revert to old?? or GET? Store might resolve this??
173- this . __resetDataToModel ( ) ;
174- const error = e . getTarget ( ) . getResponse ( ) . error ;
175- const msg = error ? error [ "errors" ] [ 0 ] . message : this . tr ( "Failed to update profile" ) ;
176- osparc . component . message . FlashMessenger . getInstance ( ) . logAs ( msg , "ERROR" ) ;
177- } , this ) ;
178-
179- req . send ( ) ;
180- } ) ;
173+ const requests = {
174+ email : null ,
175+ names : null
176+ } ;
177+ if ( this . __userProfileData [ "login" ] !== model . getEmail ( ) ) {
178+ if ( emailValidator . validate ( ) ) {
179+ const emailReq = new osparc . io . request . ApiRequest ( "/auth/change-email" , "POST" ) ;
180+ emailReq . setRequestData ( {
181+ "email" : model . getEmail ( )
182+ } ) ;
183+ requests . email = emailReq ;
184+ }
185+ }
186+
187+ if ( this . __userProfileData [ "first_name" ] !== model . getFirstName ( ) || this . __userProfileData [ "last_name" ] !== model . getLastName ( ) ) {
188+ if ( namesValidator . validate ( ) ) {
189+ const profileReq = new osparc . io . request . ApiRequest ( "/me" , "PUT" ) ;
190+ profileReq . setRequestData ( {
191+ "first_name" : model . getFirstName ( ) ,
192+ "last_name" : model . getLastName ( )
193+ } ) ;
194+ requests . names = profileReq ;
195+ }
181196 }
197+
198+ Object . keys ( requests ) . forEach ( key => {
199+ const req = requests [ key ] ;
200+ if ( req === null ) {
201+ return ;
202+ }
203+
204+ req . addListenerOnce ( "success" , e => {
205+ const reqData = e . getTarget ( ) . getRequestData ( ) ;
206+ this . __setDataToModel ( Object . assign ( this . __userProfileData , reqData ) ) ;
207+ osparc . auth . Manager . getInstance ( ) . updateProfile ( this . __userProfileData ) ;
208+ const res = e . getTarget ( ) . getResponse ( ) ;
209+ const msg = ( res && res . data ) ? res . data : this . tr ( "Profile updated" ) ;
210+ osparc . component . message . FlashMessenger . getInstance ( ) . logAs ( msg , "INFO" ) ;
211+ } , this ) ;
212+
213+ req . addListenerOnce ( "fail" , e => {
214+ this . __resetDataToModel ( ) ;
215+ const error = e . getTarget ( ) . getResponse ( ) . error ;
216+ const msg = error ? error [ "errors" ] [ 0 ] . message : this . tr ( "Failed to update profile" ) ;
217+ osparc . component . message . FlashMessenger . getInstance ( ) . logAs ( msg , "ERROR" ) ;
218+ } , this ) ;
219+
220+ req . send ( ) ;
221+ } ) ;
182222 } , this ) ;
183223
184224 return box ;
185225 } ,
186226
187- __getValuesFromServer : function ( ) {
227+ __getProfile : function ( ) {
188228 osparc . data . Resources . getOne ( "profile" , { } , null , false )
189229 . then ( profile => {
190230 this . __setDataToModel ( profile ) ;
@@ -201,7 +241,8 @@ qx.Class.define("osparc.desktop.preferences.pages.ProfilePage", {
201241 "firstName" : data [ "first_name" ] || "" ,
202242 "lastName" : data [ "last_name" ] || "" ,
203243 "email" : data [ "login" ] ,
204- "role" : data [ "role" ] || ""
244+ "role" : data [ "role" ] || "" ,
245+ "expirationDate" : data [ "expirationDate" ] || null
205246 } ) ;
206247 }
207248 } ,
0 commit comments