From 574df7179c06fb103220f7f953b3343febbaa844 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 26 Mar 2024 15:49:49 +0100 Subject: [PATCH] image delete and upload --- .../src/app/_helpers/formgroup.initializer.ts | 1 - .../new-contact/new-contact.component.html | 4 +- .../src/app/documents/documents.component.ts | 4 +- .../new-document/new-document.component.html | 13 +- .../new-document/new-document.component.ts | 265 +++++++++--------- .../new-partner/new-partner.component.html | 4 +- .../new-product/new-product.component.html | 4 +- 7 files changed, 145 insertions(+), 150 deletions(-) diff --git a/matsen-tool/src/app/_helpers/formgroup.initializer.ts b/matsen-tool/src/app/_helpers/formgroup.initializer.ts index 58ac23c..b308ee1 100644 --- a/matsen-tool/src/app/_helpers/formgroup.initializer.ts +++ b/matsen-tool/src/app/_helpers/formgroup.initializer.ts @@ -2,7 +2,6 @@ import {FormGroup} from "@angular/forms"; export class FormGroupInitializer { public static initFormGroup(formGroup: FormGroup, model: any) { - console.log(model); for (const controlName in formGroup.controls) { if (formGroup.controls.hasOwnProperty(controlName)) { formGroup.patchValue({[controlName]: model[controlName] ?? null}); diff --git a/matsen-tool/src/app/contacts/new-contact/new-contact.component.html b/matsen-tool/src/app/contacts/new-contact/new-contact.component.html index fcb1da4..024649c 100644 --- a/matsen-tool/src/app/contacts/new-contact/new-contact.component.html +++ b/matsen-tool/src/app/contacts/new-contact/new-contact.component.html @@ -41,12 +41,12 @@ -
+
-
+

{{'system.delete-image' | translate}}

diff --git a/matsen-tool/src/app/documents/documents.component.ts b/matsen-tool/src/app/documents/documents.component.ts index 976197e..01e2d2e 100644 --- a/matsen-tool/src/app/documents/documents.component.ts +++ b/matsen-tool/src/app/documents/documents.component.ts @@ -72,7 +72,7 @@ export class DocumentsComponent { ).subscribe( data => { this.documents = data["hydra:member"]; - console.log(this.documents); + //console.log(this.documents); this.documentsDataSource = new MatTableDataSource(this.documents); this.documentsLength = Number(data["hydra:totalItems"]); this.documentsPaginator.length = this.documentsLength; @@ -136,7 +136,7 @@ export class DocumentsComponent { openModalEditDocument(document: DocumentJsonld) { const modalRefDocument = this.modalService.open(NewDocumentComponent, this.modalOptions); modalRefDocument.componentInstance.document = document; - console.log(document); + // console.log(document); modalRefDocument.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefDocument.dismiss(); diff --git a/matsen-tool/src/app/documents/new-document/new-document.component.html b/matsen-tool/src/app/documents/new-document/new-document.component.html index 1d52fd0..a5f5fc2 100644 --- a/matsen-tool/src/app/documents/new-document/new-document.component.html +++ b/matsen-tool/src/app/documents/new-document/new-document.component.html @@ -28,25 +28,18 @@
-
+
-
+

{{ 'system.delete-file' | translate }}

- 1 - selectedFile:{{selectedFile === null}}
- 2 - documentForm.invalid:{{documentForm.invalid}}
- 3 - document.id:{{document.id !== undefined}}
- 4 - documentObject:{{documentForm.get('documentObject')?.value}}
- - diff --git a/matsen-tool/src/app/documents/new-document/new-document.component.ts b/matsen-tool/src/app/documents/new-document/new-document.component.ts index 9d60d23..6ca5d00 100644 --- a/matsen-tool/src/app/documents/new-document/new-document.component.ts +++ b/matsen-tool/src/app/documents/new-document/new-document.component.ts @@ -1,7 +1,7 @@ import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import { - DocumentJsonld, DocumentObjectService, - DocumentService, MediaObjectService, PartnerJsonld, PartnerService, ProductService + DocumentJsonld, DocumentObjectService, + DocumentService, MediaObjectService, PartnerJsonld, PartnerService, ProductService } from "@app/core/api/v1"; import {ModalStatus} from "@app/_helpers/modal.states"; import {FormGroup} from "@angular/forms"; @@ -13,142 +13,145 @@ import {ApiConverter} from "@app/_helpers/api.converter"; import {filter, map} from "rxjs/operators"; @Component({ - selector: 'app-new-document', - templateUrl: './new-document.component.html', - styleUrl: './new-document.component.scss' + selector: 'app-new-document', + templateUrl: './new-document.component.html', + styleUrl: './new-document.component.scss' }) export class NewDocumentComponent implements OnInit { - @Input() public document!: DocumentJsonld; - @Output() public submit: EventEmitter = new EventEmitter(); - - protected documentForm: FormGroup; - protected documentSub: Subscription; - - protected selectedFile: File | null; - protected mediaSub: Subscription; - - protected formatter = (apiData: any) => apiData.name; - - protected searchPartners: OperatorFunction = (text$: Observable) => - text$.pipe( - debounceTime(200), - distinctUntilChanged(), - filter((term) => term.length >= 2), - switchMap((term) => this.fetchPartners(term)), - map((partners) => partners.slice(0, 10)), - ); - - protected searchProducts: OperatorFunction = (text$: Observable) => - text$.pipe( - debounceTime(200), - distinctUntilChanged(), - filter((term) => term.length >= 2), - switchMap((term) => this.fetchProducts(term)), - map((products) => products.slice(0, 10)), - ); - - constructor( - private documentService: DocumentService, - private documentObjectService: DocumentObjectService, - private translateService: TranslateService, - private partnerService: PartnerService, - private productService: ProductService - ) { - this.documentForm = documentForm; - this.documentSub = new Subscription(); - - this.selectedFile = null; - this.mediaSub = new Subscription(); - } - - ngOnInit(): void { - this.documentForm = FormGroupInitializer.initFormGroup(this.documentForm, this.document); - console.log(this.document); - console.log(this.documentForm); - if (this.document.documentUrl === undefined) { - this.documentForm.patchValue({"documentObject": "-"}); - console.log(this.documentForm); + @Input() public document!: DocumentJsonld; + @Output() public submit: EventEmitter = new EventEmitter(); + + protected documentForm: FormGroup; + protected documentSub: Subscription; + + protected selectedFile: File | null; + protected mediaSub: Subscription; + + protected formatter = (apiData: any) => apiData.name; + + constructor( + private documentService: DocumentService, + private documentObjectService: DocumentObjectService, + private translateService: TranslateService, + private partnerService: PartnerService, + private productService: ProductService + ) { + this.documentForm = documentForm; + this.documentSub = new Subscription(); + + this.selectedFile = null; + this.mediaSub = new Subscription(); } - } - - protected fetchPartners(term: string): Observable<{ id: any; name: any }[]> { - return this.partnerService.partnersGetCollection(1, 50, undefined, undefined, term).pipe( - map((response) => response['hydra:member'].map(partner => ({ id: partner.id, name: partner.name }))), - ); - } - - protected fetchProducts(term: string): Observable<{ id: any; name: any }[]> { - return this.productService.productsGetCollection(1, 50, term).pipe( - map((response) => response['hydra:member'].map(product => ({ id: product.id, name: product.name }))), - ); - } - - protected onAssignedToSelectPartner(selectedItem: any): void { - this.documentForm.get('partner')?.setValue(selectedItem.item.id); - } - - protected onAssignedToSelectProduct(selectedItem: any): void { - this.documentForm.get('product')?.setValue(selectedItem.item.id); - } - - onSubmit() { - if (this.selectedFile !== null) { - this.mediaSub = this.documentObjectService.documentObjectsPost( - this.selectedFile - ).subscribe( - data => { - this.documentForm.patchValue({"documentObject": data.id}); - this.submitForm(); - } - ); + + ngOnInit(): void { + this.documentForm = FormGroupInitializer.initFormGroup(this.documentForm, this.document); + console.log(this.documentForm); } - } - - submitForm() { - if (this.documentForm.valid) { - if (this.document.id === null || this.document.id === undefined) { - // Create new product - this.documentSub = this.documentService.documentsPost( - this.documentForm.value as DocumentJsonld - ).subscribe( - data => { - this.documentForm.reset(); - this.submit.emit(ModalStatus.Submitted); - } + + protected searchPartners: OperatorFunction = (text$: Observable) => + text$.pipe( + debounceTime(200), + distinctUntilChanged(), + filter((term) => term.length >= 2), + switchMap((term) => this.fetchPartners(term)), + map((partners) => partners.slice(0, 10)), ); - } else { - // Edit contact - this.documentSub = this.documentService.documentsIdPatch( - ApiConverter.extractId(this.document.id), - this.documentForm.value as DocumentJsonld - ).subscribe( - data => { - this.documentForm.reset(); - this.submit.emit(ModalStatus.Submitted); - } + + protected searchProducts: OperatorFunction = (text$: Observable) => + text$.pipe( + debounceTime(200), + distinctUntilChanged(), + filter((term) => term.length >= 2), + switchMap((term) => this.fetchProducts(term)), + map((products) => products.slice(0, 10)), + ); + + protected fetchPartners(term: string): Observable<{ id: any; name: any }[]> { + return this.partnerService.partnersGetCollection(1, 50, undefined, undefined, term).pipe( + map((response) => response['hydra:member'].map(partner => ({id: partner.id, name: partner.name}))), + ); + } + + protected fetchProducts(term: string): Observable<{ id: any; name: any }[]> { + return this.productService.productsGetCollection(1, 50, term).pipe( + map((response) => response['hydra:member'].map(product => ({id: product.id, name: product.name}))), ); - } } - } - - onFileSelected(event: any) { - console.log(this.documentForm); - const file: File = event.target.files[0]; - if (file) { - this.selectedFile = file; - console.log(this.selectedFile); + + protected onAssignedToSelectPartner(selectedItem: any): void { + this.documentForm.get('partner')?.setValue(selectedItem.item.id); + } + + protected onAssignedToSelectProduct(selectedItem: any): void { + this.documentForm.get('product')?.setValue(selectedItem.item.id); + } + + onSubmit() { + if (this.selectedFile !== null) { + this.mediaSub = this.documentObjectService.documentObjectsPost( + this.selectedFile + ).subscribe( + data => { + this.documentForm.patchValue({"documentObject": data.id}); + this.submitForm(); + } + ); + } else { + this.submitForm(); + } + } + + submitForm() { + if (this.documentForm.valid) { + if (this.document.id === null || this.document.id === undefined) { + // Create new product + this.documentSub = this.documentService.documentsPost( + this.documentForm.value as DocumentJsonld + ).subscribe( + data => { + this.documentForm.reset(); + this.submit.emit(ModalStatus.Submitted); + } + ); + } else { + // Edit contact + this.documentSub = this.documentService.documentsIdPatch( + ApiConverter.extractId(this.document.id), + this.documentForm.value as DocumentJsonld + ).subscribe( + data => { + this.documentForm.reset(); + this.submit.emit(ModalStatus.Submitted); + } + ); + } + } } - } - - onDeleteFile() { - let confirmMessage = ""; - this.translateService.get('system.confirm-delete-file').subscribe((translation: string) => { - confirmMessage = translation; - }); - const userConfirmed = window.confirm(confirmMessage); - if (userConfirmed) { - this.documentForm.patchValue({"documentObject": null}); - this.documentForm.patchValue({"documentUrl": null}); + + onFileSelected(event: any) { + const file: File = event.target.files[0]; + if (file) { + this.selectedFile = file; + // Patch form value to apply validation + this.documentForm.patchValue({"documentObject": "-"}); + } + } + + onDeleteFile() { + let confirmMessage = ""; + this.translateService.get('system.confirm-delete-file').subscribe((translation: string) => { + confirmMessage = translation; + }); + const userConfirmed = window.confirm(confirmMessage); + if (userConfirmed) { + this.documentForm.patchValue({"documentObject": null}); + this.documentForm.patchValue({"documentUrl": null}); + } } - } } diff --git a/matsen-tool/src/app/partners/new-partner/new-partner.component.html b/matsen-tool/src/app/partners/new-partner/new-partner.component.html index 6d2043c..10d8417 100644 --- a/matsen-tool/src/app/partners/new-partner/new-partner.component.html +++ b/matsen-tool/src/app/partners/new-partner/new-partner.component.html @@ -40,12 +40,12 @@
-
+
-
+

{{ 'system.delete-image' | translate }}

diff --git a/matsen-tool/src/app/products/new-product/new-product.component.html b/matsen-tool/src/app/products/new-product/new-product.component.html index d824a8c..4431a13 100644 --- a/matsen-tool/src/app/products/new-product/new-product.component.html +++ b/matsen-tool/src/app/products/new-product/new-product.component.html @@ -18,12 +18,12 @@
-
+
-
+

{{'system.delete-image' | translate}}