Skip to content

Commit

Permalink
switch to new functional guards API
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikks committed Nov 6, 2023
1 parent 33d488f commit d6eb214
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 77 deletions.
12 changes: 6 additions & 6 deletions frontend/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { GuildPermissionGuard } from './guards/guild-permission.guard';
import { guildPermissionGuard } from './guards/guild-permission.guard';
import { KeybindGeneratorComponent } from './keybind-generator/keybind-generator.component';
import { RecorderComponent } from './recorder/recorder.component';
import { CanDeactivateGuildSettingsGuard } from './settings/guild-settings/can-deactivate-guild-settings.guard';
import { GuildSettingsComponent } from './settings/guild-settings/guild-settings.component';
import { SettingsComponent } from './settings/settings.component';
import { CanDeactivateSoundManagerGuard } from './settings/sound-manager/can-deactivate-sound-manager.guard';
import { canDeactivateSoundManagerGuard } from './settings/sound-manager/can-deactivate-sound-manager.guard';
import { SoundManagerComponent } from './settings/sound-manager/sound-manager.component';
import { UserSettingsComponent } from './settings/user-settings/user-settings.component';
import { SoundboardComponent } from './soundboard/soundboard.component';
import { canDeactivateGuildSettingsGuard } from './settings/guild-settings/can-deactivate-guild-settings.guard';

const routes: Routes = [
{
Expand Down Expand Up @@ -39,7 +39,7 @@ const routes: Routes = [
},
{
path: 'guilds/:guildId',
canActivate: [GuildPermissionGuard],
canActivate: [guildPermissionGuard],
children: [
{
path: '',
Expand All @@ -49,12 +49,12 @@ const routes: Routes = [
{
path: 'settings',
component: GuildSettingsComponent,
canDeactivate: [CanDeactivateGuildSettingsGuard],
canDeactivate: [canDeactivateGuildSettingsGuard],
},
{
path: 'sounds',
component: SoundManagerComponent,
canDeactivate: [CanDeactivateSoundManagerGuard],
canDeactivate: [canDeactivateSoundManagerGuard],
},
],
},
Expand Down
21 changes: 7 additions & 14 deletions frontend/src/app/guards/guild-permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { ApiService } from '../services/api.service';

@Injectable({
providedIn: 'root',
})
export class GuildPermissionGuard implements CanActivate {
constructor(private apiService: ApiService, private router: Router) {}
export const guildPermissionGuard: CanActivateFn = (route, _state) => {
const guildId = route.params.guildId;
const user = inject(ApiService).user();

canActivate(route: ActivatedRouteSnapshot, _state: RouterStateSnapshot) {
const guildId = route.params.guildId;
const user = this.apiService.user();

return user?.guilds.find(guild => guild.id === guildId).role !== 'user' ? true : this.router.parseUrl('/settings');
}
}
return user?.guilds.find(guild => guild.id === guildId).role !== 'user' ? true : inject(Router).parseUrl('/settings');
};
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { Injectable } from '@angular/core';
import { inject } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router';
import { CanDeactivateFn } from '@angular/router';
import { GuildSettingsComponent } from './guild-settings.component';

@Injectable({
providedIn: 'root',
})
export class CanDeactivateGuildSettingsGuard implements CanDeactivate<GuildSettingsComponent> {
constructor(private snackBar: MatSnackBar) {}
export const canDeactivateGuildSettingsGuard: CanDeactivateFn<GuildSettingsComponent> = (component, _route, _state) => {
const snackBar = inject(MatSnackBar);

canDeactivate(component: GuildSettingsComponent, _route: ActivatedRouteSnapshot, _state: RouterStateSnapshot) {
if (component.randomInfixesHasChanges()) {
this.snackBar.open('You have unsaved changes. Please save or discard them before leaving this component.');
return false;
}

if (
component.userIsSaving() === 'saving' ||
component.moderatorIsSaving() === 'saving' ||
component.maxVolumeIsSaving() === 'saving' ||
component.meanVolumeIsSaving() === 'saving' ||
component.randomInfixIsSaving()
) {
this.snackBar.open('You cannot leave this component while saving.');
return false;
}
if (component.randomInfixesHasChanges()) {
snackBar.open('You have unsaved changes. Please save or discard them before leaving this component.');
return false;
}

return true;
if (
component.userIsSaving() === 'saving' ||
component.moderatorIsSaving() === 'saving' ||
component.maxVolumeIsSaving() === 'saving' ||
component.meanVolumeIsSaving() === 'saving' ||
component.randomInfixIsSaving()
) {
snackBar.open('You cannot leave this component while saving.');
return false;
}
}

return true;
};
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import { Injectable } from '@angular/core';
import { inject } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { CanDeactivateFn } from '@angular/router';
import { SoundManagerComponent } from './sound-manager.component';

@Injectable({
providedIn: 'root',
})
export class CanDeactivateSoundManagerGuard implements CanDeactivate<SoundManagerComponent> {
constructor(private snackBar: MatSnackBar) {}
export const canDeactivateSoundManagerGuard: CanDeactivateFn<SoundManagerComponent> = (component, _route, _state) => {
const snackBar = inject(MatSnackBar);

canDeactivate(
component: SoundManagerComponent,
_route: ActivatedRouteSnapshot,
_state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
if (component.isSaving()) {
this.snackBar.open('You cannot leave this component while saving');
return false;
}
if (component.isUploading()) {
this.snackBar.open('You cannot leave this component while uploading');
return false;
}
if (component.hasChanges()) {
this.snackBar.open('There are sounds with outstanding changes. Please save or discard them before continuing.');
return false;
}
if (component.isProcessing()) {
this.snackBar.open('There are sounds currently being processed. Please wait until that is finished.');
return false;
}

return true;
if (component.isSaving()) {
snackBar.open('You cannot leave this component while saving');
return false;
}
if (component.isUploading()) {
snackBar.open('You cannot leave this component while uploading');
return false;
}
if (component.hasChanges()) {
snackBar.open('There are sounds with outstanding changes. Please save or discard them before continuing.');
return false;
}
}
if (component.isProcessing()) {
snackBar.open('There are sounds currently being processed. Please wait until that is finished.');
return false;
}

return true;
};

0 comments on commit d6eb214

Please sign in to comment.