Skip to content

Commit

Permalink
Added getDefaultServer static method to AppCconfig and updated the ap…
Browse files Browse the repository at this point in the history
…plication to use it. This includes introducing a new route guard on search routes that contain a gene but no server. For now, the method returns the first server in the list of servers in the config file.
  • Loading branch information
alancleary committed Jun 8, 2018
1 parent 26f037d commit c93449a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
7 changes: 3 additions & 4 deletions client/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
// app
import { InstructionsComponent, MultiComponent, SearchComponent } from "./components";
import { MultiGuard, SearchGuard } from "./guards";
import { DefaultSearchGuard, MultiGuard, SearchGuard } from "./guards";

const routes: Routes = [
{
Expand All @@ -27,11 +27,10 @@ const routes: Routes = [
path: "instructions",
},
{
canActivate: [DefaultSearchGuard], // use guard to redirect to default server in AppConfig
path: "search/:gene",
pathMatch: "full",
// TODO: update to use first source from config
// redirectTo: "search/" + DefaultQueryParams.DEFAULT_SOURCE + "/:gene",
redirectTo: "/instructions",
component: SearchComponent,
},
{
canActivate: [SearchGuard],
Expand Down
11 changes: 9 additions & 2 deletions client/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Inject, Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { concatMap, tap } from "rxjs/operators";
// app
import { Server } from "./models";
import { Brand, Config, Dashboard, Miscellaneous } from "./models/config.model"; // avoid circular dependencies
import { Server } from "./models/server.model"; // avoid circular dependencies
import { Brand, Config, Dashboard, Miscellaneous } from "./models/config.model"; // ditto

declare var document: any;

Expand All @@ -20,6 +20,13 @@ export class AppConfig {

constructor(private http: HttpClient) {}

public static getDefaultServer(): Server {
if (AppConfig.SERVERS.length > 0) {
return AppConfig.SERVERS[0];
}
return new Server();
}

public static getServer(id: string): Server {
let server;
AppConfig.SERVERS.forEach((s) => {
Expand Down
25 changes: 25 additions & 0 deletions client/src/app/guards/default-search.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Angular
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
// store
import { Store } from "@ngrx/store";
import * as routerActions from "../store/actions/router.actions";
import * as fromRoot from "../store/reducers";
// app
import { AppConfig } from "../app.config";

@Injectable()
export class DefaultSearchGuard implements CanActivate {

constructor(private router: Router, private store: Store<fromRoot.State>) { }

// this guard is only triggered when no server is provided, which Search needs,
// so instead of activating it always redirects to the default server
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const url = "/search" +
"/" + AppConfig.getDefaultServer().id +
"/" + route.params.gene;
this.store.dispatch(new routerActions.Go({path: [url, {routeParam: 1}]}));
return false;
}
}
3 changes: 3 additions & 0 deletions client/src/app/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DefaultSearchGuard } from "./default-search.guard";
import { MultiGuard } from "./multi.guard";
import { SearchGuard } from "./search.guard";

export const guards: any[] = [
DefaultSearchGuard,
MultiGuard,
SearchGuard,
];

export * from "./default-search.guard";
export * from "./multi.guard";
export * from "./search.guard";
3 changes: 1 addition & 2 deletions client/src/app/models/query-params.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export class QueryParams {

constructor(
public neighbors: number = 10,
// TODO: update to be first source from config
public sources: string[] = ["lis"], // Server IDs
public sources: string[] = [AppConfig.getDefaultServer().id],
public matched: number = 4,
public intermediate: number = 5,
) { }
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/models/server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const GET = "GET";
export const POST = "POST";

export class Request {
type: "GET" | "POST"; // GET or POST
type: "GET" | "POST";
url: string;
}

Expand Down

0 comments on commit c93449a

Please sign in to comment.