Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c3ca965
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 8, 2021
9beda6d
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 8, 2021
bbe0c30
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 9, 2021
b6ebf2e
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 10, 2021
25b998c
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 13, 2021
b6bd1f9
resolve(conflicts): resolve conflicts with deleting the book component
RawandDev Oct 14, 2021
354438c
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 14, 2021
e56c76e
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 14, 2021
abe7ea6
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 14, 2021
b056604
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 15, 2021
02807e1
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 16, 2021
300f13f
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 18, 2021
f8412c7
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 18, 2021
c17a7fb
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 18, 2021
cac7006
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 26, 2021
2569f64
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 26, 2021
06ee4f7
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 28, 2021
2d767f4
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 28, 2021
b985464
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 29, 2021
59f64d3
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 29, 2021
c854ffc
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 30, 2021
2ce4e24
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 30, 2021
983e7d8
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Oct 31, 2021
ce2fb2d
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 2, 2021
72a091a
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 2, 2021
d2a3a5d
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 2, 2021
f2fd1d2
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 2, 2021
0a4766b
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 3, 2021
28c1820
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 3, 2021
c391813
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 3, 2021
867f5f9
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 4, 2021
1f98d59
Merge branch 'dev' of github.com:Iraq-RBC-Capstones-2021/capstone-rea…
RawandDev Nov 6, 2021
d6156c8
feat(rating): add rating system
RawandDev Nov 6, 2021
e840a2d
refactor(bookcard): refactor bookcard component to latest changes
RawandDev Nov 6, 2021
c667ed4
feat(total): add total rating with toFixed
RawandDev Nov 6, 2021
2f1f485
resolve(conflicts): resolve conflicts
RawandDev Nov 8, 2021
643e66b
refactor(codes): refactor by deleting extra price with total rating
RawandDev Nov 8, 2021
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
8 changes: 6 additions & 2 deletions src/components/AddBookModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CloseButton from "../customs/CloseButton";
import { motion } from "framer-motion";
import { useFormik } from "formik";
import * as Yup from "yup";
import { setDoc, doc } from "firebase/firestore";
import { setDoc, doc, arrayUnion } from "firebase/firestore";
import { db } from "../firebase";
import { useDispatch, useSelector } from "react-redux";
import { addBooks } from "../store/books/booksSlice";
Expand Down Expand Up @@ -94,7 +94,7 @@ function AddBookModal({ isAddBookModalOpen, setIsAddBookModalOpen }) {
bookTitle: formik.values.bookTitle,
author: formik.values.author,
genres: formik.values.genres,
price: !formik.values.isChecked ? formik.values.price : 0,
price: !formik.values.isChecked ? formik.values.price || 0 : 0,
description: formik.values.description,
image: fileUrl,
isChecked: formik.values.isChecked,
Expand All @@ -103,6 +103,10 @@ function AddBookModal({ isAddBookModalOpen, setIsAddBookModalOpen }) {
// rating: 0,
uid: userUID,
images: [],
ratersUID: arrayUnion(),
rating: 0,
totalRaters: 0,
totalRating: 0,
});

dispatch(addBooks({ ...formik.values, id: uniqueID, uid: userUID }));
Expand Down
82 changes: 80 additions & 2 deletions src/components/BookCard.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
// import ReactStars from "react-rating-stars-component";
import React, { useState, useEffect } from "react";
import ReactStars from "react-rating-stars-component";
import { Link, useHistory } from "react-router-dom";
import { BOOKS_ROUTE } from "../routes";
import { AiOutlineHeart, AiFillHeart, AiOutlineDelete } from "react-icons/ai";
Expand All @@ -21,6 +21,8 @@ import { changeDropdown } from "../store/dropdownSlice";
import { useTranslation } from "react-i18next";
import MoonLoader from "react-spinners/MoonLoader";
import { deleteBook } from "../store/books/booksSlice";
import { toast } from "react-toastify";
import { setRatings } from "../store/ratingsSlice";

function BookCard({ user_uid, image, genres, title, price, rating, id }) {
const user = useSelector(selectorUser);
Expand All @@ -36,6 +38,9 @@ function BookCard({ user_uid, image, genres, title, price, rating, id }) {
dispatch(changeDropdown(genre));
}

const [currentRating, setCurrentRating] = useState(rating);
const books = useSelector((state) => state.books);

const genreButtons = genres?.map((genre, index) => {
return (
<button
Expand Down Expand Up @@ -80,6 +85,69 @@ function BookCard({ user_uid, image, genres, title, price, rating, id }) {
}
};

async function setRating(newR) {
const washingtonRef = doc(db, "books", `${id}`);

const totalRating =
books.books.find((book) => book.id === id).totalRating + newR;

const totalRaters =
books.books.find((book) => book.id === id).totalRaters + 1;

const rating = totalRating / totalRaters;

if (!user.uid) {
return;
} else {
if (
books.books.find((book) => book.id === id).ratersUID.includes(user.uid)
) {
toast.error("You already rated this book");
return;
} else {
if (totalRating && totalRaters && rating) {
await updateDoc(washingtonRef, {
rating: rating.toFixed(1),
totalRating: totalRating,
totalRaters: totalRaters,
ratersUID: arrayUnion(user.uid),
});
dispatch(
setRatings({
totalRaters: totalRaters + 1,
totalRating: totalRating + newR,
rating: totalRating / totalRaters,
ratersUID: arrayUnion(user.uid),
})
);
toast.success("You rated this book successfully");
} else {
await updateDoc(washingtonRef, {
rating: newR,
totalRating: newR,
totalRaters: 1,
ratersUID: arrayUnion(user.uid),
});
dispatch(
setRatings({
totalRaters: 1,
totalRating: newR,
rating: newR,
ratersUID: arrayUnion(user.uid),
})
);
toast.success("You rated this book successfully");
}
}
}
}

useEffect(() => {
if (books.books.find((book) => book.id === id)) {
setCurrentRating(books.books.find((book) => book.id === id).rating);
}
}, [books.books, id]);

return (
<>
<div className="bg-cards rounded-xl font-sans overflow-hidden shadow-md w-full lg:w-72 p-3 transform transition ease-in-out duration-200 hover:-translate-y-0.5 mx-auto md:mt-5 mb-1 sm:mb-2 md:mb-0">
Expand Down Expand Up @@ -134,6 +202,16 @@ function BookCard({ user_uid, image, genres, title, price, rating, id }) {
<div className="flex justify-between items-center pr-1">
<p className="font-semibold pl-1"> $ {price}</p>
</div>
<div className="flex justify-start items-center">
<ReactStars
className=""
size={20}
isHalf={true}
value={currentRating}
onChange={(newR) => setRating(newR)}
/>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user shouldn't be able to rate if he's not signed in, but he could see the rates only

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already implemented

<span className="pl-1 mt-1">{currentRating}</span>
</div>
<Link to={`${BOOKS_ROUTE}/${id}`} className="text-white font-semibold">
<button className="bg-secondary text-white rounded-xl p-1 w-full mt-3 transform transition ease-in-out duration-100 hover:-translate-y-0.5">
{t("buy")}
Expand Down
24 changes: 24 additions & 0 deletions src/store/ratingsSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSlice } from "@reduxjs/toolkit";

const initialState = {
totalRaters: 0,
totalRating: 0,
rating: 0,
ratersUID: [],
};

const ratingsSlice = createSlice({
name: "ratings",
initialState,
reducers: {
setRatings: (state, action) => {
state.totalRaters = action.payload.totalRaters;
state.totalRating = action.payload.totalRating;
state.rating = action.payload.rating;
state.ratersUID = action.payload.ratersUID;
},
},
});

export const { setRatings } = ratingsSlice.actions;
export default ratingsSlice.reducer;