refactor(identity): get api calls move to corresponding components

pull/2285/head
mehmet-erim 5 years ago
parent a191661535
commit e02f5fe5af

@ -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());
}
});
}

@ -66,20 +66,18 @@ export class IdentityState {
}
@Action(DeleteRole)
deleteRole({ dispatch }: StateContext<Identity.State>, { 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<Identity.State>, { 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<Identity.State>, { payload }: UpdateRole) {
return this.identityService
.updateRole({ ...getState().selectedRole, ...payload })
.pipe(switchMap(() => dispatch(new GetRoles())));
updateRole({ getState }: StateContext<Identity.State>, { payload }: UpdateRole) {
return this.identityService.updateRole({ ...getState().selectedRole, ...payload });
}
@Action(GetUsers)
@ -105,20 +103,18 @@ export class IdentityState {
}
@Action(DeleteUser)
deleteUser({ dispatch }: StateContext<Identity.State>, { 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<Identity.State>, { 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<Identity.State>, { payload }: UpdateUser) {
return this.identityService
.updateUser({ ...getState().selectedUser, ...payload })
.pipe(switchMap(() => dispatch(new GetUsers())));
updateUser({ getState }: StateContext<Identity.State>, { payload }: UpdateUser) {
return this.identityService.updateUser({ ...getState().selectedUser, ...payload });
}
@Action(GetUserRoles)

Loading…
Cancel
Save