This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
bookmarks.route.ts 2.57 KiB
Newer Older
import { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot, Routes, Router } from '@angular/router';
import { Observable, of, EMPTY } from 'rxjs';
import { flatMap } from 'rxjs/operators';

import { Authority } from 'app/shared/constants/authority.constants';
import { UserRouteAccessService } from 'app/core/auth/user-route-access-service';
import { IUserWatchList, UserWatchList } from 'app/shared/model/user-watch-list.model';
import { UserWatchListService } from 'app/entities/user-watch-list/user-watch-list.service';
import { BookmarkComponent } from './bookmarks.component';
import { UserWatchListDetailComponent } from './user-watch-list-detail.component';
import { UserWatchListUpdateComponent } from './user-watch-list-update.component';

@Injectable({ providedIn: 'root' })


export class BookmarksResolve implements Resolve<IUserWatchList> {
  constructor(private service: UserWatchListService, private router: Router) {}

  resolve(route: ActivatedRouteSnapshot): Observable<IUserWatchList> | Observable<never> {
    const id = route.params['id'];
    if (id) {
      return this.service.find(id).pipe(
        flatMap((userWatchList: HttpResponse<UserWatchList>) => {
          if (userWatchList.body) {
            return of(userWatchList.body);
          } else {
            this.router.navigate(['404']);
            return EMPTY;
          }
        })
      );
    }
    return of(new UserWatchList());
  }
}

export const bookmarksRoute: Routes = [
  {
    path: '',
    component: BookmarkComponent,
    data: {
      authorities: [Authority.USER],
      pageTitle: 'global.menu.bookmarks',
    },
    canActivate: [UserRouteAccessService],
  },
  {
    path: ':id/view',
    component: UserWatchListDetailComponent,
    resolve: {
      userWatchList: BookmarksResolve,
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'gitsearchApp.userWatchList.home.title',
    },
    canActivate: [UserRouteAccessService],
  },
  {
    path: 'new',
    component: UserWatchListUpdateComponent,
    resolve: {
      userWatchList: BookmarksResolve,
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'gitsearchApp.userWatchList.home.title',
    },
    canActivate: [UserRouteAccessService],
  },
  {
    path: ':id/edit',
    component: UserWatchListUpdateComponent,
    resolve: {
      userWatchList: BookmarksResolve,
    },
    data: {
      authorities: [Authority.USER],
      pageTitle: 'gitsearchApp.userWatchList.home.title',
    },
    canActivate: [UserRouteAccessService],
  },
];