1
- import { HttpErrorResponse } from '@angular/common/http' ;
2
1
import { Component } from '@angular/core' ;
3
2
import { FormControl , UntypedFormGroup } from '@angular/forms' ;
4
3
import { NavController } from '@ionic/angular' ;
5
4
import { TranslocoService } from '@ngneat/transloco' ;
6
5
import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ;
7
6
import { FormlyFieldConfig } from '@ngx-formly/core' ;
8
- import { combineLatest , forkJoin } from 'rxjs' ;
9
- import {
10
- catchError ,
11
- first ,
12
- map ,
13
- shareReplay ,
14
- switchMap ,
15
- tap ,
16
- } from 'rxjs/operators' ;
7
+ import { combineLatest } from 'rxjs' ;
8
+ import { catchError , first , map , tap } from 'rxjs/operators' ;
17
9
import { BlockingActionService } from '../../../shared/blocking-action/blocking-action.service' ;
18
10
import { DiaBackendAuthService } from '../../../shared/dia-backend/auth/dia-backend-auth.service' ;
19
11
import { ErrorService } from '../../../shared/error/error.service' ;
@@ -27,22 +19,21 @@ import { NetworkService } from '../../../shared/network/network.service';
27
19
} )
28
20
export class EditProfilePage {
29
21
readonly networkConnected$ = this . networkService . connected$ ;
30
- readonly username$ = this . diaBackendAuthService . username$ ;
31
- readonly avatar$ = this . diaBackendAuthService . avatar$ . pipe (
32
- shareReplay ( { bufferSize : 1 , refCount : true } )
22
+ readonly profileName$ = this . diaBackendAuthService . profile$ . pipe (
23
+ map ( profile => profile . display_name )
33
24
) ;
34
- readonly profile$ = this . diaBackendAuthService
35
- . readProfile$ ( )
36
- . pipe ( shareReplay ( { bufferSize : 1 , refCount : true } ) ) ;
37
- readonly description$ = this . profile$ . pipe (
25
+ readonly description$ = this . diaBackendAuthService . profile$ . pipe (
38
26
map ( profile => profile . description )
39
27
) ;
40
- readonly background$ = this . profile$ . pipe (
28
+ readonly avatar$ = this . diaBackendAuthService . profile$ . pipe (
29
+ map ( profile => profile . profile_picture_thumbnail )
30
+ ) ;
31
+ readonly background$ = this . diaBackendAuthService . profile$ . pipe (
41
32
map ( profile => profile . profile_background_thumbnail )
42
33
) ;
43
34
readonly form = new UntypedFormGroup ( { } ) ;
44
35
model : EditProfileFormModel = {
45
- username : '' ,
36
+ profileName : '' ,
46
37
description : '' ,
47
38
profilePicture : undefined ,
48
39
profileBackground : undefined ,
@@ -67,27 +58,27 @@ export class EditProfilePage {
67
58
68
59
createFormFields ( ) {
69
60
combineLatest ( [
70
- this . translocoService . selectTranslate ( 'home.editProfile.username ' ) ,
61
+ this . translocoService . selectTranslate ( 'home.editProfile.profileName ' ) ,
71
62
this . translocoService . selectTranslate ( 'home.editProfile.description' ) ,
72
63
] )
73
64
. pipe (
74
- tap ( ( [ usernameTranslation , descriptionTranslation ] ) => {
65
+ tap ( ( [ profileNameTranslation , descriptionTranslation ] ) => {
75
66
this . fields = [
76
67
{
77
- key : 'username ' ,
68
+ key : 'profileName ' ,
78
69
type : 'input' ,
79
70
templateOptions : {
80
- label : usernameTranslation ,
81
- placeholder : usernameTranslation ,
71
+ label : profileNameTranslation ,
72
+ placeholder : profileNameTranslation ,
82
73
appearance : 'outline' ,
83
74
} ,
84
75
validators : {
85
- username : {
86
- expression : ( c : FormControl ) => / ^ .{ 1 , 21 } $ / . test ( c . value ) ,
76
+ profileName : {
77
+ expression : ( c : FormControl ) => / ^ .{ 1 , 15 } $ / . test ( c . value ) ,
87
78
message : ( ) =>
88
79
this . translocoService . translate (
89
80
'home.editProfile.error.mustBeBetween' ,
90
- { min : 1 , max : 21 }
81
+ { min : 1 , max : 15 }
91
82
) ,
92
83
} ,
93
84
} ,
@@ -102,12 +93,12 @@ export class EditProfilePage {
102
93
rows : 4 ,
103
94
} ,
104
95
validators : {
105
- username : {
106
- expression : ( c : FormControl ) => / ^ .{ 0 , 255 } $ / . test ( c . value ) ,
96
+ description : {
97
+ expression : ( c : FormControl ) => / ^ .{ 0 , 125 } $ / . test ( c . value ) ,
107
98
message : ( ) =>
108
99
this . translocoService . translate (
109
100
'home.editProfile.error.mustBeBetween' ,
110
- { min : 0 , max : 255 }
101
+ { min : 0 , max : 125 }
111
102
) ,
112
103
} ,
113
104
} ,
@@ -130,11 +121,11 @@ export class EditProfilePage {
130
121
}
131
122
132
123
populateFormFields ( ) {
133
- combineLatest ( [ this . username $, this . description$ ] )
124
+ combineLatest ( [ this . profileName $, this . description$ ] )
134
125
. pipe (
135
126
first ( ) ,
136
- tap ( ( [ username , description ] ) => {
137
- this . model = { ...this . model , username , description } ;
127
+ tap ( ( [ profileName , description ] ) => {
128
+ this . model = { ...this . model , profileName , description } ;
138
129
} )
139
130
)
140
131
. subscribe ( ) ;
@@ -151,19 +142,11 @@ export class EditProfilePage {
151
142
}
152
143
153
144
async onSubmit ( ) {
154
- const updateUserNameAction$ = this . blockingActionService
155
- . run$ (
156
- this . diaBackendAuthService
157
- . updateUser$ ( { username : this . model . username } )
158
- . pipe (
159
- catchError ( ( err : unknown ) => this . handleUpdateUsernameError$ ( err ) )
160
- )
161
- )
162
- . pipe ( untilDestroyed ( this ) ) ;
163
- const updateProfileAction$ = this . blockingActionService
145
+ this . blockingActionService
164
146
. run$ (
165
147
this . diaBackendAuthService
166
148
. updateProfile$ ( {
149
+ profileName : this . model . profileName ,
167
150
description : this . model . description ,
168
151
profilePicture : this . model . profilePicture ,
169
152
profileBackground : this . model . profileBackground ,
@@ -173,39 +156,7 @@ export class EditProfilePage {
173
156
)
174
157
)
175
158
. pipe (
176
- switchMap ( ( ) => this . diaBackendAuthService . syncUser$ ( ) ) ,
177
- untilDestroyed ( this )
178
- ) ;
179
- forkJoin ( [ updateUserNameAction$ , updateProfileAction$ ] )
180
- . pipe ( tap ( ( ) => this . navController . back ( ) ) )
181
- . subscribe ( ) ;
182
- }
183
-
184
- private handleUpdateUsernameError$ ( err : unknown ) {
185
- if ( err instanceof HttpErrorResponse ) {
186
- const errorType = err . error . error ?. type ;
187
- if ( errorType === 'duplicate_username' ) {
188
- return this . errorService . toastError$ (
189
- this . translocoService . translate ( `error.diaBackend.${ errorType } ` )
190
- ) ;
191
- }
192
- }
193
- return this . errorService . toastError$ ( err ) ;
194
- }
195
-
196
- private updateProfile ( ) {
197
- const updateProfileAction$ = this . diaBackendAuthService
198
- . updateProfile$ ( {
199
- description : this . model . description ,
200
- profilePicture : this . model . profilePicture ,
201
- profileBackground : this . model . profileBackground ,
202
- } )
203
- . pipe ( catchError ( ( err : unknown ) => this . handleUpdateProfileError ( err ) ) ) ;
204
-
205
- this . blockingActionService
206
- . run$ ( updateProfileAction$ )
207
- . pipe (
208
- switchMap ( ( ) => this . diaBackendAuthService . syncUser$ ( ) ) ,
159
+ tap ( ( ) => this . navController . back ( ) ) ,
209
160
untilDestroyed ( this )
210
161
)
211
162
. subscribe ( ) ;
@@ -217,7 +168,7 @@ export class EditProfilePage {
217
168
}
218
169
219
170
interface EditProfileFormModel {
220
- username : string ;
171
+ profileName : string ;
221
172
description : string ;
222
173
profilePicture : File | undefined ;
223
174
profileBackground : File | undefined ;
0 commit comments