Skip to content

Commit

Permalink
update add reviews on the user interface
Browse files Browse the repository at this point in the history
  • Loading branch information
devyassin committed Dec 31, 2023
1 parent 2986ffe commit 3bec9b9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
8 changes: 6 additions & 2 deletions app/api/reviews/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { compareUserId, getDataFromToken } from "@/helpers/GetDataFromToken";
import Review from "@/models/Review";
import { ReviewType } from "@/types";
import { NextRequest, NextResponse } from "next/server";
import User from "@/models/User";

connect();

export async function GET(request: NextRequest) {
try {
const userId = await getDataFromToken(request)!;
const queryObj = FromUrlToObject(request);

const query = Review.find({
gig_id: queryObj.gig_id,
}).populate("user_id");
Expand Down Expand Up @@ -49,9 +50,12 @@ export async function POST(request: NextRequest) {
const newReview = new Review(review);
const savedReview = await newReview.save();

// Populate the user_id field
const populatedReview = await savedReview.populate("user_id");

return NextResponse.json({
message: "review created",
rating: savedReview,
review: populatedReview,
});
} catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 500 });
Expand Down
4 changes: 3 additions & 1 deletion components/ui/Review.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ const Review = ({ review }: Props) => {
height={40}
quality={100}
/>
<p className="text-[14px] text-darkentwo">({review.numberOfLikes})</p>
<p className="text-[14px] text-darkentwo">
({review.numberOfLikes ? review.numberOfLikes : 0})
</p>
</div>
<hr className="mt-4 text-[#C49494] opacity-40 pb-10" />
</div>
Expand Down
9 changes: 7 additions & 2 deletions hooks/useAddNewReview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { zodHandllingErrors } from "@/helpers/ZodHandlingErrors";
import { addReview, clearReview, clearStatusReview } from "@/store/ReviewSlice";
import {
addNewReviewOnUserInterface,
addReview,
clearReview,
clearStatusReview,
} from "@/store/ReviewSlice";
import { ReviewValidation } from "@/lib/validation/ReviewValidation";
import { Toastfailed, ToastLoading, Toastsuccess } from "@/helpers/Toast";
import { useDispatch } from "react-redux";
Expand All @@ -19,7 +24,7 @@ const useAddNewReview = () => {
useEffect(() => {
if (statusAddReview === "succeeded") {
Toastsuccess("review added successfully !");

dispatch(addNewReviewOnUserInterface(review));
setTimeout(() => {
dispatch(clearReview());
dispatch(clearStatusReview());
Expand Down
12 changes: 10 additions & 2 deletions store/ReviewSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const instance = axios.create({
// get all reviews
export const getAllReviews = createAsyncThunk(
"reviews/all",
async ({ page, gig_id }: { page: number; gig_id: any }) => {
async ({ page, gig_id }: { page: number; gig_id: string }) => {
try {
const response = await instance.get(
`/reviews?page=${
Expand Down Expand Up @@ -45,7 +45,7 @@ export const addReview = createAsyncThunk(
);

// Define the initial review state
const initialState = {
const initialState: any = {
data: [],
numPage: 0,
statusAddReview: "",
Expand Down Expand Up @@ -87,6 +87,12 @@ const reviewSlice = createSlice({
clearStatusReview: (state) => {
state.statusAddReview = "";
},
addNewReviewOnUserInterface: (
state: any,
{ payload }: PayloadAction<{ newReview: any }>
) => {
console.log(payload);
},
},
extraReducers: (builder) => {
builder
Expand All @@ -95,6 +101,7 @@ const reviewSlice = createSlice({
})
.addCase(addReview.fulfilled, (state, { payload }) => {
state.statusAddReview = "succeeded";
state.data.reviews.unshift(payload.review); // Add the new review to the beginning of the array
})
.addCase(addReview.rejected, (state, { payload }: any) => {
state.statusAddReview = "failed";
Expand All @@ -119,5 +126,6 @@ export const {
clearStatusReview,
handleReviewForm,
setUserIdAndGigId,
addNewReviewOnUserInterface,
} = reviewSlice.actions;
export default reviewSlice.reducer;

0 comments on commit 3bec9b9

Please sign in to comment.