import {useState} from 'react'; import {Constants} from '../../utils'; import {Button, HelperText, TextInput} from 'react-native-paper'; import {View} from 'react-native'; import EncryptedStorage from 'react-native-encrypted-storage'; export default function InitScanner({setScannerSession}) { const [host, setHost] = useState('http://192.168.178.93:8080/v1/scanner'); const [scannerName, setScannerName] = useState(''); const onAddScanner = () => { if ( scannerName.length >= Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH && scannerName.length <= Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH ) { 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 => { await EncryptedStorage.setItem('scanner_host', host); 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} ); }