Change to injected callback pattern, from event pattern.

This commit is contained in:
Chloe Herd 2023-03-21 17:42:54 -04:00
parent 8085759892
commit ec37b93e31
2 changed files with 42 additions and 31 deletions

View file

@ -10,7 +10,7 @@
class="input-wrapper" class="input-wrapper"
:class="[ :class="[
includeSearchIcon ? 'has-search-icon' : '', includeSearchIcon ? 'has-search-icon' : '',
includeCameraIcon ? 'has-camera-icon' : '', includeImageQuestion ? 'has-camera-icon' : '',
]"> ]">
<input <input
class="form-control" class="form-control"
@ -37,7 +37,9 @@
@focus="$emit('focus', $event.target.value)" /> @focus="$emit('focus', $event.target.value)" />
<button v-if="includeSearchIcon" type="submit" aria-label="Search button" /> <button v-if="includeSearchIcon" type="submit" aria-label="Search button" />
<template v-if="!isDisabled"> <template v-if="!isDisabled">
<label class="camera-icon-input" v-show="includeCameraIcon && !isImageLoading"> <label
class="camera-icon-input"
v-show="includeImageQuestion && !isImageProcessing">
<input <input
type="file" type="file"
id="vin-input" id="vin-input"
@ -47,7 +49,7 @@
@change="imageChanged" /> @change="imageChanged" />
</label> </label>
<loader <loader
v-show="includeCameraIcon && isImageLoading" v-show="includeImageQuestion && isImageProcessing"
loaderColor="blue" loaderColor="blue"
class="loading-icon"></loader> class="loading-icon"></loader>
</template> </template>
@ -92,13 +94,14 @@ export default {
questionAlignment: String, // Left or center. Left is default. questionAlignment: String, // Left or center. Left is default.
cornerStyle: String, // Rounded or square. Square is default. cornerStyle: String, // Rounded or square. Square is default.
includeSearchIcon: Boolean, includeSearchIcon: Boolean,
includeCameraIcon: Boolean, includeImageQuestion: Boolean,
isImageLoading: Boolean, onImageQuestionSubmit: Function,
}, },
setup(props) { setup(props) {
const propsClone = Object.assign({}, props); const propsClone = Object.assign({}, props);
const modelValue = propsClone.modelValue; const modelValue = propsClone.modelValue;
let initialValue; let initialValue;
let isImageProcessing = ref(false);
switch (typeof modelValue) { switch (typeof modelValue) {
case "number": case "number":
@ -128,12 +131,26 @@ export default {
validate, validate,
meta, meta,
errors, errors,
isImageProcessing,
}; };
}, },
methods: { methods: {
imageChanged(e) { async imageChanged(e) {
this.$emit("fileChanged", e.target.files[0]); this.isImageProcessing = true;
e.target.value = null;
await this.onImageQuestionSubmit(e.target.files[0])
.then(async (response) => {
this.value = response;
await this.$nextTick();
document.getElementById(this.inputId).dispatchEvent(new Event("change"));
})
.catch((error) => {
this.$emit("imageLookupError");
});
this.isImageProcessing = false;
}, },
}, },
computed: { computed: {

View file

@ -19,10 +19,10 @@
validationRules="vin-required|vin-format" validationRules="vin-required|vin-format"
:isDisabled="vinPopulatedOnPageLoad" :isDisabled="vinPopulatedOnPageLoad"
maxLength="17" maxLength="17"
includeCameraIcon
@file-changed="submitImage"
:mask="vinMask" :mask="vinMask"
:isImageLoading="isVinLoading" includeImageQuestion
:onImageQuestionSubmit="getVinFromImage"
@image-lookup-error="displayVinScanAlert"
data-test="vin-lookup-field" data-test="vin-lookup-field"
ref="vinLookupQuestion" /> ref="vinLookupQuestion" />
</div> </div>
@ -398,26 +398,20 @@ export default {
displayVinScanAlert() { displayVinScanAlert() {
this.displayVinScanFailedAlert = true; this.displayVinScanFailedAlert = true;
}, },
async submitImage(e) { getVinFromImage(image) {
this.isVinLoading = true; return new Promise((resolve, reject) => {
await this.dispatchStoreAction(storeActions.LOOKUP_VIN_BY_IMAGE, e) this.dispatchStoreAction(storeActions.LOOKUP_VIN_BY_IMAGE, image)
.then(async (response) => { .then((response) => {
if (response.data.length > 0) { if (response.data.length > 0) {
const result = response.data[0]; resolve(response.data[0]);
} else {
this.vin = result; reject();
}
this.$refs.vinLookupQuestion.handleChange(result); })
.catch(() => {
this.resetAlerts(); reject();
} else { });
this.displayVinScanAlert(); });
}
})
.catch((error) => {
this.displayVinScanAlert();
});
this.isVinLoading = false;
}, },
resetAlerts() { resetAlerts() {
this.displayMatchedDifferentVehicleAlert = false; this.displayMatchedDifferentVehicleAlert = false;