-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first commit for Comment Fuctionality
- Loading branch information
1 parent
cef636a
commit 03c90f7
Showing
6 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewCommentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.neuefische.team2.backend.restaurant.domain; | ||
|
||
public record NewCommentDTO( | ||
String text | ||
|
||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useState, useEffect } from "react"; | ||
import axios from "axios"; | ||
|
||
type CommentsSectionProps = { | ||
restaurantId: string; | ||
}; | ||
|
||
function CommentsSection({ restaurantId }: CommentsSectionProps) { | ||
const [comments, setComments] = useState<{ text: string }[]>([]); | ||
const [newComment, setNewComment] = useState<{ text: string }>({ text: "" }); | ||
|
||
useEffect(() => { | ||
axios | ||
.get(`/api/restaurants/${restaurantId}/comments`) | ||
.then((response) => { | ||
setComments(response.data); | ||
}) | ||
.catch((error) => { | ||
console.error("There was an error fetching the comments!", error); | ||
}); | ||
}, [restaurantId]); | ||
|
||
const handleAddComment = () => { | ||
axios | ||
.post(`/api/restaurants/${restaurantId}/comments`, newComment) | ||
.then((response) => { | ||
setComments(response.data.comments); | ||
setNewComment({ text: "" }); | ||
}) | ||
.catch((error) => { | ||
console.error("There was an error adding the comment!", error); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h3>Comments</h3> | ||
<input | ||
type="text" | ||
value={newComment.text} | ||
onChange={(e) => setNewComment({ text: e.target.value })} | ||
placeholder="Add a comment" | ||
/> | ||
<button onClick={handleAddComment}>Add Comment</button> | ||
<ul> | ||
{comments.length > 0 ? ( | ||
comments.map((comment, index) => <li key={index}>{comment.text}</li>) | ||
) : ( | ||
<li>No comments available.</li> | ||
)} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
export default CommentsSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters