Skip to content

Commit

Permalink
feat(auth): refresh contexts list when login or logout
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Sep 6, 2017
1 parent 2d320c5 commit 97d37c7
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/lib/auth/auth-form/auth-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ <h1>{{'igo.auth.connection' | translate}}</h1>

<div *ngIf="auth.logged && alreadyConnectedDiv" class="login center-block">
<p>{{'igo.auth.welcome' | translate: user}}</p>
<button (click)="logout()">{{'igo.auth.signOut' | translate}}</button>
<button type="button" (click)="logout()">{{'igo.auth.signOut' | translate}}</button>
</div>
4 changes: 2 additions & 2 deletions src/lib/auth/auth-form/auth-intern.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</md-form-field>
</div>

<button md-raised-button (click)="login(form.value)" [disabled]="!form.valid">{{'igo.auth.login' | translate}}</button>
<button md-raised-button (click)="loginAnonymous()">{{'igo.auth.accessAnonymous' | translate }}</button>
<button md-raised-button type="button" (click)="login(form.value)" [disabled]="!form.valid">{{'igo.auth.login' | translate}}</button>
<button md-raised-button type="button" (click)="loginAnonymous()">{{'igo.auth.accessAnonymous' | translate }}</button>
<div *ngIf="error">
<br/>
<font size="3" color="red">{{error}}</font>
Expand Down
127 changes: 65 additions & 62 deletions src/lib/context/context-permissions/context-permissions.component.html
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
<igo-list *ngIf="permissions">
<ng-template ngFor let-groupPermissions [ngForOf]="permissions | keyvalue">
<igo-collapsible
*ngIf="groupPermissions.value.length"
[title]="'igo.permission.' + groupPermissions.key | translate">

<ng-template ngFor let-permission [ngForOf]="groupPermissions.value">
<md-list-item>
<md-icon md-list-avatar>perm_identity</md-icon>
<h4 md-line>{{permission.profil}}</h4>

<div igoStopPropagation
class="igo-actions-container">

<button
md-icon-button
[md-tooltip]="'igo.permission.delete' | translate"
color="warn"
(click)="removePermission.emit(permission)">
<md-icon>delete</md-icon>
</button>
</div>

</md-list-item>
</ng-template>
</igo-collapsible>
</ng-template>
</igo-list>

<form class="igo-form" [formGroup]="form"
(ngSubmit)="handleFormSubmit(form.value)">

<md-form-field class="full-width">
<input mdInput required
[placeholder]="'igo.permission.profil' | translate"
formControlName="profil">
<md-error>
{{ 'igo.permission.profilRequired' | translate }}
</md-error>
</md-form-field>


<md-radio-group formControlName="typePermission">
<md-radio-button value="read">
{{ 'igo.permission.read' | translate }}
</md-radio-button>
<md-radio-button value="write">
{{ 'igo.permission.write' | translate }}
</md-radio-button>
</md-radio-group>


<div class="igo-form-button-group">
<button
md-raised-button
type="submit"
[disabled]="!form.valid">
{{ 'igo.permission.addBtn' | translate }}
</button>
</div>

</form>
<div *ngIf="context">
<igo-list *ngIf="permissions">
<ng-template ngFor let-groupPermissions [ngForOf]="permissions | keyvalue">
<igo-collapsible
*ngIf="groupPermissions.value.length"
[title]="'igo.permission.' + groupPermissions.key | translate">

<ng-template ngFor let-permission [ngForOf]="groupPermissions.value">
<md-list-item>
<md-icon md-list-avatar>perm_identity</md-icon>
<h4 md-line>{{permission.profil}}</h4>

<div igoStopPropagation
class="igo-actions-container">

<button
md-icon-button
[md-tooltip]="'igo.permission.delete' | translate"
color="warn"
(click)="removePermission.emit(permission)">
<md-icon>delete</md-icon>
</button>
</div>

</md-list-item>
</ng-template>
</igo-collapsible>
</ng-template>
</igo-list>

<form class="igo-form" [formGroup]="form"
(ngSubmit)="handleFormSubmit(form.value)">

<md-form-field class="full-width">
<input mdInput required
[placeholder]="'igo.permission.profil' | translate"
formControlName="profil">
<md-error>
{{ 'igo.permission.profilRequired' | translate }}
</md-error>
</md-form-field>


<md-radio-group formControlName="typePermission">
<md-radio-button value="read">
{{ 'igo.permission.read' | translate }}
</md-radio-button>
<md-radio-button value="write">
{{ 'igo.permission.write' | translate }}
</md-radio-button>
</md-radio-group>


<div class="igo-form-button-group">
<button
md-raised-button
type="submit"
[disabled]="!form.valid">
{{ 'igo.permission.addBtn' | translate }}
</button>
</div>

</form>

</div>
14 changes: 8 additions & 6 deletions src/lib/context/context-tools/context-tools.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div *ngFor="let tool of tools" class="toolSection">
<md-checkbox
[ngModel]="verifyIfContextToolExist(tool.id)"
(change)="handleToolChange($event, tool)">
{{tool.title || tool.name}}
</md-checkbox>
<div *ngIf="context">
<div *ngFor="let tool of tools" class="toolSection">
<md-checkbox
[ngModel]="verifyIfContextToolExist(tool.id)"
(change)="handleToolChange($event, tool)">
{{tool.title || tool.name}}
</md-checkbox>
</div>
</div>
51 changes: 50 additions & 1 deletion src/lib/context/shared/context.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Observable } from 'rxjs/Observable';
import { RequestService, ConfigService, RouteService,
Message, LanguageService } from '../../core';

import { AuthHttp } from '../../auth';
import { AuthHttp, AuthService } from '../../auth';
// Import from shared to avoid circular dependencies
import { ToolService } from '../../tool/shared';

Expand All @@ -26,6 +26,7 @@ export class ContextService {

constructor(private http: Http,
private authHttp: AuthHttp,
private authService: AuthService,
private requestService: RequestService,
private languageService: LanguageService,
private toolService: ToolService,
Expand All @@ -41,6 +42,20 @@ export class ContextService {
this.baseUrl = this.options.url;

this.readParamsFromRoute();

this.authService.authenticate$
.subscribe((authenticated) => {
if (authenticated === null) {
return;
}
const contexts$$ = this.contexts$.subscribe((contexts) => {
if (contexts$$) {
contexts$$.unsubscribe();
this.handleContextsChange(contexts);
}
});
this.loadContexts();
});
}

get(): Observable<ContextsList> {
Expand Down Expand Up @@ -316,4 +331,38 @@ deletePermissionAssociation(contextId: string, permissionId: string): Observable
throw [{title: titleError, text: textError}];
}

private handleContextsChange(contexts: ContextsList) {

const context = this.context$.value;
const editedContext = this.editedContext$.value;

if (!this.findContext(context)) {
this.loadDefaultContext();
} else {
context.map.view.keepCurrentView = true;
this.context$.next(context);
}
const editedFound = this.findContext(editedContext);
if (!editedFound || editedFound.permission !== 'write') {
this.setEditedContext(undefined);
}
}

private findContext(context: Context) {
if (!context || !context.id) {
return false;
}

const contexts = this.contexts$.value;
let found;
for (const key of Object.keys(contexts)) {
const value = contexts[key];
found = value.find(c => c.id === context.id);
if (found) {
break;
}
}

return found;
}
}
4 changes: 2 additions & 2 deletions src/lib/map/bookmark-button/bookmark-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export class BookmarkButtonComponent {
context.title = title;
this.contextService.create(context).subscribe(() => {
const translate = this.languageService.translate;
const title = translate.instant('igo.bookmarkButton.dialog.createTitle');
const titleD = translate.instant('igo.bookmarkButton.dialog.createTitle');
const message = translate.instant('igo.bookmarkButton.dialog.createMsg', {
value: context.title
});
this.messageService.info(message, title);
this.messageService.info(message, titleD);
});
}
});
Expand Down
25 changes: 16 additions & 9 deletions src/lib/map/poi-button/poi-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, Input, OnInit } from '@angular/core';
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { MdDialog } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';

import { MessageService, LanguageService } from '../../core';
import { ConfirmDialogService } from '../../shared';
Expand All @@ -12,7 +13,7 @@ import { PoiDialogComponent } from './poi-dialog.component';
templateUrl: './poi-button.component.html',
styleUrls: ['./poi-button.component.styl']
})
export class PoiButtonComponent implements OnInit {
export class PoiButtonComponent implements OnInit, OnDestroy {

@Input()
get map(): IgoMap { return this._map; }
Expand All @@ -29,6 +30,7 @@ export class PoiButtonComponent implements OnInit {
private _color: string;

public pois: Poi[];
private authenticate$$: Subscription;

constructor(
private dialog: MdDialog,
Expand All @@ -40,11 +42,16 @@ export class PoiButtonComponent implements OnInit {
) {}

ngOnInit() {
this.authService.authenticate$.subscribe((auth) => {
if (auth) {
this.getPois();
}
});
this.authenticate$$ = this.authService.authenticate$
.subscribe((auth) => {
if (auth) {
this.getPois();
}
});
}

ngOnDestroy() {
this.authenticate$$.unsubscribe();
}

deletePoi(poi: Poi) {
Expand Down Expand Up @@ -91,11 +98,11 @@ export class PoiButtonComponent implements OnInit {
poi.title = title;
this.poiService.create(poi).subscribe((newPoi) => {
const translate = this.languageService.translate;
const title = translate.instant('igo.poiButton.dialog.createTitle');
const titleD = translate.instant('igo.poiButton.dialog.createTitle');
const message = translate.instant('igo.poiButton.dialog.createMsg', {
value: poi.title
});
this.messageService.info(message, title);
this.messageService.info(message, titleD);
poi.id = newPoi.id;
this.pois.push(poi);
});
Expand Down

0 comments on commit 97d37c7

Please sign in to comment.