Skip to content

Commit 57ba818

Browse files
authored
Merge pull request #193 from atlp-rwanda/ft-add-remove-favoriteDoctors
Add functionality to manage favorite doctors (add/remove)
2 parents c86e3df + 3f41b08 commit 57ba818

File tree

8 files changed

+717
-102
lines changed

8 files changed

+717
-102
lines changed

app/(app)/ActionMenu/AllDoctorScreen.tsx

Lines changed: 126 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { AntDesign } from '@expo/vector-icons';
55
import { Ionicons } from '@expo/vector-icons';
66
import DoctorComponent from '@/components/DoctorComponent';
77
import { FontAwesome } from '@expo/vector-icons';
8-
import { SvgXml } from "react-native-svg"
9-
import { WhiteHeart } from '@/components/UI/icons/WhiteHeart';
8+
import { SvgXml } from "react-native-svg"
9+
import { whiteHeart } from '@/assets/icons/whiteHeart';
10+
import { blueheart } from '@/assets/icons/blueHeart';
1011
import { star } from '@/assets/icons/star';
1112
import { search } from '@/assets/icons/search';
1213
import { more } from '@/assets/icons/more';
@@ -27,6 +28,7 @@ import { router } from 'expo-router';
2728

2829

2930
const tableName = "doctors";
31+
const favoriteTable="favorite_doctors"
3032

3133
interface imageMapProp {
3234
[key: string]: ReturnType<typeof require>;
@@ -47,25 +49,65 @@ interface Doctor{
4749
}
4850

4951
export const iconMapping: iconMappingProp = {
50-
heart: <SvgXml xml={WhiteHeart} />,
52+
heart: <SvgXml xml={whiteHeart} />,
5153
star: <SvgXml xml={star} />,
5254
};
5355

5456
function DoctorScreen() {
5557
const [showSearch, setShowSearch] = useState<boolean>(false);
5658
const [searchTerm, setSearchTerm] = useState<string>("");
5759
const [showpopUp, setShowPopup] = useState(false);
58-
const [selectedDoctor, setSelectedDoctor] = useState();
60+
const [selectedDoctor, setSelectedDoctor] = useState<Doctor>();
5961
const [showFilter, setShowfilter] = useState(false);
6062
const [doctors, setDoctors] = useState<Doctor[]>([]);
6163
const { theme, changeTheme } = useContext(ThemeContext);
6264
const [selectedSpecilization, setSelectedSpecilization] = useState<string>("All")
63-
const [specialization,setSpecialization]=useState<string[]>([])
65+
const [specialization, setSpecialization] = useState<string[]>([])
66+
const [favoriteDoctors,setFavoriteDoctors]=useState<number[]>([])
6467
const containerStyle =
6568
theme === "dark" ? styles.outerDark : styles.outerLight;
6669
const scrollbackColor =
6770
theme === "dark" ? styles.scrollDark : styles.scrollLight;
71+
const [loggeduser, setLoggedUser] = useState<string>()
72+
const [profile, setProfile] = useState<any>(null)
73+
const [patient_id,setPatient_id]=useState<string>()
6874

75+
useEffect(() => {
76+
const fetchUser = async () => {
77+
78+
const { data: { user },error } = await supabase.auth.getUser()
79+
if (error) {
80+
console.error("error fetching user")
81+
} else {
82+
setLoggedUser(user?.id)
83+
}
84+
}
85+
fetchUser()
86+
87+
}, [loggeduser])
88+
useEffect(() => {
89+
const fetchUserProfile = async () => {
90+
if (loggeduser) {
91+
const { data, error } = await supabase
92+
.from("patients")
93+
.select("*")
94+
.eq('auth_id', loggeduser)
95+
.single()
96+
if (error) {
97+
console.error("error while retrieving profile",error)
98+
} else {
99+
setProfile(data)
100+
setPatient_id(data.id)
101+
console.log(data)
102+
103+
}
104+
105+
106+
}
107+
}
108+
fetchUserProfile()
109+
}, [loggeduser])
110+
69111
useEffect(() => {
70112
async function fetchData() {
71113
const { data, error } = await supabase.from(tableName).select("*");
@@ -83,7 +125,25 @@ function DoctorScreen() {
83125

84126
fetchData();
85127
}, []);
86-
console.log("this is retrived specilization:",specialization)
128+
console.log("this is retrived specilization:", specialization)
129+
useEffect(() => {
130+
const fetchFavoritesDoctor = async () => {
131+
if (patient_id) {
132+
const { data, error } = await supabase
133+
.from(favoriteTable)
134+
.select("*")
135+
.eq("patient", patient_id)
136+
if (error) {
137+
console.error("error while fetching ",error)
138+
}
139+
140+
setFavoriteDoctors(data?.map((item:any)=>item.favorite_doctor)||[])
141+
}
142+
143+
}
144+
fetchFavoritesDoctor()
145+
146+
},[patient_id])
87147
const handleSearchPressed = () => {
88148
setShowSearch(true)
89149
}
@@ -94,16 +154,43 @@ function DoctorScreen() {
94154
const handleFilter = () => {
95155
setShowfilter(true)
96156
}
97-
const handleRemove = (doctor:any) => {
98-
setSelectedDoctor(doctor)
99-
100-
setShowPopup(true)
101-
}
157+
102158
const handleSpecializationChange = (specialization: string) => {
103159
setSelectedSpecilization(specialization)
104160
setSearchTerm('')
105161

106162
}
163+
const handleAddfovorite = async (doctorId: number) => {
164+
const patientId = patient_id
165+
const { error } = await supabase.from(favoriteTable).insert({ patient: patientId, favorite_doctor: doctorId })
166+
if (error) {
167+
console.error("error while adding doctor to favorite", error)
168+
return;
169+
}
170+
setFavoriteDoctors(prev=>[...prev,doctorId])
171+
172+
}
173+
const updateFavoriteDoctors = async () => {
174+
const { data, error } = await supabase
175+
.from("favorite_doctors")
176+
.select("favorite_doctor")
177+
.eq("patient", patient_id);
178+
if (error) {
179+
console.error("Error fetching favorite doctors:", error);
180+
} else {
181+
setFavoriteDoctors(data.map((item: { favorite_doctor: number }) => item.favorite_doctor));
182+
}
183+
};
184+
const handleIconClick = (doctor: Doctor,doctorId:number) => {
185+
if (favoriteDoctors.includes(doctor.id)) {
186+
setSelectedDoctor(doctor);
187+
setShowPopup(true);
188+
} else {
189+
handleAddfovorite(doctorId);
190+
}
191+
};
192+
193+
107194
const filteredDoctors = doctors.filter(doctor => {
108195
const matchSearchTerm = searchTerm.length > 0 ? doctor.last_name.toLowerCase().includes(searchTerm.toLowerCase())||doctor.first_name.toLowerCase().includes(searchTerm.toLowerCase()) : true
109196
const matchSpecialization = selectedSpecilization === 'All' || doctor.specialization === selectedSpecilization
@@ -176,27 +263,34 @@ function DoctorScreen() {
176263
paddingTop:20
177264
}}
178265
>
179-
{filteredDoctors.length > 0 ? (
266+
{filteredDoctors.length > 0 ? (
180267

181-
filteredDoctors.map((doctor: any, index: any) =>
182-
268+
filteredDoctors.map((doctor: any, index: number) => {
269+
270+
271+
return(
183272
<View key={index} style={styles.componentView}>
184-
<DoctorComponent
185-
path={() => router.push({ pathname: "/ActionMenu/Booking/Doctor_details",params:{id:doctor.id} }) }
186-
imageSource={{uri:doctor.image}}
187-
name={`${doctor.first_name} ${doctor.last_name}`}
188-
iconComponent={<SvgXml xml={WhiteHeart } />}
189-
professionalTitle={doctor.specialization}
190-
hospital={doctor.hospital_name}
191-
star={<SvgXml xml={star} />}
192-
review={doctor.review}
193-
rate={doctor.rate}
194-
remove={()=>handleRemove(doctor)}
273+
<DoctorComponent
274+
path={() => router.push({ pathname: "/ActionMenu/Booking/Doctor_details", params: { id: doctor.id } })}
275+
imageSource={{ uri: doctor.image }}
276+
name={`${doctor.first_name} ${doctor.last_name}`}
277+
iconComponent={favoriteDoctors.includes(doctor.id) ? (
278+
<SvgXml xml={blueheart} />
279+
) : (
280+
<SvgXml xml={whiteHeart} />
281+
)}
282+
professionalTitle={doctor.specialization}
283+
hospital={doctor.hospital_name}
284+
star={<SvgXml xml={star} />}
285+
review={doctor.review}
286+
rate={doctor.rate}
287+
addRemoveFavorite={() => handleIconClick(doctor,doctor.id) }
288+
195289

196-
/>
197-
</View>
290+
/>
291+
</View>
198292

199-
)
293+
)})
200294

201295
) : (
202296
<NofoundComponent/>
@@ -210,11 +304,13 @@ function DoctorScreen() {
210304

211305

212306
</View>
213-
<RemovefavoritePopup
307+
<RemovefavoritePopup
308+
userId={patient_id}
214309
cancel={()=>setShowPopup(false)}
215310
visible={showpopUp}
216311
onClose={() => setShowPopup(false)}
217-
doctor={selectedDoctor}
312+
doctor={selectedDoctor}
313+
updateFavoriteDoctors={updateFavoriteDoctors}
218314

219315

220316
/>

0 commit comments

Comments
 (0)