diff --git a/frontend_react/package-lock.json b/frontend_react/package-lock.json index 48eba78..bf84b6f 100644 --- a/frontend_react/package-lock.json +++ b/frontend_react/package-lock.json @@ -20,6 +20,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" @@ -15238,6 +15239,14 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz", "integrity": "sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==" }, + "node_modules/react-icons": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-5.3.0.tgz", + "integrity": "sha512-DnUk8aFbTyQPSkCfF8dbX6kQjXA9DktMeJqfjrg6cK9vwQVMxmcA3BfP4QoiztVmEHtwlTgLFsPuH2NskKT6eg==", + "peerDependencies": { + "react": "*" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", @@ -29209,6 +29218,12 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz", "integrity": "sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==" }, + "react-icons": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-5.3.0.tgz", + "integrity": "sha512-DnUk8aFbTyQPSkCfF8dbX6kQjXA9DktMeJqfjrg6cK9vwQVMxmcA3BfP4QoiztVmEHtwlTgLFsPuH2NskKT6eg==", + "requires": {} + }, "react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/frontend_react/package.json b/frontend_react/package.json index 13f4038..521996d 100644 --- a/frontend_react/package.json +++ b/frontend_react/package.json @@ -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" diff --git a/frontend_react/src/components/Blog.js b/frontend_react/src/components/Blog.js index e95c7ea..3e099ed 100644 --- a/frontend_react/src/components/Blog.js +++ b/frontend_react/src/components/Blog.js @@ -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 = () => { @@ -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 ()=>{ @@ -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) { @@ -42,7 +45,7 @@ const Blog = () => { } } fetchBlog(); - },[]) + },[id]) const handleAddComment = async () => { if (newComment.trim() === '') { @@ -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'); + } + }; @@ -130,6 +166,12 @@ const Blog = () => { Add Comment + +
+

Rate this Blog:

+ +
+ )} diff --git a/frontend_react/src/components/Blog/Card.js b/frontend_react/src/components/Blog/Card.js index 7e69a99..81d51f0 100644 --- a/frontend_react/src/components/Blog/Card.js +++ b/frontend_react/src/components/Blog/Card.js @@ -26,11 +26,11 @@ const Card = ({ cardItem }) => {
- {cardItem.comments} Comments + {cardItem.comments && cardItem.comments.length} Comments
- - {cardItem.ratings} Ratings + + {cardItem.rating} Ratings
diff --git a/frontend_react/src/components/Blog/Rating.js b/frontend_react/src/components/Blog/Rating.js new file mode 100644 index 0000000..77c08e4 --- /dev/null +++ b/frontend_react/src/components/Blog/Rating.js @@ -0,0 +1,31 @@ +import React from 'react'; +import { FaStar } from 'react-icons/fa'; + +const Rating = ({ rating, onRating }) => { + return ( +
+ {[...Array(5)].map((star, index) => { + const ratingValue = index + 1; + + return ( + + ); + })} +
+ ); +}; + +export default Rating;