Skip to content

Instantly share code, notes, and snippets.

@tlimpanont
Last active April 5, 2018 07:54
Show Gist options
  • Save tlimpanont/e3737bcd40eb6062a838ac337abd5c27 to your computer and use it in GitHub Desktop.
Save tlimpanont/e3737bcd40eb6062a838ac337abd5c27 to your computer and use it in GitHub Desktop.
clearance-overview.effects.ts
import * as clearanceSelectors from "../selectors";
import {Injectable} from "@angular/core";
import {Actions, Effect} from "@ngrx/effects";
import {Observable} from "rxjs";
import {
ACTION_AUTO_REFRESH_TIMEOUT_OCCURRED,
ACTION_COLUMN_ORDER_UPDATED,
ACTION_COLUMN_WIDTH_UPDATED,
ACTION_COLUMN_SORT_UPDATED,
ACTION_FILTER_UPDATED,
ACTION_FOLLOW_UPDATED,
ACTION_GET_CLEARANCES,
ACTION_GET_CLEARANCES_ERROR,
ACTION_PROCESS_CLEARANCE_WEBSOCKET_UPDATES,
ACTION_PROCESS_CLEARANCE_WEBSOCKET_UPDATES_SUCCESS,
ACTION_RESTORE_PREFERENCES,
ACTION_SELECT_CLEARANCE,
ACTION_SELECT_MOVEMENT,
ACTION_SELECT_SECURITY,
ACTION_SELECT_SUBSTANCES,
GetClearancesAction,
GetClearancesSuccessAction,
PreferencesSavedToPortviewAction,
ProcessClearanceWebsocketUpdatesSuccessAction,
SelectMovementAction,
SelectSecurityAction,
SelectSubstancesAction,
} from "../actions";
import {ClearanceOverviewDockableFrameService, ClearanceService} from "../services";
import {Clearance} from "../models";
import {select, Store} from "@ngrx/store";
import {Message, WebsocketConnectionService} from "websocket-client";
const {stringify} = require("querystring");
@Injectable()
export class ClearanceEffects {
@Effect() getClearances$ = this._actions$
.ofType(ACTION_GET_CLEARANCES)
.switchMap((payload: any) => {
const {queryString}: { queryString: string } = payload;
return this._clearanceService.getClearancesToEvaluate(queryString)
.map((clearancesToEvaluate: Clearance[]) => new GetClearancesSuccessAction(clearancesToEvaluate))
.catch(() => Observable.of({type: ACTION_GET_CLEARANCES_ERROR}));
});
@Effect() selectedClearance$ = this._actions$
.ofType(ACTION_SELECT_CLEARANCE)
.map((data) => {
const payload = data["payload"] as Clearance;
if (payload.type.toLowerCase() === "movement") {
return new SelectMovementAction(payload);
} else if (payload.type.toLowerCase() === "substances") {
return new SelectSubstancesAction(payload);
} else if (payload.type.toLowerCase() === "security") {
return new SelectSecurityAction(payload);
}
return new Error(`unknown type ${payload.type}`);
});
@Effect() selectMovement$ = this._actions$
.ofType(ACTION_SELECT_MOVEMENT)
.switchMap((data) => {
const payload = data["payload"] as Clearance;
return this._dockableFrameService.evaluateMovementWithUcrnAndId(payload.ucrn, payload.secondaryId)
.map(() => ({type: "ACTION_SENT_TO_DOCKABLE_FRAME_SUCCESS"}));
});
@Effect() selectSubstances$ = this._actions$
.ofType(ACTION_SELECT_SUBSTANCES)
.switchMap((data) => {
const payload = data["payload"] as Clearance;
return this._dockableFrameService.evaluateVisitWithUcrn(payload.ucrn, payload.type)
.map(() => ({type: "ACTION_SENT_TO_DOCKABLE_FRAME_SUCCESS"}));
});
@Effect() selectSecurity$ = this._actions$
.ofType(ACTION_SELECT_SECURITY)
.switchMap((data) => {
const payload = data["payload"] as Clearance;
return this._dockableFrameService.evaluateVisitWithUcrn(payload.ucrn, payload.type)
.map(() => ({type: "ACTION_SENT_TO_DOCKABLE_FRAME_SUCCESS"}));
});
@Effect() processWebSocketUpdates$ = this._actions$
.ofType(ACTION_PROCESS_CLEARANCE_WEBSOCKET_UPDATES)
.switchMap(() => {
const messages$ = this._webSocketService.messages.share();
return messages$.filter(msg => msg && (msg.type === "ClearanceSummary"));
})
.map((message: Message) => new ProcessClearanceWebsocketUpdatesSuccessAction(message));
@Effect() refreshClearances$ = this._actions$
.ofType(
ACTION_PROCESS_CLEARANCE_WEBSOCKET_UPDATES_SUCCESS,
ACTION_AUTO_REFRESH_TIMEOUT_OCCURRED,
ACTION_FILTER_UPDATED,
ACTION_RESTORE_PREFERENCES)
.withLatestFrom(this._store.pipe(select(clearanceSelectors.selectorPreferencesFilter)))
.switchMap(([payload, filter]) => {
const queryString = (filter) ? `?${stringify(filter)}` : "";
return Observable.of(queryString);
})
.map((queryString: string) => new GetClearancesAction(queryString));
@Effect() savePreferencesToPortview$ = this._actions$
.ofType(ACTION_FILTER_UPDATED, ACTION_FOLLOW_UPDATED, ACTION_COLUMN_SORT_UPDATED, ACTION_COLUMN_WIDTH_UPDATED, ACTION_COLUMN_ORDER_UPDATED)
.withLatestFrom(this._store.pipe(select(clearanceSelectors.selectorPreferences)))
.switchMap(([payload, preferences]) => {
console.log(JSON.stringify(preferences, null, 2));
this._dockableFrameService.saveJSPreferences(JSON.stringify(preferences));
return Observable.of(preferences);
})
.map((preferences) => {
return new PreferencesSavedToPortviewAction(preferences);
});
constructor(private _actions$: Actions,
private _store: Store<any>,
private _clearanceService: ClearanceService,
private _dockableFrameService: ClearanceOverviewDockableFrameService,
private _webSocketService: WebsocketConnectionService) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment