|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { action } from '@ember/object'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import { toastNotificationTimeoutOptions } from '../../constants/toast-notification'; |
| 5 | +import { inject as service } from '@ember/service'; |
| 6 | + |
| 7 | +export default class UploadImageComponent extends Component { |
| 8 | + formData; |
| 9 | + @service toast; |
| 10 | + @service router; |
| 11 | + @tracked image; |
| 12 | + @tracked isImageSelected = false; |
| 13 | + @tracked overDropZone = false; |
| 14 | + @tracked isImageUploading = false; |
| 15 | + @tracked imageUploadSuccess = false; |
| 16 | + @tracked statusMessage; |
| 17 | + @tracked imageFileName; |
| 18 | + imageCoordinates = null; |
| 19 | + uploadUrl = this.args.uploadUrl; |
| 20 | + formKeyName = this.args.formKeyName; |
| 21 | + |
| 22 | + @action goBack() { |
| 23 | + this.image = null; |
| 24 | + this.setImageSelected(false); |
| 25 | + } |
| 26 | + @action updateImage(file) { |
| 27 | + this.setStatusMessage(''); |
| 28 | + const reader = new FileReader(); |
| 29 | + |
| 30 | + if (file) { |
| 31 | + this.updateFormData(file, this.formKeyName); |
| 32 | + reader.readAsDataURL(file); |
| 33 | + this.imageFileName = file.name; |
| 34 | + } |
| 35 | + reader.onload = () => { |
| 36 | + const image = reader.result; |
| 37 | + this.image = image; |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + @action setImageCoordinates(data) { |
| 42 | + this.imageCoordinates = data; |
| 43 | + } |
| 44 | + |
| 45 | + @action handleBrowseImage(e) { |
| 46 | + const [file] = e.target.files; |
| 47 | + this.updateImage(file); |
| 48 | + this.setImageSelected(true); |
| 49 | + } |
| 50 | + |
| 51 | + @action handleDrop(e) { |
| 52 | + this.preventDefaults(e); // This is used to prevent opening of image in new tab while drag and drop |
| 53 | + const [file] = e.dataTransfer.files; |
| 54 | + this.updateImage(file); |
| 55 | + this.setImageSelected(true); |
| 56 | + this.setOverDropZone(false); |
| 57 | + } |
| 58 | + @action handleDragOver(e) { |
| 59 | + this.preventDefaults(e); |
| 60 | + this.setOverDropZone(true); |
| 61 | + e.dataTransfer.dropEffect = 'move'; |
| 62 | + } |
| 63 | + @action handleDragEnter(e) { |
| 64 | + this.preventDefaults(e); |
| 65 | + this.setOverDropZone(true); |
| 66 | + } |
| 67 | + @action handleDragLeave(e) { |
| 68 | + this.preventDefaults(e); |
| 69 | + this.setOverDropZone(false); |
| 70 | + } |
| 71 | + |
| 72 | + @action onSubmit(e) { |
| 73 | + this.preventDefaults(e); |
| 74 | + this.formData.set('coordinates', JSON.stringify(this.imageCoordinates)); |
| 75 | + this.uploadImage(this.formData); |
| 76 | + } |
| 77 | + |
| 78 | + uploadImage(data) { |
| 79 | + const url = this.uploadUrl; |
| 80 | + this.setImageUploading(true); |
| 81 | + fetch(`${url}`, { |
| 82 | + method: 'POST', |
| 83 | + credentials: 'include', |
| 84 | + body: data, |
| 85 | + }) |
| 86 | + .then(async (res) => { |
| 87 | + const status = res.status; |
| 88 | + const data = await res.json(); |
| 89 | + const message = data.message; |
| 90 | + this.handleResponseStatusMessage(status, message); |
| 91 | + }) |
| 92 | + .catch((err) => { |
| 93 | + this.setImageUploadSuccess(false); |
| 94 | + this.setStatusMessage( |
| 95 | + 'Error occured, please try again and if the issue still exists contact administrator and create a issue on the repo with logs', |
| 96 | + ); |
| 97 | + console.error(err); |
| 98 | + }) |
| 99 | + .finally(() => { |
| 100 | + this.setImageUploading(false); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + updateFormData(file, key) { |
| 105 | + const formData = new FormData(); |
| 106 | + formData.append(key, file); |
| 107 | + this.formData = formData; |
| 108 | + } |
| 109 | + |
| 110 | + handleResponseStatusMessage(status, message) { |
| 111 | + if (status === 200) { |
| 112 | + this.setImageUploadSuccess(true); |
| 113 | + this.args.outsideClickModel(); |
| 114 | + this.toast.success(message, '', toastNotificationTimeoutOptions); |
| 115 | + } else { |
| 116 | + this.setImageUploadSuccess(false); |
| 117 | + this.setStatusMessage(message); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + preventDefaults(e) { |
| 122 | + e.preventDefault(); |
| 123 | + e.stopPropagation(); |
| 124 | + } |
| 125 | + |
| 126 | + setOverDropZone(bool) { |
| 127 | + this.overDropZone = bool; |
| 128 | + } |
| 129 | + |
| 130 | + setImageSelected(bool) { |
| 131 | + this.isImageSelected = bool; |
| 132 | + } |
| 133 | + |
| 134 | + setImageUploading(bool) { |
| 135 | + this.isImageUploading = bool; |
| 136 | + } |
| 137 | + |
| 138 | + setImageUploadSuccess(bool) { |
| 139 | + this.imageUploadSuccess = bool; |
| 140 | + } |
| 141 | + |
| 142 | + setStatusMessage(message) { |
| 143 | + this.statusMessage = message; |
| 144 | + } |
| 145 | +} |
0 commit comments