Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ A short example can be found here
});
```

### dispatchDataToSTore
### dispatchDataToStore

An additional util that works in tandem with the aforementioned store assets is the `dispatchDataToStore` util. Using the assets, the util will automatically handle the loading and error state of the provided data.

Expand Down
5 changes: 4 additions & 1 deletion apps/store-test/src/services/courses.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { CoursesStore, actions, selectors } from '../store/courses.store';
import { StoreService, dispatchDataToStore } from '@ngx/store';

Expand All @@ -15,6 +16,8 @@ export class CoursesService extends StoreService<CoursesStore> {
}

dispatchCourses() {
return dispatchDataToStore(actions.courses, of(['hello', 'world']), this.store);
return dispatchDataToStore(actions.courses, of(['hello', 'world']), this.store).pipe(
switchMap(() => this.state.courses$)
);
}
}
5 changes: 3 additions & 2 deletions libs/angular/store/src/lib/store/spec/store-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';

import { HttpClient } from '@angular/common/http';
import { StoreService } from '../abstracts';
Expand Down Expand Up @@ -49,7 +50,7 @@ export class SpecStoreService extends StoreService {
actions.channel,
this.httpClient.get<DataType>('test'),
this.store
);
).pipe(switchMap(() => this.channel$));
}

public clearChannel(): void {
Expand All @@ -62,7 +63,7 @@ export class SpecStoreService extends StoreService {
this.httpClient.get<DataType[]>('test'),
this.store,
actionType
);
).pipe(switchMap(() => this.videos$));
}

public clearVideos(): void {
Expand Down
12 changes: 8 additions & 4 deletions libs/angular/store/src/lib/store/spec/store-state.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable, of, throwError } from 'rxjs';
import { Observable, of, throwError, switchMap } from 'rxjs';
import { BaseStoreAssets, EntityStoreAssets, StoreFlowAssets } from '../interfaces';
import {
createBaseStoreAssets,
Expand Down Expand Up @@ -38,14 +38,18 @@ export class StoreStateService extends StoreService<StoreState> {
actions.data,
throwError(() => new Error('This is an error')),
this.store
);
).pipe(switchMap(() => throwError(() => new Error('This is an error'))));
}

setData(payload: string[]): Observable<string[]> {
return dispatchDataToStore(actions.data, of(payload), this.store);
return dispatchDataToStore(actions.data, of(payload), this.store).pipe(
switchMap(() => this.state.data$)
);
}

setCompleted(payload: boolean): Observable<boolean> {
return dispatchDataToStore(actions.isCompleted, of(payload), this.store);
return dispatchDataToStore(actions.isCompleted, of(payload), this.store).pipe(
switchMap(() => this.state.isCompleted$)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Store } from '@ngrx/store';
import { Observable, throwError } from 'rxjs';
import { catchError, finalize, tap } from 'rxjs/operators';
import { catchError, finalize, tap, map } from 'rxjs/operators';
/**
* Dispatches data to the store based on a provided action and Observable. Loading and error state will be handled by default.
*
Expand All @@ -15,7 +15,7 @@ export const dispatchDataToStore = <DataType>(
data: Observable<DataType>,
store: Store,
dispatchType: 'set' | 'add' | 'update' = 'set'
): Observable<DataType> => {
): Observable<void> => {
// Iben: Set the loading state to true and the error state to false to start a new set
store.dispatch(dispatchAction.loading({ payload: true }));
store.dispatch(dispatchAction.error({ payload: false }));
Expand All @@ -40,6 +40,7 @@ export const dispatchDataToStore = <DataType>(
// Iben: Set the loading state to false
finalize(() => {
store.dispatch(dispatchAction.loading({ payload: false }));
})
}),
map(() => void 0)
);
};