-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
45 changes: 20 additions & 25 deletions
45
frontend/src/app/settings/guild-settings/can-deactivate-guild-settings.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
54 changes: 22 additions & 32 deletions
54
frontend/src/app/settings/sound-manager/can-deactivate-sound-manager.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |