Skip to content

Commit

Permalink
feat(stark-ui): update progress-indicator actions style
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Due to an improvement on how actions are defined, the enum `StarkProgressIndicatorActionsTypes`
became obsolete so it has been removed.

As a result, the following actions have been changed:
- `StarkProgressIndicatorRegister(public progressIndicatorConfig: StarkProgressIndicatorFullConfig
  )` -> `StarkProgressIndicatorActions.register({
  progressIndicatorConfig: StarkProgressIndicatorFullConfig })`
- `StarkProgressIndicatorDeregister(public topic: string)` ->
  `StarkProgressIndicatorActions.deregister({ topic: string })`
- `StarkProgressIndicatorHide(public topic: string)`
  -> `StarkProgressIndicatorActions.hide({ topic: string })`
- `StarkProgressIndicatorShow(public topic: string)`
  -> `StarkProgressIndicatorActions.show({ topic: string })`

And also the previous union type has been replaced:
`StarkProgressIndicatorActions` -> `StarkProgressIndicatorActions.Types`.

Change in effect:

```typescript
// Before
@effect({ dispatch: false })
public starkProgressIndicatorShow$(): Observable<void> {
    return this.actions$.pipe(
        ofType<StarkProgressIndicatorShow>(
            StarkProgressIndicatorActionsTypes.PROGRESS_INDICATOR_SHOW
        ),
        map((action: StarkProgressIndicatorShow) => {
            // some logic
        })
    );
}

// After
public starkProgressIndicatorShow$ = createEffect(
    () => this.actions$.pipe(
        ofType(StarkProgressIndicatorActions.show),
        map((action) => {
            // some logic
        })
    ),
    { dispatch: false }
);
```

Change in `action` usage:

```typescript
// Before
this.store.dispatch(new StarkProgressIndicatorShow(topic));

// After
this.store.dispatch(StarkProgressIndicatorActions.show({ topic: topic }));
```
  • Loading branch information
SuperITMan committed May 3, 2021
1 parent 97b067d commit 374c429
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 245 deletions.
6 changes: 6 additions & 0 deletions packages/stark-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/stark-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"@ngrx/effects": "^8.6.1",
"@ngrx/store": "^8.6.1",
"@types/jasmine": "^3.6.4",
"@types/node": "^10.17.54"
},
Expand Down
1 change: 1 addition & 0 deletions packages/stark-ui/src/modules/progress-indicator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./progress-indicator/progress-indicator.module";
export * from "./progress-indicator/actions";
export * from "./progress-indicator/components";
export * from "./progress-indicator/constants";
export * from "./progress-indicator/directives";
export * from "./progress-indicator/entities";
export * from "./progress-indicator/reducers";
Expand Down
3 changes: 2 additions & 1 deletion packages/stark-ui/src/modules/progress-indicator/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./actions/progress-indicator.actions";
import * as StarkProgressIndicatorActions from "./actions/progress-indicator.actions";
export { StarkProgressIndicatorActions };
Original file line number Diff line number Diff line change
@@ -1,86 +1,44 @@
import { Action } from "@ngrx/store";
import { createAction, props, union } from "@ngrx/store";
import { StarkProgressIndicatorFullConfig } from "../entities";

/**
* Progress indicator action types enumeration
*/
export enum StarkProgressIndicatorActionTypes {
PROGRESS_INDICATOR_REGISTER = "PROGRESS_INDICATOR_REGISTER",
PROGRESS_INDICATOR_DEREGISTER = "PROGRESS_INDICATOR_DEREGISTER",
PROGRESS_INDICATOR_HIDE = "PROGRESS_INDICATOR_HIDE",
PROGRESS_INDICATOR_SHOW = "PROGRESS_INDICATOR_SHOW"
}
import { starkProgressIndicatorStoreKey } from "../constants";

/**
* Triggered by the {@link StarkProgressIndicatorService} register() method.
*
* Parameter:
* - progressIndicatorConfig - Configuration of the indicator
*/
export class StarkProgressIndicatorRegister implements Action {
/**
* The type of action
*/
public readonly type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_REGISTER =
StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_REGISTER;

/**
* Class constructor
* @param progressIndicatorConfig - Configuration of the indicator
*/
public constructor(public progressIndicatorConfig: StarkProgressIndicatorFullConfig) {}
}
export const register = createAction(
`[${starkProgressIndicatorStoreKey}] Register`,
props<{ progressIndicatorConfig: StarkProgressIndicatorFullConfig }>()
);

/**
* Triggered by the {@link StarkProgressIndicatorService} deregister() method.
*
* Parameter:
* - topic - The topic of the indicator
*/
export class StarkProgressIndicatorDeregister implements Action {
/**
* The type of action
*/
public readonly type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_DEREGISTER =
StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_DEREGISTER;

/**
* Class constructor
* @param topic - The topic of the indicator
*/
public constructor(public topic: string) {}
}
export const deregister = createAction(`[${starkProgressIndicatorStoreKey}] Deregister`, props<{ topic: string }>());

/**
* Triggered by the {@link StarkProgressIndicatorService} hide() method.
*
* Parameter:
* - topic - The topic of the indicator
*/
export class StarkProgressIndicatorHide implements Action {
/**
* The type of action
*/
public readonly type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_HIDE =
StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_HIDE;

/**
* Class constructor
* @param topic - The topic of the indicator
*/
public constructor(public topic: string) {}
}
export const hide = createAction(`[${starkProgressIndicatorStoreKey}] Hide`, props<{ topic: string }>());

/**
* Triggered by the {@link StarkProgressIndicatorService} show() method.
*
* Parameter:
* - topic - The topic of the indicator
*/
export class StarkProgressIndicatorShow implements Action {
/**
* The type of action
*/
public readonly type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_SHOW =
StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_SHOW;

/**
* Class constructor
* @param topic - The topic of the indicator
*/
public constructor(public topic: string) {}
}
export const show = createAction(`[${starkProgressIndicatorStoreKey}] Show`, props<{ topic: string }>());

export type StarkProgressIndicatorActions =
| StarkProgressIndicatorRegister
| StarkProgressIndicatorDeregister
| StarkProgressIndicatorHide
| StarkProgressIndicatorShow;
/**
* @ignore
*/
const all = union({ register, deregister, hide, show });
export type Types = typeof all;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constants/progress-indicator-store-key";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Key defined to find the service in a store
*/
export const starkProgressIndicatorStoreKey = "StarkProgressIndicator";
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { CommonModule } from "@angular/common";
import { StoreModule } from "@ngrx/store";
import { StarkProgressIndicatorDirective } from "./directives";
import { STARK_PROGRESS_INDICATOR_SERVICE, StarkProgressIndicatorServiceImpl } from "./services";
import { starkProgressIndicatorStoreKey } from "./constants";
import { starkProgressIndicatorReducers } from "./reducers";
import { StarkProgressIndicatorComponent } from "./components";

@NgModule({
declarations: [StarkProgressIndicatorDirective, StarkProgressIndicatorComponent],
imports: [CommonModule, StoreModule.forFeature("StarkProgressIndicator", starkProgressIndicatorReducers)],
imports: [CommonModule, StoreModule.forFeature(starkProgressIndicatorStoreKey, starkProgressIndicatorReducers)],
exports: [StarkProgressIndicatorDirective],
entryComponents: [StarkProgressIndicatorComponent]
})
Expand Down
4 changes: 2 additions & 2 deletions packages/stark-ui/src/modules/progress-indicator/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./reducers/index";
export * from "./reducers/progress-indicator.reducer";
export { selectStarkProgressIndicator, starkProgressIndicatorReducers, StarkProgressIndicatorState } from "./reducers/index";
export { progressIndicatorReducer } from "./reducers/progress-indicator.reducer";
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ActionReducerMap, createFeatureSelector, createSelector, MemoizedSelector } from "@ngrx/store";
import { ActionReducerMap, createFeatureSelector, createSelector } from "@ngrx/store";
import { StarkProgressIndicatorFullConfig } from "../entities";
import { StarkProgressIndicatorActions } from "../actions";
import { progressIndicatorReducer } from "./progress-indicator.reducer";
import { starkProgressIndicatorStoreKey } from "../constants";

/**
* Defines the part of the state assigned to the {@link StarkProgressIndicatorModule}
Expand All @@ -16,7 +17,7 @@ export interface StarkProgressIndicatorState {
/**
* Reducers assigned to the each property of the {@link StarkProgressIndicatorModule}'s state
*/
export const starkProgressIndicatorReducers: ActionReducerMap<StarkProgressIndicatorState, StarkProgressIndicatorActions> = {
export const starkProgressIndicatorReducers: ActionReducerMap<StarkProgressIndicatorState, StarkProgressIndicatorActions.Types> = {
/**
* Reducer assigned to the state's `progressIndicator` property
*/
Expand All @@ -26,7 +27,7 @@ export const starkProgressIndicatorReducers: ActionReducerMap<StarkProgressIndic
/**
* NGRX Selector for the {@link StarkProgressIndicatorModule}'s state
*/
export const selectStarkProgressIndicator: MemoizedSelector<object, Map<string, StarkProgressIndicatorFullConfig>> = createSelector(
createFeatureSelector<StarkProgressIndicatorState>("StarkProgressIndicator"),
export const selectStarkProgressIndicator = createSelector(
createFeatureSelector<StarkProgressIndicatorState>(starkProgressIndicatorStoreKey),
(state: StarkProgressIndicatorState) => state.progressIndicator
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable:completed-docs*/
import { progressIndicatorReducer } from "./progress-indicator.reducer";
import { StarkProgressIndicatorFullConfig, StarkProgressIndicatorFullConfigImpl, StarkProgressIndicatorType } from "../entities";
import { StarkProgressIndicatorActionTypes, StarkProgressIndicatorRegister } from "../actions";
import { StarkProgressIndicatorActions } from "../actions";

const deepFreeze: Function = require("deep-freeze-strict");

Expand All @@ -17,13 +17,13 @@ describe("Reducer: ProgressIndicatorReducer", () => {
mockProgressIndicator = new StarkProgressIndicatorFullConfigImpl("dummy", StarkProgressIndicatorType.SPINNER, false);
});

describe("on PROGRESS_INDICATOR_REGISTER", () => {
describe("on `StarkProgressIndicatorActions.register`", () => {
it("should add the progressIndicator when state given", () => {
deepFreeze(initialState); // Enforce immutability
const config = mockProgressIndicator;
deepFreeze(config); // Enforce immutability

changedState = progressIndicatorReducer(initialState, new StarkProgressIndicatorRegister(config));
changedState = progressIndicatorReducer(initialState, StarkProgressIndicatorActions.register({progressIndicatorConfig: config}));
expect(changedState.size).toBe(1);
expect(changedState.get(mockProgressIndicator.topic)).toBe(mockProgressIndicator);
});
Expand All @@ -32,25 +32,25 @@ describe("Reducer: ProgressIndicatorReducer", () => {
const config = mockProgressIndicator;
deepFreeze(config); // Enforce immutability

// Send the PROGRESS_INDICATOR_REGISTER action to the progressIndicatorReducer
changedState = progressIndicatorReducer(<any>undefined, new StarkProgressIndicatorRegister(config));
// Send the `StarkProgressIndicatorActions.register` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(<any>undefined, StarkProgressIndicatorActions.register({progressIndicatorConfig: config}));

expect(changedState.size).toBe(1);
expect(changedState.get(mockProgressIndicator.topic)).toBe(mockProgressIndicator);
});
});

describe("on PROGRESS_INDICATOR_DEREGISTER", () => {
describe("on `StarkProgressIndicatorActions.deregister`", () => {
it("should remove the progressIndicator when state given", () => {
// create the initial state object
initialState.set(mockProgressIndicator.topic, mockProgressIndicator);
deepFreeze(initialState); // Enforce immutability
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_DEREGISTER action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.deregister` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(initialState, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_DEREGISTER,
type: StarkProgressIndicatorActions.deregister.type,
topic
});

Expand All @@ -62,9 +62,9 @@ describe("Reducer: ProgressIndicatorReducer", () => {
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_DEREGISTER action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.deregister` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(<any>undefined, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_DEREGISTER,
type: StarkProgressIndicatorActions.deregister.type,
topic
});

Expand All @@ -73,17 +73,17 @@ describe("Reducer: ProgressIndicatorReducer", () => {
});
});

describe("on PROGRESS_INDICATOR_SHOW", () => {
describe("on `StarkProgressIndicatorActions.show`", () => {
it("should set the progressConfig 'visible' property to TRUE and 'pendingListenersCount' to 1 when state given", () => {
// create the initial state object
initialState.set(mockProgressIndicator.topic, mockProgressIndicator);
deepFreeze(initialState); // Enforce immutability
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_SHOW action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.show` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(initialState, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_SHOW,
type: StarkProgressIndicatorActions.show.type,
topic
});

Expand All @@ -98,9 +98,9 @@ describe("Reducer: ProgressIndicatorReducer", () => {
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_SHOW action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.show` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(<any>undefined, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_SHOW,
type: StarkProgressIndicatorActions.show.type,
topic
});

Expand All @@ -109,7 +109,7 @@ describe("Reducer: ProgressIndicatorReducer", () => {
});
});

describe("on PROGRESS_INDICATOR_HIDE", () => {
describe("on `StarkProgressIndicatorActions.hide`", () => {
it("should set the progressConfig 'visible' property to FALSE and 'pendingListenersCount' to 0 when state given", () => {
// create the initial state object
mockProgressIndicator.visible = true;
Expand All @@ -118,9 +118,9 @@ describe("Reducer: ProgressIndicatorReducer", () => {
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_HIDE action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.hide` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(initialState, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_HIDE,
type: StarkProgressIndicatorActions.hide.type,
topic
});

Expand All @@ -135,9 +135,9 @@ describe("Reducer: ProgressIndicatorReducer", () => {
const topic = mockProgressIndicator.topic;
deepFreeze(topic); // Enforce immutability

// Send the PROGRESS_INDICATOR_HIDE action to the progressIndicatorReducer
// Send the `StarkProgressIndicatorActions.hide` action to the progressIndicatorReducer
changedState = progressIndicatorReducer(<any>undefined, {
type: StarkProgressIndicatorActionTypes.PROGRESS_INDICATOR_HIDE,
type: StarkProgressIndicatorActions.hide.type,
topic
});

Expand Down

0 comments on commit 374c429

Please sign in to comment.