From c54ae80271a77fdc9c0ab69b842e5a39400a126d Mon Sep 17 00:00:00 2001 From: nuhrberg <48205130+nuhrberg@users.noreply.github.com> Date: Thu, 18 Apr 2019 13:19:33 +0200 Subject: [PATCH] Issue214 (#215) * Fixes #214 * Deleted unused file * Immediate loading of available worlds optimized when adding or removing a world of the currently logged in user * Fix SonarCloud Bug --- .../sonarquest/services/UserService.java | 4 +- sonarQuest-frontend/src/app/app.component.ts | 83 ++++++++++--------- .../admin-developer-edit.component.ts | 11 ++- .../admin-world/admin-world.component.ts | 68 ++++++++------- 4 files changed, 91 insertions(+), 75 deletions(-) diff --git a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java index ae6e650e..6850b71d 100644 --- a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java +++ b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/UserService.java @@ -198,12 +198,14 @@ public void updateLastLogin(final String username) { } public Boolean updateUserToWorld(List userToWorlds) { - userToWorlds.forEach(userToWorld -> { User user = userRepository.findOne(userToWorld.getUserId()); if (userToWorld.getJoined()) { user.addWorld(worldRepository.findOne(userToWorld.getWorldId())); } else { + if(user.getCurrentWorld() != null && user.getCurrentWorld().getId().equals(userToWorld.getWorldId())) { + user.setCurrentWorld(null); + } user.removeWorld(worldRepository.findOne(userToWorld.getWorldId())); } diff --git a/sonarQuest-frontend/src/app/app.component.ts b/sonarQuest-frontend/src/app/app.component.ts index acec659d..f68b7376 100644 --- a/sonarQuest-frontend/src/app/app.component.ts +++ b/sonarQuest-frontend/src/app/app.component.ts @@ -1,18 +1,18 @@ -import {UiDesignService} from './services/ui-design.service'; -import {MatDialog} from '@angular/material'; -import {AfterViewInit, Component, OnInit} from '@angular/core'; -import {TdMediaService} from '@covalent/core'; -import {Router} from '@angular/router'; -import {WorldService} from './services/world.service'; -import {World} from './Interfaces/World'; -import {TranslateService} from '@ngx-translate/core'; -import {UiDesign} from './Interfaces/UiDesign'; -import {AuthenticationService} from './login/authentication.service'; -import {LoginComponent} from './login/login.component'; -import {UserService} from './services/user.service'; -import {User} from './Interfaces/User'; -import {PermissionService} from './services/permission.service'; -import {RoutingUrls} from './app-routing/routing-urls'; +import { UiDesignService } from './services/ui-design.service'; +import { MatDialog } from '@angular/material'; +import { AfterViewInit, Component, OnInit } from '@angular/core'; +import { TdMediaService } from '@covalent/core'; +import { Router } from '@angular/router'; +import { WorldService } from './services/world.service'; +import { World } from './Interfaces/World'; +import { TranslateService } from '@ngx-translate/core'; +import { UiDesign } from './Interfaces/UiDesign'; +import { AuthenticationService } from './login/authentication.service'; +import { LoginComponent } from './login/login.component'; +import { UserService } from './services/user.service'; +import { User } from './Interfaces/User'; +import { PermissionService } from './services/permission.service'; +import { RoutingUrls } from './app-routing/routing-urls'; @Component({ selector: 'app-root', @@ -58,15 +58,15 @@ export class AppComponent implements OnInit, AfterViewInit { private authService: AuthenticationService, private permissionService: PermissionService, private userService: UserService) { - + translate.setDefaultLang('en'); // Fallback language when a translation isn't found in the current language. translate.use(translate.getBrowserLang()); // The lang to use. If the lang isn't available, it will use the current loader to get them. } protected login(): void { - this.dialog.open(LoginComponent, {panelClass: 'dialog-sexy', width: '500px'}).afterClosed() - .subscribe(() => this.setDesign()); + this.dialog.open(LoginComponent, { panelClass: 'dialog-sexy', width: '500px' }).afterClosed() + .subscribe(() => this.setDesign()); } protected logout(): void { @@ -88,7 +88,8 @@ export class AppComponent implements OnInit, AfterViewInit { this.user = this.userService.getUser(); this.updateMenu(); this.susbcribeWorlds(); - this.setDesign() + this.setDesign(); + this.updateWorldsFromCurrentUser(); } }); this.setPreDesign(); @@ -118,21 +119,25 @@ export class AppComponent implements OnInit, AfterViewInit { } - private susbcribeWorlds(){ - this.worldService.currentWorld$.subscribe(world =>{ - this.currentWorld = world; + private susbcribeWorlds() { + this.worldService.currentWorld$.subscribe(world => { + this.currentWorld = world; this.setBackground(); }) - this.worldService.worlds$ .subscribe(worlds=>{ - this.worlds = worlds; + this.worldService.worlds$.subscribe(worlds => { + this.worlds = worlds; }) } - private setBackground(){ - if (this.currentWorld && this.user ) { - this.changebackground( this.currentWorld.image); + protected updateWorldsFromCurrentUser(): void { + this.worldService.getWorlds(); + } + + private setBackground() { + if (this.currentWorld && this.user) { + this.changebackground(this.currentWorld.image); } else if (!this.currentWorld && this.user) { - this.changebackground(""); + this.changebackground(""); } else if (!this.currentWorld && !this.user) { this.changebackground("bg13"); } @@ -177,7 +182,7 @@ export class AppComponent implements OnInit, AfterViewInit { } changebackground(image: string) { - if (image != ""){ + if (image != "") { this.body.style.backgroundImage = 'url("/assets/images/background/' + image + '.jpg")'; } else { this.body.style.backgroundImage = 'url("")'; @@ -186,23 +191,23 @@ export class AppComponent implements OnInit, AfterViewInit { } setDesign() { - if (this.user){ + if (this.user) { this.uiDesignService.getUiDesign().subscribe(ui => { - this.ui = ui; - console.log(ui) - this.body.className = ''; + this.ui = ui; + console.log(ui) + this.body.className = ''; this.addClass(this.body, this.ui.name); this.addClass(this.body, "background-image"); - }); + }); } } - setPreDesign(){ + setPreDesign() { this.toggleDesign(); } toggleDesign() { - const dark = 'dark'; + const dark = 'dark'; const light = 'light'; if (this.hasClass(this.body, light)) { // If light is choosen, toggle to dark @@ -219,7 +224,7 @@ export class AppComponent implements OnInit, AfterViewInit { this.addClass(this.body, "background-image"); } - hasClass(element: HTMLScriptElement, cssClass: string): Boolean{ + hasClass(element: HTMLScriptElement, cssClass: string): Boolean { return element.classList.contains(cssClass); } @@ -228,8 +233,8 @@ export class AppComponent implements OnInit, AfterViewInit { return newString.replace(' ', ' '); } - addClass(element, cssClass){ - if (!this.hasClass(element, cssClass)){ + addClass(element, cssClass) { + if (!this.hasClass(element, cssClass)) { element.className += ' ' + cssClass + ' '; } element.className.replace(' ', ' '); diff --git a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts index 04ab7822..29834dce 100644 --- a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts +++ b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-developer/components/admin-developer-edit/admin-developer-edit.component.ts @@ -12,6 +12,7 @@ import { UserToWorldService } from '../../../../../../services/user-to-world.ser import { Role } from 'app/Interfaces/Role'; import { RoleService } from 'app/services/role.service'; import { FormGroup, FormControl, Validators } from '@angular/forms'; +import { WorldService } from 'app/services/world.service'; @Component({ selector: 'app-admin-developer-edit', @@ -46,18 +47,17 @@ export class AdminDeveloperEditComponent implements OnInit { @Inject(MAT_DIALOG_DATA) public data: {user: User, users: User[]}, private imageService: ImageService, private roleService: RoleService, - private userToWorldService: UserToWorldService + private userToWorldService: UserToWorldService, + private worldService: WorldService ) { } ngOnInit() { - this.translateTable(); this.loadImages(); this.userToWorldService.getUserToWorlds(this.data.user).then(userToWorlds => { this.userToWorlds = userToWorlds - }); - + }); this.roleService.getRoles().then(roles => this.roles = roles); this.nameTaken = false; this.mailTaken = false; @@ -75,6 +75,9 @@ export class AdminDeveloperEditComponent implements OnInit { this.userService.updateUser(this.data.user).then(() => { this.userToWorldService.updateUserToWorld(this.userToWorlds); this.dialogRef.close(true); + if(this.data.user.id === this.userService.getUser().id){ + this.worldService.getWorlds() + } }) } diff --git a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-world/admin-world.component.ts b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-world/admin-world.component.ts index 64e1af13..25771925 100644 --- a/sonarQuest-frontend/src/app/pages/admin-page/components/admin-world/admin-world.component.ts +++ b/sonarQuest-frontend/src/app/pages/admin-page/components/admin-world/admin-world.component.ts @@ -1,17 +1,19 @@ -import {TranslateService} from '@ngx-translate/core'; -import {Component, OnInit} from '@angular/core'; -import {WorldService} from '../../../../services/world.service'; -import {World} from '../../../../Interfaces/World'; +import { TranslateService } from '@ngx-translate/core'; +import { Component, OnInit } from '@angular/core'; +import { WorldService } from '../../../../services/world.service'; +import { World } from '../../../../Interfaces/World'; import { IPageChangeEvent, ITdDataTableColumn, ITdDataTableSortChangeEvent, TdDataTableService, TdDataTableSortingOrder } from '@covalent/core'; -import {MatDialog} from '@angular/material'; -import {EditWorldComponent} from './components/edit-world/edit-world.component'; -import {TaskService} from '../../../../services/task.service'; -import {QuestService} from '../../../../services/quest.service'; -import {AdventureService} from '../../../../services/adventure.service'; -import {LoadingService} from '../../../../services/loading.service'; +import { MatDialog } from '@angular/material'; +import { EditWorldComponent } from './components/edit-world/edit-world.component'; +import { TaskService } from '../../../../services/task.service'; +import { QuestService } from '../../../../services/quest.service'; +import { AdventureService } from '../../../../services/adventure.service'; +import { LoadingService } from '../../../../services/loading.service'; +import { UserService } from 'app/services/user.service'; +import { RoleService } from 'app/services/role.service'; @Component({ selector: 'app-admin-world', @@ -23,12 +25,12 @@ export class AdminWorldComponent implements OnInit { currentWorld: World; worlds: World[]; columns: ITdDataTableColumn[] = [ - {name: 'id', label: 'Id'}, - {name: 'name', label: 'Name'}, - {name: 'project', label: 'Project', width: { min: 400 } }, - {name: 'active', label: 'Active'}, - {name: 'usequestcards', label: 'Questcards'}, - {name: 'edit', label: ''} + { name: 'id', label: 'Id' }, + { name: 'name', label: 'Name' }, + { name: 'project', label: 'Project', width: { min: 400 } }, + { name: 'active', label: 'Active' }, + { name: 'usequestcards', label: 'Questcards' }, + { name: 'edit', label: '' } ]; // Sort / Filter / Paginate variables @@ -43,13 +45,14 @@ export class AdminWorldComponent implements OnInit { sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Ascending; constructor(private worldService: WorldService, - private questService: QuestService, - private adventureService: AdventureService, - private taskService: TaskService, - private _dataTableService: TdDataTableService, - private translateService: TranslateService, - private dialog: MatDialog, - private loadingService: LoadingService) { + private questService: QuestService, + private adventureService: AdventureService, + private taskService: TaskService, + private _dataTableService: TdDataTableService, + private translateService: TranslateService, + private dialog: MatDialog, + private loadingService: LoadingService, + private userService: UserService) { } ngOnInit() { @@ -62,18 +65,21 @@ export class AdminWorldComponent implements OnInit { if (this.worldService.getCurrentWorld()) { this.currentWorld = this.worldService.getCurrentWorld(); } - this.loadWorlds(); + if (this.userService.getUser().role.name.toLocaleUpperCase() === 'ADMIN') { + this.loadWorlds(); + } + } translateTable() { this.translateService.get('TABLE.COLUMNS').subscribe((col_names) => { this.columns = [ - {name: 'id', label: col_names.ID, width: 35}, - {name: 'name', label: col_names.NAME}, - {name: 'project', label: col_names.PROJECT, width: { min: 400 }}, - {name: 'active', label: col_names.ACTIVE}, - {name: 'usequestcards', label: col_names.USE_QUEST_CARDS}, - {name: 'edit', label: ''}] + { name: 'id', label: col_names.ID, width: 35 }, + { name: 'name', label: col_names.NAME }, + { name: 'project', label: col_names.PROJECT, width: { min: 400 } }, + { name: 'active', label: col_names.ACTIVE }, + { name: 'usequestcards', label: col_names.USE_QUEST_CARDS }, + { name: 'edit', label: '' }] }); } @@ -85,7 +91,7 @@ export class AdminWorldComponent implements OnInit { } protected editWorld(world: World) { - this.dialog.open(EditWorldComponent, {data: world}).afterClosed().subscribe(() => { + this.dialog.open(EditWorldComponent, { data: world }).afterClosed().subscribe(() => { this.loadWorlds(); this.worldService.loadWorld(); })