|
| 1 | +import { Component, Inject, OnInit } from '@angular/core'; |
| 2 | +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 3 | +import { ToastrService } from 'ngx-toastr'; |
| 4 | +import { ObserversService } from 'src/app/services/observers.service'; |
| 5 | +import { BASE_BUTTON_VARIANTS, Variants } from 'src/app/shared/base-button/base-button.component'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'observer-import', |
| 9 | + templateUrl: './observer-import.component.html', |
| 10 | + styleUrls: ['./observer-import.component.scss'] |
| 11 | +}) |
| 12 | +export class ObserverImportComponent implements OnInit { |
| 13 | + |
| 14 | + formGroup: FormGroup |
| 15 | + |
| 16 | + constructor ( |
| 17 | + private fb: FormBuilder, |
| 18 | + private toastr: ToastrService, |
| 19 | + private observerService: ObserversService, |
| 20 | + @Inject(BASE_BUTTON_VARIANTS) public BaseButtonVariants: typeof Variants |
| 21 | + ) { } |
| 22 | + |
| 23 | + ngOnInit(): void { |
| 24 | + this.formGroup = this.fb.group({ |
| 25 | + ngoId: ['', Validators.required], |
| 26 | + file: ['', Validators.required], |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + onSubmit () { |
| 31 | + const formData = new FormData(); |
| 32 | + formData.append('file', this.formGroup.get('file').value); |
| 33 | + formData.append('ongId', this.formGroup.get('ngoId').value); |
| 34 | + |
| 35 | + this.observerService.uploadCsv(formData).subscribe( |
| 36 | + (res) => { |
| 37 | + this.toastr.success( |
| 38 | + `${res} observers have been added successfully`, |
| 39 | + 'Success' |
| 40 | + ); |
| 41 | + }, |
| 42 | + (err) => { |
| 43 | + this.toastr.error('Encountered error while uploading csv', 'Error'); |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + onFileChange(event) { |
| 49 | + if (event.target.files.length > 0) { |
| 50 | + const file = event.target.files[0]; |
| 51 | + this.formGroup.get('file').setValue(file); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments