From 4910bf65ce430158d469b05952ec32fa7ff491b4 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 15 Mar 2021 18:20:27 -0400 Subject: [PATCH] Ajout de messages d'erreur --- .../mix-editor/mix-editor.component.ts | 15 +- .../company/pages/edit/edit.component.ts | 8 +- .../pages/edit/edit.component.ts | 12 +- .../material/pages/edit/edit.component.ts | 202 +++++++++--------- 4 files changed, 132 insertions(+), 105 deletions(-) diff --git a/src/app/modules/colors/components/mix-editor/mix-editor.component.ts b/src/app/modules/colors/components/mix-editor/mix-editor.component.ts index 6420169..cab63ae 100644 --- a/src/app/modules/colors/components/mix-editor/mix-editor.component.ts +++ b/src/app/modules/colors/components/mix-editor/mix-editor.component.ts @@ -1,6 +1,6 @@ import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core' import {Mix, MixMaterial, Recipe} from '../../../shared/model/recipe.model' -import {SubscribingComponent} from '../../../shared/components/subscribing.component' +import {ErrorHandlingComponent} from '../../../shared/components/subscribing.component' import {MixService} from '../../services/mix.service' import {Observable} from 'rxjs' import {RecipeService} from '../../services/recipe.service' @@ -23,7 +23,7 @@ import {MatSelect} from '@angular/material/select' templateUrl: './mix-editor.component.html', styleUrls: ['./mix-editor.component.sass'] }) -export class MixEditorComponent extends SubscribingComponent { +export class MixEditorComponent extends ErrorHandlingComponent { @ViewChild('mixTable') mixTable: MatTable @ViewChild('deleteConfirmBox') deleteConfirmBox: ConfirmBoxComponent @@ -47,6 +47,16 @@ export class MixEditorComponent extends SubscribingComponent { hoveredMixMaterial: MixMaterial | null columns = ['position', 'material', 'quantity', 'units', 'buttonRemove'] + deleting = false + handledErrorModels = [{ + filter: error => error.status === 409 && !this.deleting, + messageProducer: error => `Un mélange avec le nom '${error.id}' existe déjà dans cette recette` + }, { + filter: error => error.error.status === 409 && this.deleting, + consumer: () => this.deleting = false, + messageProducer: () => 'Ce mélange est utilisé par un ou plusieurs autres mélanges' + }] + constructor( private mixService: MixService, private recipeService: RecipeService, @@ -116,6 +126,7 @@ export class MixEditorComponent extends SubscribingComponent { } delete() { + this.deleting = true this.subscribeAndNavigate(this.mixService.delete(this.mixId), `/color/edit/${this.recipeId}`) } diff --git a/src/app/modules/company/pages/edit/edit.component.ts b/src/app/modules/company/pages/edit/edit.component.ts index 2ea1b35..5037b3e 100644 --- a/src/app/modules/company/pages/edit/edit.component.ts +++ b/src/app/modules/company/pages/edit/edit.component.ts @@ -2,7 +2,6 @@ import {Component} from '@angular/core' import {ErrorHandlingComponent} from '../../../shared/components/subscribing.component' import {Company} from '../../../shared/model/company.model' import {FormField} from '../../../shared/components/entity-add/entity-add.component' -import {Validators} from '@angular/forms' import {CompanyService} from '../../service/company.service' import {ActivatedRoute, Router} from '@angular/router' import {ErrorModel, ErrorService} from '../../../shared/service/error.service' @@ -27,9 +26,13 @@ export class EditComponent extends ErrorHandlingComponent { } ] + deleting = false handledErrorModels: ErrorModel[] = [{ - filter: error => error.status === 409, + filter: error => error.status === 409 && !this.deleting, messageProducer: error => `Une bannière avec le nom '${error.error.id}' existe déjà` + }, { + filter: error => error.status === 409 && this.deleting, + messageProducer: () => 'Cette bannière est utilisée par une ou plusieurs recettes' }] constructor( @@ -61,6 +64,7 @@ export class EditComponent extends ErrorHandlingComponent { } delete() { + this.deleting = true this.subscribeAndNavigate( this.companyService.delete(this.company.id), '/catalog/company/list' diff --git a/src/app/modules/material-type/pages/edit/edit.component.ts b/src/app/modules/material-type/pages/edit/edit.component.ts index 96a642c..73e8a69 100644 --- a/src/app/modules/material-type/pages/edit/edit.component.ts +++ b/src/app/modules/material-type/pages/edit/edit.component.ts @@ -41,14 +41,19 @@ export class EditComponent extends ErrorHandlingComponent { ] } ] - submittedValues: any | null + deleting = false + submittedValues: any | null handledErrorModels: ErrorModel[] = [{ - filter: error => error.status === 409 && error.error.id === this.submittedValues.name, + filter: error => error.status === 409 && !this.deleting && error.error.id === this.submittedValues.name, messageProducer: error => `Un type de produit avec le nom '${error.error.id}' existe déjà` }, { - filter: error => error.status === 409 && error.error.id === this.submittedValues.prefix, + filter: error => error.status === 409 && !this.deleting && error.error.id === this.submittedValues.prefix, messageProducer: error => `Un type de produit avec le préfixe '${error.error.id}' existe déjà` + }, { + filter: error => error.status === 409 && this.deleting, + consumer: () => this.deleting = true, + messageProducer: () => 'Ce type de produit est utilisé dans une ou plusieurs recettes ou produits' }] constructor( @@ -81,6 +86,7 @@ export class EditComponent extends ErrorHandlingComponent { } delete() { + this.deleting = true this.subscribeAndNavigate( this.materialTypeService.delete(this.materialType.id), '/catalog/materialtype/list' diff --git a/src/app/modules/material/pages/edit/edit.component.ts b/src/app/modules/material/pages/edit/edit.component.ts index c8078aa..236075d 100644 --- a/src/app/modules/material/pages/edit/edit.component.ts +++ b/src/app/modules/material/pages/edit/edit.component.ts @@ -11,113 +11,119 @@ import {environment} from '../../../../../environments/environment' import {ErrorModel, ErrorService} from '../../../shared/service/error.service' @Component({ - selector: 'cre-edit', - templateUrl: './edit.component.html', - styleUrls: ['./edit.component.sass'] + selector: 'cre-edit', + templateUrl: './edit.component.html', + styleUrls: ['./edit.component.sass'] }) export class EditComponent extends ErrorHandlingComponent { - @ViewChild('simdutTemplate', {static: true}) simdutTemplateRef + @ViewChild('simdutTemplate', {static: true}) simdutTemplateRef - material: Material | null - formFields: FormField[] = [ - { - name: 'name', - label: 'Code', - icon: 'form-textbox', - type: 'text', - required: true, - errorMessages: [ - {conditionFn: (errors) => errors.required, message: 'Un code est requis'} - ] - }, - { - name: 'inventoryQuantity', - label: 'Quantité en inventaire', - icon: 'beaker-outline', - type: 'number', - required: true, - validator: Validators.min(0), - errorMessages: [ - {conditionFn: errors => errors.required, message: 'Une quantité en inventaire est requise'}, - {conditionFn: errors => errors.min, message: 'La quantité doit être supérieure ou égale à 0'} - ], - step: '0.01' - }, - { - name: 'materialType', - label: 'Type de produit', - icon: 'shape-outline', - type: 'select', - required: true, - errorMessages: [ - {conditionFn: errors => errors.required, message: 'Un type de produit est requis'} - ], - valueFn: material => material.materialType.id, - options$: this.materialTypeService.all.pipe(map(types => types.map(t => { - return {value: t.id, label: t.name} - }))) - }, - { - name: 'simdutFile', - label: 'Fiche signalitique', - icon: 'file-outline', - type: 'file', - fileType: 'application/pdf' + material: Material | null + formFields: FormField[] = [ + { + name: 'name', + label: 'Code', + icon: 'form-textbox', + type: 'text', + required: true, + errorMessages: [ + {conditionFn: (errors) => errors.required, message: 'Un code est requis'} + ] + }, + { + name: 'inventoryQuantity', + label: 'Quantité en inventaire', + icon: 'beaker-outline', + type: 'number', + required: true, + validator: Validators.min(0), + errorMessages: [ + {conditionFn: errors => errors.required, message: 'Une quantité en inventaire est requise'}, + {conditionFn: errors => errors.min, message: 'La quantité doit être supérieure ou égale à 0'} + ], + step: '0.01' + }, + { + name: 'materialType', + label: 'Type de produit', + icon: 'shape-outline', + type: 'select', + required: true, + errorMessages: [ + {conditionFn: errors => errors.required, message: 'Un type de produit est requis'} + ], + valueFn: material => material.materialType.id, + options$: this.materialTypeService.all.pipe(map(types => types.map(t => { + return {value: t.id, label: t.name} + }))) + }, + { + name: 'simdutFile', + label: 'Fiche signalitique', + icon: 'file-outline', + type: 'file', + fileType: 'application/pdf' + } + ] + hasSimdut = false + selectedSimdutFile: File | null + + deleting = false + handledErrorModels: ErrorModel[] = [{ + filter: error => error.status === 409 && !this.deleting, + messageProducer: error => `Un produit avec le nom '${error.error.id}' existe déjà` + }, { + filter: error => error.status === 409 && this.deleting, + consumer: () => this.deleting = false, + messageProducer: () => `Ce produit est utilisé dans une plusieurs recettes` + }] + + constructor( + private materialService: MaterialService, + private materialTypeService: MaterialTypeService, + errorService: ErrorService, + router: Router, + activatedRoute: ActivatedRoute + ) { + super(errorService, activatedRoute, router) } - ] - hasSimdut = false - selectedSimdutFile: File | null - handledErrorModels: ErrorModel[] = [{ - filter: error => error.status === 409, - messageProducer: error => `Un produit avec le nom '${error.error.id}' existe déjà` - }] + ngOnInit() { + super.ngOnInit() - constructor( - private materialService: MaterialService, - private materialTypeService: MaterialTypeService, - errorService: ErrorService, - router: Router, - activatedRoute: ActivatedRoute - ) { - super(errorService, activatedRoute, router) - } + this.formFields[3].template = this.simdutTemplateRef - ngOnInit() { - super.ngOnInit() + const id = parseInt(this.activatedRoute.snapshot.paramMap.get('id')) + this.subscribeEntityById( + this.materialService, + id, + material => this.material = material, + '/catalog/material/list' + ) - this.formFields[3].template = this.simdutTemplateRef + this.subscribe( + this.materialService.hasSimdut(id), + b => this.hasSimdut = b + ) + } - const id = parseInt(this.activatedRoute.snapshot.paramMap.get('id')) - this.subscribeEntityById( - this.materialService, - id, - material => this.material = material, - '/catalog/material/list' - ) + submit(values) { + this.subscribeAndNavigate( + this.materialService.update(this.material.id, values.name, values.inventoryQuantity, values.materialType, this.selectedSimdutFile), + '/catalog/material/list' + ) + } - this.subscribe( - this.materialService.hasSimdut(id), - b => this.hasSimdut = b - ) - } + delete() { + this.deleting = true + this.subscribeAndNavigate( + this.materialService.delete(this.material.id), + '/catalog/material/list' + ) + } - submit(values) { - this.subscribeAndNavigate( - this.materialService.update(this.material.id, values.name, values.inventoryQuantity, values.materialType, this.selectedSimdutFile), - '/catalog/material/list' - ) - } - - delete() { - this.subscribeAndNavigate( - this.materialService.delete(this.material.id), - '/catalog/material/list' - ) - } - - openSimdutUrl() { - const simdutUrl = environment.apiUrl + `/material/${this.material.id}/simdut` - window.open(simdutUrl, '_blank') - } + openSimdutUrl() { + const simdutUrl = environment.apiUrl + `/material/${this.material.id}/simdut` + window.open(simdutUrl, '_blank') + } }