-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHomeScreen.js
148 lines (135 loc) · 4.24 KB
/
HomeScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Component name: HomeScreen
// This component will list all the complaints lodged by the user
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { Button } from 'react-native-material-ui';
import axios from 'axios';
import { Divider } from 'react-native-material-ui';
class HomeScreen extends React.Component {
// This constructor function initialises the state's complaint list
constructor() {
super();
this.state = {
complaints: [
]
}
}
// This function fetches complaints by the logged in user from the server
fetchComplaints() {
const headers = {
'Authorization': 'Bearer ' + this.props.navigation.getParam('token', 'token').access
}
axios({
method: 'GET',
url: 'https://201751025.pythonanywhere.com/complaint/',
headers: headers,
}).then((response) => {
this.setState({
complaints: response.data
})
}).catch((error) => {
console.log(error)
});
}
// This hook calls the function to fetch the complaints when Complaint component is mounted
componentDidMount() {
this.fetchComplaints()
}
// This hook calls the function to fetch the complaints when Complaint component is updated
componentDidUpdate() {
this.fetchComplaints()
}
render() {
//complaints is the list of Buttons leading to detail view of all the fetched complaints
complaints = this.state.complaints.map(
c => (
<View style={styles.list} key={c.id}>
<Button
onPress={() => this.props.navigation.navigate('editCView',
{
'id': c.id,
'title': c.title,
'content': c.description,
'role': this.props.navigation.getParam('role', 'role'),
'status': c.status,
'token': this.props.navigation.getParam('token', 'token').access
}
)}
text={c.title}
key={c.id}
raised
primary
/>
<Divider />
</View>
)
)
const token = this.props.navigation.getParam('token', 'token')
return (
<View style={styles.container}>
<View>
<ScrollView style={styles.scroll}>
{complaints}
</ScrollView>
</View>
<View
style={styles.circleButton}
>
{/* Radio button access to change the status of complaint */}
{ this.props.navigation.getParam('role', 'role') == "student" &&
(<Button
style={styles.createButton} raised primary text="+"
onPress={() => this.props.navigation.navigate('Create_complaint', { 'token': token.access })}
title="Create_complaint"
/>)
}
</View>
</View>
);
}
};
// Styles of all the elements used in this component
const styles = StyleSheet.create({
container: {
flex: 1
},
list: {
paddingLeft: 30,
paddingRight: 30,
paddingTop: 30
},
scroll: {
height: 500,
paddingBottom: 50
},
button: {
width: 200,
height: 70,
backgroundColor: 'blue',
marginTop: 150,
marginLeft: 100,
},
circleButton: {
padding: 5,
height: 50,
width: 50,
right: 60,
bottom: 40,
position: "absolute",
},
createButton: {
borderRadius: 1000,
right: 60,
bottom: 80,
position: "absolute",
}
});
export default HomeScreen;