Skip to content

Commit 851a4bf

Browse files
committed
favorites functionality added
1 parent 1b0cdd4 commit 851a4bf

File tree

4 files changed

+190
-93
lines changed

4 files changed

+190
-93
lines changed

src/components/MeditationCard.tsx

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react'; // Import useEffect
32
import { cn } from '@/lib/utils';
43
import { Play, Pause, Heart } from 'lucide-react';
54
import { Button } from '@/components/ui/button';
@@ -13,13 +12,17 @@ import {
1312
} from '@/components/ui/card';
1413
import { Badge } from '@/components/ui/badge';
1514
import { Progress } from '@/components/ui/progress';
15+
import { useAuth } from '@/utils/auth';
16+
import { db } from '@/utils/firebase';
17+
import { doc, updateDoc, arrayUnion, arrayRemove, getDoc } from 'firebase/firestore';
1618

1719
interface MeditationCardProps {
1820
id: string;
1921
title: string;
2022
description: string;
2123
duration: number; // in minutes
2224
category: string;
25+
favorite: boolean;
2326
imageUrl?: string;
2427
className?: string;
2528
}
@@ -30,6 +33,7 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
3033
description,
3134
duration,
3235
category,
36+
favorite,
3337
imageUrl,
3438
className,
3539
}) => {
@@ -38,6 +42,23 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
3842
const [progress, setProgress] = useState(0);
3943
const [elapsedTime, setElapsedTime] = useState(0);
4044
let intervalRef: NodeJS.Timeout | null = null;
45+
const { currentUser } = useAuth(); // Get the current user from auth context
46+
47+
useEffect(() => {
48+
const fetchFavoriteStatus = async () => {
49+
if (currentUser) {
50+
const userDocRef = doc(db, 'users', currentUser.uid);
51+
const userDocSnap = await getDoc(userDocRef);
52+
53+
if (userDocSnap.exists()) {
54+
const userData = userDocSnap.data();
55+
setIsFavorited(userData.meditations?.includes(id) || false);
56+
}
57+
}
58+
};
59+
60+
fetchFavoriteStatus();
61+
}, [currentUser, id]); // Re-run when currentUser or id changes
4162

4263
const handlePlayPause = () => {
4364
if (isPlaying) {
@@ -47,7 +68,7 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
4768
setIsPlaying(true);
4869
const totalSeconds = duration * 60;
4970
const incrementPerInterval = 100 / (totalSeconds / 0.1); // Update every 100ms
50-
71+
5172
intervalRef = setInterval(() => {
5273
setProgress(prev => {
5374
const newProgress = prev + incrementPerInterval;
@@ -58,7 +79,7 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
5879
}
5980
return newProgress;
6081
});
61-
82+
6283
setElapsedTime(prev => {
6384
const newTime = prev + 0.1;
6485
return newTime;
@@ -67,8 +88,31 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
6788
}
6889
};
6990

70-
const handleFavorite = () => {
71-
setIsFavorited(!isFavorited);
91+
const handleFavorite = async () => {
92+
if (!currentUser) {
93+
console.error('User not authenticated');
94+
return;
95+
}
96+
97+
const userDocRef = doc(db, 'users', currentUser.uid);
98+
99+
try {
100+
if (!isFavorited) {
101+
await updateDoc(userDocRef, {
102+
meditations: arrayUnion(id),
103+
});
104+
console.log('Added to favorites');
105+
} else {
106+
await updateDoc(userDocRef, {
107+
meditations: arrayRemove(id),
108+
});
109+
console.log('Removed from favorites');
110+
}
111+
112+
setIsFavorited(!isFavorited); // Update local state *after* Firebase update
113+
} catch (error) {
114+
console.error('Error updating favorites:', error);
115+
}
72116
};
73117

74118
const formatTime = (totalSeconds: number) => {
@@ -81,9 +125,9 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
81125
<Card className={cn("overflow-hidden transition-all duration-300 ease-apple hover:shadow-md", className)}>
82126
{imageUrl && (
83127
<div className="h-48 overflow-hidden">
84-
<img
85-
src={imageUrl}
86-
alt={title}
128+
<img
129+
src={imageUrl}
130+
alt={title}
87131
className="w-full h-full object-cover transition-transform duration-500 ease-apple hover:scale-105"
88132
/>
89133
</div>
@@ -101,11 +145,11 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
101145
onClick={handleFavorite}
102146
className="mt-0"
103147
>
104-
<Heart
148+
<Heart
105149
className={cn(
106-
"h-5 w-5 transition-all duration-300 ease-apple",
150+
"h-5 w-5 transition-all duration-300 ease-apple",
107151
isFavorited ? "fill-destructive text-destructive" : "text-muted-foreground"
108-
)}
152+
)}
109153
/>
110154
</Button>
111155
</div>
@@ -120,8 +164,8 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
120164
<div className="text-sm text-muted-foreground">
121165
{duration} min
122166
</div>
123-
<Button
124-
variant="secondary"
167+
<Button
168+
variant="secondary"
125169
size="sm"
126170
onClick={handlePlayPause}
127171
className="transition-all duration-200 ease-apple flex items-center"
@@ -143,4 +187,4 @@ const MeditationCard: React.FC<MeditationCardProps> = ({
143187
);
144188
};
145189

146-
export default MeditationCard;
190+
export default MeditationCard;

src/components/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const Navbar: React.FC = () => {
4848
const usersRef = collection(db, "users");
4949
const q = query(usersRef, where("email", "==", currentUser.email));
5050
const querySnapshot = await getDocs(q);
51-
console.log(q);
51+
// console.log(q);
5252
if (!querySnapshot.empty) {
5353
// Assuming only one document matches the email.
5454
const userDoc = querySnapshot.docs[0].data();

0 commit comments

Comments
 (0)