Skip to content

Feature added #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions frontend_react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend_react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react-cookie": "^7.1.4",
"react-cookies": "^0.1.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.23.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
Expand Down
46 changes: 44 additions & 2 deletions frontend_react/src/components/Blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import API_BASE_URL from '../config/environment';
import { useAuth } from '../context/AuthContext';
import { useCookies } from 'react-cookie';
import { formatDistanceToNow } from 'date-fns';
import Rating from './Blog/Rating';


const Blog = () => {
Expand All @@ -14,6 +15,7 @@ const Blog = () => {
const [error, setError] = useState(null);
const [newComment, setNewComment] = useState('');
const [comments, setComments] = useState([]);
const [rating, setRating] = useState(0);

useEffect(()=>{
const fetchBlog = async ()=>{
Expand All @@ -33,7 +35,8 @@ const Blog = () => {
const data = await response.json();
//console.log('blog response data', data);
setBlog(data);
setComments(...comments, data.comments);
setComments(data.comments || []);
setRating(data.rating || 0);
setLoading(false);

} catch (error) {
Expand All @@ -42,7 +45,7 @@ const Blog = () => {
}
}
fetchBlog();
},[])
},[id])

const handleAddComment = async () => {
if (newComment.trim() === '') {
Expand Down Expand Up @@ -75,6 +78,39 @@ const Blog = () => {
setError('An error occurred while adding the comment');
}
};

const handleAddRating = async () => {
if (rating === 0) {
setError('Please select a rating');
return;
}

try {
const ratingInfo = {
rating: rating,
blog: id
}
const url = `${API_BASE_URL}/api/v1/rating/`
const response = await fetch(url, {
method: "POST",
headers:{
'Content-Type': 'application/json',
Authorization: `Bearer ${cookies.accessToken}`,
},
body: JSON.stringify(ratingInfo),
})

if (!response.ok) {
let res = await response.json();
throw new Error(res.error);
}
const data = await response.json();
//console.log('comment response data', data);
//setRating(0);
} catch (error) {
setError('An error occurred while adding the rating');
}
};



Expand Down Expand Up @@ -130,6 +166,12 @@ const Blog = () => {
Add Comment
</button>
</div>

<div className="mt-6">
<h3 className="text-lg font-semibold mb-2">Rate this Blog:</h3>
<Rating rating={rating} onRating={handleAddRating} />
</div>

</div>
)}
</div>
Expand Down
6 changes: 3 additions & 3 deletions frontend_react/src/components/Blog/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const Card = ({ cardItem }) => {
<div className="px-6 py-4 flex flex-wrap justify-between items-center border-t border-gray-200">
<div className="flex items-center text-gray-700 mb-2 md:mb-0">
<FontAwesomeIcon icon={faComments} className="mr-2" />
<span className="font-bold">{cardItem.comments}</span> Comments
<span className="font-bold">{cardItem.comments && cardItem.comments.length}</span> Comments
</div>
<div className="flex items-center text-gray-700">
<FontAwesomeIcon icon={faStar} className="mr-2" />
<span className="font-bold">{cardItem.ratings}</span> Ratings
<FontAwesomeIcon icon={faStar} className="mr-1" />
<span className="font-bold">{cardItem.rating}</span> Ratings
</div>
</div>
</div>
Expand Down
31 changes: 31 additions & 0 deletions frontend_react/src/components/Blog/Rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { FaStar } from 'react-icons/fa';

const Rating = ({ rating, onRating }) => {
return (
<div className="flex">
{[...Array(5)].map((star, index) => {
const ratingValue = index + 1;

return (
<label key={index}>
<input
type="radio"
name="rating"
value={ratingValue}
onClick={() => onRating(ratingValue)}
className="hidden"
/>
<FaStar
size={24}
color={ratingValue <= rating ? "#ffc107" : "#e4e5e9"}
className="cursor-pointer hover:fill-slate-400"
/>
</label>
);
})}
</div>
);
};

export default Rating;