components/drawer/History.tsx   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 271
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 216
mnd 2
bc 2
fnc 3
dl 0
loc 271
rs 10
bpm 0.6666
cpm 1.6666
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B History.tsx ➔ Wallet 0 104 5
1
import React from 'react';
2
import { useState, useEffect } from 'react';
3
import { StyleSheet, Text, View, Pressable, Modal, TextInput, ScrollView} from 'react-native';
4
import userModel from '../../models/user';
5
import Icon from 'react-native-vector-icons/Octicons';
6
import mapModel from '../../models/map';
7
import HistoryMap from './HistoryMap';
8
9
export default function Wallet({navigation}): any {
10
    const [history, setHistory] = useState([]);
11
    const [modalVisible, setModalVisible] = useState(false);
12
    const [currentJourney, setCurrentJourney] = useState(null);
13
    const [noHistory, setNoHistory] = useState(false);
14
    const [distance, setDistance] = useState(null)
15
    
16
    
17
    async function getHistory(): Promise<void> {
18
        const result = await userModel.getHistory();
19
20
        if (result.length === 0) {
21
            setNoHistory(true);
22
            return;
23
        }
24
        setHistory(result);
25
        
26
        console.log(history);
27
        
28
        // const travelDistance = mapModel.calcDistance(result['startPosition']['latitude'], result['startPosition']['longitudo'], result['endPosition']['latitude'], result['endPosition']['longitude'])
29
30
        // setDistance(travelDistance);
31
32
        setNoHistory(false);
33
    };
34
35
36
        // Get history for logged in user
37
        useEffect(() => {
38
            getHistory();
39
            
40
        }, []);
41
42
        // Reload history on focus
43
        useEffect(() => {
44
            navigation.addListener('focus', () => getHistory())
45
        }, []);
46
47
48
        function getDistance(history) {
49
            const travelDistance = mapModel.calcDistance(history['startPosition']['latitude'], history['startPosition']['longitude'], history['endPosition']['latitude'], history['endPosition']['longitude']);
50
51
            return travelDistance.toFixed(2).toString();
52
            
53
            
54
        }
55
    return (
56
        <View style={styles.container}>
57
58
            <View style={styles.titleContainer}>
59
                
60
                <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => navigation.navigate('Map')}>
61
                    <Icon 
62
                        name='x' 
63
                        size={25} 
64
                        color='black'
65
                    />
66
                </Pressable>
67
                
68
                <Text style={styles.title}>Ride History</Text>
69
70
                <View style={{width: 50}}></View>
71
72
            </View>
73
            
74
            {noHistory ? 
75
                <Text style={{color: 'gray'}}>No travel history to show</Text>
76
                :
77
                <ScrollView style={styles.prepaidContainer}>
78
                {history.map((h, index) => (
79
                <Pressable onPress={() => {
80
                    setCurrentJourney(h)
81
                    setModalVisible(true)
82
                }}
83
                key={index}
84
                >
85
86
         
87
                <View style={styles.rideHistory}>
88
89
                    <View style={styles.primaryInfo}>
90
                        <Text style={styles.textDistance}>
91
                            {getDistance(h)} km / {h.totalMin} min 
92
                        </Text>
93
                        <Text style={styles.textCost}>{h.totalPrice} kr</Text>
94
                    </View>
95
                    
96
                    <View style={styles.secondaryInfo}>
97
                        <Text style={styles.textDate}>{h.date}</Text>
98
                    </View>
99
                </View>
100
                </Pressable>
101
                ))}
102
                <HistoryMap navigation={navigation} journey={currentJourney} modalVisible={modalVisible} setModalVisible={setModalVisible}></HistoryMap>
103
                </ScrollView>
104
                  
105
                }
106
           
107
            
108
109
              
110
                
111
        </View>
112
    )
113
}
114
115
const styles = StyleSheet.create({
116
    container: {
117
      flex: 1,
118
      backgroundColor: '#fff',
119
      alignItems: 'center',
120
      height: '50%',
121
      width: '100%'
122
    },
123
124
    rideHistory: {
125
        width: '100%',
126
        flexDirection: 'column',
127
        marginBottom: 20,
128
        borderBottomWidth: 1,
129
        padding: 5,
130
        borderBottomColor: 'grey',
131
    },
132
133
    titleContainer: {
134
        marginTop: 20,
135
        width: '100%',
136
        flexDirection: 'row',
137
        height: 100,
138
        justifyContent: 'center',
139
        alignItems: 'center'
140
    },
141
142
    textDistance: {
143
        textAlign: 'left',
144
        fontWeight: '600',
145
        fontSize: 16,
146
    },
147
148
    textCost: {
149
        textAlign: 'right',
150
        fontWeight: '600',
151
        fontSize: 16
152
    },
153
154
    textDate: {
155
        width: '100%',
156
        color: 'grey'
157
    },
158
159
160
161
    primaryInfo: {
162
        justifyContent: 'space-between',
163
        width: '100%',
164
        flexDirection: 'row'
165
    },
166
167
    secondaryInfo: {
168
        justifyContent: 'flex-start',
169
        width: '100%',
170
        marginBottom: 10
171
    },
172
173
174
    infoContainer: {
175
        width: '100%',
176
        height: '20%',
177
        alignItems: 'center',
178
        justifyContent: 'space-evenly',
179
180
    },
181
182
    prepaidContainer: {
183
        width: '90%',
184
        height: '90%',
185
    },
186
187
    buttonText: {
188
        color: 'white'
189
    },
190
191
    backButton: {
192
        width: 40,
193
        height: 40, 
194
        backgroundColor: 'white',
195
        borderRadius: 25,
196
        borderWidth: 1,
197
        borderColor: 'gray',
198
        alignItems: 'center',
199
        justifyContent: 'center'
200
    },
201
    modalCloseContainer: {
202
        width: '90%',
203
        alignItems: 'flex-end',
204
    },
205
206
    closeButton: {
207
        width: 25,
208
        height: 25, 
209
        backgroundColor: 'white',
210
        borderRadius: 25,
211
        borderWidth: 1,
212
        borderColor: 'gray',
213
        alignItems: 'center',
214
        justifyContent: 'center'
215
    },
216
217
    title: {
218
        fontWeight: 'bold',
219
        fontSize: 20,
220
        width: '70%',
221
        textAlign: 'center'
222
    },
223
224
    balance: {
225
        fontWeight: 'bold',
226
        fontSize: 50
227
    },
228
229
    info: {
230
        fontSize: 15,
231
        width: '80%',
232
        color: 'gray',
233
        textAlign: 'center'
234
    },
235
236
    prepaidInfo: {
237
        fontWeight: 'bold',
238
        fontSize: 18
239
    },
240
241
    shadowProp: {
242
        elevation: 5,
243
        shadowColor: 'black'
244
      },
245
246
    modalContainer: {
247
        backgroundColor: 'rgba(220, 220, 220, 0.6)',
248
        width: '100%',
249
        height: '100%',
250
        flex: 1,
251
    },
252
    
253
    modalMessage: {
254
        backgroundColor: 'white',
255
        width: '100%',
256
        height: '25%',
257
        justifyContent: 'center',
258
        alignItems: 'center',
259
        borderRadius: 25
260
    },
261
262
    input: {
263
        width: '90%',
264
        marginBottom: 30,
265
        borderRadius: 10,
266
        height: 50,
267
        padding: 10,
268
        borderBottomWidth: 2,
269
        borderColor: 'gray'
270
    },
271
});