From e02f5fe5af20a7c3315d1472f24085bc34b33f80 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 29 Nov 2019 19:02:58 +0300 Subject: [PATCH] refactor(identity): get api calls move to corresponding components --- .../lib/components/users/users.component.ts | 30 +++++++++++++++---- .../identity/src/lib/states/identity.state.ts | 28 ++++++++--------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts index 3992b358d8..aaf59c09ef 100644 --- a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts +++ b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts @@ -1,7 +1,14 @@ import { ABP, ConfigState } from '@abp/ng.core'; import { ConfirmationService, Toaster } from '@abp/ng.theme.shared'; import { Component, TemplateRef, TrackByFunction, ViewChild, OnInit } from '@angular/core'; -import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'; +import { + AbstractControl, + FormArray, + FormBuilder, + FormGroup, + Validators, + FormControl, +} from '@angular/forms'; import { Select, Store } from '@ngxs/store'; import { Observable } from 'rxjs'; import { finalize, pluck, switchMap, take } from 'rxjs/operators'; @@ -66,7 +73,11 @@ export class UsersComponent implements OnInit { return snq(() => (this.form.get('roleNames') as FormArray).controls as FormGroup[], []); } - constructor(private confirmationService: ConfirmationService, private fb: FormBuilder, private store: Store) {} + constructor( + private confirmationService: ConfirmationService, + private fb: FormBuilder, + private store: Store, + ) {} ngOnInit() { this.get(); @@ -106,7 +117,10 @@ export class UsersComponent implements OnInit { this.roles = this.store.selectSnapshot(IdentityState.getRoles); this.form = this.fb.group({ userName: [this.selected.userName || '', [Validators.required, Validators.maxLength(256)]], - email: [this.selected.email || '', [Validators.required, Validators.email, Validators.maxLength(256)]], + email: [ + this.selected.email || '', + [Validators.required, Validators.email, Validators.maxLength(256)], + ], name: [this.selected.name || '', [Validators.maxLength(64)]], surname: [this.selected.surname || '', [Validators.maxLength(64)]], phoneNumber: [this.selected.phoneNumber || '', [Validators.maxLength(16)]], @@ -115,7 +129,9 @@ export class UsersComponent implements OnInit { roleNames: this.fb.array( this.roles.map(role => this.fb.group({ - [role.name]: [!!snq(() => this.selectedUserRoles.find(userRole => userRole.id === role.id))], + [role.name]: [ + !!snq(() => this.selectedUserRoles.find(userRole => userRole.id === role.id)), + ], }), ), ), @@ -168,7 +184,8 @@ export class UsersComponent implements OnInit { const { roleNames } = this.form.value; const mappedRoleNames = snq( - () => roleNames.filter(role => !!role[Object.keys(role)[0]]).map(role => Object.keys(role)[0]), + () => + roleNames.filter(role => !!role[Object.keys(role)[0]]).map(role => Object.keys(role)[0]), [], ); @@ -189,6 +206,7 @@ export class UsersComponent implements OnInit { .pipe(finalize(() => (this.modalBusy = false))) .subscribe(() => { this.isModalVisible = false; + this.get(); }); } @@ -199,7 +217,7 @@ export class UsersComponent implements OnInit { }) .subscribe((status: Toaster.Status) => { if (status === Toaster.Status.confirm) { - this.store.dispatch(new DeleteUser(id)); + this.store.dispatch(new DeleteUser(id)).subscribe(() => this.get()); } }); } diff --git a/npm/ng-packs/packages/identity/src/lib/states/identity.state.ts b/npm/ng-packs/packages/identity/src/lib/states/identity.state.ts index c12eac8013..142fa79b74 100644 --- a/npm/ng-packs/packages/identity/src/lib/states/identity.state.ts +++ b/npm/ng-packs/packages/identity/src/lib/states/identity.state.ts @@ -66,20 +66,18 @@ export class IdentityState { } @Action(DeleteRole) - deleteRole({ dispatch }: StateContext, { payload }: GetRoleById) { - return this.identityService.deleteRole(payload).pipe(switchMap(() => dispatch(new GetRoles()))); + deleteRole(_, { payload }: GetRoleById) { + return this.identityService.deleteRole(payload); } @Action(CreateRole) - addRole({ dispatch }: StateContext, { payload }: CreateRole) { - return this.identityService.createRole(payload).pipe(switchMap(() => dispatch(new GetRoles()))); + addRole(_, { payload }: CreateRole) { + return this.identityService.createRole(payload); } @Action(UpdateRole) - updateRole({ getState, dispatch }: StateContext, { payload }: UpdateRole) { - return this.identityService - .updateRole({ ...getState().selectedRole, ...payload }) - .pipe(switchMap(() => dispatch(new GetRoles()))); + updateRole({ getState }: StateContext, { payload }: UpdateRole) { + return this.identityService.updateRole({ ...getState().selectedRole, ...payload }); } @Action(GetUsers) @@ -105,20 +103,18 @@ export class IdentityState { } @Action(DeleteUser) - deleteUser({ dispatch }: StateContext, { payload }: GetUserById) { - return this.identityService.deleteUser(payload).pipe(switchMap(() => dispatch(new GetUsers()))); + deleteUser(_, { payload }: GetUserById) { + return this.identityService.deleteUser(payload); } @Action(CreateUser) - addUser({ dispatch }: StateContext, { payload }: CreateUser) { - return this.identityService.createUser(payload).pipe(switchMap(() => dispatch(new GetUsers()))); + addUser(_, { payload }: CreateUser) { + return this.identityService.createUser(payload); } @Action(UpdateUser) - updateUser({ getState, dispatch }: StateContext, { payload }: UpdateUser) { - return this.identityService - .updateUser({ ...getState().selectedUser, ...payload }) - .pipe(switchMap(() => dispatch(new GetUsers()))); + updateUser({ getState }: StateContext, { payload }: UpdateUser) { + return this.identityService.updateUser({ ...getState().selectedUser, ...payload }); } @Action(GetUserRoles)