Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-reload-for-screens #52

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/screens/Energy/energyEntryEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function EnergyEntryEdit(): JSX.Element {
household: user.household,
};
void EnergyAPI.updateEnergy(newEntry).then(() => {
navigation.navigate('DashBoard');
navigation.navigate('YourForms');
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/Food/foodEntryEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default function FoodEntryEdit(): JSX.Element {
food_waste: foodWaste,
};
void FoodAPI.updateFood(newEntry).then(() => {
navigation.navigate('DashBoard');
navigation.navigate('YourForms');
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default function TransportationEntryEdit(): JSX.Element {
fuel_efficiency: user.fuel_efficiency,
};
void TransportationAPI.updateTransportation(newEntry).then(() => {
navigation.navigate('DashBoard');
navigation.navigate('YourForms');
});
}
};
Expand Down
41 changes: 38 additions & 3 deletions frontend/src/screens/communityHub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ScrollView,
ImageBackground,
FlatList,
RefreshControl,
} from 'react-native';
import { useFonts } from 'expo-font';
import Colors from '../../assets/colorConstants';
Expand All @@ -32,6 +33,7 @@ export default function DashBoardScreen(): JSX.Element {
const [topUsersMonthly, setMonthlyUsers] = useState<RankUser[]>([]);
const [, setYearlyUsers] = useState<RankUser[]>([]);
const [topUsersOverall, setOverallUsers] = useState<RankUser[]>([]);
const [refreshing, setRefreshing] = useState(false);

// User's monthly rank
const userRankMonthly: RankUser = { rank: 89, name: 'Squishyhoshi', footprint: 200, score: 5 };
Expand Down Expand Up @@ -160,23 +162,56 @@ export default function DashBoardScreen(): JSX.Element {
useEffect(() => {
void UsersAPI.getTopUsers(10).then(async (res) => {
if (res != null) {
if (res.top_monthly_users != null && res.top_yearly_users != null && res.top_overall_users != null) {
if (
res.top_monthly_users != null &&
res.top_yearly_users != null &&
res.top_overall_users != null
) {
setMonthlyUsers(res.top_monthly_users);
setYearlyUsers(res.top_yearly_users);
setOverallUsers(res.top_overall_users);
console.log(res.top_overall_users)
console.log(res.top_overall_users);
}
}
});
}, [loaded]);

const onRefresh = (): void => {
// Simulate a refresh action (e.g., fetch new data)
setRefreshing(true);

void UsersAPI.getTopUsers(10).then(async (res) => {
if (res != null) {
if (
res.top_monthly_users != null &&
res.top_yearly_users != null &&
res.top_overall_users != null
) {
setMonthlyUsers(res.top_monthly_users);
setYearlyUsers(res.top_yearly_users);
setOverallUsers(res.top_overall_users);
console.log(res.top_overall_users);
}
}
});

setTimeout(() => {
setRefreshing(false); // Set refreshing to false after data is fetched
}, 2000); // Simulating a 2-second delay
};

if (!loaded) {
return <></>;
}

return (
<View style={styles.container}>
<ScrollView style={styles.scrollContainer}>
<ScrollView
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#0000ff" />
}
style={styles.scrollContainer}
>
<View style={styles.halfScreen}>
<ImageBackground
source={{
Expand Down
90 changes: 64 additions & 26 deletions frontend/src/screens/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import { ScrollView, View, StyleSheet, TouchableOpacity } from 'react-native';
import { ScrollView, View, StyleSheet, TouchableOpacity, RefreshControl } from 'react-native';
import { useFonts } from 'expo-font';
import Colors from '../../assets/colorConstants';

Expand Down Expand Up @@ -27,7 +27,7 @@ export default function DashBoardScreen(): JSX.Element {
const [transportationEntry, setTransportationEntry] = useState<TransportationEntry>();
const [foodEntry, setFoodEntry] = useState<FoodEntry>();
const [energyEntry, setEnergyEntry] = useState<EnergyEntry>();

const [refreshing, setRefreshing] = useState(false);

const navigation = useNavigation<StackNavigation>();

Expand All @@ -44,47 +44,85 @@ export default function DashBoardScreen(): JSX.Element {
});
void TransportationAPI.getTransportationMetricForToday().then((res) => {
if (res != null) {
setTransportationEntry(res)
setTransportationEntry(res);
}
});
void FoodAPI.getFoodMetricForToday().then((res) => {
if (res != null) {
setFoodEntry(res)
setFoodEntry(res);
}
});
void EnergyAPI.getEnergyMetricForToday().then((res) => {
if (res != null) {
setEnergyEntry(res)
setEnergyEntry(res);
}
});
}, [loaded]);

if (!loaded || user === undefined || transportationEntry === undefined || foodEntry === undefined || energyEntry === undefined) {
const onRefresh = (): void => {
// Simulate a refresh action (e.g., fetch new data)
setRefreshing(true);

void UsersAPI.GetLoggedInUser().then((res) => {
if (res != null) {
setUser(res);
}
});
void TransportationAPI.getTransportationMetricForToday().then((res) => {
if (res != null) {
setTransportationEntry(res);
}
});
void FoodAPI.getFoodMetricForToday().then((res) => {
if (res != null) {
setFoodEntry(res);
}
});
void EnergyAPI.getEnergyMetricForToday().then((res) => {
if (res != null) {
setEnergyEntry(res);
}
});

setTimeout(() => {
setRefreshing(false); // Set refreshing to false after data is fetched
}, 2000); // Simulating a 2-second delay
};

if (
!loaded ||
user === undefined ||
transportationEntry === undefined ||
foodEntry === undefined ||
energyEntry === undefined
) {
return <></>;
}

return (
<ScrollView style={styles.container}>
<ScrollView
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#0000ff" />
}
style={styles.container}
>
<View style={styles.profileWidgetContainer}>
<View style={styles.widgetBoarder}>
<ProfileWidgetBox
user={user}
/>
</View>
<View style={styles.widgetBoarder}>
<TouchableOpacity
onPress={() => {
navigation.navigate('FootprintDecomp');
}}
>
<CarbonWidgetBox carbonUser={user} />
</TouchableOpacity>
</View>
<View style={styles.profileWidgetContainer}>
<ChallengesWidget challenges={sampleChallenges}/>
</View>
<ProfileWidgetBox user={user} />
</View>
<View style={styles.widgetBoarder}>
<TouchableOpacity
onPress={() => {
navigation.navigate('FootprintDecomp');
}}
>
<CarbonWidgetBox carbonUser={user} />
</TouchableOpacity>
</View>
<View style={styles.profileWidgetContainer}>
<ChallengesWidget challenges={sampleChallenges} />
</View>
</View>

</ScrollView>
);
}
Expand All @@ -95,8 +133,8 @@ const styles = StyleSheet.create({
flexGrow: 1,
},
profileWidgetContainer: {
alignItems: 'center',
marginHorizontal: 10
alignItems: 'center',
marginHorizontal: 10,
},
widgetBoarder: {
padding: 10,
Expand Down
43 changes: 41 additions & 2 deletions frontend/src/screens/yourForms.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import React, { useState } from 'react';
import { Text, StyleSheet, ScrollView, View, TouchableOpacity, Image } from 'react-native';
import {
Text,
StyleSheet,
ScrollView,
View,
TouchableOpacity,
Image,
RefreshControl,
} from 'react-native';
import { useFonts } from 'expo-font';
import Colors from '../../assets/colorConstants';
import { useNavigation, useFocusEffect } from '@react-navigation/native';
Expand Down Expand Up @@ -27,6 +35,7 @@ export default function YourForms(): JSX.Element {
const [transportationEntry, setTransportationEntry] = useState<TransportationEntry>();
const [foodEntry, setFoodEntry] = useState<FoodEntry>();
const [energyEntry, setEnergyEntry] = useState<EnergyEntry>();
const [refreshing, setRefreshing] = useState(false);

useFocusEffect(
React.useCallback(() => {
Expand Down Expand Up @@ -78,6 +87,31 @@ export default function YourForms(): JSX.Element {
}, [startDate, endDate]) // Include startDate and endDate in the dependency array
);

const onRefresh = (): void => {
// Simulate a refresh action (e.g., fetch new data)
setRefreshing(true);

void TransportationAPI.getTransportationMetricForToday().then((res) => {
if (res != null) {
setTransportationEntry(res);
}
});
void FoodAPI.getFoodMetricForToday().then((res) => {
if (res != null) {
setFoodEntry(res);
}
});
void EnergyAPI.getEnergyMetricForToday().then((res) => {
if (res != null) {
setEnergyEntry(res);
}
});

setTimeout(() => {
setRefreshing(false); // Set refreshing to false after data is fetched
}, 2000); // Simulating a 2-second delay
};

if (
!loaded ||
monthlyData === undefined ||
Expand All @@ -89,7 +123,12 @@ export default function YourForms(): JSX.Element {
}

return (
<ScrollView contentContainerStyle={styles.container}>
<ScrollView
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#0000ff" />
}
contentContainerStyle={styles.container}
>
<View style={styles.tabContainer}>
<TouchableOpacity
style={styles.tab}
Expand Down