import {Vue, Component, Prop} from 'vue-property-decorator'; import FileUploadResponse from '@/interfaces/responses/FileUploadResponse'; import {isServerError} from "@/SearchDisplace/helpers"; import { Searcher } from '@/interfaces/Searcher'; import { eventBus } from '@/app'; @Component export default class Home extends Vue { private availableSearchers: Array = []; public uiBlocked = false; public uploading = false; public fileUploaded: boolean = false; public document: File | null = null; public uploadResult: FileUploadResponse = { id: '', file_name: '', }; public error: string = ''; @Prop({default: []}) public searchers!: Searcher[]; public mounted() { eventBus.$on('changeRoute', this.changeRoute); this.availableSearchers = this.searchers; } /** * A method which uploads the files to the server for processing * * @param {any} event The event containing the uploaded files information */ public async uploadFile(event: any): Promise { let file = event.files[0]; return this.uploadNewFile(file); } /** * A method which uploads the files to the server for processing * * @param {File} file The event containing the uploaded files information */ public async uploadNewFile(file: File): Promise { this.uploading = true; this.fileUploaded = false; this.$toast.add({severity: 'info', summary: 'Uploading...', detail: 'Uploading your file...', life: 3000}); try { let response = await this.$api.uploadFile(file); this.fileUploaded = true; this.uploadResult = response; this.document = file; } catch (e) { this.uploading = false; this.fileUploaded = false; if (isServerError(e)) { if (e.response.data.hasOwnProperty('errors')) { const errors = e.response.data.errors; if (errors.hasOwnProperty('file')) { this.error = errors.file[0]; return; } } if (e.response.data.hasOwnProperty('message')) { this.error = e.response.data.message; return; } } this.error = 'There was an error uploading your file. Please try again later.'; } } private onNewSearcher(searcher: Searcher): void { this.availableSearchers.unshift(searcher); } public onError(error: string) { this.error = error; } public changeRoute(url: string) { if (!this.fileUploaded) { window.location.href = url; } } }