Skip to content

Commit

Permalink
fix: user basic info MatchValidator error
Browse files Browse the repository at this point in the history
  • Loading branch information
meta-d committed Jan 10, 2024
1 parent 53aadb0 commit fdd3ae6
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 47 deletions.
26 changes: 13 additions & 13 deletions apps/cloud/src/app/@core/services/toastr.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TranslateService } from '@ngx-translate/core'
import { ConfirmSnackBar } from '@metad/components/confirm'
import { catchError, EMPTY, map, merge, Observable, take, takeUntil, tap } from 'rxjs'
import { SnackProcessingComponent } from '../../@theme/snack/processing'
import { getErrorMessage } from '../types'

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -46,19 +47,18 @@ export class ToastrService {
}

danger(error: any, title: string = 'PAC.TOASTR.TITLE.ERROR', translationParams: Object = {}) {
let displayMessage = ''

if (error instanceof HttpErrorResponse && typeof error.error.message === 'string') {
displayMessage = error.error.message
}
// 等同于 HttpErrorResponse ?
else if (error.error && error.error.message && typeof error.error.message === 'string') {
displayMessage = error.error.message
} else if (error.message && typeof error.message === 'string') {
displayMessage = error.message
} else {
displayMessage = error
}
const displayMessage = getErrorMessage(error)
// if (error instanceof HttpErrorResponse && typeof error.error.message === 'string') {
// displayMessage = error.error.message
// }
// // 等同于 HttpErrorResponse ?
// else if (error.error && error.error.message && typeof error.error.message === 'string') {
// displayMessage = error.error.message
// } else if (error.message && typeof error.message === 'string') {
// displayMessage = error.message
// } else {
// displayMessage = error
// }

this._snackBar.open(
this.getTranslation(displayMessage, translationParams),
Expand Down
12 changes: 6 additions & 6 deletions apps/cloud/src/app/@core/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpErrorResponse } from '@angular/common/http'
import { IProject, ISubscription, IUser } from '@metad/contracts'
import { StorySubscription } from '@metad/story/core'
import { IProject, IUser } from '@metad/contracts'
import { enUS, zhCN } from 'date-fns/locale'
import ShortUniqueId from 'short-unique-id'
export * from '@metad/contracts'
Expand Down Expand Up @@ -62,10 +61,6 @@ export enum MenuCatalog {
IndicatorApp
}

export function convertStorySubscriptionResult(result: ISubscription): StorySubscription {
return result
}

export function getErrorMessage(err: any): string {
let error: string
if (typeof err === 'string') {
Expand All @@ -81,6 +76,11 @@ export function getErrorMessage(err: any): string {
error = JSON.stringify(err)
}

// Stringify error object
if (error && typeof error !== 'string') {
error = JSON.stringify(error)
}

return error
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<formly-form [form]="form" [fields]="fields" [model]="model"
(modelChange)="onFormChange(model)"
>
</formly-form>
<formly-form
[form]="form"
[fields]="fields"
[model]="model"
(modelChange)="onFormChange(model)">
</formly-form>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ElementRef, forwardRef, Input, ViewChild } from '@angular/core'
import { Component, ElementRef, forwardRef, inject, Input, ViewChild } from '@angular/core'
import { ControlValueAccessor, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'
import { ITag, IUser } from '@metad/contracts'
import { FormlyFieldConfig } from '@ngx-formly/core'
Expand All @@ -7,6 +7,7 @@ import { firstValueFrom, map } from 'rxjs'
import { LANGUAGES, MatchValidator, RoleService, Store } from '../../../../@core'
import { TranslationBaseComponent } from '../../../language/translation-base.component'


@Component({
selector: 'pac-user-basic-info-form',
templateUrl: 'basic-info-form.component.html',
Expand All @@ -20,6 +21,10 @@ import { TranslationBaseComponent } from '../../../language/translation-base.com
]
})
export class BasicInfoFormComponent extends TranslationBaseComponent implements ControlValueAccessor {
readonly #store = inject(Store)
readonly #roleService = inject(RoleService)
readonly #authService = inject(AuthService)

UPLOADER_PLACEHOLDER = 'FORM.PLACEHOLDERS.UPLOADER_PLACEHOLDER'

@ViewChild('imagePreview')
Expand All @@ -33,21 +38,14 @@ export class BasicInfoFormComponent extends TranslationBaseComponent implements
@Input() public createdById: string
@Input() public selectedTags: ITag[]

private readonly roles$ = this.roleService.getAll().pipe(map(({ items }) => items))
readonly roles$ = this.#roleService.getAll().pipe(map(({ items }) => items))

//Fields for the form
public form = new FormGroup({})
model = {} as any
fields: FormlyFieldConfig[] = []

onChange: (value: any) => any
constructor(
private readonly store: Store,
private readonly roleService: RoleService,
private readonly authService: AuthService,
) {
super()
}

writeValue(obj: any): void {
if (obj) {
Expand Down Expand Up @@ -165,7 +163,7 @@ export class BasicInfoFormComponent extends TranslationBaseComponent implements
}
],
validators: {
validation: this.password ? [ MatchValidator.mustMatch('password', 'repeatPassword') ] : []
validation: this.password ? [ MatchValidator.mustMatch('password', 'confirmPassword') ] : []
}
},
{
Expand All @@ -187,14 +185,14 @@ export class BasicInfoFormComponent extends TranslationBaseComponent implements

async registerUser(organizationId?: string, createdById?: string) {
if (this.form.valid) {
const { tenant } = this.store.user
const { tenant } = this.#store.user
const user: IUser = {
...this.model,
tenantId: tenant.id
}

return await firstValueFrom(
this.authService.register({
this.#authService.register({
user,
password: this.model.password,
confirmPassword: this.model.confirmPassword,
Expand Down
2 changes: 1 addition & 1 deletion apps/cloud/src/app/@shared/user/forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { BasicInfoFormComponent } from './basic-info/basic-info-form.component'

export const COMPONENTS = [BasicInfoFormComponent]

export * from './user-forms.module'
export * from './user-forms.module'
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
</mat-card-header>
<mat-card-content >
<div class="w-full flex justify-center">
<pac-user-basic-info-form #form class="block max-w-[700px]" [isSuperAdmin]="true" [password]="true"
<pac-user-basic-info-form #userBasicInfo class="block max-w-[700px]"
[isSuperAdmin]="true"
[password]="true"
[(ngModel)]="user">
</pac-user-basic-info-form>
</div>
</mat-card-content>

<mat-card-actions align="end">
<button mat-flat-button color="primary" (click)="save()" [disabled]="form.form.invalid">
<button mat-flat-button color="primary" (click)="save()" [disabled]="userBasicInfo.form.pristine || userBasicInfo.form.invalid">
{{ 'PAC.ACTIONS.SAVE' | translate: {Default: 'Save'} }}
</button>
</mat-card-actions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Component, Input, OnInit, effect } from '@angular/core'
import { Component, Input, OnInit, ViewChild, effect } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { UsersService } from '@metad/cloud/state'
import { IUserUpdateInput, LanguagesEnum } from '@metad/contracts'
import { NgmCommonModule } from '@metad/ocap-angular/common'
import { UserFormsModule } from 'apps/cloud/src/app/@shared/user/forms'
import { BasicInfoFormComponent, UserFormsModule } from 'apps/cloud/src/app/@shared/user/forms'
import { ToastrService, User } from '../../../../@core'
import { CreatedByPipe, MaterialModule, SharedModule, TranslationBaseComponent } from '../../../../@shared'
import { PACEditUserComponent } from '../edit-user/edit-user.component'
Expand All @@ -27,6 +27,8 @@ import { PACEditUserComponent } from '../edit-user/edit-user.component'
export class UserBasicComponent extends TranslationBaseComponent implements OnInit {
@Input() allowRoleChange: boolean

@ViewChild('userBasicInfo') userBasicInfo: BasicInfoFormComponent

user: User
constructor(
private readonly userComponent: PACEditUserComponent,
Expand Down Expand Up @@ -72,9 +74,11 @@ export class UserBasicComponent extends TranslationBaseComponent implements OnIn
}

try {
await this.userService.update(this.user.id, request).then(() => {
this._toastrService.success(`PAC.NOTES.USERS.USER_UPDATED`, { name: new CreatedByPipe().transform(this.user) })
})
} catch (error) {}
await this.userService.update(this.user.id, request)
this._toastrService.success(`PAC.NOTES.USERS.USER_UPDATED`, { name: new CreatedByPipe().transform(this.user) })
this.userBasicInfo.form.markAsPristine()
} catch (error) {
this._toastrService.danger(error)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</span>
</nav>
</div>

<mat-divider></mat-divider>

<mat-tab-nav-panel #tabPanel class="pac-page-body" [@routeAnimations]="o.isActivated && o.activatedRoute.routeConfig.path">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Subject, firstValueFrom, map } from 'rxjs'
import { Group, IUser, ROUTE_ANIMATIONS_ELEMENTS, routeAnimations } from '../../../@core/index'
import { MaterialModule, SharedModule, UserMutationComponent, userLabel } from '../../../@shared'
import { InviteMutationComponent } from '../../../@shared/invite'
import { TranslationBaseComponent } from '../../../@shared/language/translation-base.component'
import { TranslationBaseComponent } from '../../../@shared/'


@Component({
Expand Down
2 changes: 1 addition & 1 deletion apps/cloud/src/app/services/story-public.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { PACNotificationDestinationsService } from '../@core'
import {
convertStoryPointResult,
convertStoryResult,
convertStorySubscriptionResult,
ID,
IStory,
ISubscription
} from '../@core/types'
import { convertStorySubscriptionResult } from './types'

@Injectable()
export class StoryPublicService implements NxStoryStore {
Expand Down
2 changes: 1 addition & 1 deletion apps/cloud/src/app/services/story.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import {
ISubscription,
convertStoryPointResult,
convertStoryResult,
convertStorySubscriptionResult,
convertStoryWidgetResult
} from '../@core/types'
import { convertStorySubscriptionResult } from './types'

@Injectable()
export class StoryStoreService extends ComponentStore<{ entities?: Array<Story> }> implements NxStoryStore {
Expand Down
6 changes: 6 additions & 0 deletions apps/cloud/src/app/services/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { StorySubscription } from '@metad/story/core'
import { ISubscription } from '../@core'

export function convertStorySubscriptionResult(result: ISubscription): StorySubscription {
return result
}
1 change: 1 addition & 0 deletions packages/analytics/src/core/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function prepare() {
DEFAULT_FEATURES.forEach((feature) => {
const index = features.findIndex((item) => item.code === feature.code)
if (index > -1) {
features[index].children ??= []
features[index].children.push(...feature.children)
} else {
features.push(feature as IFeatureCreateInput)
Expand Down

0 comments on commit fdd3ae6

Please sign in to comment.