import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { AddressesServiceProxy, AddressDto } from '@shared/service-proxies/service-proxies'; import { NotifyService } from '@abp/notify/notify.service'; import { AppComponentBase } from '@shared/common/app-component-base'; import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies'; import { CreateOrEditAddressModalComponent } from './create-or-edit-address-modal.component'; import { ViewAddressModalComponent } from './view-address-modal.component'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { Table } from 'primeng/components/table/table'; import { Paginator } from 'primeng/components/paginator/paginator'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { FileDownloadService } from '@shared/utils/file-download.service'; import * as _ from 'lodash'; import * as moment from 'moment'; import { HttpClient } from '@angular/common/http'; import { AppConsts } from '@shared/AppConsts'; import { finalize } from 'rxjs/operators'; import { FileUpload } from 'primeng/fileupload'; import { formatDate } from '@angular/common'; @Component({ templateUrl: './addresses.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], styleUrls: ['./addresses.component.less'] }) export class AddressesComponent extends AppComponentBase { @ViewChild('createOrEditAddressModal', { static: true }) createOrEditAddressModal: CreateOrEditAddressModalComponent; @ViewChild('viewAddressModalComponent', { static: true }) viewAddressModal: ViewAddressModalComponent; @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; @ViewChild('ExcelFileUpload', { static: true }) excelFileUpload: FileUpload; uploadUrl: string; advancedFiltersAreShown = false; filterText = ''; addressLine1Filter = ''; addressLine2Filter = ''; postalCodeValueFilter = ''; countryFilter = ''; stateFilter = ''; cityFilter = ''; geocodeLatitudeFilter = ''; geocodeLongitudeFilter = ''; addressTypeNameFilter = ''; constructor( injector: Injector, private _addressesServiceProxy: AddressesServiceProxy, private _notifyService: NotifyService, private _tokenAuth: TokenAuthServiceProxy, private _activatedRoute: ActivatedRoute, private _fileDownloadService: FileDownloadService, private _httpClient: HttpClient, ) { super(injector); this.uploadUrl = AppConsts.remoteServiceBaseUrl + '/Import/ImportFromExcel'; } getAddresses(event?: LazyLoadEvent) { if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } this.spinnerService.show(); this._addressesServiceProxy.getAll( this.filterText, this.addressLine1Filter, this.addressLine2Filter, this.postalCodeValueFilter, this.countryFilter, this.stateFilter, this.cityFilter, this.geocodeLatitudeFilter, this.geocodeLongitudeFilter, this.addressTypeNameFilter, this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getSkipCount(this.paginator, event), this.primengTableHelper.getMaxResultCount(this.paginator, event) ).subscribe(result => { this.primengTableHelper.totalRecordsCount = result.totalCount; this.primengTableHelper.records = result.items; this.spinnerService.hide(); }); } reloadPage(): void { this.paginator.changePage(this.paginator.getPage()); } createAddress(): void { this.createOrEditAddressModal.show(); } deleteAddress(address: AddressDto): void { this.message.confirm( '', (isConfirmed) => { if (isConfirmed) { this._addressesServiceProxy.delete(address.id) .subscribe(() => { this.reloadPage(); this.notify.success(this.l('SuccessfullyDeleted')); }); } } ); } exportToExcel(): void { this._addressesServiceProxy.getAddressesToExcel( this.filterText, this.addressLine1Filter, this.addressLine2Filter, this.postalCodeValueFilter, this.geocodeLatitudeFilter, this.addressTypeNameFilter, ) .subscribe(result => { this._fileDownloadService.downloadTempFile(result); }); } uploadExcel(data: { files: File }): void { const formData: FormData = new FormData(); const file = data.files[0]; formData.append('file', file, file.name); formData.append('addresses','addresses'); // formData.append('entity','addresses','addresses'); this._httpClient .post(this.uploadUrl, formData) .pipe(finalize(() => this.excelFileUpload.clear())) .subscribe(response => { if (response.success) { this.notify.success(this.l('ImportAddressProcessStart')); } else if (response.error != null) { this.notify.error(this.l('ImportAddressUploadFailed')); } }); } onUploadExcelError(): void { this.notify.error(this.l('ImportAddressUploadFailed')); } }