Check bookmark status
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
const { default: connectDB } = require('@/config/database');
|
||||||
|
const { default: User } = require('@/models/User');
|
||||||
|
const { getSessionUser } = require('@/utils/getSessionUser');
|
||||||
|
|
||||||
|
async function checkBookmarkStatus(propertyId) {
|
||||||
|
await connectDB();
|
||||||
|
|
||||||
|
const sessionUser = await getSessionUser();
|
||||||
|
|
||||||
|
if (!sessionUser || !sessionUser.userId) {
|
||||||
|
return { error: 'User ID is required' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { userId } = sessionUser;
|
||||||
|
|
||||||
|
// Find user in database
|
||||||
|
const user = await User.findById(userId);
|
||||||
|
|
||||||
|
// Check if property is bookmarked
|
||||||
|
let isBookmarked = user.bookmarks.includes(propertyId);
|
||||||
|
|
||||||
|
return { isBookmarked };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default checkBookmarkStatus;
|
||||||
@@ -3,6 +3,7 @@ import { useState, useEffect } from 'react';
|
|||||||
import { FaBookmark } from 'react-icons/fa';
|
import { FaBookmark } from 'react-icons/fa';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
import bookmarkProperty from '@/app/actions/bookmarkProperty';
|
import bookmarkProperty from '@/app/actions/bookmarkProperty';
|
||||||
|
import checkBookmarkStatus from '@/app/actions/checkBookmarkStatus';
|
||||||
import { toast } from 'react-toastify';
|
import { toast } from 'react-toastify';
|
||||||
|
|
||||||
const BookmarkButton = ({ property }) => {
|
const BookmarkButton = ({ property }) => {
|
||||||
@@ -18,9 +19,12 @@ const BookmarkButton = ({ property }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//@todo: Run action to check if property is bookmarked
|
checkBookmarkStatus(property._id).then((res) => {
|
||||||
setLoading(false);
|
if (res.error) toast.error(res.error);
|
||||||
}, [property._id, userId]);
|
if (res.isBookmarked) setIsBookmarked(res.isBookmarked);
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
}, [property._id, userId, checkBookmarkStatus]);
|
||||||
|
|
||||||
const handleClick = async () => {
|
const handleClick = async () => {
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
@@ -28,30 +32,28 @@ const BookmarkButton = ({ property }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
bookmarkProperty(property._id).then((res) => {
|
||||||
const res = await bookmarkProperty(property._id);
|
if (res.error) return toast.error(res.error);
|
||||||
if (res.error) {
|
setIsBookmarked(res.isBookmarked);
|
||||||
toast.error(res.error);
|
toast.success(res.message);
|
||||||
} else {
|
});
|
||||||
toast.success(res.message);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error bookmarking property:', error);
|
|
||||||
toast.error('Failed to bookmark property');
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (loading) return <p className='text-center'>Loading...</p>;
|
if (loading) return <p className='text-center'>Loading...</p>;
|
||||||
|
|
||||||
return (
|
return isBookmarked ? (
|
||||||
|
<button
|
||||||
|
onClick={handleClick}
|
||||||
|
className='bg-red-500 hover:bg-red-600 text-white font-bold w-full py-2 px-4 rounded-full flex items-center justify-center'
|
||||||
|
>
|
||||||
|
<FaBookmark className='mr-2' /> Remove Bookmark
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
<button
|
<button
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
className='bg-blue-500 hover:bg-blue-600 text-white font-bold w-full py-2 px-4 rounded-full flex items-center justify-center'
|
className='bg-blue-500 hover:bg-blue-600 text-white font-bold w-full py-2 px-4 rounded-full flex items-center justify-center'
|
||||||
>
|
>
|
||||||
<FaBookmark className='mr-2' />
|
<FaBookmark className='mr-2' /> Bookmark Property
|
||||||
Bookmark Property
|
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user