Browse Source

chore: remove replaceable components state

pull/6169/head
Arman Ozak 5 years ago
parent
commit
041f9bd20d
  1. 3
      npm/ng-packs/packages/core/src/lib/actions/index.ts
  2. 13
      npm/ng-packs/packages/core/src/lib/actions/replaceable-components.actions.ts
  3. 3
      npm/ng-packs/packages/core/src/lib/core.module.ts
  4. 1
      npm/ng-packs/packages/core/src/lib/states/index.ts
  5. 74
      npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts
  6. 59
      npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts

3
npm/ng-packs/packages/core/src/lib/actions/index.ts

@ -1,5 +1,4 @@
export { SetEnvironment, GetAppConfiguration } from './config.actions';
export { GetAppConfiguration, SetEnvironment } from './config.actions';
export * from './loader.actions';
export * from './profile.actions';
export * from './replaceable-components.actions';
export * from './rest.actions';

13
npm/ng-packs/packages/core/src/lib/actions/replaceable-components.actions.ts

@ -1,13 +0,0 @@
import { ReplaceableComponents } from '../models/replaceable-components';
// tslint:disable: max-line-length
/**
* @deprecated To be deleted in v4.0. Use ReplaceableComponentsService instead. See the doc (https://docs.abp.io/en/abp/latest/UI/Angular/Component-Replacement)
*/
export class AddReplaceableComponent {
static readonly type = '[ReplaceableComponents] Add';
constructor(
public payload: ReplaceableComponents.ReplaceableComponent,
public reload?: boolean,
) {}
}

3
npm/ng-packs/packages/core/src/lib/core.module.ts

@ -30,7 +30,6 @@ import { SortPipe } from './pipes/sort.pipe';
import { LocaleProvider } from './providers/locale.provider';
import { LocalizationService } from './services/localization.service';
import { ProfileState } from './states/profile.state';
import { ReplaceableComponentsState } from './states/replaceable-components.state';
import { oAuthStorage } from './strategies/auth-flow.strategy';
import { coreOptionsFactory, CORE_OPTIONS } from './tokens/options.token';
import { noop } from './utils/common-utils';
@ -113,7 +112,7 @@ export class BaseCoreModule {}
imports: [
BaseCoreModule,
LocalizationModule,
NgxsModule.forFeature([ReplaceableComponentsState, ProfileState]),
NgxsModule.forFeature([ProfileState]),
NgxsRouterPluginModule.forRoot(),
OAuthModule.forRoot(),
HttpClientXsrfModule.withOptions({

1
npm/ng-packs/packages/core/src/lib/states/index.ts

@ -1,3 +1,2 @@
export * from './replaceable-components.state';
export * from './config.state';
export * from './profile.state';

74
npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts

@ -1,74 +0,0 @@
import { Injectable, isDevMode } from '@angular/core';
import { Action, createSelector, Selector, State, StateContext } from '@ngxs/store';
import snq from 'snq';
import { AddReplaceableComponent } from '../actions/replaceable-components.actions';
import { ReplaceableComponents } from '../models/replaceable-components';
import { ReplaceableComponentsService } from '../services/replaceable-components.service';
function logDeprecationMsg() {
if (isDevMode()) {
console.warn(`
ReplacableComponentsState has been deprecated. Use ReplaceableComponentsService instead.
See the doc https://docs.abp.io/en/abp/latest/UI/Angular/Component-Replacement
`);
}
}
// tslint:disable: max-line-length
/**
* @deprecated To be deleted in v4.0. Use ReplaceableComponentsService instead. See the doc (https://docs.abp.io/en/abp/latest/UI/Angular/Component-Replacement)
*/
@State<ReplaceableComponents.State>({
name: 'ReplaceableComponentsState',
defaults: { replaceableComponents: [] } as ReplaceableComponents.State,
})
@Injectable()
export class ReplaceableComponentsState {
@Selector()
static getAll({
replaceableComponents,
}: ReplaceableComponents.State): ReplaceableComponents.ReplaceableComponent[] {
logDeprecationMsg();
return replaceableComponents || [];
}
static getComponent(key: string) {
const selector = createSelector(
[ReplaceableComponentsState],
(state: ReplaceableComponents.State): ReplaceableComponents.ReplaceableComponent => {
logDeprecationMsg();
return snq(() => state.replaceableComponents.find(component => component.key === key));
},
);
return selector;
}
constructor(private service: ReplaceableComponentsService) {}
@Action(AddReplaceableComponent)
replaceableComponentsAction(
{ getState, patchState }: StateContext<ReplaceableComponents.State>,
{ payload, reload }: AddReplaceableComponent,
) {
logDeprecationMsg();
let { replaceableComponents } = getState();
const index = snq(
() => replaceableComponents.findIndex(component => component.key === payload.key),
-1,
);
if (index > -1) {
replaceableComponents[index] = payload;
} else {
replaceableComponents = [...replaceableComponents, payload];
}
patchState({
replaceableComponents,
});
this.service.add(payload, reload);
}
}

59
npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts

@ -1,59 +0,0 @@
import { APP_BASE_HREF } from '@angular/common';
import { Component } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
import { SpyObject } from '@ngneat/spectator';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest';
import { NgxsModule, Store } from '@ngxs/store';
import { AddReplaceableComponent } from '../actions';
import { ReplaceableComponentsState } from '../states/replaceable-components.state';
@Component({ selector: 'abp-dummy', template: 'dummy works' })
class DummyComponent {}
describe('ReplaceableComponentsState', () => {
let spectator: SpectatorHost<DummyComponent>;
let router: SpyObject<Router>;
const createHost = createHostFactory({
component: DummyComponent,
providers: [{ provide: APP_BASE_HREF, useValue: '/' }],
imports: [RouterModule.forRoot([]), NgxsModule.forRoot([ReplaceableComponentsState])],
});
beforeEach(() => {
spectator = createHost('<abp-dummy></abp-dummy>');
router = spectator.inject(Router);
});
it('should add a component to the state', () => {
const store = spectator.inject(Store);
expect(store.selectSnapshot(ReplaceableComponentsState.getAll)).toEqual([]);
store.dispatch(new AddReplaceableComponent({ component: DummyComponent, key: 'Dummy' }));
expect(store.selectSnapshot(ReplaceableComponentsState.getComponent('Dummy'))).toEqual({
component: DummyComponent,
key: 'Dummy',
});
});
it('should replace a exist component', () => {
const store = spectator.inject(Store);
store.dispatch(new AddReplaceableComponent({ component: DummyComponent, key: 'Dummy' }));
store.dispatch(new AddReplaceableComponent({ component: null, key: 'Dummy' }));
expect(store.selectSnapshot(ReplaceableComponentsState.getComponent('Dummy'))).toEqual({
component: null,
key: 'Dummy',
});
expect(store.selectSnapshot(ReplaceableComponentsState.getAll)).toHaveLength(1);
});
it('should call reloadRoute when reload parameter is given as true to AddReplaceableComponent', async () => {
const spy = jest.spyOn(router, 'navigateByUrl');
const store = spectator.inject(Store);
store.dispatch(new AddReplaceableComponent({ component: DummyComponent, key: 'Dummy' }));
store.dispatch(new AddReplaceableComponent({ component: null, key: 'Dummy' }, true));
await spectator.fixture.whenStable();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(router.url);
});
});
Loading…
Cancel
Save