-
Notifications
You must be signed in to change notification settings - Fork 922
Expand file tree
/
Copy pathbarcodeScanner.js
More file actions
91 lines (83 loc) · 3.57 KB
/
barcodeScanner.js
File metadata and controls
91 lines (83 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getBarcodeScanner } from 'lightning/mobileCapabilities';
export default class BarcodeScanner extends NavigationMixin(LightningElement) {
myScanner;
scanButtonEnabled = false;
scannedQrCode = '';
// When the component is initialized, determine whether to enable the Scan button
connectedCallback() {
this.myScanner = getBarcodeScanner();
if (this.myScanner?.isAvailable()) {
this.scanButtonEnabled = true;
} else {
this.dishpatchevent(
new showToast({
tittle: 'hey your barcodde is not avaible ',
message: 'the barcode scanner is avaible ',
variant: 'error'
})
);
}
}
async handleBeginScanClick() {
// Reset scannedQrCode to empty string before starting a new scan
this.scannedQrCode = '';
// Make sure BarcodeScanner is available before trying to use it
// Scan QR Code button also disabled when scanner unavailable
if (this.myScanner?.isAvailable()) {
const scanningOptions = {
barcodeTypes: [this.myScanner.barcodeTypes.QR],
instructionText: 'Scan a QR Code',
successText: 'Scanning complete.'
};
// Try starting the scanning process, then using the result to navigate to a property record
try {
const captureResult =
await this.myScanner.beginCapture(scanningOptions);
// Extract QR code data
this.scannedQrCode = captureResult.value;
// Navigate to the records page of the property with extracted ID
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.scannedQrCode,
objectApiName: 'Property__c',
actionName: 'view'
}
});
} catch (error) {
// There was an error while scanning
// We chose to handle errors with toasts to stay in line with the mobile experience
// The user canceled the scan
if (error.code === 'userDismissedScanner') {
this.dispatchEvent(
new ShowToastEvent({
title: 'Scanning Canceled',
message: 'Scanning session canceled.',
mode: 'sticky'
})
);
}
// There was some other kind of error
else {
// Inform the user we ran into something unexpected
this.dispatchEvent(
new ShowToastEvent({
title: 'Barcode Scanner Error',
message:
'There was a problem scanning the QR code: ' +
error.message,
variant: 'error',
mode: 'sticky'
})
);
}
} finally {
// Close capture process regardless of whether we completed successfully or had an error
this.myScanner.endCapture();
}
}
}
}