Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import {Vue, Component, Prop} from 'vue-property-decorator';
  2. import FileUploadResponse from '@/interfaces/responses/FileUploadResponse';
  3. import {isServerError} from "@/SearchDisplace/helpers";
  4. import { Searcher } from '@/interfaces/Searcher';
  5. import { eventBus } from '@/app';
  6. @Component
  7. export default class Home extends Vue {
  8. @Prop({default: []})
  9. public readonly searchers!: Searcher[];
  10. public uiBlocked = false;
  11. public uploading = false;
  12. public fileUploaded: boolean = false;
  13. public uploadResult: FileUploadResponse = {
  14. id: '',
  15. file_name: '',
  16. };
  17. public error: string = '';
  18. public mounted()
  19. {
  20. eventBus.$on('changeRoute', this.changeRoute);
  21. }
  22. /**
  23. * A method which uploads the files to the server for processing
  24. *
  25. * @param {any} event The event containing the uploaded files information
  26. */
  27. public async uploadFile(event: any): Promise<void> {
  28. let file = event.files[0];
  29. return this.uploadNewFile(file);
  30. }
  31. /**
  32. * A method which uploads the files to the server for processing
  33. *
  34. * @param {File} file The event containing the uploaded files information
  35. */
  36. public async uploadNewFile(file: File): Promise<void> {
  37. this.uploading = true;
  38. this.fileUploaded = false;
  39. this.$toast.add({severity: 'info', summary: 'Uploading...', detail: 'Uploading your file...', life: 3000});
  40. try {
  41. let response = await this.$api.uploadFile(file);
  42. this.fileUploaded = true;
  43. this.uploadResult = response;
  44. } catch (e) {
  45. this.uploading = false;
  46. this.fileUploaded = false;
  47. if (isServerError(e)) {
  48. if (e.response.data.hasOwnProperty('errors')) {
  49. const errors = e.response.data.errors;
  50. if (errors.hasOwnProperty('file')) {
  51. this.error = errors.file[0];
  52. return;
  53. }
  54. }
  55. if (e.response.data.hasOwnProperty('message')) {
  56. this.error = e.response.data.message;
  57. return;
  58. }
  59. }
  60. this.error = 'There was an error uploading your file. Please try again later.';
  61. }
  62. }
  63. public onError(error: string) {
  64. this.error = error;
  65. }
  66. public changeRoute(url: string) {
  67. if (!this.fileUploaded) {
  68. window.location.href = url;
  69. }
  70. }
  71. }