Passed
Push — dev ( 960b6d...f9a638 )
by Kasper
01:04 queued 12s
created

components/modals/JourneyModal.tsx   A

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 250
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 200
mnd 5
bc 5
fnc 5
dl 0
loc 250
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
D JourneyModal.tsx ➔ JourneyModal 0 142 10
1
import { stopLocationUpdatesAsync } from "expo-location";
2
import React from "react";
3
import { useState, useEffect, useRef } from 'react';
4
import { View, Text, TextInput, Button, Pressable, StyleSheet, Image, StatusBar, Modal } from "react-native";
5
import mapModel from "../../models/map";
6
import GestureRecognizer from 'react-native-swipe-gestures';
7
import scooterModel from "../../models/scooter";
8
import { showMessage, hideMessage } from "react-native-flash-message";
9
import Icon from 'react-native-vector-icons/Octicons';
10
import { Stopwatch, Timer } from 'react-native-stopwatch-timer';
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
    )
154
}
155
156
const styles = StyleSheet.create({
157
    modalContainer: {
158
        // backgroundColor: 'rgba(80, 80, 80, 0.6)',
159
        width: '100%',
160
        height: '100%',
161
        flex: 1,
162
    },
163
164
    titleContainer: {
165
        flexDirection: 'row',
166
        alignItems: 'center',
167
        marginBottom: 10,
168
        marginTop: 30
169
    },
170
171
    swipeButton: {
172
        width: '25%',
173
        height: 5,
174
        backgroundColor: 'lightgrey',
175
        borderRadius: 25
176
    },
177
178
    textContainer: {
179
        width: '60%',
180
        marginBottom: 10,
181
        // padding: 0
182
        alignItems: 'center',
183
        // flexDirection: 'row'
184
    },
185
186
    modalMessage: {
187
        backgroundColor: 'white',
188
        width: '100%',
189
        height: '35%',
190
        justifyContent: 'space-around',
191
        alignItems: 'center',
192
        paddingTop: 10,
193
        // flexDirection: 'row'
194
        borderTopLeftRadius: 25,
195
        borderTopRightRadius: 25
196
    },
197
198
    scooterTitle: {
199
        fontWeight: 'bold',
200
        marginBottom: 40,
201
        fontSize: 26,
202
        // backgroundColor: 'red',
203
        // width: '100%'
204
    },
205
    
206
    battery: {
207
        marginLeft: 5,
208
        // width: 58,
209
        // height: 25
210
        marginBottom: 15
211
    },
212
213
    scooterImage: {
214
        margin: 10
215
    },
216
217
    tourButton: {
218
        backgroundColor: 'cornflowerblue',
219
        width: '80%',
220
        height: 50,
221
        borderRadius: 10,
222
        // display: 'flex',
223
        justifyContent: 'center',
224
        alignItems: 'center',
225
        // marginBottom: 
226
        // marginTop: 120,
227
    },
228
229
    timer: {
230
        backgroundColor: 'white'
231
    },
232
233
    travelInfoContainer: {
234
        flexDirection: 'row',
235
        // backgroundColor: 'red',
236
        justifyContent: 'space-evenly',
237
        width: '100%'
238
    },
239
240
    travelInfo: {
241
        flexDirection: 'column',
242
        justifyContent: 'space-around',
243
        alignItems: 'center',
244
        // backgroundColor: 'red',
245
        height: 75
246
        // width: '100%'
247
    },
248
249
250
})