| Conditions | 10 |
| Total Lines | 171 |
| Code Lines | 140 |
| 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:
Complex classes like JourneyModal.tsx ➔ JourneyModal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import React from "react"; |
||
| 35 | |||
| 36 | export default function JourneyModal({navigation, scooter, journeyModal, setJourneyModal, toggleTimer, setToggleTimer}) { |
||
| 37 | const [scooterName, setScooterName] = useState(null); |
||
| 38 | const [scooterNumber, setScooterNumber] = useState(null); |
||
| 39 | const [battery, setBattery] = useState(null); |
||
| 40 | const [scooterPosition, setScooterPosition] = useState(null); |
||
| 41 | const [scooterId, setScooterId] = useState(null); |
||
| 42 | const markerRef = useRef(null); |
||
| 43 | const [distance, setDistance] = useState(null); |
||
| 44 | const [batteryPercentage, setBatteryPercentage] = useState(null); |
||
| 45 | const [currentScooter, setCurrentScooter] = useState(null); |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | async function getScooterInfo(): Promise<void> { |
||
| 50 | if (scooter) { |
||
| 51 | const title = scooter['name'].split('#'); |
||
| 52 | setScooterName(title[0]); |
||
| 53 | setScooterNumber(title[1]); |
||
| 54 | setBattery(getBattery(scooter['battery'])); |
||
| 55 | setScooterId(scooter['_id']); |
||
| 56 | |||
| 57 | const getScooter = await scooterModel.getSpecificScooter(scooter['_id']); |
||
| 58 | |||
| 59 | setCurrentScooter(getScooter); |
||
| 60 | |||
| 61 | setScooterPosition(getScooter['scooter']['coordinates']); |
||
| 62 | setBatteryPercentage(getScooter['scooter']['battery'].toFixed(1)); |
||
| 63 | setDistance(getScooter['scooter']['trip']['distance'].toFixed(2)); |
||
| 64 | |||
| 65 | } |
||
| 66 | }; |
||
| 67 | |||
| 68 | useEffect(() => { |
||
| 69 | const interval = setInterval(() => { |
||
| 70 | |||
| 71 | journeyModal ? getScooterInfo() : null; |
||
| 72 | |||
| 73 | if (markerRef.current) { |
||
| 74 | markerRef.current.animateMarkerToCoordinate(scooterPosition, 100); |
||
| 75 | }; |
||
| 76 | |||
| 77 | |||
| 78 | }, 100); |
||
| 79 | return () => clearInterval(interval); |
||
| 80 | }); |
||
| 81 | |||
| 82 | |||
| 83 | async function stopJourney() { |
||
| 84 | const result = await scooterModel.stopScooter(scooterId); |
||
| 85 | |||
| 86 | if (Object.prototype.hasOwnProperty.call(result, 'errors')) { |
||
| 87 | showMessage({ |
||
| 88 | message: result['errors']['title'], |
||
| 89 | type: 'danger', |
||
| 90 | position: 'bottom' |
||
| 91 | }) |
||
| 92 | |||
| 93 | return; |
||
| 94 | }; |
||
| 95 | |||
| 96 | showMessage({ |
||
| 97 | message: result['message'], |
||
| 98 | type: 'success', |
||
| 99 | position: 'bottom' |
||
| 100 | }); |
||
| 101 | |||
| 102 | setJourneyModal(!journeyModal) |
||
| 103 | }; |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | return ( |
||
| 108 | <GestureRecognizer |
||
| 109 | style={{flex: 1}} |
||
| 110 | > |
||
| 111 | <Modal |
||
| 112 | animationType="slide" |
||
| 113 | transparent={true} |
||
| 114 | visible={journeyModal} |
||
| 115 | onRequestClose={() => { |
||
| 116 | }} |
||
| 117 | |||
| 118 | > |
||
| 119 | <View style={styles.modalContainer}></View> |
||
| 120 | |||
| 121 | <MapView |
||
| 122 | // ref={mapRef} |
||
| 123 | style={styles.map} |
||
| 124 | region={{ |
||
| 125 | latitude: scooterPosition? scooterPosition['latitude'] : 56.161013580817986, |
||
| 126 | longitude: scooterPosition? scooterPosition['longitude'] : 15.587742977884904, |
||
| 127 | latitudeDelta: 0.001, |
||
| 128 | longitudeDelta: 0.001, |
||
| 129 | }} |
||
| 130 | userInterfaceStyle={'dark'} |
||
| 131 | > |
||
| 132 | {scooterPosition ? |
||
| 133 | <Marker |
||
| 134 | ref={markerRef} |
||
| 135 | coordinate={scooterPosition} |
||
| 136 | icon={require('../../assets/scooter_green.png')} |
||
| 137 | tappable={true} |
||
| 138 | onPress={() => { |
||
| 139 | }} |
||
| 140 | > |
||
| 141 | </Marker> |
||
| 142 | : |
||
| 143 | <View></View> |
||
| 144 | } |
||
| 145 | |||
| 146 | </MapView> |
||
| 147 | |||
| 148 | <View style={[styles.modalMessage, styles.shadowProp]}> |
||
| 149 | |||
| 150 | <View style={styles.titleContainer}> |
||
| 151 | <Image style={styles.scooterImage} source={require('../../assets/scooter_large_white.png')}></Image> |
||
| 152 | |||
| 153 | <View style={styles.textContainer}> |
||
| 154 | <Text style={styles.scooterTitle}> {scooterName} {scooterNumber}</Text> |
||
| 155 | |||
| 156 | |||
| 157 | <View style={styles.travelInfoContainer}> |
||
| 158 | <View style={styles.travelInfo}> |
||
| 159 | <Icon |
||
| 160 | name='location' |
||
| 161 | size={30} |
||
| 162 | color='black' |
||
| 163 | /> |
||
| 164 | |||
| 165 | <Text>{distance} km</Text> |
||
| 166 | </View> |
||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | <View style={styles.travelInfo}> |
||
| 171 | <Icon |
||
| 172 | name='clock' |
||
| 173 | size={30} |
||
| 174 | color='black' |
||
| 175 | /> |
||
| 176 | <Stopwatch start={toggleTimer} |
||
| 177 | options={styles.timer} |
||
| 178 | getTime={(time) => { |
||
| 179 | getFormattedTime(time); |
||
| 180 | }} |
||
| 181 | /> |
||
| 182 | |||
| 183 | </View> |
||
| 184 | |||
| 185 | <View style={styles.travelInfo}> |
||
| 186 | <Image style={styles.battery} source={batteryImages[`${battery}`]}></Image> |
||
| 187 | |||
| 188 | <Text>{batteryPercentage}%</Text> |
||
| 189 | </View> |
||
| 190 | </View> |
||
| 191 | |||
| 192 | </View> |
||
| 193 | |||
| 194 | </View> |
||
| 195 | |||
| 196 | <Pressable style={styles.tourButton} onPress={() => { |
||
| 197 | stopJourney(); |
||
| 198 | setToggleTimer(false); |
||
| 199 | }}> |
||
| 200 | <Text style={{color: 'black'}}>Finish the ride</Text> |
||
| 201 | </Pressable> |
||
| 202 | |||
| 203 | </View> |
||
| 204 | </Modal> |
||
| 205 | </GestureRecognizer> |
||
| 206 | ) |
||
| 307 | }) |