Merge pull request 'codex/add-barcode-scan-to-item' (#6) from codex/add-barcode-scan-to-item into main
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
@@ -843,6 +843,9 @@ export function labelCreatePageData(store) {
|
||||
|
||||
this.stopScanner();
|
||||
this.scannerState.isLoading = true;
|
||||
const shouldLogDecodeErrors = import.meta.env.DEV;
|
||||
let lastDecodeErrorName = '';
|
||||
let lastDecodeErrorAt = 0;
|
||||
|
||||
try {
|
||||
if (!this.scannerReader) {
|
||||
@@ -863,12 +866,19 @@ export function labelCreatePageData(store) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!error || error?.name === 'NotFoundException') {
|
||||
return;
|
||||
if (error) {
|
||||
// Continuous decode emits expected per-frame misses/errors before a valid barcode is found.
|
||||
// Keep the modal quiet and only surface startup failures from the outer catch block.
|
||||
if (shouldLogDecodeErrors) {
|
||||
const errorName = String(error?.name || 'UnknownError');
|
||||
const now = Date.now();
|
||||
if (errorName !== lastDecodeErrorName || now - lastDecodeErrorAt > 2000) {
|
||||
console.debug('[scanner] Ignoring frame decode error while scanning:', errorName, error?.message || '');
|
||||
lastDecodeErrorName = errorName;
|
||||
lastDecodeErrorAt = now;
|
||||
}
|
||||
|
||||
if (!this.scannerState.error) {
|
||||
this.scannerState.error = this.normalizeScannerError(error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
patchStockItem,
|
||||
useStockItem,
|
||||
} from '../../api/stock.js';
|
||||
import { BrowserMultiFormatReader } from '@zxing/browser';
|
||||
import { formatPrintErrorMessage, printItemLabel } from '../../api/labels.js';
|
||||
import { fetchLocations } from '../../api/locations.js';
|
||||
import { getRouteContext } from '../../app/router.js';
|
||||
@@ -147,6 +148,7 @@ export function renderStockDetailPage() {
|
||||
class="form-control"
|
||||
type="text"
|
||||
x-model="identifierDraft"
|
||||
@input="identifierState.error = ''"
|
||||
inputmode="numeric"
|
||||
autocomplete="off"
|
||||
placeholder="EAN / UPC / GTIN"
|
||||
@@ -160,8 +162,16 @@ export function renderStockDetailPage() {
|
||||
<span x-show="!identifierState.isLoading">Save identifier</span>
|
||||
<span x-show="identifierState.isLoading">Saving...</span>
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-outline-secondary"
|
||||
type="button"
|
||||
@click="openScanner()"
|
||||
x-show="scannerState.hasCamera"
|
||||
>
|
||||
Camera
|
||||
</button>
|
||||
</div>
|
||||
<div class="form-text">Used for OpenFoodFacts lookups and product metadata refresh.</div>
|
||||
<div class="form-text">Used for product identifier tracking and metadata lookups.</div>
|
||||
<template x-if="identifierState.error">
|
||||
<div class="small text-danger mt-1" x-text="identifierState.error"></div>
|
||||
</template>
|
||||
@@ -200,7 +210,6 @@ export function renderStockDetailPage() {
|
||||
></div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<h3 class="h6 mb-3">Nutrition</h3>
|
||||
<dl class="row mb-0 detail-grid">
|
||||
@@ -356,6 +365,39 @@ export function renderStockDetailPage() {
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<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>
|
||||
`;
|
||||
}
|
||||
@@ -366,6 +408,15 @@ export function stockDetailPageData(store) {
|
||||
adjustmentState: createAsyncState(),
|
||||
printState: createAsyncState(),
|
||||
identifierState: createAsyncState(),
|
||||
scannerReader: null,
|
||||
scannerControls: null,
|
||||
scannerState: {
|
||||
isOpen: false,
|
||||
isLoading: false,
|
||||
hasCamera: false,
|
||||
error: '',
|
||||
lastDetectedCode: '',
|
||||
},
|
||||
lookupDetailsState: createAsyncState(),
|
||||
printFeedback: {
|
||||
type: '',
|
||||
@@ -384,6 +435,7 @@ export function stockDetailPageData(store) {
|
||||
level: 'plenty',
|
||||
},
|
||||
async init() {
|
||||
this.scannerState.hasCamera = this.canUseCameraScanner();
|
||||
if (!store.isConnected) {
|
||||
return;
|
||||
}
|
||||
@@ -404,6 +456,170 @@ export function stockDetailPageData(store) {
|
||||
this.adjustment.level = this.entry?.level || 'plenty';
|
||||
}).catch(() => {});
|
||||
},
|
||||
destroy() {
|
||||
this.stopScanner();
|
||||
},
|
||||
canUseCameraScanner() {
|
||||
return Boolean(
|
||||
typeof navigator !== 'undefined'
|
||||
&& navigator.mediaDevices
|
||||
&& typeof navigator.mediaDevices.getUserMedia === 'function',
|
||||
);
|
||||
},
|
||||
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;
|
||||
const shouldLogDecodeErrors = import.meta.env.DEV;
|
||||
let lastDecodeErrorName = '';
|
||||
let lastDecodeErrorAt = 0;
|
||||
|
||||
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) {
|
||||
// Continuous decode emits expected per-frame misses/errors before a valid barcode is found.
|
||||
// Keep the modal quiet and only surface startup failures from the outer catch block.
|
||||
if (shouldLogDecodeErrors) {
|
||||
const errorName = String(error?.name || 'UnknownError');
|
||||
const now = Date.now();
|
||||
if (errorName !== lastDecodeErrorName || now - lastDecodeErrorAt > 2000) {
|
||||
console.debug('[scanner] Ignoring frame decode error while scanning:', errorName, error?.message || '');
|
||||
lastDecodeErrorName = errorName;
|
||||
lastDecodeErrorAt = now;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
} 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 = normalizeIdentifierCode(rawCode);
|
||||
if (!code || !this.scannerState.isOpen) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.identifierDraft = code;
|
||||
this.scannerState.lastDetectedCode = code;
|
||||
this.closeScanner();
|
||||
store.addAlert({
|
||||
type: 'success',
|
||||
message: `Scanned identifier code: ${code}`,
|
||||
});
|
||||
},
|
||||
async saveIdentifierCode() {
|
||||
if (!this.entry?.uuid_b64) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.identifierState.error = '';
|
||||
await runAsyncState(this.identifierState, async () => {
|
||||
const identifierCode = normalizeIdentifierCode(this.identifierDraft);
|
||||
const updated = await patchStockItem(store, this.entry.uuid_b64, {
|
||||
identifier_code: identifierCode || null,
|
||||
});
|
||||
|
||||
this.entry = updated;
|
||||
this.identifierDraft = normalizeIdentifierCode(updated?.identifier_code || identifierCode);
|
||||
store.addAlert({
|
||||
type: 'success',
|
||||
message: identifierCode
|
||||
? `Identifier code saved for ${this.entry.name}.`
|
||||
: `Identifier code cleared for ${this.entry.name}.`,
|
||||
});
|
||||
}).catch(() => {});
|
||||
},
|
||||
|
||||
normalizedIdentifierDraft() {
|
||||
return normalizeIdentifierCode(this.identifierDraft);
|
||||
},
|
||||
@@ -471,32 +687,6 @@ export function stockDetailPageData(store) {
|
||||
|
||||
return parts.join(' ');
|
||||
},
|
||||
async saveIdentifierCode() {
|
||||
if (!this.entry?.uuid_b64) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.identifierState.error = '';
|
||||
await runAsyncState(this.identifierState, async () => {
|
||||
const identifierCode = this.normalizedIdentifierDraft();
|
||||
const updated = await patchStockItem(store, this.entry.uuid_b64, {
|
||||
identifier_code: identifierCode || null,
|
||||
});
|
||||
|
||||
this.entry = updated;
|
||||
this.identifierDraft = normalizeIdentifierCode(updated?.identifier_code || identifierCode);
|
||||
this.offLookupFeedback = {
|
||||
type: '',
|
||||
message: '',
|
||||
};
|
||||
store.addAlert({
|
||||
type: 'success',
|
||||
message: identifierCode
|
||||
? `Identifier code saved for ${this.entry.name}.`
|
||||
: `Identifier code cleared for ${this.entry.name}.`,
|
||||
});
|
||||
}).catch(() => {});
|
||||
},
|
||||
async runItemLookup(update) {
|
||||
if (!this.entry?.uuid_b64) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user