diff --git a/src/screens/ScannerScreen.js b/src/screens/ScannerScreen.js index c448304..08641af 100644 --- a/src/screens/ScannerScreen.js +++ b/src/screens/ScannerScreen.js @@ -1,95 +1,99 @@ -import { Button, Divider, Flex, useToast } from "@chakra-ui/react"; -import axios from "axios"; -import { useEffect, useState } from "react"; -import QrReader from "react-qr-reader"; -import { useDispatch, useSelector } from "react-redux"; -import { Redirect, useHistory } from "react-router-dom"; -import { authLogout } from "../features/auth/authSlice"; +import { Button, Divider, Flex, useToast } from '@chakra-ui/react'; +import axios from 'axios'; +import { useEffect, useState } from 'react'; +import QrReader from 'react-qr-reader'; +import { useDispatch, useSelector } from 'react-redux'; +import { Redirect, useHistory } from 'react-router-dom'; +import { authLogout } from '../features/auth/authSlice'; function Scanner() { + const toast = useToast(); + const history = useHistory(); + const dispatch = useDispatch(); + const [scanData, setScanData] = useState(null); - const toast = useToast(); - const history = useHistory(); - const dispatch = useDispatch(); + const handleError = err => { + console.log(err); + }; - const [scanData, setScanData] = useState(null); + const handleScan = res => { + if (res && res !== scanData) { + setScanData(res); + } + }; - - - const handleError = (err) => { - console.log(err); - }; - - const handleScan = (res) => { - if (res && res !== scanData) { - setScanData(res); - } - }; - - useEffect(() => { - if (scanData) { - // fucking safari on ios doesn't support lookbehind, so have to workaround with substr - // const re = /(?<=verify\/).*$/; - const re = /verify\/.*$/; - const hash = re.exec(scanData); - if (hash) { - axios.post(`${process.env.REACT_APP_API_URL}/verify`, - { - id: hash[0].substr(7), - }, - { withCredentials: true }) - .then((res) => { - - if (res.data.success) { - if (res.data.loggedIn) { - toast({ - title: 'Contact Succesfully Logged', - status: 'info', - duration: 2000, - }); - } else { - console.log("Time to logout!"); - dispatch(authLogout()); - history.push("/login"); - } - } - }) - .catch((e) => { - toast({ - title: 'Bad Verification', - status: 'error', - duration: 2000, - }); - }); + useEffect(() => { + if (scanData) { + // fucking safari on ios doesn't support lookbehind, so have to workaround with substr + // const re = /(?<=verify\/).*$/; + const re = /verify\/.*$/; + const hash = re.exec(scanData); + if (hash) { + axios + .post( + `${process.env.REACT_APP_API_URL}/verify`, + { + id: hash[0].substr(7), + }, + { withCredentials: true } + ) + .then(res => { + if (res.data.success) { + if (res.data.loggedIn) { + toast({ + title: 'Contact Succesfully Logged', + status: 'info', + duration: 2000, + }); + } else { + console.log('Time to logout!'); + dispatch(authLogout()); + history.push('/login'); + } } - } - }, [scanData, dispatch, history, toast]); + }) + .catch(e => { + toast({ + title: 'Bad Verification', + status: 'error', + duration: 2000, + }); + }); + } + } + }, [scanData, dispatch, history, toast]); - const isAuthenticated = useSelector(state => state.auth.isAuthenticated); - if (!isAuthenticated) return ; + const isAuthenticated = useSelector(state => state.auth.isAuthenticated); + if (!isAuthenticated) return ; - return ( - + + + + - - - ); + Show my QR Code + + + + ); } -export default Scanner; \ No newline at end of file +export default Scanner;