|
|
|
@@ -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<ModalStatus> = new EventEmitter<ModalStatus>(); |
|
|
|
|
|
|
|
protected documentForm: FormGroup; |
|
|
|
protected documentSub: Subscription; |
|
|
|
|
|
|
|
protected selectedFile: File | null; |
|
|
|
protected mediaSub: Subscription; |
|
|
|
|
|
|
|
protected formatter = (apiData: any) => apiData.name; |
|
|
|
|
|
|
|
protected searchPartners: OperatorFunction<string, readonly { id: any; name: any }[]> = (text$: Observable<string>) => |
|
|
|
text$.pipe( |
|
|
|
debounceTime(200), |
|
|
|
distinctUntilChanged(), |
|
|
|
filter((term) => term.length >= 2), |
|
|
|
switchMap((term) => this.fetchPartners(term)), |
|
|
|
map((partners) => partners.slice(0, 10)), |
|
|
|
); |
|
|
|
|
|
|
|
protected searchProducts: OperatorFunction<string, readonly { id: any; name: any }[]> = (text$: Observable<string>) => |
|
|
|
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<ModalStatus> = new EventEmitter<ModalStatus>(); |
|
|
|
|
|
|
|
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<string, readonly { |
|
|
|
id: any; |
|
|
|
name: any |
|
|
|
}[]> = (text$: Observable<string>) => |
|
|
|
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<string, readonly { |
|
|
|
id: any; |
|
|
|
name: any |
|
|
|
}[]> = (text$: Observable<string>) => |
|
|
|
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}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |