Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Injectable } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { of } from 'rxjs';
import { map, tap, switchMap, withLatestFrom, catchError } from 'rxjs/operators';
import * as coneSearchActions from './cone-search.action';
import * as fromRouter from '@ngrx/router-store';
import * as fromConeSearch from './search.reducer';
import * as utils from '../../../utils';
import { ConeSearchService } from './cone-search.service';
@Injectable()
export class ConeSearchEffects {
constructor(
private actions$: Actions,
private coneSearchService: ConeSearchService,
private toastr: ToastrService,
private store$: Store<{
router: fromRouter.RouterReducerState<utils.RouterStateUrl>,
search: fromConeSearch.State
}>
) { }
@Effect()
retrieveCoordinatesAction$ = this.actions$.pipe(
ofType(coneSearchActions.RETRIEVE_COORDINATES),
withLatestFrom(this.store$),
switchMap(([action, state]) => {
const retrieveCoordinatesAction = action as coneSearchActions.RetrieveCoordinatesAction;
return this.coneSearchService.retrieveCoordinates(retrieveCoordinatesAction.payload).pipe(
map((response) => {
const parser = new DOMParser();
const xml = parser.parseFromString(response,'text/xml');
if (xml.getElementsByTagName('Resolver').length === 0) {
const name = xml.getElementsByTagName('name')[0].childNodes[0].nodeValue;
return new coneSearchActions.RetrieveCoordinatesFailAction(name);
}
const name = xml.getElementsByTagName('name')[0].childNodes[0].nodeValue;
const ra = +xml.getElementsByTagName('jradeg')[0].childNodes[0].nodeValue;
const dec = +xml.getElementsByTagName('jdedeg')[0].childNodes[0].nodeValue;
return new coneSearchActions.RetrieveCoordinatesSuccessAction({name, ra, dec});
}),
catchError(() => of(new coneSearchActions.RetrieveCoordinatesFailAction(null)))
);
})
);
@Effect({ dispatch: false })
retrieveCoordinatesFailAction$ = this.actions$.pipe(
ofType(coneSearchActions.RETRIEVE_COORDINATES_FAIL),
tap(action => {
const retrieveCoordinatesFailAction = action as coneSearchActions.RetrieveCoordinatesFailAction;
if (retrieveCoordinatesFailAction.payload) {
this.toastr.error(retrieveCoordinatesFailAction.payload + ' not found');
} else {
this.toastr.error('Connection to Sesame Name Resolver failed', 'Resolver Failed!');
}
})
);
}