Browse Source

refactor(identity): rename actions and service functions

pull/1612/head
TheDiaval 7 years ago
parent
commit
38d18b32fd
  1. 26
      npm/ng-packs/packages/identity/src/lib/actions/identity.actions.ts
  2. 18
      npm/ng-packs/packages/identity/src/lib/components/roles/roles.component.ts
  3. 24
      npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts
  4. 4
      npm/ng-packs/packages/identity/src/lib/resolvers/roles.resolver.ts
  5. 4
      npm/ng-packs/packages/identity/src/lib/resolvers/users.resolver.ts
  6. 4
      npm/ng-packs/packages/identity/src/lib/services/identity.service.ts
  7. 82
      npm/ng-packs/packages/identity/src/lib/states/identity.state.ts

26
npm/ng-packs/packages/identity/src/lib/actions/identity.actions.ts

@ -1,57 +1,57 @@
import { Identity } from '../models/identity'; import { Identity } from '../models/identity';
import { ABP } from '@abp/ng.core'; import { ABP } from '@abp/ng.core';
export class IdentityGetRoles { export class GetRoles {
static readonly type = '[Identity] Get Roles'; static readonly type = '[Identity] Get Roles';
constructor(public payload?: ABP.PageQueryParams) {} constructor(public payload?: ABP.PageQueryParams) {}
} }
export class IdentityGetRoleById { export class GetRoleById {
static readonly type = '[Identity] Get Role By Id'; static readonly type = '[Identity] Get Role By Id';
constructor(public payload: string) {} constructor(public payload: string) {}
} }
export class IdentityDeleteRole { export class DeleteRole {
static readonly type = '[Identity] Delete Role'; static readonly type = '[Identity] Delete Role';
constructor(public payload: string) {} constructor(public payload: string) {}
} }
export class IdentityAddRole { export class CreateRole {
static readonly type = '[Identity] Add Role'; static readonly type = '[Identity] Create Role';
constructor(public payload: Identity.RoleSaveRequest) {} constructor(public payload: Identity.RoleSaveRequest) {}
} }
export class IdentityUpdateRole { export class UpdateRole {
static readonly type = '[Identity] Update Role'; static readonly type = '[Identity] Update Role';
constructor(public payload: Identity.RoleItem) {} constructor(public payload: Identity.RoleItem) {}
} }
export class IdentityGetUsers { export class GetUsers {
static readonly type = '[Identity] Get Users'; static readonly type = '[Identity] Get Users';
constructor(public payload?: ABP.PageQueryParams) {} constructor(public payload?: ABP.PageQueryParams) {}
} }
export class IdentityGetUserById { export class GetUserById {
static readonly type = '[Identity] Get User By Id'; static readonly type = '[Identity] Get User By Id';
constructor(public payload: string) {} constructor(public payload: string) {}
} }
export class IdentityDeleteUser { export class DeleteUser {
static readonly type = '[Identity] Delete User'; static readonly type = '[Identity] Delete User';
constructor(public payload: string) {} constructor(public payload: string) {}
} }
export class IdentityAddUser { export class CreateUser {
static readonly type = '[Identity] Add User'; static readonly type = '[Identity] Create User';
constructor(public payload: Identity.UserSaveRequest) {} constructor(public payload: Identity.UserSaveRequest) {}
} }
export class IdentityUpdateUser { export class UpdateUser {
static readonly type = '[Identity] Update User'; static readonly type = '[Identity] Update User';
constructor(public payload: Identity.UserSaveRequest & { id: string }) {} constructor(public payload: Identity.UserSaveRequest & { id: string }) {}
} }
export class IdentityGetUserRoles { export class GetUserRoles {
static readonly type = '[Identity] Get User Roles'; static readonly type = '[Identity] Get User Roles';
constructor(public payload: string) {} constructor(public payload: string) {}
} }

18
npm/ng-packs/packages/identity/src/lib/components/roles/roles.component.ts

@ -5,13 +5,7 @@ import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'
import { Select, Store } from '@ngxs/store'; import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { finalize, pluck } from 'rxjs/operators'; import { finalize, pluck } from 'rxjs/operators';
import { import { CreateRole, DeleteRole, GetRoleById, GetRoles, UpdateRole } from '../../actions/identity.actions';
IdentityAddRole,
IdentityDeleteRole,
IdentityGetRoleById,
IdentityGetRoles,
IdentityUpdateRole,
} from '../../actions/identity.actions';
import { Identity } from '../../models/identity'; import { Identity } from '../../models/identity';
import { IdentityState } from '../../states/identity.state'; import { IdentityState } from '../../states/identity.state';
@ -75,7 +69,7 @@ export class RolesComponent {
onEdit(id: string) { onEdit(id: string) {
this.store this.store
.dispatch(new IdentityGetRoleById(id)) .dispatch(new GetRoleById(id))
.pipe(pluck('IdentityState', 'selectedRole')) .pipe(pluck('IdentityState', 'selectedRole'))
.subscribe(selectedRole => { .subscribe(selectedRole => {
this.selected = selectedRole; this.selected = selectedRole;
@ -89,8 +83,8 @@ export class RolesComponent {
this.store this.store
.dispatch( .dispatch(
this.selected.id this.selected.id
? new IdentityUpdateRole({ ...this.form.value, id: this.selected.id }) ? new UpdateRole({ ...this.form.value, id: this.selected.id })
: new IdentityAddRole(this.form.value), : new CreateRole(this.form.value),
) )
.subscribe(() => { .subscribe(() => {
this.isModalVisible = false; this.isModalVisible = false;
@ -104,7 +98,7 @@ export class RolesComponent {
}) })
.subscribe((status: Toaster.Status) => { .subscribe((status: Toaster.Status) => {
if (status === Toaster.Status.confirm) { if (status === Toaster.Status.confirm) {
this.store.dispatch(new IdentityDeleteRole(id)); this.store.dispatch(new DeleteRole(id));
} }
}); });
} }
@ -119,7 +113,7 @@ export class RolesComponent {
get() { get() {
this.loading = true; this.loading = true;
this.store this.store
.dispatch(new IdentityGetRoles(this.pageQuery)) .dispatch(new GetRoles(this.pageQuery))
.pipe(finalize(() => (this.loading = false))) .pipe(finalize(() => (this.loading = false)))
.subscribe(); .subscribe();
} }

24
npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts

@ -7,12 +7,12 @@ import { Observable } from 'rxjs';
import { finalize, pluck, switchMap, take } from 'rxjs/operators'; import { finalize, pluck, switchMap, take } from 'rxjs/operators';
import snq from 'snq'; import snq from 'snq';
import { import {
IdentityAddUser, CreateUser,
IdentityDeleteUser, DeleteUser,
IdentityGetUserById, GetUserById,
IdentityGetUserRoles, GetUserRoles,
IdentityGetUsers, GetUsers,
IdentityUpdateUser, UpdateUser,
} from '../../actions/identity.actions'; } from '../../actions/identity.actions';
import { Identity } from '../../models/identity'; import { Identity } from '../../models/identity';
import { IdentityState } from '../../states/identity.state'; import { IdentityState } from '../../states/identity.state';
@ -99,9 +99,9 @@ export class UsersComponent {
onEdit(id: string) { onEdit(id: string) {
this.store this.store
.dispatch(new IdentityGetUserById(id)) .dispatch(new GetUserById(id))
.pipe( .pipe(
switchMap(() => this.store.dispatch(new IdentityGetUserRoles(id))), switchMap(() => this.store.dispatch(new GetUserRoles(id))),
pluck('IdentityState'), pluck('IdentityState'),
take(1), take(1),
) )
@ -124,12 +124,12 @@ export class UsersComponent {
this.store this.store
.dispatch( .dispatch(
this.selected.id this.selected.id
? new IdentityUpdateUser({ ? new UpdateUser({
...this.form.value, ...this.form.value,
id: this.selected.id, id: this.selected.id,
roleNames: mappedRoleNames, roleNames: mappedRoleNames,
}) })
: new IdentityAddUser({ : new CreateUser({
...this.form.value, ...this.form.value,
roleNames: mappedRoleNames, roleNames: mappedRoleNames,
}), }),
@ -146,7 +146,7 @@ export class UsersComponent {
}) })
.subscribe((status: Toaster.Status) => { .subscribe((status: Toaster.Status) => {
if (status === Toaster.Status.confirm) { if (status === Toaster.Status.confirm) {
this.store.dispatch(new IdentityDeleteUser(id)); this.store.dispatch(new DeleteUser(id));
} }
}); });
} }
@ -161,7 +161,7 @@ export class UsersComponent {
get() { get() {
this.loading = true; this.loading = true;
this.store this.store
.dispatch(new IdentityGetUsers(this.pageQuery)) .dispatch(new GetUsers(this.pageQuery))
.pipe(finalize(() => (this.loading = false))) .pipe(finalize(() => (this.loading = false)))
.subscribe(); .subscribe();
} }

4
npm/ng-packs/packages/identity/src/lib/resolvers/roles.resolver.ts

@ -1,7 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router'; import { Resolve } from '@angular/router';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { IdentityGetRoles } from '../actions/identity.actions'; import { GetRoles } from '../actions/identity.actions';
import { Identity } from '../models/identity'; import { Identity } from '../models/identity';
import { IdentityState } from '../states/identity.state'; import { IdentityState } from '../states/identity.state';
@ -11,6 +11,6 @@ export class RoleResolver implements Resolve<Identity.State> {
resolve() { resolve() {
const roles = this.store.selectSnapshot(IdentityState.getRoles); const roles = this.store.selectSnapshot(IdentityState.getRoles);
return roles && roles.length ? null : this.store.dispatch(new IdentityGetRoles()); return roles && roles.length ? null : this.store.dispatch(new GetRoles());
} }
} }

4
npm/ng-packs/packages/identity/src/lib/resolvers/users.resolver.ts

@ -1,7 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router'; import { Resolve } from '@angular/router';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { IdentityGetUsers } from '../actions/identity.actions'; import { GetUsers } from '../actions/identity.actions';
import { Identity } from '../models/identity'; import { Identity } from '../models/identity';
import { IdentityState } from '../states/identity.state'; import { IdentityState } from '../states/identity.state';
@ -11,6 +11,6 @@ export class UserResolver implements Resolve<Identity.State> {
resolve() { resolve() {
const users = this.store.selectSnapshot(IdentityState.getUsers); const users = this.store.selectSnapshot(IdentityState.getUsers);
return users && users.length ? null : this.store.dispatch(new IdentityGetUsers()); return users && users.length ? null : this.store.dispatch(new GetUsers());
} }
} }

4
npm/ng-packs/packages/identity/src/lib/services/identity.service.ts

@ -37,7 +37,7 @@ export class IdentityService {
return this.rest.request<null, Identity.RoleItem>(request); return this.rest.request<null, Identity.RoleItem>(request);
} }
addRole(body: Identity.RoleSaveRequest): Observable<Identity.RoleItem> { createRole(body: Identity.RoleSaveRequest): Observable<Identity.RoleItem> {
const request: Rest.Request<Identity.RoleSaveRequest> = { const request: Rest.Request<Identity.RoleSaveRequest> = {
method: 'POST', method: 'POST',
url: '/api/identity/roles', url: '/api/identity/roles',
@ -97,7 +97,7 @@ export class IdentityService {
return this.rest.request<null, null>(request); return this.rest.request<null, null>(request);
} }
addUser(body: Identity.UserSaveRequest): Observable<Identity.UserItem> { createUser(body: Identity.UserSaveRequest): Observable<Identity.UserItem> {
const request: Rest.Request<Identity.UserSaveRequest> = { const request: Rest.Request<Identity.UserSaveRequest> = {
method: 'POST', method: 'POST',
url: '/api/identity/users', url: '/api/identity/users',

82
npm/ng-packs/packages/identity/src/lib/states/identity.state.ts

@ -1,17 +1,17 @@
import { Action, Selector, State, StateContext } from '@ngxs/store'; import { Action, Selector, State, StateContext } from '@ngxs/store';
import { switchMap, tap, pluck } from 'rxjs/operators'; import { switchMap, tap, pluck } from 'rxjs/operators';
import { import {
IdentityAddRole, CreateRole,
IdentityAddUser, CreateUser,
IdentityDeleteRole, DeleteRole,
IdentityDeleteUser, DeleteUser,
IdentityGetRoleById, GetRoleById,
IdentityGetRoles, GetRoles,
IdentityGetUserById, GetUserById,
IdentityGetUsers, GetUsers,
IdentityUpdateRole, UpdateRole,
IdentityUpdateUser, UpdateUser,
IdentityGetUserRoles, GetUserRoles,
} from '../actions/identity.actions'; } from '../actions/identity.actions';
import { Identity } from '../models/identity'; import { Identity } from '../models/identity';
import { IdentityService } from '../services/identity.service'; import { IdentityService } from '../services/identity.service';
@ -43,8 +43,8 @@ export class IdentityState {
constructor(private identityService: IdentityService) {} constructor(private identityService: IdentityService) {}
@Action(IdentityGetRoles) @Action(GetRoles)
getRoles({ patchState }: StateContext<Identity.State>, { payload }: IdentityGetRoles) { getRoles({ patchState }: StateContext<Identity.State>, { payload }: GetRoles) {
return this.identityService.getRoles(payload).pipe( return this.identityService.getRoles(payload).pipe(
tap(roles => tap(roles =>
patchState({ patchState({
@ -54,8 +54,8 @@ export class IdentityState {
); );
} }
@Action(IdentityGetRoleById) @Action(GetRoleById)
getRole({ patchState }: StateContext<Identity.State>, { payload }: IdentityGetRoleById) { getRole({ patchState }: StateContext<Identity.State>, { payload }: GetRoleById) {
return this.identityService.getRoleById(payload).pipe( return this.identityService.getRoleById(payload).pipe(
tap(selectedRole => tap(selectedRole =>
patchState({ patchState({
@ -65,26 +65,26 @@ export class IdentityState {
); );
} }
@Action(IdentityDeleteRole) @Action(DeleteRole)
deleteRole({ dispatch }: StateContext<Identity.State>, { payload }: IdentityGetRoleById) { deleteRole({ dispatch }: StateContext<Identity.State>, { payload }: GetRoleById) {
return this.identityService.deleteRole(payload).pipe(switchMap(() => dispatch(new IdentityGetRoles()))); return this.identityService.deleteRole(payload).pipe(switchMap(() => dispatch(new GetRoles())));
} }
@Action(IdentityAddRole) @Action(CreateRole)
addRole({ dispatch }: StateContext<Identity.State>, { payload }: IdentityAddRole) { addRole({ dispatch }: StateContext<Identity.State>, { payload }: CreateRole) {
return this.identityService.addRole(payload).pipe(switchMap(() => dispatch(new IdentityGetRoles()))); return this.identityService.createRole(payload).pipe(switchMap(() => dispatch(new GetRoles())));
} }
@Action(IdentityUpdateRole) @Action(UpdateRole)
updateRole({ getState, dispatch }: StateContext<Identity.State>, { payload }: IdentityUpdateRole) { updateRole({ getState, dispatch }: StateContext<Identity.State>, { payload }: UpdateRole) {
return dispatch(new IdentityGetRoleById(payload.id)).pipe( return dispatch(new GetRoleById(payload.id)).pipe(
switchMap(() => this.identityService.updateRole({ ...getState().selectedRole, ...payload })), switchMap(() => this.identityService.updateRole({ ...getState().selectedRole, ...payload })),
switchMap(() => dispatch(new IdentityGetRoles())), switchMap(() => dispatch(new GetRoles())),
); );
} }
@Action(IdentityGetUsers) @Action(GetUsers)
getUsers({ patchState }: StateContext<Identity.State>, { payload }: IdentityGetUsers) { getUsers({ patchState }: StateContext<Identity.State>, { payload }: GetUsers) {
return this.identityService.getUsers(payload).pipe( return this.identityService.getUsers(payload).pipe(
tap(users => tap(users =>
patchState({ patchState({
@ -94,8 +94,8 @@ export class IdentityState {
); );
} }
@Action(IdentityGetUserById) @Action(GetUserById)
getUser({ patchState }: StateContext<Identity.State>, { payload }: IdentityGetUserById) { getUser({ patchState }: StateContext<Identity.State>, { payload }: GetUserById) {
return this.identityService.getUserById(payload).pipe( return this.identityService.getUserById(payload).pipe(
tap(selectedUser => tap(selectedUser =>
patchState({ patchState({
@ -105,26 +105,26 @@ export class IdentityState {
); );
} }
@Action(IdentityDeleteUser) @Action(DeleteUser)
deleteUser({ dispatch }: StateContext<Identity.State>, { payload }: IdentityGetUserById) { deleteUser({ dispatch }: StateContext<Identity.State>, { payload }: GetUserById) {
return this.identityService.deleteUser(payload).pipe(switchMap(() => dispatch(new IdentityGetUsers()))); return this.identityService.deleteUser(payload).pipe(switchMap(() => dispatch(new GetUsers())));
} }
@Action(IdentityAddUser) @Action(CreateUser)
addUser({ dispatch }: StateContext<Identity.State>, { payload }: IdentityAddUser) { addUser({ dispatch }: StateContext<Identity.State>, { payload }: CreateUser) {
return this.identityService.addUser(payload).pipe(switchMap(() => dispatch(new IdentityGetUsers()))); return this.identityService.createUser(payload).pipe(switchMap(() => dispatch(new GetUsers())));
} }
@Action(IdentityUpdateUser) @Action(UpdateUser)
updateUser({ getState, dispatch }: StateContext<Identity.State>, { payload }: IdentityUpdateUser) { updateUser({ getState, dispatch }: StateContext<Identity.State>, { payload }: UpdateUser) {
return dispatch(new IdentityGetUserById(payload.id)).pipe( return dispatch(new GetUserById(payload.id)).pipe(
switchMap(() => this.identityService.updateUser({ ...getState().selectedUser, ...payload })), switchMap(() => this.identityService.updateUser({ ...getState().selectedUser, ...payload })),
switchMap(() => dispatch(new IdentityGetUsers())), switchMap(() => dispatch(new GetUsers())),
); );
} }
@Action(IdentityGetUserRoles) @Action(GetUserRoles)
getUserRoles({ patchState }: StateContext<Identity.State>, { payload }: IdentityGetUserRoles) { getUserRoles({ patchState }: StateContext<Identity.State>, { payload }: GetUserRoles) {
return this.identityService.getUserRoles(payload).pipe( return this.identityService.getUserRoles(payload).pipe(
pluck('items'), pluck('items'),
tap(selectedUserRoles => tap(selectedUserRoles =>

Loading…
Cancel
Save