Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush Srivastava committed Jun 16, 2022
1 parent 6c939b1 commit 0ba88eb
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 71 deletions.
29 changes: 4 additions & 25 deletions Components/AboutInfo.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import React, { useState, useEffect, useCallback } from "react";
import React from "react";
import {
View,
StyleSheet,
Text,
} from "react-native";
import { Divider } from "react-native-elements";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { FontAwesome } from "@expo/vector-icons";

const AboutInfo = () => {
const [status, setStatus] = useState(true);
const [userData, setUserData] = useState({});

useEffect(() => {
getUserData();
}, [])


const getUserData = useCallback(() => {

AsyncStorage.getItem("userData").then((value) => {
let parseData = JSON.parse(value);
setUserData(parseData);
});

}, [])
const AboutInfo = ({userData}) => {

const username = (name) => {
var str = name;
Expand All @@ -49,15 +32,11 @@ const AboutInfo = () => {
<Divider />
<View style={{ flexDirection: "row" }}>
<View style={styles.imageView}>
{status ? (

<View style={styles.ProfilePicBorder}>
<Text style={styles.ProfilePicText}>{username(userData.name)}</Text>
</View>
) : (
<View>
<Image source={{ uri: image }} style={styles.ImageStyle} />
</View>
)}

</View>

<View style={styles.nameAndCat}>
Expand Down
161 changes: 161 additions & 0 deletions Components/History.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { StyleSheet, Text, View, FlatList, ActivityIndicator, TouchableOpacity } from 'react-native'
import React, { useEffect, useState } from 'react'
import { FontAwesome } from '@expo/vector-icons';
import { Divider } from "react-native-elements";

const History = ({ token }) => {

const [loading, setLoading] = useState();
const [historyData, setHistoryData] = useState([]);

const parameters = ['Nitrogen(N)', 'Phosphorus(P)', 'Potassium(K)', 'Avg. Temperature', 'Avg. Humidity', 'pH', 'Avg. Rainfall']

useEffect(() => {
fetchHistory();
}, [])

const fetchHistory = () => {
setLoading(true);
var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${token}`);

var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};

fetch("https://growmoreweb.herokuapp.com/v1/model/history", requestOptions)
.then((response) => {
// if (response.ok)
return response.json()
// else
// throw 'Fetch History API error : ' + response.status;
})
.then(result => setHistoryData(result.history))
.finally(() => setLoading(false))
.catch(error => console.log(error));
}
console.log(historyData)

const renderItem = ({ item, index }) => {

return (
<View style={styles.historyItems}>
<View>
{
parameters.map((i, idx) => (
<Text style={styles.parametersText} key={idx}>{`${i}: ${item.details[idx]}`}</Text>
))
}
</View>
<View style={{justifyContent:'center'}}>
<Text style={styles.cropText}>{item.cropSuggested}</Text>
</View>

</View>
)
}

return (
<View style={styles.cardView}>
<View style={styles.headingIcon}>
<View style={{flexDirection:'row'}}>
<FontAwesome name="history" size={24} color="#707070" />
<Text style={styles.cvHeading}>History</Text>
</View>
<TouchableOpacity onPress={()=>fetchHistory()}>
<FontAwesome name="refresh" size={22} color="#808080" />
</TouchableOpacity>
</View>
<Divider />
<View style={styles.headingIcon}>
<Text style={styles.headingText}>Parameters</Text>
<Text style={styles.headingText}>Crop Recommended</Text>
</View>
<Divider />
{
loading ? <ActivityIndicator style={styles.container} size="large" color="#00aced" />
:
historyData.length === 0 ?
<View style={styles.emptyDataText}><Text style={{ color: '#ca5b51' }}>No Data Found !</Text></View>
: <FlatList
data={historyData}
keyExtractor={item => item._id}
renderItem={renderItem}
/>
}
</View>
)
}

export default History

const styles = StyleSheet.create({
cardView: {
width: "95%",
backgroundColor: "white",
alignSelf: "center",
// minHeight:'65%',
flex:1,
padding: 1,
marginHorizontal: 10,
marginBottom:10,
borderRadius: 10,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 10,

elevation: 5,
},
container: {
flex: 1,
backgroundColor: '#fff',
padding: 10
},
emptyDataText: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
historyItems:{
margin:5,
flexDirection:'row',
padding:5,
justifyContent:'space-between',
borderBottomColor:'#707070',
borderBottomWidth:0.5,
},
headingIcon: {
flexDirection: "row",
justifyContent:'space-between',
padding: 10,
},
cvHeading: {
color: "#707070",
marginLeft: 5,
fontSize: 18,
},
parametersText:{
fontSize:16,
fontWeight:'700',
color:'#909090'
},
headingText:{
fontSize:16,
fontWeight:'bold',
color:'black'
},
cropText:{
fontSize:16,
fontWeight:'bold',
color:'#0cac75'
}


})
24 changes: 11 additions & 13 deletions Navigation/GrowMoreNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ export const TabNavigation = (props) => {
return (
<BottomTab.Navigator
screenOptions={({ route, navigation }) => ({

tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === "News") {
iconName = focused ? "newspaper-variant" : "newspaper-variant-outline";
} else if (route.name === "Recommender") {
iconName = focused ? "thumb-up" : "thumb-up-outline";
} else if (route.name === "Dashboard") {
} else if (route.name === "Dashboard") {
iconName = focused ? "view-dashboard" : "view-dashboard-outline";
}

Expand All @@ -70,18 +71,15 @@ export const TabNavigation = (props) => {
/>
);
},
tabBarHideOnKeyboard: true,
tabBarActiveTintColor: "#2b5c4c",
tabBarInactiveTintColor: "grey",
tabBarStyle: [
{
display: "flex"
},]
})}
tabBarOptions={{
keyboardHidesTabBar: true,
style: {
backgroundColor: "white",
justifyContent: "center",
alignItems: "center",
alignSelf: "center",
},
activeTintColor: "#2b5c4c",
inactiveTintColor: "grey",
}}

>
<BottomTab.Screen
name="News"
Expand All @@ -92,7 +90,7 @@ export const TabNavigation = (props) => {
headerTitleStyle: { color: "#fff" },
}}
/>

<BottomTab.Screen
name="Recommender"
component={CropRecommender}
Expand Down
27 changes: 13 additions & 14 deletions Screens/CropRecommender.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const CropRecommender = ({ navigation }) => {
}

}, [weatherData])




const getWeatherData = async () => {
setLoading('weatherFetching');
Expand Down Expand Up @@ -211,25 +212,23 @@ const CropRecommender = ({ navigation }) => {
setPredictedCrop(result.prediction);
setCropVisible(true);
setLoading(false);

saveHistory(result.prediction);
})
.catch(error => console.log('error', error));

}

}

const saveHistory = () => {
const saveHistory = (crop) => {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", `Bearer ${accessToken}`);
console.log(`Bearer ${accessToken}`)


var raw = JSON.stringify({
"details": [nitrogen, phosphorus, potassium, temperature.toString(), humidity.toString(), pH, rainfall.toString()],
"cropSuggested": predictedCrop
"cropSuggested": crop
});
console.log(raw)

var requestOptions = {
method: 'POST',
Expand All @@ -240,12 +239,12 @@ const CropRecommender = ({ navigation }) => {

fetch("https://growmoreweb.herokuapp.com/v1/model/history", requestOptions)
.then((response) => {
// if (response.ok)
if (response.ok)
return response.json()
// else
// throw 'History API error : ' + response.status;
else
throw 'History API error : ' + response.status;
})
.then(result => console.log(result))
.then(result => console.log("history success"))
.catch(error => console.warn(error));
}

Expand Down Expand Up @@ -274,7 +273,7 @@ const CropRecommender = ({ navigation }) => {
editable={!(loading === 'toWeather')}
onChangeText={(text) => setPhosphorus(text)}
value={phosphorus}
placeholder="Enter value of Nitrogen"
placeholder="Enter value of Phosphorus"
autoCapitalize="none"
keyboardType="numeric"
/>
Expand All @@ -285,7 +284,7 @@ const CropRecommender = ({ navigation }) => {
editable={!(loading === 'toWeather')}
onChangeText={(text) => setPotassium(text)}
value={potassium}
placeholder="Enter value of Nitrogen"
placeholder="Enter value of Potassium"
autoCapitalize="none"
keyboardType="numeric"
/>
Expand All @@ -295,7 +294,7 @@ const CropRecommender = ({ navigation }) => {
editable={!(loading === 'toWeather')}
onChangeText={(text) => setpH(text)}
value={pH}
placeholder="Enter value of Nitrogen"
placeholder="Enter value of pH"
autoCapitalize="none"
keyboardType="numeric"
/>
Expand Down
38 changes: 31 additions & 7 deletions Screens/Dashboard.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import React from 'react'
import React, { useState, useEffect, useCallback } from 'react'
import { StyleSheet, View, TouchableOpacity, Alert, ScrollView } from 'react-native'
import AboutInfo from '../Components/AboutInfo';
import History from '../Components/History';
import AsyncStorage from "@react-native-async-storage/async-storage";

const Dashboard = () => {

const [userData, setUserData] = useState({});

useEffect(() => {
getUserData();
}, [])


const getUserData = useCallback(() => {

AsyncStorage.getItem("userData").then((value) => {
let parseData = JSON.parse(value);
setUserData(parseData);
});

}, [])
return (
<View>
<ScrollView>

<AboutInfo />
</ScrollView>
<View style={styles.container}>
{/* <ScrollView> */}

<AboutInfo userData={userData} />
<History token={userData.token} />
{/* </ScrollView> */}
</View>
)
}

export default Dashboard

const styles = StyleSheet.create({})
const styles = StyleSheet.create({
container:{
flex:1,
backgroundColor:'#fff'
}
})

0 comments on commit 0ba88eb

Please sign in to comment.