From 1c16f680ed38addddb1de49e962252868a504837 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Tue, 5 Nov 2019 12:37:52 +0100
Subject: [PATCH] FEAT #12003 TIME 0:30 refresh current after update in admin

---
 .../group/group-administration.component.ts   | 14 +++++-----
 .../user/user-administration.component.html   |  2 +-
 .../user/user-administration.component.ts     | 27 +++++++++++++++++++
 .../app/menu/menu-shortcut.component.html     |  2 +-
 .../app/menu/menu-shortcut.component.ts       |  5 +++-
 src/frontend/service/header.service.ts        | 19 ++++++++++++-
 6 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/src/frontend/app/administration/group/group-administration.component.ts b/src/frontend/app/administration/group/group-administration.component.ts
index 1f255473324..52f1e8259d3 100755
--- a/src/frontend/app/administration/group/group-administration.component.ts
+++ b/src/frontend/app/administration/group/group-administration.component.ts
@@ -131,21 +131,18 @@ export class GroupAdministrationComponent implements OnInit {
     }
 
     toggleService(ev: any, service: any) {
-        console.log(ev);
-
-        // TO DO : WAIT BACK
-        /*if (ev.checked) {
+        if (ev.checked) {
             this.addService(service);
         } else {
             this.removeService(service);
-        }*/
-        
+        }  
     }
 
     addService(service: any) {
-        this.http.post(`../../rest/groups/${this.group.id}/services/${service.id}`, {}).pipe(
+        this.http.post(`../../rest/groups/${this.group.id}/privileges/${service.id}`, {}).pipe(
             tap(() => {
                 this.group.privileges.push(service.id);
+                this.headerService.resfreshCurrentUser();
                 this.notify.success(this.lang.groupServicesUpdated);
             }),
             catchError((err: any) => {
@@ -156,9 +153,10 @@ export class GroupAdministrationComponent implements OnInit {
     }
 
     removeService(service: any) {
-        this.http.delete(`../../rest/groups/${this.group.id}/services/${service.id}`).pipe(
+        this.http.delete(`../../rest/groups/${this.group.id}/privileges/${service.id}`).pipe(
             tap(() => {
                 this.group.privileges.splice(this.group.privileges.indexOf(service.id), 1);
+                this.headerService.resfreshCurrentUser();
                 this.notify.success(this.lang.groupServicesUpdated);
             }),
             catchError((err: any) => {
diff --git a/src/frontend/app/administration/user/user-administration.component.html b/src/frontend/app/administration/user/user-administration.component.html
index f3c08aa5811..8eeb8b70766 100755
--- a/src/frontend/app/administration/user/user-administration.component.html
+++ b/src/frontend/app/administration/user/user-administration.component.html
@@ -2,7 +2,7 @@
     <mat-sidenav-container autosize class="admin-sidenav-container">
         <mat-sidenav #snav [mode]="appService.getViewMode() ? 'over' : 'side'" [fixedInViewport]="appService.getViewMode()"
             fixedTopGap="56" [opened]="appService.getViewMode() ? false : true">
-            <menu-shortcut></menu-shortcut>
+            <menu-shortcut #appShortcut></menu-shortcut>
             <menu-nav></menu-nav>
             <mat-nav-list *ngIf="!creationMode && !loading">
                 <h3 mat-subheader>{{lang.actions}}</h3>
diff --git a/src/frontend/app/administration/user/user-administration.component.ts b/src/frontend/app/administration/user/user-administration.component.ts
index ea03dfecd39..819dd24ad2a 100755
--- a/src/frontend/app/administration/user/user-administration.component.ts
+++ b/src/frontend/app/administration/user/user-administration.component.ts
@@ -14,6 +14,7 @@ import { HeaderService } from '../../../service/header.service';
 import { SelectionModel } from '@angular/cdk/collections';
 import { AccountLinkComponent } from './account-link/account-link.component';
 import { AppService } from '../../../service/app.service';
+import { MenuShortcutComponent } from '../../menu/menu-shortcut.component';
 
 declare function $j(selector: any): any;
 
@@ -83,6 +84,8 @@ export class UserAdministrationComponent implements OnInit {
     selectedTabIndex: number = 0;
     maarchParapheurConnectionStatus = true;
 
+    @ViewChild('appShortcut', { static: false }) appShortcut: MenuShortcutComponent;
+
     @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
     @ViewChild(MatSort, { static: false }) sort: MatSort;
     applyFilter(filterValue: string) {
@@ -370,6 +373,12 @@ export class UserAdministrationComponent implements OnInit {
                 .subscribe((data: any) => {
                     this.user.groups = data.groups;
                     this.user.baskets = data.baskets;
+                    if (this.headerService.user.id == this.serialId) {
+                        this.headerService.resfreshCurrentUser();
+                        setTimeout(() => {
+                            this.appShortcut.loadShortcuts();   
+                        }, 200); 
+                    }
                     this.notify.success(this.lang.groupAdded);
                 }, (err) => {
                     this.notify.error(err.error.errors);
@@ -380,6 +389,12 @@ export class UserAdministrationComponent implements OnInit {
                     this.user.groups = data.groups;
                     this.user.baskets = data.baskets;
                     this.user.redirectedBaskets = data.redirectedBaskets;
+                    if (this.headerService.user.id == this.serialId) {
+                        this.headerService.resfreshCurrentUser();
+                        setTimeout(() => {
+                            this.appShortcut.loadShortcuts();   
+                        }, 200); 
+                    }
                     this.notify.success(this.lang.groupDeleted);
                 }, (err) => {
                     this.notify.error(err.error.errors);
@@ -406,6 +421,9 @@ export class UserAdministrationComponent implements OnInit {
             .subscribe((data: any) => {
                 this.user.entities = data.entities;
                 this.user.allEntities = data.allEntities;
+                if (this.headerService.user.id == this.serialId) {
+                    this.headerService.resfreshCurrentUser();
+                }
                 this.notify.success(this.lang.entityAdded);
             }, (err) => {
                 this.notify.error(err.error.errors);
@@ -441,6 +459,9 @@ export class UserAdministrationComponent implements OnInit {
                         .subscribe((data: any) => {
                             this.user.entities = data.entities;
                             this.user.allEntities = data.allEntities;
+                            if (this.headerService.user.id == this.serialId) {
+                                this.headerService.resfreshCurrentUser();
+                            }
                             this.notify.success(this.lang.entityDeleted);
                         }, (err) => {
                             this.notify.error(err.error.errors);
@@ -456,6 +477,9 @@ export class UserAdministrationComponent implements OnInit {
                                 .subscribe((data: any) => {
                                     this.user.entities = data.entities;
                                     this.user.allEntities = data.allEntities;
+                                    if (this.headerService.user.id == this.serialId) {
+                                        this.headerService.resfreshCurrentUser();
+                                    }
                                     this.notify.success(this.lang.entityDeleted);
                                 }, (err) => {
                                     this.notify.error(err.error.errors);
@@ -845,6 +869,9 @@ export class UserAdministrationComponent implements OnInit {
         } else {
             this.http.put("../../rest/users/" + this.serialId, this.user)
                 .subscribe((data: any) => {
+                    if (this.headerService.user.id == this.serialId) {
+                        this.headerService.resfreshCurrentUser();
+                    }
                     this.notify.success(this.lang.userUpdated);
                 }, (err: any) => {
                     this.notify.error(err.error.errors);
diff --git a/src/frontend/app/menu/menu-shortcut.component.html b/src/frontend/app/menu/menu-shortcut.component.html
index adc3541351a..cd28add8014 100755
--- a/src/frontend/app/menu/menu-shortcut.component.html
+++ b/src/frontend/app/menu/menu-shortcut.component.html
@@ -20,7 +20,7 @@
                     </smd-fab-trigger>
 
                     <smd-fab-actions>
-                        <button color="primary" mat-mini-fab (click)="onSpeedDialFabClicked(group,shortcut)" *ngFor="let group of shortcut.groups">
+                        <button color="primary" mat-mini-fab (click)="onSpeedDialFabClicked(group,shortcut)" *ngFor="let group of shortcut.groups | sortBy : 'label' ">
                             <span class="speedDialLabel" color="primary" style="display:none;position: absolute;margin-left: 20px;margin-top: -5px;">{{group.label | shorten: 20: '...'}}</span>
                             <mat-icon class="fa fa-plus"></mat-icon>
                         </button>
diff --git a/src/frontend/app/menu/menu-shortcut.component.ts b/src/frontend/app/menu/menu-shortcut.component.ts
index fb0049f1dcb..9baee6b2929 100755
--- a/src/frontend/app/menu/menu-shortcut.component.ts
+++ b/src/frontend/app/menu/menu-shortcut.component.ts
@@ -3,7 +3,6 @@ import { Router }               from '@angular/router';
 import { HttpClient }           from '@angular/common/http';
 import { LANG }                 from '../translate.component';
 import { MAT_DIALOG_DATA, MatDialogRef, MatDialog } from '@angular/material/dialog';
-import { HeaderService } from '../../service/header.service';
 import { AppService } from '../../service/app.service';
 import { PrivilegeService } from '../../service/privileges.service';
 
@@ -37,6 +36,10 @@ export class MenuShortcutComponent implements OnInit {
     }
 
     ngOnInit(): void {
+        this.loadShortcuts();
+    }
+
+    loadShortcuts() {
         this.shortcuts = this.privilegeService.getCurrentUserShortcuts();
     }
 
diff --git a/src/frontend/service/header.service.ts b/src/frontend/service/header.service.ts
index 78013faa16c..b472a279443 100755
--- a/src/frontend/service/header.service.ts
+++ b/src/frontend/service/header.service.ts
@@ -1,7 +1,7 @@
 import { Injectable } from '@angular/core';
 import { HttpClient } from '@angular/common/http';
 import { LANG } from '../app/translate.component';
-import { tap, catchError, filter } from 'rxjs/operators';
+import { tap, catchError, filter, map } from 'rxjs/operators';
 import { of } from 'rxjs';
 
 @Injectable()
@@ -24,6 +24,23 @@ export class HeaderService {
         ).subscribe();
     }
 
+    resfreshCurrentUser() {
+        this.http.get('../../rest/currentUser/profile')
+            .pipe(
+                map((data: any) => {
+                    this.user = {
+                        id: data.id,
+                        userId: data.user_id,
+                        firstname: data.firstname,
+                        lastname: data.lastname,
+                        entities: data.entities,
+                        groups: data.groups,
+                        privileges: data.privileges
+                    }
+                })
+            ).subscribe();
+    }
+
     setUser(user: any) {
         this.user = user;
     }
-- 
GitLab