إليك تطبيق مكتوب باستخدام React Native الإصدار 0.75 و Expo يتيح حجز موعد مع طبيب. قمت بتنظيم الكود بشكل جيد لتحسين الأداء وسهولة الصيانة، واستخدمت الـAPI المطلوب https://dc.me/api .
المتطلبات الأساسية:
تأكد من تثبيت بيئة تطوير React Native و Expo على جهازك.
تنصيب التبعيات:
قم بتنصيب التبعيات اللازمة باستخدام الأوامر التالية:
bash Copy code
npm install @react-navigation/native @react-navigation/bottom-tabs @react-navigation/stack
npx expo install react-native-screens react-native-safe-area-context
هيكلية المجلدات:
code Copy code
- App.js
- screens/
- HomeScreen.js
- AppointmentsScreen.js
- BookingScreen.js
- components/
- DoctorInfoHeader.js
- SpecialtyCard.js
- navigation/
- BottomTabNavigator.js
الكود:
App.js
jsx Copy code
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import BottomTabNavigator from './navigation/BottomTabNavigator';
export default function App() {
return (
<NavigationContainer>
<BottomTabNavigator />
</NavigationContainer>
);
}
navigation/BottomTabNavigator.js
jsx Copy code
import React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import HomeStackNavigator from './HomeStackNavigator';
import AppointmentsScreen from '../screens/AppointmentsScreen';
const Tab = createBottomTabNavigator();
const BottomTabNavigator = () => {
return (
<Tab.Navigator>
<Tab.Screen name="الرئيسية" component={HomeStackNavigator} />
<Tab.Screen name="المواعيد" component={AppointmentsScreen} />
</Tab.Navigator>
);
};
export default BottomTabNavigator;
navigation/HomeStackNavigator.js
jsx Copy code
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from '../screens/HomeScreen';
import BookingScreen from '../screens/BookingScreen';
const Stack = createStackNavigator();
const HomeStackNavigator = () => {
return (
<Stack.Navigator>
<Stack.Screen name="الرئيسية" component={HomeScreen} />
<Stack.Screen name="حجز موعد" component={BookingScreen} />
</Stack.Navigator>
);
};
export default HomeStackNavigator;
screens/HomeScreen.js
jsx Copy code
import React, { useEffect, useState } from 'react';
import { View, ScrollView, ActivityIndicator } from 'react-native';
import DoctorInfoHeader from '../components/DoctorInfoHeader';
import SpecialtyCard from '../components/SpecialtyCard';
const HomeScreen = ({ navigation }) => {
const [specialties, setSpecialties] = useState([]);
const [loading, setLoading] = useState(true);
// جلب التخصصات من الـAPI
useEffect(() => {
fetch('https://dc.me/api/specialties')
.then((response) => response.json())
.then((data) => {
setSpecialties(data);
setLoading(false);
})
.catch((error) => {
console.error(error);
setLoading(false);
});
}, []);
if (loading) {
return <ActivityIndicator size="large" color="#0000ff" />;
}
return (
<ScrollView>
<DoctorInfoHeader navigation={navigation} />
{/* قسم الحجوزات النشطة */}
{/* قسم تخصصات الطبيب */}
{specialties.map((specialty) => (
<SpecialtyCard
key={specialty.id}
specialty={specialty}
navigation={navigation}
/>
))}
</ScrollView>
);
};
export default HomeScreen;
components/DoctorInfoHeader.js
jsx Copy code
import React, { useEffect, useState } from 'react';
import { View, Text, Button, StyleSheet, ActivityIndicator } from 'react-native';
const DoctorInfoHeader = ({ navigation }) => {
const [doctorInfo, setDoctorInfo] = useState(null);
const [loading, setLoading] = useState(true);
// جلب بيانات الطبيب من الـAPI
useEffect(() => {
fetch('https://dc.me/api/doctor')
.then((response) => response.json())
.then((data) => {
setDoctorInfo(data);
setLoading(false);
})
.catch((error) => {
console.error(error);
setLoading(false);
});
}, []);
if (loading) {
return <ActivityIndicator size="small" color="#0000ff" />;
}
if (!doctorInfo) {
return null;
}
return (
<View style={styles.container}>
<Text style={styles.name}>{doctorInfo.name}</Text>
<Text style={styles.specialty}>{doctorInfo.specialty}</Text>
<Text style={styles.address}>العنوان: {doctorInfo.address}</Text>
<Text style={styles.hours}>ساعات العمل: {doctorInfo.workingHours}</Text>
<Button
title="حجز موعد"
onPress={() => navigation.navigate('حجز موعد')}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
padding: 16,
backgroundColor: '#fff',
},
name: {
fontSize: 24,
fontWeight: 'bold',
},
specialty: {
fontSize: 18,
color: 'gray',
},
address: {
marginTop: 8,
},
hours: {
marginBottom: 8,
},
});
export default DoctorInfoHeader;
components/SpecialtyCard.js
jsx Copy code
import React from 'react';
import { View, Text, Image, Button, StyleSheet } from 'react-native';
const SpecialtyCard = ({ specialty, navigation }) => {
const { title, price, image } = specialty;
return (
<View style={styles.card}>
<Image source={{ uri: image }} style={styles.image} />
<Text style={styles.title}>{title}</Text>
<Text style={styles.price}>السعر: {price} دولار</Text>
<Button
title="حجز"
onPress={() => navigation.navigate('حجز موعد', { specialty })}
/>
</View>
);
};
const styles = StyleSheet.create({
card: {
margin: 16,
backgroundColor: '#fff',
borderRadius: 8,
overflow: 'hidden',
elevation: 2,
},
image: {
height: 150,
width: '100%',
},
title: {
fontSize: 18,
margin: 8,
},
price: {
fontSize: 16,
marginHorizontal: 8,
marginBottom: 8,
color: 'gray',
},
});
export default SpecialtyCard;
screens/AppointmentsScreen.js
jsx Copy code
import React, { useEffect, useState } from 'react';
import { View, Text, ScrollView, StyleSheet, ActivityIndicator } from 'react-native';
const AppointmentsScreen = () => {
const [activeAppointments, setActiveAppointments] = useState([]);
const [previousAppointments, setPreviousAppointments] = useState([]);
const [loading, setLoading] = useState(true);
// جلب المواعيد من الـAPI
useEffect(() => {
fetch('https://dc.me/api/appointments')
.then((response) => response.json())
.then((data) => {
setActiveAppointments(data.active);
setPreviousAppointments(data.previous);
setLoading(false);
})
.catch((error) => {
console.error(error);
setLoading(false);
});
}, []);
if (loading) {
return <ActivityIndicator size="large" color="#0000ff" />;
}
return (
<ScrollView>
<Text style={styles.sectionTitle}>المواعيد النشطة</Text>
{/* عرض المواعيد النشطة */}
{activeAppointments.map((appointment) => (
<View key={appointment.id} style={styles.appointment}>
<Text>{appointment.details}</Text>
</View>
))}
<Text style={styles.sectionTitle}>المواعيد السابقة</Text>
{/* عرض المواعيد السابقة */}
{previousAppointments.map((appointment) => (
<View key={appointment.id} style={styles.appointment}>
<Text>{appointment.details}</Text>
</View>
))}
</ScrollView>
);
};
const styles = StyleSheet.create({
sectionTitle: {
fontSize: 20,
margin: 16,
fontWeight: 'bold',
},
appointment: {
marginHorizontal: 16,
marginBottom: 8,
padding: 16,
backgroundColor: '#fff',
borderRadius: 8,
},
});
export default AppointmentsScreen;
screens/BookingScreen.js
jsx Copy code
import React, { useState } from 'react';
import { View, TextInput, Button, StyleSheet, Alert } from 'react-native';
const BookingScreen = ({ route, navigation }) => {
const { specialty } = route.params || {};
const [name, setName] = useState('');
const [phone, setPhone] = useState('');
const [appointmentDate, setAppointmentDate] = useState('');
const [appointmentType, setAppointmentType] = useState(specialty ? specialty.title : '');
const handleBooking = () => {
// تنفيذ عملية الحجز باستخدام الـAPI
const bookingData = {
name,
phone,
appointmentDate,
appointmentType,
};
fetch('https://dc.me/api/bookings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(bookingData),
})
.then((response) => response.json())
.then((data) => {
Alert.alert('تم الحجز بنجاح');
navigation.goBack();
})
.catch((error) => {
console.error(error);
Alert.alert('حدث خطأ أثناء الحجز');
});
};
return (
<View style={styles.container}>
<TextInput
placeholder="الاسم"
value={name}
onChangeText={setName}
style={styles.input}
/>
<TextInput
placeholder="رقم الهاتف"
value={phone}
onChangeText={setPhone}
style={styles.input}
keyboardType="phone-pad"
/>
<TextInput
placeholder="تاريخ الحجز المطلوب"
value={appointmentDate}
onChangeText={setAppointmentDate}
style={styles.input}
/>
<TextInput
placeholder="نوع الحجز"
value={appointmentType}
onChangeText={setAppointmentType}
style={styles.input}
/>
<Button title="تأكيد الحجز" onPress={handleBooking} />
</View>
);
};
const styles = StyleSheet.create({
container: {
padding: 16,
},
input: {
borderBottomWidth: 1,
marginBottom: 16,
},
});
export default BookingScreen;
ملاحظات:
جلب البيانات من الـAPI: تم إضافة أكواد جلب البيانات من الـAPI باستخدام fetch. تأكد من استبدال الروابط بالمسارات المناسبة المتوفرة من خلال الـAPI الخاص بك.
معالجة حالات التحميل والأخطاء: تم استخدام ActivityIndicator لإظهار مؤشر الانتظار أثناء تحميل البيانات، ومعالجة الأخطاء بواسطة عرض رسائل في وحدة التحكم أو باستخدام Alert.
التنقل بين الشاشات: تم استخدام Stack Navigator للتنقل بين الشاشة الرئيسية وشاشة الحجز، وتم تمرير المعلومات اللازمة عبر route.params.
تحسين الأداء وتجربة المستخدم: يمكن تحسين تجربة المستخدم بإضافة ميزات إضافية مثل التحقق من صحة الحقول وتنسيقات التاريخ.
التنسيقات والتصميم: استخدمنا StyleSheet لتنسيق العناصر. يمكنك تخصيص التصميم وفقًا لاحتياجاتك.
استخدام مكتبات إضافية: إذا كنت ترغب في استخدام مكتبات مثل axios لجلب البيانات، أو مكتبات لاختيار التاريخ، فيمكنك إضافتها حسب الحاجة.
الخلاصة:
هذا الكود يقدم تطبيقًا أساسيًا لتحقيق المتطلبات التي طلبتها. يجب عليك تكملة وتخصيص الكود بناءً على تفاصيل الـAPI الخاص بك والميزات الإضافية التي ترغب في تضمينها. تأكد من اتباع أفضل الممارسات في كتابة الكود للحفاظ على الأداء وسهولة الصيانة.