| Conditions | 7 |
| Total Lines | 94 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React, { useState, useEffect } from 'react'; |
||
| 7 | |||
| 8 | export default function QrScanner({navigation, cameraVisible, setCameraVisible, scooter, setModalVisible, currentCity, setCurrentScooter}) { |
||
| 9 | const [hasPermission, setHasPermission] = useState(null); |
||
| 10 | const [scanned, setScanned] = useState(false); |
||
| 11 | const [code, setCode] = useState(null); |
||
| 12 | |||
| 13 | useEffect(() => { |
||
| 14 | const getBarCodeScannerPermissions = async () => { |
||
| 15 | const { status } = await BarCodeScanner.requestPermissionsAsync(); |
||
| 16 | setHasPermission(status === 'granted'); |
||
| 17 | }; |
||
| 18 | |||
| 19 | getBarCodeScannerPermissions(); |
||
| 20 | }, []); |
||
| 21 | |||
| 22 | async function handleBarCodeScanned({ type, data }) { |
||
| 23 | setScanned(true); |
||
| 24 | |||
| 25 | await startScooter(data); |
||
| 26 | // alert(`Bar code with type ${type} and data ${data} has been scanned!`); |
||
| 27 | }; |
||
| 28 | |||
| 29 | if (hasPermission === null) { |
||
| 30 | return <Text>Requesting for camera permission</Text>; |
||
| 31 | } |
||
| 32 | if (hasPermission === false) { |
||
| 33 | return <Text>No access to camera</Text>; |
||
| 34 | } |
||
| 35 | |||
| 36 | async function startScooter(scooter): Promise<void> { |
||
| 37 | // Check if QR code is a valid scooter |
||
| 38 | const result = await scooterModel.checkIfValidScooter(scooter, currentCity); |
||
| 39 | |||
| 40 | |||
| 41 | if (result) { |
||
| 42 | showMessage({ |
||
| 43 | message: 'Scooter scanned!', |
||
| 44 | type: 'success' |
||
| 45 | }); |
||
| 46 | |||
| 47 | setCameraVisible(!cameraVisible); |
||
| 48 | setModalVisible(true); |
||
| 49 | setCurrentScooter(result); |
||
| 50 | } else { |
||
| 51 | showMessage({ |
||
| 52 | message: 'Not a scooter!', |
||
| 53 | type: 'danger' |
||
| 54 | }) |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | return ( |
||
| 61 | <Modal |
||
| 62 | animationType="slide" |
||
| 63 | transparent={true} |
||
| 64 | visible={cameraVisible} |
||
| 65 | onRequestClose={() => { |
||
| 66 | setCameraVisible(!cameraVisible); |
||
| 67 | }} |
||
| 68 | > |
||
| 69 | <View style={[styles.container, styles.shadowProp]}> |
||
| 70 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => setCameraVisible(!cameraVisible)}> |
||
| 71 | <Icon |
||
| 72 | name='x' |
||
| 73 | size={25} |
||
| 74 | color='black' |
||
| 75 | /> |
||
| 76 | </Pressable> |
||
| 77 | |||
| 78 | <Text style={styles.title}>Scan the QR-code</Text> |
||
| 79 | <TextInput |
||
| 80 | placeholder="or enter code manually" |
||
| 81 | style={styles.input} |
||
| 82 | onChangeText={(content: string) => { |
||
| 83 | setCode(content) |
||
| 84 | }} |
||
| 85 | onSubmitEditing={() => startScooter(code)} |
||
| 86 | /> |
||
| 87 | <BarCodeScanner |
||
| 88 | onBarCodeScanned={scanned ? undefined : handleBarCodeScanned} |
||
| 89 | style={styles.viewFinder} |
||
| 90 | /> |
||
| 91 | |||
| 92 | <Pressable style={[styles.cameraButton, styles.shadowProp]} onPress={() => setScanned(false)} > |
||
| 93 | <Icon |
||
| 94 | name='screen-full' |
||
| 95 | size={24} |
||
| 96 | color='black' |
||
| 97 | /> |
||
| 98 | </Pressable> |
||
| 99 | </View> |
||
| 100 | </Modal> |
||
| 101 | |||
| 183 | }) |