import {useRef, useState} from 'react'; import {Constants} from '../../utils'; import {Button, HelperText, Text, TextInput} from 'react-native-paper'; import {View} from 'react-native'; import EncryptedStorage from 'react-native-encrypted-storage'; export default function InitScanner({setScannerSession}) { const [viewMode, setViewMode] = useState(0); const [host, setHost] = useState('http://192.168.178.93:8080/v1/scanner'); const [scannerName, setScannerName] = useState(''); const onAddScanner = () => { console.log(scannerName); if ( scannerName.length > Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH && scannerName.length < Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH ) { console.log('valid'); fetch(host, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ scannerName: scannerName, }), }) .then(res => { if (res.status === 200) { return res.json(); } return Promise.reject(res.status); }) .then(async data => { console.log('data', data); await EncryptedStorage.setItem('scanner_id', data.Id); setScannerSession(data.Session); }) .catch(err => { console.error(err); }); } else { console.log('scanner name invalid'); } }; const hasScannerNameErrors = () => { return ( scannerName.length < Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH || scannerName.length > Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH ); }; return ( setHost(value)} /> setScannerName(value)} /> {'Scanner name is invalid! Min: ' + Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH + ', Max: ' + Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH} ); }