Skip to content

Commit

Permalink
fix(context): update context when auth changed
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Mar 18, 2021
1 parent c47999f commit 882e663
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/context/src/lib/context-manager/shared/context.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Injectable, Optional } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';

import { BehaviorSubject, Observable, of } from 'rxjs';
import { BehaviorSubject, Observable, of, Subject } from 'rxjs';
import {
map,
tap,
catchError,
debounceTime,
mergeMap,
first
first,
skip
} from 'rxjs/operators';

import olPoint from 'ol/geom/Point';
Expand Down Expand Up @@ -49,6 +50,7 @@ export class ContextService {
public defaultContextId$ = new BehaviorSubject<string>(undefined);
public editedContext$ = new BehaviorSubject<DetailedContext>(undefined);
public importedContext: Array<DetailedContext> = [];
public toolsChanged$ = new Subject<DetailedContext>();
private mapViewFromRoute: ContextMapView = {};
private options: ContextServiceOptions;
private baseUrl: string;
Expand Down Expand Up @@ -88,14 +90,10 @@ export class ContextService {

this.readParamsFromRoute();

this.authService.authenticate$.subscribe((authenticated) => {
if (authenticated && this.baseUrl) {
this.get().subscribe((contexts) => {
this.handleContextsChange(contexts);
});
} else {
this.contexts$.pipe(first()).subscribe((contexts) => {
this.handleContextsChange(contexts);
this.authService.logged$.subscribe((logged) => {
if (logged) {
this.contexts$.pipe(skip(1), first()).subscribe((c) => {
this.handleContextsChange();
});
this.loadContexts();
}
Expand Down Expand Up @@ -715,19 +713,25 @@ export class ContextService {
}

private handleContextsChange(
contexts: ContextsList,
keepCurrentContext = true
) {
const context = this.context$.value;
const editedContext = this.editedContext$.value;

if (!context || context.uri === this.options.defaultContextUri) {
keepCurrentContext = false;
}
if (!keepCurrentContext || !this.findContext(context)) {
this.defaultContextUri = undefined;
this.loadDefaultContext();
} else {
if (context.map.view.keepCurrentView === undefined) {
context.map.view.keepCurrentView = true;
}
this.context$.next(context);
this.getContextByUri(context.uri)
.pipe(first())
.subscribe(
(newContext: DetailedContext) => {
this.toolsChanged$.next(newContext);
}
);

if (this.baseUrl && this.authService.authenticated) {
this.getDefault().subscribe();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/integration/src/lib/context/context.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class ContextState {
this.contextService.context$.subscribe((context: DetailedContext) => {
this.onContextChange(context);
});
this.contextService.toolsChanged$.subscribe((context: DetailedContext) => {
this.updateTools(context);
});
}

/**
Expand Down

0 comments on commit 882e663

Please sign in to comment.