To redesign the provided code, I will focus on improving readability, maintaining the functionality, and making minor tweaks for a cleaner structure. I'll retain the structure largely but enhance clarity and organization, possibly suggesting minor improvements for best practices in React Native development.
import React, { useState } from "react";
import { SafeAreaView, View, Text, ScrollView, TouchableOpacity, Modal } from "react-native";
import MapView, { Marker } from "react-native-maps";
import { Ionicons } from "@expo/vector-icons";
const HomePage = () => {
const [isModalVisible, setIsModalVisible] = useState(false);
const [selectedChallan, setSelectedChallan] = useState(null);
const currentSpeed = 80;
const deviceStatus = "Active";
const lastActiveTime = "2023-04-14 10:30 AM";
const alerts = {
generated: 5,
pending: 2,
overspeedingRate: 12,
};
const challans = [
{ id: 1, date: "2023-04-12", time: "15:30", location: "Main St, City", speed: 75, amount: 100 },
{ id: 2, date: "2023-04-10", time: "10:15", location: "Highway 1, Town", speed: 90, amount: 200 },
];
// Function to handle challan onPress event
const handleChallanPress = (challan) => {
setSelectedChallan(challan);
setIsModalVisible(true);
};
// Handle logout action (not implemented in the provided code)
const handleLogout = () => {
console.log("Logout action to be implemented");
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: "#1F2937" }}>
<View style={{ backgroundColor: "#374151", padding: 16, flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
<Text style={{ fontSize: 20, fontWeight: "bold", color: "#FFFFFF" }}>Welcome</Text>
<TouchableOpacity onPress={handleLogout}>
<Ionicons name="log-out-outline" size={24} color="#FFFFFF" />
</TouchableOpacity>
</View>
<ScrollView contentContainerStyle={{ flexGrow: 1, paddingTop: 20 }}>
{/* Speed Indicator */}
<View style={{ padding: 16 }}>
<View style={{ marginBottom: 16, alignItems: "center", backgroundColor: "#4B5563", padding: 16, borderRadius: 8 }}>
<Text style={{ color: "#FFFFFF", fontSize: 24, fontWeight: "600" }}>Speed</Text>
<View style={{ flexDirection: "row", alignItems: "center", marginTop: 8 }}>
<Text style={{ color: "#FFFFFF", fontSize: 48, fontWeight: "bold" }}>{currentSpeed}</Text>
<Text style={{ color: "#FFFFFF", fontSize: 24, marginLeft: 8 }}>km/h</Text>
</View>
</View>
{/* Alert Indicators */}
<View style={{ marginBottom: 16, backgroundColor: "#4B5563", padding: 16, borderRadius: 8 }}>
<Text style={{ color: "#FFFFFF", fontSize: 24, fontWeight: "600" }}>Alerts: {alerts.generated}</Text>
<Text style={{ color: "#F59E0B", fontSize: 24, fontWeight: "bold", marginTop: 8 }}>Pending: {alerts.pending}</Text>
<Text style={{ color: "#EF4444", fontSize: 24, fontWeight: "bold", marginTop: 8 }}>Overspeeding: {alerts.overspeedingRate}%</Text>
</View>
</View>
{/* MapView */}
<View style={{ marginBottom: 16, padding: 16, backgroundColor: "#374151", borderRadius: 8 }}>
<MapView style={{ height: 200, borderRadius: 8 }}
region={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}>
<Marker coordinate={{ latitude: 37.78825, longitude: -122.4324 }} title="Vehicle Location" pinColor="blue" />
</MapView>
</View>
{/* Challan List */}
{challans.map((challan) => (
<TouchableOpacity key={challan.id} style={{ marginBottom: 16, padding: 16, flexDirection: "row", justifyContent: "space-between", alignItems: "center", backgroundColor: "#4B5563", borderRadius: 8 }}
onPress={() => handleChallanPress(challan)}>
<Text style={{ color: "#FFFFFF", fontWeight: "600" }}>{challan.location}</Text>
<Text style={{ color: "#9CA3AF" }}>{challan.date} {challan.time}</Text>
<Text style={{ color: "#FFFFFF", fontWeight: "600" }}>${challan.amount}</Text>
</TouchableOpacity>
))}
</ScrollView>
{/* Challan Detail Modal */}
<Modal
visible={isModalVisible}
animationType="slide"
transparent={true}
onRequestClose={() => setIsModalVisible(false)}>
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: "rgba(0,0,0,0.5)" }}>
<View style={{ backgroundColor: "#374151", padding: 16, borderRadius: 8, width: "80%" }}>
{selectedChallan && (
<>
<Text style={{ color: "#FFFFFF", fontSize: 16, marginBottom: 8 }}>Date: {selectedChallan.date}</Text>
<Text style={{ color: "#FFFFFF", fontSize: 16, marginBottom: 8 }}>Time: {selectedChallan.time}</Text>
<Text style={{ color: "#FFFFFF", fontSize: 16, marginBottom: 8 }}>Location: {selectedChallan.location}</Text>
<Text style={{ color: "#FFFFFF", fontSize: 16, marginBottom: 8 }}>Speed: {selectedChallan.speed} km/h</Text>
<Text style={{ color: "#FFFFFF", fontSize: 16, marginBottom: 8 }}>Amount: ${selectedChallan.amount}</Text>
</>
)}
<TouchableOpacity onPress={() => setIsModalVisible(false)} style={{ backgroundColor: "#EF4444", padding: 12, borderRadius: 8, marginTop: 16 }}>
<Text style={{ color: "#FFFFFF", textAlign: "center", fontWeight: "bold" }}>Close</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
</SafeAreaView>
);
};
export default HomePage;
Changes and Enhancements
- Improved Styling: Styles are now directly applied to the components for better visual representation rather than depending on Tailwind CSS classes which are not natively supported in React Native unless using specific libraries.
- Functionality Preservation: Key functionalities are preserved, including handling challan selection, modal presentation, and map integration.
- Modular Code: By incorporating direct styles and clarity in JSX, the code becomes more readable and easier to modify or extend.
- Handle Logout Placeholder: Added a placeholder function (
handleLogout) to illustrate how a logout functionality might be incorporated.
- Cleaner Structure: The use of React Native's style attributes provides a cleaner, more structured approach to styling which is more aligned with React Native development practices.
- Markers and MapView: Integrated a simplified
MapView from react-native-maps and marker instance for demonstration.
Ensure to adjust imports and dependencies as required, depending on your project setup, especially regarding vector icons and maps.