mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.0 KiB
130 lines
4.0 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import type { SelectProps } from 'ant-design-vue'
|
||
|
import { ref } from 'vue'
|
||
2 years ago
|
import { StreamBarcodeReader } from 'vue-barcode-reader'
|
||
2 years ago
|
import { NOCO } from '#imports'
|
||
2 years ago
|
import QrCodeScan from '~icons/mdi/qrcode-scan'
|
||
2 years ago
|
|
||
|
const meta = inject(MetaInj, ref())
|
||
|
|
||
2 years ago
|
const { t } = useI18n()
|
||
2 years ago
|
const route = useRoute()
|
||
|
const router = useRouter()
|
||
|
|
||
2 years ago
|
const { $api } = useNuxtApp()
|
||
|
const { project } = useProject()
|
||
|
|
||
2 years ago
|
const { isMobileMode } = useGlobal()
|
||
|
|
||
2 years ago
|
const view = inject(ActiveViewInj, ref())
|
||
|
|
||
2 years ago
|
const codeFieldOptions = computed<SelectProps['options']>(
|
||
|
() =>
|
||
|
meta?.value?.columns!.map((field) => {
|
||
|
return {
|
||
|
value: field.id,
|
||
|
label: field.title,
|
||
|
}
|
||
|
}) || [],
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const showCodeScannerOverlay = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const selectedCodeColumnIdToScanFor = ref('')
|
||
2 years ago
|
const lastScannedCode = ref('')
|
||
|
|
||
|
const scannerIsReady = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const onLoaded = async () => {
|
||
|
scannerIsReady.value = true
|
||
|
}
|
||
|
|
||
|
const showScannerField = computed(() => scannerIsReady.value && selectedCodeColumnIdToScanFor.value !== '')
|
||
|
const showPleaseSelectColumnMessage = computed(() => !selectedCodeColumnIdToScanFor.value)
|
||
|
const showScannerIsLoadingMessage = computed(() => !!selectedCodeColumnIdToScanFor.value && !scannerIsReady.value)
|
||
|
|
||
|
const onDecode = async (codeValue: string) => {
|
||
|
if (!showScannerField.value || codeValue === lastScannedCode.value) {
|
||
|
return
|
||
|
}
|
||
2 years ago
|
try {
|
||
2 years ago
|
const nameOfSelectedColumnToScanFor = meta.value?.columns?.find(
|
||
|
(column) => column.id === selectedCodeColumnIdToScanFor.value,
|
||
|
)?.title
|
||
2 years ago
|
const whereClause = `(${nameOfSelectedColumnToScanFor},eq,${codeValue})`
|
||
|
const foundRowsForCode = (
|
||
2 years ago
|
await $api.dbViewRow.list(NOCO, project.value!.id!, meta.value!.id!, view.value!.title!, {
|
||
|
where: whereClause,
|
||
|
})
|
||
|
).list
|
||
|
|
||
2 years ago
|
if (foundRowsForCode.length !== 1) {
|
||
|
showCodeScannerOverlay.value = true
|
||
|
lastScannedCode.value = codeValue
|
||
|
setTimeout(() => {
|
||
|
lastScannedCode.value = ''
|
||
|
}, 4000)
|
||
|
if (foundRowsForCode.length === 0) {
|
||
|
// extract into localisation file
|
||
2 years ago
|
message.info(t('msg.info.codeScanner.noRowFoundForCode'))
|
||
2 years ago
|
}
|
||
|
if (foundRowsForCode.length > 1) {
|
||
|
// extract into localisation file
|
||
2 years ago
|
message.warn(t('msg.info.codeScanner.moreThanOneRowFoundForCode'))
|
||
2 years ago
|
}
|
||
2 years ago
|
return
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
showCodeScannerOverlay.value = false
|
||
|
lastScannedCode.value = ''
|
||
|
const primaryKeyValueForFoundRow = extractPkFromRow(foundRowsForCode[0], meta!.value!.columns!)
|
||
2 years ago
|
|
||
2 years ago
|
router.push({
|
||
|
query: {
|
||
|
...route.query,
|
||
|
rowId: primaryKeyValueForFoundRow,
|
||
|
},
|
||
|
})
|
||
2 years ago
|
} catch (error) {
|
||
|
console.error(error)
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div>
|
||
2 years ago
|
<a-button class="nc-btn-find-row-by-scan nc-toolbar-btn" @click="showCodeScannerOverlay = true">
|
||
2 years ago
|
<div class="flex items-center gap-1">
|
||
|
<QrCodeScan />
|
||
2 years ago
|
<span v-if="!isMobileMode" class="!text-xs font-weight-normal"> {{ $t('activity.scanCode') }}</span>
|
||
2 years ago
|
</div>
|
||
|
</a-button>
|
||
2 years ago
|
<a-modal
|
||
2 years ago
|
v-model:visible="showCodeScannerOverlay"
|
||
2 years ago
|
:closable="false"
|
||
|
width="28rem"
|
||
|
centered
|
||
|
:footer="null"
|
||
|
wrap-class-name="nc-modal-generate-token"
|
||
2 years ago
|
destroy-on-close
|
||
2 years ago
|
@cancel="scannerIsReady = false"
|
||
2 years ago
|
>
|
||
|
<div class="relative flex flex-col h-full">
|
||
2 years ago
|
<a-form-item :label="$t('labels.columnToScanFor')">
|
||
2 years ago
|
<a-select v-model:value="selectedCodeColumnIdToScanFor" class="w-full" :options="codeFieldOptions" />
|
||
2 years ago
|
</a-form-item>
|
||
2 years ago
|
|
||
2 years ago
|
<div>
|
||
2 years ago
|
<StreamBarcodeReader v-show="showScannerField" @decode="onDecode" @loaded="onLoaded"></StreamBarcodeReader>
|
||
|
<div v-if="showPleaseSelectColumnMessage" class="text-left text-wrap mt-2 text-[#e65100] text-xs">
|
||
2 years ago
|
{{ $t('msg.info.codeScanner.selectColumn') }}
|
||
2 years ago
|
</div>
|
||
|
<div v-if="showScannerIsLoadingMessage" class="text-left text-wrap mt-2 text-[#e65100] text-xs">
|
||
2 years ago
|
{{ $t('msg.info.codeScanner.loadingScanner') }}
|
||
2 years ago
|
</div>
|
||
2 years ago
|
</div>
|
||
2 years ago
|
</div>
|
||
|
</a-modal>
|
||
|
</div>
|
||
|
</template>
|