Conditions | 10 |
Total Lines | 142 |
Code Lines | 119 |
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 { stopLocationUpdatesAsync } from "expo-location"; |
||
11 | |||
12 | export default function JourneyModal({navigation, scooter, journeyModal, setJourneyModal, toggleTimer, setToggleTimer}) { |
||
13 | const [scooterName, setScooterName] = useState(null); |
||
14 | const [scooterNumber, setScooterNumber] = useState(null); |
||
15 | const [battery, setBattery] = useState(null); |
||
16 | const [fixedRate, setFixedRate] = useState(null); |
||
17 | const [scooterId, setScooterId] = useState(null); |
||
18 | const [timer, setTimer] = useState(null); |
||
19 | |||
20 | const batteryImages = { |
||
21 | '100': require('../../assets/battery_100.png'), |
||
22 | '75': require('../../assets/battery_75.png'), |
||
23 | '50': require('../../assets/battery_50.png'), |
||
24 | '25': require('../../assets/battery_25.png') |
||
25 | } |
||
26 | |||
27 | function getBattery(batteryPercentage) { |
||
28 | if (batteryPercentage >= 75) { |
||
29 | return '100' |
||
30 | } else if (batteryPercentage >= 50) { |
||
31 | return '75' |
||
32 | } else if (batteryPercentage >= 25) { |
||
33 | return '50' |
||
34 | } else { |
||
35 | return '25' |
||
36 | } |
||
37 | }; |
||
38 | |||
39 | useEffect(() => { |
||
40 | function getScooterInfo(): void { |
||
41 | if (scooter) { |
||
42 | const title = scooter['name'].split('#'); |
||
43 | setScooterName(title[0]); |
||
44 | setScooterNumber(title[1]); |
||
45 | setBattery(getBattery(scooter['battery'])); |
||
46 | setScooterId(scooter['_id']); |
||
47 | } |
||
48 | } |
||
49 | getScooterInfo(); |
||
50 | }); |
||
51 | |||
52 | |||
53 | async function stopJourney() { |
||
54 | const result = await scooterModel.stopScooter(scooterId); |
||
55 | |||
56 | if (Object.prototype.hasOwnProperty.call(result, 'errors')) { |
||
57 | showMessage({ |
||
58 | message: result['errors']['title'], |
||
59 | type: 'danger' |
||
60 | }) |
||
61 | |||
62 | return; |
||
63 | }; |
||
64 | |||
65 | showMessage({ |
||
66 | message: result['message'], |
||
67 | type: 'success' |
||
68 | }); |
||
69 | |||
70 | setJourneyModal(!journeyModal) |
||
71 | }; |
||
72 | |||
73 | function getFormattedTime(time) { |
||
74 | const currentTime = time; |
||
75 | }; |
||
76 | |||
77 | return ( |
||
78 | <GestureRecognizer |
||
79 | style={{flex: 1}} |
||
80 | // onSwipeDown={ () => setModalVisible(false) } |
||
81 | > |
||
82 | <Modal |
||
83 | animationType="slide" |
||
84 | transparent={true} |
||
85 | visible={journeyModal} |
||
86 | onRequestClose={() => { |
||
87 | // setJourneyModal(!journeyModal) |
||
88 | }} |
||
89 | |||
90 | > |
||
91 | <View style={styles.modalContainer}></View> |
||
92 | |||
93 | |||
94 | <View style={styles.modalMessage}> |
||
95 | {/* <View style={styles.swipeButton}></View> */} |
||
96 | |||
97 | <View style={styles.titleContainer}> |
||
98 | <Image style={styles.scooterImage} source={require('../../assets/scooter_large.png')}></Image> |
||
99 | |||
100 | <View style={styles.textContainer}> |
||
101 | <Text style={styles.scooterTitle}> {scooterName} {scooterNumber}</Text> |
||
102 | |||
103 | |||
104 | <View style={styles.travelInfoContainer}> |
||
105 | <View style={styles.travelInfo}> |
||
106 | <Icon |
||
107 | name='location' |
||
108 | size={30} |
||
109 | color='black' |
||
110 | /> |
||
111 | |||
112 | <Text>1.2 km</Text> |
||
113 | </View> |
||
114 | |||
115 | |||
116 | |||
117 | <View style={styles.travelInfo}> |
||
118 | <Icon |
||
119 | name='clock' |
||
120 | size={30} |
||
121 | color='black' |
||
122 | /> |
||
123 | <Stopwatch start={toggleTimer} |
||
124 | reset={toggleTimer} |
||
125 | options={styles.timer} |
||
126 | // options={options} |
||
127 | getTime={getFormattedTime} |
||
128 | /> |
||
129 | |||
130 | </View> |
||
131 | |||
132 | <View style={styles.travelInfo}> |
||
133 | <Image style={styles.battery} source={batteryImages[`${battery}`]}></Image> |
||
134 | |||
135 | <Text>35 km</Text> |
||
136 | </View> |
||
137 | </View> |
||
138 | |||
139 | </View> |
||
140 | |||
141 | </View> |
||
142 | |||
143 | <Pressable style={styles.tourButton} onPress={() => { |
||
144 | stopJourney(); |
||
145 | setToggleTimer(false); |
||
146 | }}> |
||
147 | <Text style={{color: 'white'}}>Finish the ride</Text> |
||
148 | </Pressable> |
||
149 | |||
150 | </View> |
||
151 | </Modal> |
||
152 | </GestureRecognizer> |
||
153 | ) |
||
250 | }) |