Move routes to a constant

This commit is contained in:
William Nolin 2022-05-19 07:30:49 -04:00
parent 63e70cd109
commit be92c22de0
37 changed files with 349 additions and 167 deletions

View File

@ -4,68 +4,69 @@ import {CatalogComponent} from './pages/catalog/catalog.component'
import {AdministrationComponent} from './pages/administration/administration.component'
import {MiscComponent} from './pages/others/misc.component'
import {CreConfigEditor} from './modules/configuration/config-editor'
import {routes as routesConst} from './routes'
const routes: Routes = [{
path: 'color',
path: routesConst.recipes.route,
loadChildren: () => import('./modules/recipes/recipes.module').then(m => m.RecipesModule)
}, {
path: 'account',
path: routesConst.accounts.route,
loadChildren: () => import('./modules/accounts/accounts.module').then(m => m.AccountsModule)
}, {
path: 'catalog',
path: routesConst.catalog.route,
component: CatalogComponent,
children: [{
path: 'materialtype',
path: routesConst.catalog.children.materialTypes.simpleRoute,
loadChildren: () => import('./modules/material-type/material-type.module').then(m => m.MaterialTypeModule),
}, {
path: 'material',
path: routesConst.catalog.children.materials.simpleRoute,
loadChildren: () => import('./modules/material/material.module').then(m => m.MaterialModule)
}, {
path: 'company',
path: routesConst.catalog.children.companies.simpleRoute,
loadChildren: () => import('./modules/company/company.module').then(m => m.CompanyModule)
}, {
path: '',
pathMatch: 'full',
redirectTo: 'materialtype'
redirectTo: routesConst.catalog.children.materials.simpleRoute
}]
}, {
path: 'admin',
path: routesConst.administration.route,
component: AdministrationComponent,
children: [
{
path: 'user',
path: routesConst.administration.children.users.simpleRoute,
loadChildren: () => import('./modules/users/user.module').then(m => m.UserModule)
}, {
path: 'group',
path: routesConst.administration.children.groups.simpleRoute,
loadChildren: () => import('./modules/groups/group.module').then(m => m.GroupModule)
}, {
path: 'group-token',
path: routesConst.administration.children.groupTokens.simpleRoute,
loadChildren: () => import('./modules/groupTokens/group-tokens.module').then(m => m.GroupTokensModule)
}, {
path: 'config',
path: routesConst.administration.children.configurations.simpleRoute,
loadChildren: () => import('./modules/configuration/config.module').then(m => m.ConfigModule),
component: CreConfigEditor
}, {
path: '',
pathMatch: 'full',
redirectTo: 'user'
redirectTo: routesConst.administration.children.users.simpleRoute
}
]
}, {
path: 'misc',
path: routesConst.misc.route,
component: MiscComponent,
children: [{
path: 'touch-up-kit',
path: routesConst.misc.children.touchUpKits.simpleRoute,
loadChildren: () => import('./modules/touch-up-kit/touch-up-kit.module').then(m => m.TouchUpKitModule)
}, {
path: '',
pathMatch: 'full',
redirectTo: 'touch-up-kit'
redirectTo: routesConst.misc.children.touchUpKits.simpleRoute
}]
}]
@NgModule({
imports: [RouterModule.forRoot(routes, {relativeLinkResolution: 'legacy'})],
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {

View File

@ -7,6 +7,7 @@ import {ErrorHandler, ErrorService} from '../shared/service/error.service'
import {ActivatedRoute, Router} from '@angular/router'
import {CreForm, ICreForm} from "../shared/components/forms/forms";
import {AlertService} from "../shared/service/alert.service";
import {routes} from "../../routes";
@Component({
selector: 'cre-login',
@ -49,7 +50,7 @@ export class Login extends ErrorHandlingComponent {
submit() {
this.subscribeAndNavigate(
this.accountService.loginAsUser(this.userIdControl.value, this.passwordControl.value),
'/color/list'
`/${routes.recipes.route}/list`
)
}
@ -83,6 +84,6 @@ export class Logout extends SubscribingComponent {
this.accountService.logout()
}
this.urlUtils.navigateTo('/account/login')
this.urlUtils.navigateTo(`/${routes.accounts.route}/login`)
}
}

View File

@ -59,7 +59,7 @@ export class AccountService implements OnDestroy {
.pipe(take(1), takeUntil(this.destroy$))
login$.subscribe({
next: result => this.appState.authenticateUser(result),
next: result => this.appState.authenticateUser(result, isGroup),
error: error => {
if (errorConsumer(error)) return;

View File

@ -23,4 +23,14 @@ export class GroupTokenService {
save(name: string, groupId: number): Observable<GroupToken> {
return this.api.post<GroupToken>('/account/group/token', {name, groupId})
}
toggle(token: GroupToken): Observable<void> {
return token.enabled ?
this.api.put<void>(`/account/group/token/${token.id}/disable`) :
this.api.put<void>(`/account/group/token/${token.id}/enable`)
}
delete(id: string): Observable<void> {
return this.api.delete<void>(`/account/group/token/${id}`)
}
}

View File

@ -5,6 +5,7 @@ import {FormField} from '../../../shared/components/entity-add/entity-add.compon
import {ActivatedRoute, Router} from '@angular/router'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-add',
@ -50,7 +51,7 @@ export class AddComponent extends ErrorHandlingComponent {
this.submittedValues = values
this.subscribeAndNavigate(
this.companyService.save(values.name),
'/catalog/company/list'
routes.catalog.children.companies.route + '/list'
)
}
}

View File

@ -6,6 +6,7 @@ import {CompanyService} from '../../service/company.service'
import {ActivatedRoute, Router} from '@angular/router'
import {ErrorHandler, ErrorHandlerComponent, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-edit',
@ -29,7 +30,7 @@ export class EditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-company-id',
consumer: error => this.urlUtils.navigateTo('/catalog/company/list')
consumer: _ => this.urlUtils.navigateTo(routes.catalog.children.companies.route + '/list')
}, {
filter: error => error.type === 'exists-company-name',
messageProducer: error => `Une bannière avec le nom '${error.name}' existe déjà`
@ -65,14 +66,14 @@ export class EditComponent extends ErrorHandlingComponent {
submit(values) {
this.subscribeAndNavigate(
this.companyService.update(this.company.id, values.name),
'/catalog/company/list'
routes.catalog.children.companies.route + '/list'
)
}
delete() {
this.subscribeAndNavigate(
this.companyService.delete(this.company.id),
'/catalog/company/list'
routes.catalog.children.companies.route + '/list'
)
}
}

View File

@ -6,8 +6,7 @@ const routes: Routes = [{
path: 'list',
component: GroupTokenList
}, {
path: '',
redirectTo: 'list'
path: '', redirectTo: 'list'
}]
@NgModule({

View File

@ -4,6 +4,8 @@ import {SharedModule} from '../shared/shared.module'
import {CreInputsModule} from '../shared/components/inputs/inputs.module'
import {CreButtonsModule} from '../shared/components/buttons/buttons.module'
import {GroupTokenAdd, GroupTokenList} from "../groupTokens/group-tokens";
import {GroupTokensRoutingModule} from "./group-tokens-routing.module";
import {CreTablesModule} from "../shared/components/tables/tables.module";
@NgModule({
@ -15,9 +17,11 @@ import {GroupTokenAdd, GroupTokenList} from "../groupTokens/group-tokens";
GroupTokenAdd
],
imports: [
GroupTokensRoutingModule,
SharedModule,
CreInputsModule,
CreButtonsModule
CreButtonsModule,
CreTablesModule
]
})
export class GroupTokensModule {

View File

@ -6,15 +6,21 @@ import {GroupTokenService} from "../accounts/services/group-token.service";
import {SubscribingComponent} from "../shared/components/subscribing.component";
import {ErrorService} from "../shared/service/error.service";
import {ActivatedRoute, Router} from "@angular/router";
import {tap} from "rxjs/operators";
@Component({
selector: 'cre-group-token-list',
templateUrl: 'list.html'
})
export class GroupTokenList extends SubscribingComponent {
tokens$ = this.groupTokenService.all
tokens$ = this.groupTokenService.all.pipe(tap(
tokens => this.tokensEmpty = tokens.length <= 0))
tokensEmpty = false
columns = ['id', 'name', 'groupName', 'deleteButton']
columns = ['id', 'name', 'groupName', 'state', 'stateButton', 'deleteButton']
@ViewChild(CrePromptDialog) removePrompt: CrePromptDialog
removePromptToken: GroupToken | null = null
constructor(
private groupTokenService: GroupTokenService,
@ -24,6 +30,25 @@ export class GroupTokenList extends SubscribingComponent {
) {
super(errorService, activatedRoute, router)
}
showRemovePrompt(token: GroupToken) {
this.removePromptToken = token
this.removePrompt.show()
}
toggle(token: GroupToken) {
this.subscribe(
this.groupTokenService.toggle(token),
_ => window.location.reload()
)
}
delete() {
this.subscribe(
this.groupTokenService.delete(this.removePromptToken.id),
_ => window.location.reload()
)
}
}
@Component({
@ -63,7 +88,7 @@ export class GroupTokenAdd extends SubscribingComponent {
const name = this.controls.name.value
this.subscribe(
this.groupTokenService.save(name, this.group.id),
groupToken => this.defaultGroupUpdate.emit(groupToken.group)
groupToken => this.defaultGroupUpdate.emit(groupToken)
)
}
}

View File

@ -1 +1,54 @@
<div>A list</div>
<div class="mt-5"></div>
<cre-warning-alert *ngIf="tokensEmpty">
<p>Il n'y a aucun ordinateur enregistré dans le système.</p>
<p>Vous pouvez en ajouter un en définissant un groupe par défaut <b><a routerLink="/admin/group/list">ici</a></b>.</p>
</cre-warning-alert>
<cre-table *ngIf="!tokensEmpty" class="mx-auto" [data]="tokens$ | async" [columns]="columns">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef>Identifiant</th>
<td mat-cell *matCellDef="let token">{{token.id}}</td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>Nom</th>
<td mat-cell *matCellDef="let token">{{token.name}}</td>
</ng-container>
<ng-container matColumnDef="groupName">
<th mat-header-cell *matHeaderCellDef>Groupe</th>
<td mat-cell *matCellDef="let token">{{token.group.name}}</td>
</ng-container>
<ng-container matColumnDef="state">
<th mat-header-cell *matHeaderCellDef>État</th>
<td mat-cell *matCellDef="let token">{{token.enabled ? 'Activé' : 'Désactivé'}}</td>
</ng-container>
<ng-container matColumnDef="stateButton">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let token; let i = index">
<cre-accent-button [creInteractiveCell]="i" (click)="toggle(token)">
<ng-container *ngIf="!token.enabled">Activer</ng-container>
<ng-container *ngIf="token.enabled">Désactiver</ng-container>
</cre-accent-button>
</td>
</ng-container>
<ng-container matColumnDef="deleteButton">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let token; let i = index">
<cre-warn-button [creInteractiveCell]="i" (click)="showRemovePrompt(token)">
Supprimer
</cre-warn-button>
</td>
</ng-container>
</cre-table>
<cre-prompt-dialog title="Retirer un ordinateur" (continue)="delete()">
<cre-dialog-body *ngIf="removePromptToken">
Voulez-vous vraiment retirer l'ordinateur '{{removePromptToken.name}}' du système?<br/>
Ses utilisateurs ne seront plus connectés automatiquement.
</cre-dialog-body>
</cre-prompt-dialog>

View File

@ -7,6 +7,7 @@ import {ErrorHandlingComponent} from '../../../shared/components/subscribing.com
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {FormField} from '../../../shared/components/entity-add/entity-add.component'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-add',
@ -60,7 +61,7 @@ export class AddComponent extends ErrorHandlingComponent {
if (permissionsField.valid()) {
this.subscribeAndNavigate(
this.groupService.save(values.name, permissionsField.allEnabledPermissions),
'/admin/group/list'
routes.administration.children.groups.route + '/list'
)
}
}

View File

@ -3,12 +3,15 @@ import {ActivatedRoute, Router} from '@angular/router'
import {Group} from '../../../shared/model/account.model'
import {GroupService} from '../../services/group.service'
import {Validators} from '@angular/forms'
import {currentPermissionsFieldComponent} from '../../../shared/components/permissions-field/permissions-field.component'
import {
currentPermissionsFieldComponent
} from '../../../shared/components/permissions-field/permissions-field.component'
import {AccountService} from '../../../accounts/services/account.service'
import {ErrorHandlingComponent} from '../../../shared/components/subscribing.component'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {FormField} from '../../../shared/components/entity-add/entity-add.component'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-edit',
@ -38,7 +41,7 @@ export class EditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-group-id',
consumer: error => this.urlUtils.navigateTo('/admin/group/list')
consumer: _ => this.urlUtils.navigateTo(routes.administration.children.groups.route + '/list')
}, {
filter: error => error.type === 'exists-group-name',
messageProducer: error => `Un groupe avec le nom '${error.name}' existe déjà`
@ -74,7 +77,7 @@ export class EditComponent extends ErrorHandlingComponent {
if (permissionsField.valid()) {
this.subscribeAndNavigate(
this.groupService.update(this.group.id, values.name, permissionsField.allEnabledPermissions),
'/admin/group/list'
routes.administration.children.groups.route + '/list'
)
}
}
@ -82,7 +85,7 @@ export class EditComponent extends ErrorHandlingComponent {
delete() {
this.subscribeAndNavigate(
this.groupService.delete(this.group.id),
'/admin/group/list'
routes.administration.children.groups.route + '/list'
)
}
}

View File

@ -9,6 +9,7 @@ import {AlertService} from '../../../shared/service/alert.service'
import {AppState} from '../../../shared/app-state'
import {GroupTokenService} from "../../../accounts/services/group-token.service";
import {GroupTokenAdd} from "../../../groupTokens/group-tokens";
import {tap} from "rxjs/operators";
@Component({
selector: 'cre-groups',
@ -16,7 +17,8 @@ import {GroupTokenAdd} from "../../../groupTokens/group-tokens";
styleUrls: ['./list.component.sass']
})
export class ListComponent extends ErrorHandlingComponent {
groups$ = this.groupService.all
groups$ = this.groupService.all.pipe(tap(
groups => this.groupsEmpty = groups.length <= 0))
groupsEmpty = false
currentGroupToken: GroupToken | null

View File

@ -6,6 +6,7 @@ import {ErrorHandlingComponent} from '../../../shared/components/subscribing.com
import {ActivatedRoute, Router} from '@angular/router'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-add',
@ -69,7 +70,7 @@ export class AddComponent extends ErrorHandlingComponent {
submit(values) {
this.subscribeAndNavigate(
this.materialTypeService.save(values.name, values.prefix, values.usePercentages),
'/catalog/materialtype/list'
routes.catalog.children.materialTypes.route + '/list'
)
}
}

View File

@ -7,6 +7,7 @@ import {FormField} from '../../../shared/components/entity-add/entity-add.compon
import {Validators} from '@angular/forms'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-edit',
@ -45,7 +46,7 @@ export class EditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-materialtype-id',
consumer: error => this.urlUtils.navigateTo('/catalog/materialtype/list')
consumer: _ => this.urlUtils.navigateTo(routes.catalog.children.materialTypes.route + '/list')
}, {
filter: error => error.type === 'exists-materialtype-name',
messageProducer: error => `Un type de produit avec le nom '${error.name}' existe déjà`
@ -84,14 +85,14 @@ export class EditComponent extends ErrorHandlingComponent {
submit(values) {
this.subscribeAndNavigate(
this.materialTypeService.update(this.materialType.id, values.name, values.prefix),
'/catalog/materialtype/list'
routes.catalog.children.materialTypes.route + '/list'
)
}
delete() {
this.subscribeAndNavigate(
this.materialTypeService.delete(this.materialType.id),
'/catalog/materialtype/list'
routes.catalog.children.materialTypes.route + '/list'
)
}
}

View File

@ -8,6 +8,7 @@ import {ErrorHandlingComponent} from '../../../shared/components/subscribing.com
import {map} from 'rxjs/operators'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-add',
@ -81,7 +82,7 @@ export class AddComponent extends ErrorHandlingComponent {
submit(values) {
this.subscribeAndNavigate(
this.materialService.save(values.name, values.inventoryQuantity, values.materialType, values.simdutFile),
'/catalog/material/list'
routes.catalog.children.materials.route + '/list'
)
}
}

View File

@ -9,6 +9,7 @@ import {ErrorHandlingComponent} from '../../../shared/components/subscribing.com
import {Material, openSimdut} from '../../../shared/model/material.model'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-edit',
@ -69,7 +70,7 @@ export class EditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-material-id',
consumer: error => this.urlUtils.navigateTo('/catalog/material/list')
consumer: _ => this.urlUtils.navigateTo(routes.catalog.children.materials.route + '/list')
}, {
filter: error => error.type === 'exists-material-name',
messageProducer: error => `Un produit avec le nom '${error.name}' existe déjà`
@ -108,14 +109,14 @@ export class EditComponent extends ErrorHandlingComponent {
submit(values) {
this.subscribeAndNavigate(
this.materialService.update(this.material.id, values.name, values.inventoryQuantity, values.materialType, this.selectedSimdutFile),
'/catalog/material/list'
routes.catalog.children.materials.route + '/list'
)
}
delete() {
this.subscribeAndNavigate(
this.materialService.delete(this.material.id),
'/catalog/material/list'
routes.catalog.children.materials.route + '/list'
)
}

View File

@ -22,6 +22,7 @@ import {Permission} from '../shared/model/account.model'
import {FormControl} from '@angular/forms';
import {map} from 'rxjs/operators';
import {CreInputEntry} from '../shared/components/inputs/inputs';
import {routes} from "../../routes";
@Component({
selector: 'cre-recipe-explore',
@ -48,7 +49,7 @@ export class CreRecipeExplore extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-recipe-id',
consumer: error => this.urlUtils.navigateTo('/color/list')
consumer: _ => this.urlUtils.navigateTo(routes.recipes.route + '/list')
}, {
filter: error => error.type === 'notenoughinventory-multiple',
consumer: error => this.deductErrorBody = {mix: this.deductedMixId, lowQuantities: error.lowQuantities},

View File

@ -12,6 +12,7 @@ import {ActivatedRoute, Router} from '@angular/router'
import {Config} from '../shared/model/config.model'
import {Permission} from '../shared/model/account.model'
import {FormControl} from '@angular/forms'
import {routes} from "../../routes";
@Component({
selector: 'cre-recipe-list',
@ -52,7 +53,7 @@ export class RecipeList extends ErrorHandlingComponent {
this.configService.get(Config.EMERGENCY_MODE),
config => {
if (config.content == 'true') {
this.urlUtils.navigateTo('/admin/config/')
this.urlUtils.navigateTo(routes.administration.children.configurations.route)
}
}
)

View File

@ -15,6 +15,7 @@ import {MaterialService} from '../../material/service/material.service'
import {CreForm} from '../../shared/components/forms/forms'
import {MixMaterialsForm} from './materials-form'
import {MixSaveDto, MixService, MixUpdateDto} from '../services/mix.service'
import {routes} from "../../../routes";
@Directive()
abstract class _BaseMixPage extends SubscribingComponent {
@ -74,7 +75,7 @@ export class MixAdd extends _BaseMixPage {
submit(dto: MixSaveDto) {
this.subscribeAndNavigate(
this.mixService.saveDto(dto),
`/color/edit/${this.recipe.id}`
`/${routes.recipes.route}/edit/${this.recipe.id}`
)
}
}
@ -110,14 +111,14 @@ export class MixEdit extends _BaseMixPage {
submit(dto: MixSaveDto) {
this.subscribeAndNavigate(
this.mixService.updateDto({...dto, id: this.mix.id}),
`/color/edit/${this.recipe.id}`
`/${routes.recipes.route}/edit/${this.recipe.id}`
)
}
delete() {
this.subscribeAndNavigate(
this.mixService.delete(this.mixId),
'/color/edit/' + this.recipe.id
`/${routes.recipes.route}/edit/` + this.recipe.id
)
}
}

View File

@ -17,6 +17,7 @@ import {GroupService} from '../groups/services/group.service';
import {StepTableComponent} from './components/step-table/step-table.component';
import {anyMap} from '../shared/utils/map.utils';
import {CreForm, ICreForm} from '../shared/components/forms/forms';
import {routes} from "../../routes";
@Component({
selector: 'recipe-form',
@ -117,7 +118,7 @@ export class RecipeAdd extends ErrorHandlingComponent {
submit(recipe: Recipe) {
this.subscribe(
this.recipeService.save(recipe),
recipe => this.urlUtils.navigateTo(`/color/edit/${recipe.id}`)
recipe => this.urlUtils.navigateTo(routes.recipes.route + `/edit/${recipe.id}`)
)
}
}
@ -137,7 +138,7 @@ export class RecipeEdit extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-recipe-id',
consumer: _ => this.urlUtils.navigateTo('/color/list')
consumer: _ => this.urlUtils.navigateTo(routes.recipes.route + '/list')
}]
constructor(
@ -190,14 +191,14 @@ export class RecipeEdit extends ErrorHandlingComponent {
this.subscribeAndNavigate(
this.recipeService.update(recipe, steps),
'/color/list'
routes.recipes.route + '/list'
)
}
delete() {
this.subscribeAndNavigate(
this.recipeService.delete(this.recipe.id),
'/color/list'
routes.recipes.route + '/list'
)
}

View File

@ -7,11 +7,10 @@ import {Title} from '@angular/platform-browser'
providedIn: 'root'
})
export class AppState {
private readonly KEY_AUTHENTICATED = 'authenticated'
private readonly KEY_DEFAULT_GROUP_USER_AUTHENTICATED = 'default-group-user-authenticated'
private readonly KEY_LOGGED_IN_USER = 'logged-in-user'
private readonly KEY_AUTHENTICATED_USER = 'authenticated-user'
private readonly KEY_GROUP_USER = 'authenticated-user-group'
authenticatedUser$ = new Subject<{ authenticated: boolean, authenticatedUser: LoginDto }>()
authenticatedUser$ = new Subject<AuthenticationEvent>()
serverOnline$ = new Subject<boolean>()
constructor(
@ -19,67 +18,49 @@ export class AppState {
) {
}
authenticateUser(user: LoginDto) {
this.authenticatedUser = user
this.isAuthenticated = true
}
authenticateGroupUser(user: LoginDto) {
this.authenticatedUser = user
this.isDefaultGroupUserAuthenticated = true
authenticateUser(user: LoginDto, isGroup: boolean) {
this.setAuthenticatedUser(user, isGroup)
}
resetAuthenticatedUser() {
this.isAuthenticated = false
this.isDefaultGroupUserAuthenticated = false
this.authenticatedUser = null
this.removeAuthenticatedUser()
}
set isServerOnline(isOnline: boolean) {
if (!isOnline) {
this.authenticatedUser = null
this.removeAuthenticatedUser()
}
this.serverOnline$.next(isOnline)
}
get isAuthenticated(): boolean {
return sessionStorage.getItem(this.KEY_AUTHENTICATED) === 'true'
}
private set isAuthenticated(value: boolean) {
sessionStorage.setItem(this.KEY_AUTHENTICATED, value.toString())
this.authenticatedUser$.next({
authenticated: value,
authenticatedUser: this.authenticatedUser
})
}
get isDefaultGroupUserAuthenticated(): boolean {
return sessionStorage.getItem(this.KEY_DEFAULT_GROUP_USER_AUTHENTICATED) === 'true'
}
private set isDefaultGroupUserAuthenticated(value: boolean) {
sessionStorage.setItem(this.KEY_DEFAULT_GROUP_USER_AUTHENTICATED, value.toString())
}
get hasCredentials(): boolean {
return this.isAuthenticated || this.isDefaultGroupUserAuthenticated
return !!this.authenticatedUser
}
get authenticatedUser(): LoginDto {
const userString = sessionStorage.getItem(this.KEY_LOGGED_IN_USER)
const userString = sessionStorage.getItem(this.KEY_AUTHENTICATED_USER)
return userString ? JSON.parse(userString) : null
}
private set authenticatedUser(value: LoginDto) {
if (value === null) {
sessionStorage.removeItem(this.KEY_LOGGED_IN_USER)
} else {
sessionStorage.setItem(this.KEY_LOGGED_IN_USER, JSON.stringify(value))
}
get isGroupUserAuthenticated(): boolean {
return sessionStorage.getItem(this.KEY_GROUP_USER) === 'true'
}
private setAuthenticatedUser(login: LoginDto, isGroup: boolean) {
sessionStorage.setItem(this.KEY_AUTHENTICATED_USER, JSON.stringify(login))
sessionStorage.setItem(this.KEY_GROUP_USER, JSON.stringify(isGroup))
this.authenticatedUser$.next({
authenticated: this.isAuthenticated,
authenticatedUser: value
authenticatedUser: login,
isGroup
})
}
private removeAuthenticatedUser() {
sessionStorage.removeItem(this.KEY_AUTHENTICATED_USER)
sessionStorage.removeItem(this.KEY_GROUP_USER)
this.authenticatedUser$.next({
authenticatedUser: null,
isGroup: false
})
}
@ -87,3 +68,8 @@ export class AppState {
this.titleService.setTitle(`CRE: ${value}`)
}
}
export interface AuthenticationEvent {
authenticatedUser: LoginDto | null
isGroup: boolean
}

View File

@ -1,9 +1,9 @@
<nav mat-tab-nav-bar backgroundColor="primary">
<ng-container *ngFor="let link of links">
<a
*ngIf="link.enabled && hasPermission(link)"
*ngIf="isLinkEnabled(link)"
mat-tab-link
[active]="activeLink.startsWith(link.route)"
[active]="isLinkActive(link)"
(click)="activeLink = link.route">
{{ link.title }}
</a>

View File

@ -47,11 +47,15 @@ export class NavComponent implements OnInit, OnDestroy {
set activeLink(link: string) {
this._activeLink = link
this.router.navigate([link])
this.router.navigate(['/' + link])
}
get activeLink() {
return this._activeLink
isLinkEnabled(link: NavLink): boolean {
return link.enabled && this.hasPermission(link)
}
isLinkActive(link: NavLink): boolean {
return this._activeLink.startsWith(link.route)
}
private updateEnabledLinks(user: LoginDto) {

View File

@ -1,18 +1,17 @@
<div class="user-menu-wrapper">
<div *ngIf="user" class="user-info-wrapper d-flex flex-column" (click)="menuEnabled = !menuEnabled">
<div *ngIf="user" class="user-info-wrapper d-flex flex-column" (click)="showMenu = !showMenu">
<labeled-icon
*ngIf="authenticated"
icon="account"
label="{{user.fullName}}">
</labeled-icon>
<div class="d-flex flex-row">
<labeled-icon
*ngIf="authenticated"
*ngIf="!groupUser"
icon="pound"
[label]="user.id.toString()">
</labeled-icon>
<labeled-icon
*ngIf="userInGroup"
*ngIf="user.groupId"
class="user-info-group"
icon="account-multiple"
[label]="user.groupName">
@ -21,21 +20,21 @@
</div>
<button
*ngIf="!authenticated && !userInGroup"
*ngIf="!user"
(click)="openLogout()">
Connexion
</button>
<mat-action-list *ngIf="menuEnabled">
<mat-action-list *ngIf="showMenu">
<button
*ngIf="!authenticated && userInGroup"
*ngIf="!user || groupUser"
mat-list-item
class="user-menu-item-login"
(click)="openLogin()">
Connexion
</button>
<button
*ngIf="authenticated"
*ngIf="user && !groupUser"
mat-list-item
class="user-menu-item-logout"
(click)="openLogout()">

View File

@ -11,7 +11,7 @@ p, labeled-icon
&:hover labeled-icon
text-decoration: underline
.user-info-group
.user-info-group:nth-child(2)
margin-left: 0.7rem
mat-action-list

View File

@ -1,10 +1,11 @@
import {Component, OnDestroy, OnInit} from '@angular/core'
import {AppState} from '../../app-state'
import {AppState, AuthenticationEvent} from '../../app-state'
import {LoginDto} from '../../model/account.model'
import {Subject} from 'rxjs'
import {takeUntil} from 'rxjs/operators'
import {UrlUtils} from '../../utils/url.utils'
import {ActivatedRoute, Router} from '@angular/router'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-user-menu',
@ -12,10 +13,10 @@ import {ActivatedRoute, Router} from '@angular/router'
styleUrls: ['./user-menu.component.sass']
})
export class UserMenuComponent implements OnInit, OnDestroy {
authenticated = false
user: LoginDto = null
userInGroup = false
menuEnabled = false
groupUser: boolean = false
showMenu = false
private destroy$ = new Subject<boolean>()
private urlUtils: UrlUtils
@ -29,11 +30,12 @@ export class UserMenuComponent implements OnInit, OnDestroy {
}
ngOnInit(): void {
this.authenticationState(this.appState.isAuthenticated, this.appState.authenticatedUser)
this.setInitialAuthenticationState()
this.appState.authenticatedUser$
.pipe(takeUntil(this.destroy$))
.subscribe({
next: authentication => this.authenticationState(authentication.authenticated, authentication.authenticatedUser)
next: event => this.processAuthenticationEvent(event)
})
}
@ -43,20 +45,26 @@ export class UserMenuComponent implements OnInit, OnDestroy {
}
openLogin() {
this.urlUtils.navigateTo('/account/login')
this.menuEnabled = false
this.urlUtils.navigateTo(routes.accounts.route + '/login')
this.hideMenu()
}
openLogout() {
this.urlUtils.navigateTo('/account/logout')
this.menuEnabled = false
this.urlUtils.navigateTo(routes.accounts.route + '/logout')
this.hideMenu()
}
private authenticationState(authenticated: boolean, user: LoginDto) {
this.authenticated = authenticated
this.user = user
if (this.user != null) {
this.userInGroup = this.user.groupId != null
}
private setInitialAuthenticationState() {
this.user = this.appState.authenticatedUser
this.groupUser = this.appState.isGroupUserAuthenticated
}
private processAuthenticationEvent(event: AuthenticationEvent) {
this.user = event.authenticatedUser
this.groupUser = event.isGroup
}
private hideMenu() {
this.showMenu = false
}
}

View File

@ -1,12 +1,13 @@
import {Injectable, OnDestroy} from '@angular/core'
import {HttpClient} from '@angular/common/http'
import {Observable, Subject} from 'rxjs'
import {catchError, Observable, Subject} from 'rxjs'
import {environment} from '../../../../environments/environment'
import {AppState} from '../app-state'
import {Router} from '@angular/router'
import {map, share, takeUntil} from 'rxjs/operators'
import {valueOr} from '../utils/utils'
import {ErrorService} from './error.service'
import {routes} from "../../../routes";
@Injectable({
providedIn: 'root'
@ -70,7 +71,7 @@ export class ApiService implements OnDestroy {
observe: 'response'
}
if (needAuthentication) {
if (this.appState.hasCredentials) {
if (this.appState.isAuthenticated) {
if (httpOptions) {
httpOptions.withCredentials = true
} else {
@ -81,15 +82,26 @@ export class ApiService implements OnDestroy {
}
}
return requestOptions.takeFullResponse
? requestFn(httpOptions)
.pipe(takeUntil(this._destroy$), share())
: requestFn(httpOptions)
.pipe(takeUntil(this._destroy$), map(r => r.body), share())
let request$ = requestFn(httpOptions)
if (!requestOptions.takeFullResponse) {
request$ = request$.pipe(map(r => r.body))
}
return request$.pipe(share(), takeUntil(this._destroy$),
catchError(err => this.navigateOnError(err)))
}
private navigateOnError(error: any) {
if (error && error.status && error.status === 403) {
console.warn("Request was forbidden, redirecting to login")
this.navigateToLogin()
}
return error
}
private navigateToLogin() {
this.router.navigate(['/account/login'])
this.router.navigate([routes.accounts.route + '/login'])
}
}

View File

@ -134,5 +134,5 @@ function isServerOfflineError(response: any): boolean {
}
function isHandledError(error: any): error is HandledError {
return true
return error && error.status && error.type
}

View File

@ -13,6 +13,7 @@ import {map} from 'rxjs/operators'
import {LocalDate, Period} from '@js-joda/core'
import {ConfigService} from '../../shared/service/config.service'
import {Config} from '../../shared/model/config.model'
import {routes} from "../../../routes";
@Component({
selector: 'touchupkit-banner',
@ -138,14 +139,14 @@ export class TouchUpKitDetails extends ErrorHandlingComponent {
save() {
this.subscribeAndNavigate(
this.touchUpKitService.update(this.touchUpKit),
`/misc/touch-up-kit/details/${this.touchUpKit.id}`
routes.misc.children.touchUpKits.route + `/details/${this.touchUpKit.id}`
)
}
complete() {
this.subscribeAndNavigate(
this.touchUpKitService.complete(this.touchUpKit),
'/misc/touch-up-kit'
routes.misc.children.touchUpKits.route
)
}
@ -173,7 +174,7 @@ export class TouchUpKitAdd extends ErrorHandlingComponent {
submit(touchUpKit) {
this.subscribeAndNavigate(
this.touchUpKitService.save(touchUpKit),
'/misc/touch-up-kit/list'
routes.misc.children.touchUpKits.route + '/list'
)
}
}
@ -211,14 +212,14 @@ export class TouchUpKitEdit extends ErrorHandlingComponent {
submit(touchUpKit) {
this.subscribeAndNavigate(
this.touchUpKitService.update(touchUpKit),
'/misc/touch-up-kit/list'
routes.misc.children.touchUpKits.route + '/list'
)
}
delete() {
this.subscribeAndNavigate(
this.touchUpKitService.delete(this.touchUpKit.id),
'/misc/touch-up-kit/list'
routes.misc.children.touchUpKits.route + '/list'
)
}
}

View File

@ -9,6 +9,7 @@ import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {FormField} from '../../../shared/components/entity-add/entity-add.component'
import {map} from 'rxjs/operators'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-add',
@ -111,7 +112,7 @@ export class AddComponent extends ErrorHandlingComponent {
values.groupId,
permissionsField.allEnabledPermissions
),
'/admin/user/list'
routes.administration.children.users.route + '/list'
)
}
}

View File

@ -10,6 +10,7 @@ import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {FormField} from '../../../shared/components/entity-add/entity-add.component'
import {map} from 'rxjs/operators'
import {AppState} from '../../../shared/app-state'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-edit',
@ -61,7 +62,7 @@ export class EditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-user-id',
consumer: error => this.urlUtils.navigateTo('/admin/user/list')
consumer: _ => this.urlUtils.navigateTo(routes.administration.children.users.route + '/list')
}, {
filter: error => error.type === 'exists-user-fullName',
messageProducer: error => `Un utilisateur nommé '${error.fullName}' existe déjà`
@ -105,7 +106,7 @@ export class EditComponent extends ErrorHandlingComponent {
permissionsField.allEnabledPermissions
),
() => {
this.urlUtils.navigateTo('/admin/user/list')
this.urlUtils.navigateTo(routes.administration.children.users.route + '/list')
}
)
}
@ -114,7 +115,7 @@ export class EditComponent extends ErrorHandlingComponent {
delete() {
this.subscribeAndNavigate(
this.userService.delete(this.user.id),
'/admin/user/list'
routes.administration.children.users.route + '/list'
)
}
}

View File

@ -5,6 +5,7 @@ import {AccountModel} from '../../../shared/model/account.model'
import {ActivatedRoute, Router} from '@angular/router'
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms'
import {ErrorHandler, ErrorService} from '../../../shared/service/error.service'
import {routes} from "../../../../routes";
@Component({
selector: 'cre-password-edit',
@ -19,7 +20,7 @@ export class PasswordEditComponent extends ErrorHandlingComponent {
errorHandlers: ErrorHandler[] = [{
filter: error => error.type === 'notfound-user-id',
consumer: error => this.urlUtils.navigateTo('/admin/user/list')
consumer: _ => this.urlUtils.navigateTo(routes.administration.children.users.route + '/list')
}]
constructor(
@ -49,7 +50,7 @@ export class PasswordEditComponent extends ErrorHandlingComponent {
if (this.form.valid) {
this.subscribeAndNavigate(
this.userService.updatePassword(this.user.id, this.passwordControl.value),
'/admin/user/list'
routes.administration.children.users.route + '/list'
)
}
}

View File

@ -1,7 +1,7 @@
import {Component} from '@angular/core'
import {SubMenuComponent} from '../../modules/shared/components/sub-menu/sub-menu.component'
import {NavLink} from '../../modules/shared/components/nav/nav.component'
import {Permission} from '../../modules/shared/model/account.model'
import {routes} from "../../routes";
@Component({
selector: 'cre-administration',
@ -9,10 +9,5 @@ import {Permission} from '../../modules/shared/model/account.model'
styleUrls: ['./administration.component.sass']
})
export class AdministrationComponent extends SubMenuComponent {
links: NavLink[] = [
{route: '/admin/user', title: 'Utilisateurs', permission: Permission.VIEW_USERS},
{route: '/admin/group', title: 'Groupes', permission: Permission.VIEW_USERS},
{route: '/admin/group-token', title: 'Ordinateurs', permission: Permission.ADMIN},
{route: '/admin/config', title: 'Configuration', permission: Permission.ADMIN}
]
links: NavLink[] = Object.values(routes.administration.children)
}

View File

@ -1,7 +1,7 @@
import {Component} from '@angular/core'
import {NavLink} from '../../modules/shared/components/nav/nav.component'
import {Permission} from '../../modules/shared/model/account.model'
import {SubMenuComponent} from '../../modules/shared/components/sub-menu/sub-menu.component'
import {routes} from "../../routes";
@Component({
selector: 'cre-inventory-page',
@ -9,9 +9,5 @@ import {SubMenuComponent} from '../../modules/shared/components/sub-menu/sub-men
styleUrls: ['./catalog.component.sass']
})
export class CatalogComponent extends SubMenuComponent {
links: NavLink[] = [
{route: '/catalog/materialtype', title: 'Types de produit', permission: Permission.VIEW_CATALOG},
{route: '/catalog/material', title: 'Inventaire', permission: Permission.VIEW_CATALOG},
{route: '/catalog/company', title: 'Bannières', permission: Permission.VIEW_CATALOG}
]
links: NavLink[] = Object.values(routes.catalog.children)
}

View File

@ -1,7 +1,7 @@
import {Component} from '@angular/core'
import {SubMenuComponent} from '../../modules/shared/components/sub-menu/sub-menu.component'
import {NavLink} from '../../modules/shared/components/nav/nav.component'
import {Permission} from '../../modules/shared/model/account.model'
import {routes} from "../../routes";
@Component({
selector: 'cre-others',
@ -9,7 +9,5 @@ import {Permission} from '../../modules/shared/model/account.model'
styleUrls: ['./misc.component.sass']
})
export class MiscComponent extends SubMenuComponent{
links: NavLink[] = [
{route: '/misc/touch-up-kit', title: 'Kits de retouche', permission: Permission.VIEW_TOUCH_UP_KITS}
]
links: NavLink[] = Object.values(routes.misc.children)
}

71
src/app/routes.ts Normal file
View File

@ -0,0 +1,71 @@
import {Permission} from "./modules/shared/model/account.model";
export const routes = {
recipes: {
route: 'color',
title: 'Couleurs',
requiredPermission: Permission.VIEW_RECIPES
},
catalog: {
route: 'catalog',
title: 'Catalogue',
requiredPermission: Permission.VIEW_CATALOG,
children: {
materialTypes: {
route: 'catalog/materialtype',
simpleRoute: 'materialtype',
title: 'Types de produit',
permission: Permission.VIEW_CATALOG
},
materials: {
route: 'catalog/inventory',
simpleRoute: 'inventory',
title: 'Inventaire',
permission: Permission.VIEW_CATALOG
},
companies: {
route: 'catalog/company',
simpleRoute: 'company',
title: 'Bannières',
permission: Permission.VIEW_CATALOG
}
}
},
misc: {
route: 'misc',
title: 'Autres',
enabled: true,
children: {
touchUpKits: {
route: 'misc/touch-up-kit',
simpleRoute: 'touch-up-kit',
title: 'Kits de retouche',
permission: Permission.VIEW_TOUCH_UP_KITS
}
}
},
administration: {
route: 'admin',
title: 'Administration',
requiredPermission: Permission.VIEW_USERS,
children: {
users: {route: 'admin/user', simpleRoute: 'user', title: 'Utilisateurs', permission: Permission.VIEW_USERS},
groups: {route: 'admin/group', simpleRoute: 'group', title: 'Groupes', permission: Permission.VIEW_USERS},
groupTokens: {
route: 'admin/grp-tokens',
simpleRoute: 'grp-tokens',
title: 'Ordinateurs',
permission: Permission.ADMIN
}, // Not 'group-tokens' so the navbar doesn't detect 'group' as active
configurations: {
route: 'admin/config',
simpleRoute: 'config',
title: 'Configuration',
permission: Permission.ADMIN
}
}
},
accounts: {
route: 'account'
}
}