Add barcode scanner integration, identifier lookup, and enhanced field mapping for label creation
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import {
|
||||
applyItemUpsert,
|
||||
lookupItemByIdentifier,
|
||||
previewItemUpsert,
|
||||
searchItemDefinitions,
|
||||
} from '../../api/stock.js';
|
||||
import { BrowserMultiFormatReader } from '@zxing/browser';
|
||||
import { mapLookupItemToForm } from './identifier-lookup-mapper.js';
|
||||
import { fetchLocations } from '../../api/locations.js';
|
||||
import {
|
||||
formatPrintErrorMessage,
|
||||
@@ -54,28 +57,75 @@ export function renderLabelCreatePage() {
|
||||
<div class="card border-0 shadow-sm">
|
||||
<div class="card-body p-4">
|
||||
<form class="vstack gap-3" @submit.prevent="create()" autocomplete="off" x-ref="labelForm">
|
||||
<div class="position-relative search-field-with-clear">
|
||||
<label class="form-label">Search item definitions</label>
|
||||
<input class="form-control pe-5" type="text" x-model="form.search" @input="onSearchInput()" placeholder="Search by item name" autocomplete="off" />
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-sm btn-link text-body-secondary clear-field-button search-clear-button"
|
||||
x-show="form.search || form.itemId"
|
||||
@click="clearItemSearch()"
|
||||
aria-label="Clear item search"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
<template x-if="suggestions.length">
|
||||
<div class="list-group shadow-sm position-absolute start-0 end-0 z-3 mt-1 search-suggestions-picker">
|
||||
<template x-for="item in suggestions" :key="item.id">
|
||||
<button class="list-group-item list-group-item-action" type="button" @click="pickSuggestion(item)">
|
||||
<div class="fw-semibold" x-text="item.name"></div>
|
||||
<div class="small text-body-secondary" x-text="item.description || 'Existing item definition'"></div>
|
||||
</button>
|
||||
</template>
|
||||
<div class="row g-3 align-items-start">
|
||||
<div class="col-12 col-md-6 position-relative search-field-with-clear">
|
||||
<label class="form-label">Search item definitions</label>
|
||||
<input class="form-control pe-5" type="text" x-model="form.search" @input="onSearchInput()" placeholder="Search by item name" autocomplete="off" />
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-sm btn-link text-body-secondary clear-field-button search-clear-button"
|
||||
x-show="form.search || form.itemId"
|
||||
@click="clearItemSearch()"
|
||||
aria-label="Clear item search"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
<template x-if="suggestions.length">
|
||||
<div class="list-group shadow-sm position-absolute start-0 end-0 z-3 mt-1 search-suggestions-picker">
|
||||
<template x-for="item in suggestions" :key="item.id">
|
||||
<button class="list-group-item list-group-item-action" type="button" @click="pickSuggestion(item)">
|
||||
<div class="fw-semibold" x-text="item.name"></div>
|
||||
<div class="small text-body-secondary" x-text="item.description || 'Existing item definition'"></div>
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
<div class="col-12 col-md-6">
|
||||
<label class="form-label">Identifier code</label>
|
||||
<div class="input-group">
|
||||
<input
|
||||
class="form-control"
|
||||
type="text"
|
||||
x-model="form.identifierCode"
|
||||
@input="lookupState.error = ''"
|
||||
inputmode="numeric"
|
||||
autocomplete="off"
|
||||
placeholder="EAN / UPC / GTIN"
|
||||
/>
|
||||
<button
|
||||
class="btn btn-outline-primary"
|
||||
type="button"
|
||||
@click="lookupIdentifierDetails()"
|
||||
:disabled="lookupState.isLoading || !hasLookupIdentifierCode()"
|
||||
>
|
||||
<span x-show="!lookupState.isLoading">Lookup</span>
|
||||
<span x-show="lookupState.isLoading">Looking up...</span>
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-outline-secondary"
|
||||
type="button"
|
||||
@click="openScanner()"
|
||||
x-show="scannerState.hasCamera"
|
||||
>
|
||||
Camera
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-outline-secondary"
|
||||
type="button"
|
||||
x-show="form.identifierCode"
|
||||
@click="form.identifierCode = ''"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<div class="form-text">
|
||||
Optional. Scan with camera or enter manually.
|
||||
</div>
|
||||
<template x-if="lookupState.error">
|
||||
<div class="small text-danger mt-1" x-text="lookupState.error"></div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row g-3">
|
||||
@@ -470,6 +520,39 @@ export function renderLabelCreatePage() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="scanner-modal-backdrop"
|
||||
x-show="scannerState.isOpen"
|
||||
@click.self="closeScanner()"
|
||||
@keydown.escape.window="closeScanner()"
|
||||
>
|
||||
<div class="scanner-modal card border-0 shadow-lg">
|
||||
<div class="card-body p-3 p-md-4">
|
||||
<div class="d-flex align-items-center justify-content-between gap-3 mb-3">
|
||||
<div>
|
||||
<h2 class="h5 mb-1">Scan barcode</h2>
|
||||
<p class="text-body-secondary small mb-0">Point your camera at the barcode to fill the identifier field.</p>
|
||||
</div>
|
||||
<button class="btn btn-sm btn-outline-secondary" type="button" @click="closeScanner()">Close</button>
|
||||
</div>
|
||||
|
||||
<div class="scanner-video-shell mb-3">
|
||||
<video class="scanner-video" x-ref="scannerVideo" autoplay muted playsinline></video>
|
||||
</div>
|
||||
|
||||
<div class="d-flex flex-wrap align-items-center gap-2">
|
||||
<div class="small text-body-secondary" x-show="scannerState.isLoading">Starting camera...</div>
|
||||
<div class="small text-success" x-show="scannerState.lastDetectedCode" x-text="'Detected: ' + scannerState.lastDetectedCode"></div>
|
||||
<button class="btn btn-outline-secondary btn-sm" type="button" @click="startScanner()" :disabled="scannerState.isLoading">Retry</button>
|
||||
</div>
|
||||
|
||||
<template x-if="scannerState.error">
|
||||
<div class="alert alert-warning py-2 mt-3 mb-0" x-text="scannerState.error"></div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
`;
|
||||
}
|
||||
@@ -559,6 +642,7 @@ export function labelCreatePageData(store) {
|
||||
return {
|
||||
previewState: createAsyncState(),
|
||||
createState: createAsyncState(),
|
||||
lookupState: createAsyncState(),
|
||||
stockTypeOptions: STOCK_TYPE_OPTIONS,
|
||||
stockLevelOptions: STOCK_LEVEL_OPTIONS,
|
||||
quantityUnitOptions: QUANTITY_UNIT_OPTIONS,
|
||||
@@ -577,10 +661,20 @@ export function labelCreatePageData(store) {
|
||||
upsertPreview: null,
|
||||
printLabelOnSave: true,
|
||||
printIssue: '',
|
||||
scannerReader: null,
|
||||
scannerControls: null,
|
||||
scannerState: {
|
||||
isOpen: false,
|
||||
isLoading: false,
|
||||
hasCamera: false,
|
||||
error: '',
|
||||
lastDetectedCode: '',
|
||||
},
|
||||
form: {
|
||||
...loadLabelDraft(),
|
||||
},
|
||||
async init() {
|
||||
this.scannerState.hasCamera = this.canUseCameraScanner();
|
||||
if (!store.isConnected) {
|
||||
return;
|
||||
}
|
||||
@@ -614,6 +708,243 @@ export function labelCreatePageData(store) {
|
||||
this.suggestions = await searchItemDefinitions(store, this.form.search.trim());
|
||||
}, 250);
|
||||
},
|
||||
destroy() {
|
||||
this.stopScanner();
|
||||
},
|
||||
canUseCameraScanner() {
|
||||
return Boolean(
|
||||
typeof navigator !== 'undefined'
|
||||
&& navigator.mediaDevices
|
||||
&& typeof navigator.mediaDevices.getUserMedia === 'function',
|
||||
);
|
||||
},
|
||||
normalizeIdentifierCode(value) {
|
||||
return String(value || '').replace(/\s+/g, '').trim();
|
||||
},
|
||||
hasLookupIdentifierCode() {
|
||||
return Boolean(this.normalizeIdentifierCode(this.form.identifierCode));
|
||||
},
|
||||
lookupStatusMessage(status, identifierCode) {
|
||||
const normalizedCode = this.normalizeIdentifierCode(identifierCode);
|
||||
|
||||
if (!normalizedCode || status === 'missing_identifier') {
|
||||
return 'Provide an identifier code before lookup.';
|
||||
}
|
||||
|
||||
if (status === 'not_found') {
|
||||
return `No lookup result found for code ${normalizedCode}.`;
|
||||
}
|
||||
|
||||
if (status === 'lookup_failed') {
|
||||
return 'Lookup failed on the server. You can still fill the form manually.';
|
||||
}
|
||||
|
||||
return 'Lookup response could not be applied to this form.';
|
||||
},
|
||||
normalizeScannerError(error) {
|
||||
const message = String(error?.message || '');
|
||||
const normalized = message.toLowerCase();
|
||||
|
||||
if (error?.name === 'NotAllowedError' || normalized.includes('permission')) {
|
||||
return 'Camera access was denied. Allow access to scan, or enter the code manually.';
|
||||
}
|
||||
|
||||
if (error?.name === 'NotFoundError' || normalized.includes('requested device not found')) {
|
||||
return 'No camera was found on this device. Enter the identifier code manually.';
|
||||
}
|
||||
|
||||
if (error?.name === 'NotReadableError' || normalized.includes('could not start video source')) {
|
||||
return 'Camera is busy in another app. Close it there and try scanning again.';
|
||||
}
|
||||
|
||||
return 'Could not start barcode scanning. Enter the identifier code manually.';
|
||||
},
|
||||
async openScanner() {
|
||||
this.scannerState.error = '';
|
||||
this.scannerState.lastDetectedCode = '';
|
||||
this.scannerState.isOpen = true;
|
||||
await this.$nextTick();
|
||||
await this.startScanner();
|
||||
},
|
||||
async startScanner() {
|
||||
this.scannerState.error = '';
|
||||
this.scannerState.lastDetectedCode = '';
|
||||
|
||||
if (!this.canUseCameraScanner()) {
|
||||
this.scannerState.hasCamera = false;
|
||||
this.scannerState.error = 'Camera scanning is not supported in this browser. Enter the identifier code manually.';
|
||||
return;
|
||||
}
|
||||
|
||||
const videoElement = this.$refs.scannerVideo;
|
||||
if (!videoElement) {
|
||||
this.scannerState.error = 'Scanner video element is unavailable. Close and reopen scanner.';
|
||||
return;
|
||||
}
|
||||
|
||||
this.stopScanner();
|
||||
this.scannerState.isLoading = true;
|
||||
|
||||
try {
|
||||
if (!this.scannerReader) {
|
||||
this.scannerReader = new BrowserMultiFormatReader();
|
||||
}
|
||||
|
||||
this.scannerControls = await this.scannerReader.decodeFromConstraints(
|
||||
{
|
||||
audio: false,
|
||||
video: {
|
||||
facingMode: { ideal: 'environment' },
|
||||
},
|
||||
},
|
||||
videoElement,
|
||||
(result, error) => {
|
||||
if (result) {
|
||||
this.onBarcodeDetected(result.getText?.() || '');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!error || error?.name === 'NotFoundException') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.scannerState.error) {
|
||||
this.scannerState.error = this.normalizeScannerError(error);
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
this.scannerState.error = this.normalizeScannerError(error);
|
||||
} finally {
|
||||
this.scannerState.isLoading = false;
|
||||
}
|
||||
},
|
||||
stopScanner() {
|
||||
try {
|
||||
this.scannerControls?.stop?.();
|
||||
} catch {
|
||||
// Ignore cleanup errors when scanner is already stopped.
|
||||
}
|
||||
this.scannerControls = null;
|
||||
|
||||
try {
|
||||
this.scannerReader?.reset?.();
|
||||
} catch {
|
||||
// Ignore cleanup errors from stale reader state.
|
||||
}
|
||||
|
||||
const videoElement = this.$refs.scannerVideo;
|
||||
const stream = videoElement?.srcObject;
|
||||
if (stream && typeof stream.getTracks === 'function') {
|
||||
stream.getTracks().forEach((track) => track.stop());
|
||||
}
|
||||
if (videoElement) {
|
||||
videoElement.srcObject = null;
|
||||
}
|
||||
},
|
||||
closeScanner() {
|
||||
this.stopScanner();
|
||||
this.scannerState.isOpen = false;
|
||||
this.scannerState.isLoading = false;
|
||||
this.scannerState.error = '';
|
||||
},
|
||||
onBarcodeDetected(rawCode) {
|
||||
const code = this.normalizeIdentifierCode(rawCode);
|
||||
if (!code || !this.scannerState.isOpen) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.form.identifierCode = code;
|
||||
this.scannerState.lastDetectedCode = code;
|
||||
this.closeScanner();
|
||||
store.addAlert({
|
||||
type: 'success',
|
||||
message: `Scanned identifier code: ${code}`,
|
||||
});
|
||||
},
|
||||
async lookupIdentifierDetails() {
|
||||
const identifierCode = this.normalizeIdentifierCode(this.form.identifierCode);
|
||||
this.form.identifierCode = identifierCode;
|
||||
this.lookupState.error = '';
|
||||
|
||||
if (!identifierCode) {
|
||||
this.lookupState.error = 'Provide an identifier code before lookup.';
|
||||
return;
|
||||
}
|
||||
|
||||
await runAsyncState(this.lookupState, async () => {
|
||||
const response = await lookupItemByIdentifier(store, identifierCode);
|
||||
if (response.status !== 'ok') {
|
||||
const message = this.lookupStatusMessage(response.status, identifierCode);
|
||||
this.lookupState.error = message;
|
||||
store.addAlert({
|
||||
type: response.status === 'not_found' ? 'info' : 'warning',
|
||||
message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.item || typeof response.item !== 'object') {
|
||||
const message = 'Lookup returned no item payload to apply.';
|
||||
this.lookupState.error = message;
|
||||
store.addAlert({
|
||||
type: 'warning',
|
||||
message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const mapped = mapLookupItemToForm({
|
||||
form: this.form,
|
||||
lookupItem: response.item,
|
||||
locations: this.locations,
|
||||
});
|
||||
|
||||
if (!mapped.didUpdate) {
|
||||
const message = 'Lookup finished, but no compatible fields were returned.';
|
||||
this.lookupState.error = message;
|
||||
store.addAlert({
|
||||
type: 'info',
|
||||
message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.form = {
|
||||
...mapped.form,
|
||||
itemId: '',
|
||||
itemUuidB64: '',
|
||||
};
|
||||
if (mapped.locationSearch !== null) {
|
||||
this.locationSearch = mapped.locationSearch;
|
||||
}
|
||||
this.syncStockTypeState(this.form.stockType);
|
||||
this.syncStockTypeSelect();
|
||||
this.syncStockLevelSelect();
|
||||
this.syncLocationValidity();
|
||||
this.upsertPreview = null;
|
||||
this.previewState.error = '';
|
||||
this.submitError = '';
|
||||
if (this.previewUrl.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(this.previewUrl);
|
||||
}
|
||||
this.previewUrl = '';
|
||||
this.suggestions = [];
|
||||
this.persistDraft();
|
||||
|
||||
const sourceSuffix = response.source ? ` (${response.source})` : '';
|
||||
const cacheSuffix = response.cacheHit ? ', cache hit' : '';
|
||||
store.addAlert({
|
||||
type: 'success',
|
||||
message: `Lookup applied product details${sourceSuffix}${cacheSuffix}.`,
|
||||
});
|
||||
}).catch((error) => {
|
||||
store.addAlert({
|
||||
type: 'warning',
|
||||
message: `Could not complete lookup: ${error.message || 'Unknown lookup error.'}`,
|
||||
});
|
||||
});
|
||||
},
|
||||
async loadLocations() {
|
||||
if (!store.isConnected) {
|
||||
return;
|
||||
@@ -1098,6 +1429,7 @@ export function labelCreatePageData(store) {
|
||||
}).catch(() => {});
|
||||
},
|
||||
reset(revokePreview = true) {
|
||||
this.closeScanner();
|
||||
this.form = createDefaultForm();
|
||||
this.syncStockTypeState(this.form.stockType);
|
||||
this.suggestions = [];
|
||||
@@ -1105,6 +1437,7 @@ export function labelCreatePageData(store) {
|
||||
this.locationPickerOpen = false;
|
||||
this.successMessage = '';
|
||||
this.submitError = '';
|
||||
this.lookupState.error = '';
|
||||
this.fieldErrors = {};
|
||||
this.upsertPreview = null;
|
||||
this.printIssue = '';
|
||||
|
||||
Reference in New Issue
Block a user