Skip to content

Commit af14a74

Browse files
committed
It is now possible to vote via chat box.
1 parent befe7d4 commit af14a74

File tree

7 files changed

+66
-23
lines changed

7 files changed

+66
-23
lines changed

app/src/App.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,19 @@
854854
padding: 8px;
855855
border-radius: 5px;
856856
}
857+
858+
.chat-buttons > button {
859+
background-color: transparent;
860+
border: none;
861+
cursor: pointer;
862+
padding: 5px;
863+
margin: 2px;
864+
font-weight: 600;
865+
}
866+
867+
.chat-buttons {
868+
display: flex;
869+
justify-content: center;
870+
width: 30%;
871+
margin-right: 5px;
872+
}

app/src/components/Chat.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,32 @@ export default function Chat({ botId }) {
2525
// Copy code to clipboard (implementation required)
2626
// Alert message (implementation required)
2727
};
28+
29+
const votePositive = () => {
30+
sendVote(1);
31+
};
32+
33+
const voteNegative = () => {
34+
sendVote(-1);
35+
};
36+
37+
const sendVote = (vote) => {
38+
// Determine the chatId for the vote
39+
const chatId = messages.length > 0 ? messages[messages.length - 1].chatId : -1;
40+
41+
// Sending a POST request to vote
42+
axios.post(`http://localhost:8080/api/botdata/${botId}/chats/${chatId}/${vote}`)
43+
.then(response => {
44+
// Handle successful vote response if needed
45+
console.log(`Voted ${vote === 1 ? 'positive' : 'negative'}`);
46+
})
47+
.catch(error => {
48+
console.error('Error sending vote:', error);
49+
});
50+
};
2851

2952
const sendMessage = (e) => {
30-
e.preventDefault(); // Prevents the default form submission behavior
53+
e.preventDefault();
3154

3255
if (inputText.trim() === '') return;
3356

@@ -40,7 +63,6 @@ export default function Chat({ botId }) {
4063
const chatId = messages.length === 0 ? -1 : messages[messages.length - 1].chatId;
4164
axios.post(`http://localhost:8080/api/bots/${botId}/chat/${chatId}`, {
4265
question: inputText,
43-
// Include any necessary data for the message
4466
})
4567
.then(response => {
4668
let newAssistantMessage = response.data.answer.content;
@@ -62,12 +84,22 @@ export default function Chat({ botId }) {
6284
});
6385
};
6486

65-
6687
return (
6788
<div className='message-body'>
6889
<div className="chat-header">
6990
<span style={{ margin: '15px' }}>{botName}</span>
70-
<button style={{ margin: '15px' }} onClick={generateCode}>{'</>'}</button>
91+
<div className='chat-buttons'>
92+
<button style={{ color: 'white' }} onClick={generateCode}>{'</>'}</button>
93+
<button onClick={votePositive}>
94+
<span style={{ color: 'white', display: 'flex', alignItems: 'center' }}
95+
className="material-symbols-outlined">thumb_up</span>
96+
</button>
97+
<button onClick={voteNegative}>
98+
<span style={{ color: 'white', display: 'flex', alignItems: 'center' }}
99+
className="material-symbols-outlined">thumb_down
100+
</span>
101+
</button>
102+
</div>
71103
</div>
72104
<div className='chat-main'>
73105
<div className="chat-messages">
@@ -92,7 +124,7 @@ export default function Chat({ botId }) {
92124
onChange={(e) => setInputText(e.target.value)}
93125
/>
94126
<button className="message-send-btn" type="submit">
95-
<span style={{ color: 'white', display: 'flex', alignItems: 'center' }} class="material-symbols-outlined">send</span>
127+
<span style={{ color: 'white', display: 'flex', alignItems: 'center' }} className="material-symbols-outlined">send</span>
96128
</button>
97129
</form>
98130
</div>

app/src/components/ChatDetailBox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export default function ChatDetailBox({ chatBotId, onChatClick }) {
4343
<div className='cdb-2'>{chat.date}</div>
4444
<div className='cdb-3'
4545
style={{
46-
color: chat.vote === "\"1\"" ? 'green' : chat.vote === "\"-1\"" ? 'red' : 'inherit'
46+
color: chat.vote === "1" ? 'green' : chat.vote === "-1" ? 'tomato' : 'inherit'
4747
}}
48-
>{chat.vote === "\"1\"" ? <span className="material-symbols-outlined">
48+
>{chat.vote === "1" ? <span className="material-symbols-outlined">
4949
sentiment_satisfied
50-
</span> : chat.vote === "\"-1\"" ? <span className="material-symbols-outlined">
50+
</span> : chat.vote === "-1" ? <span className="material-symbols-outlined">
5151
sentiment_dissatisfied
5252
</span> : ''}
5353
</div>

app/src/components/Dashboard.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ export default function Dashboard({ userData }) {
162162
<div className='db-ch-chatid'>#{chatID}</div>
163163
<div className='db-ch-chatvote'
164164
style={{
165-
color: chatVote === "\"1\"" ? 'green' : chatVote === "\"-1\"" ? 'red' : 'inherit'
165+
color: chatVote === "1" ? 'green' : chatVote === "-1" ? 'tomato' : 'inherit'
166166
}}
167167
>{
168-
chatVote === "\"1\"" ? <span class="material-symbols-outlined">sentiment_satisfied</span>
169-
: chatVote === "\"-1\"" ? <span class="material-symbols-outlined">sentiment_dissatisfied</span> : ''}</div>
168+
chatVote === "1" ? <span class="material-symbols-outlined">sentiment_satisfied</span>
169+
: chatVote === "-1" ? <span class="material-symbols-outlined">sentiment_dissatisfied</span> : ''}</div>
170170
</div>
171171
</center>
172172
<center>

server/src/main/java/com/semantiq/server/controller/BotDataController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.semantiq.server.controller;
22

3-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
4-
import com.fasterxml.jackson.databind.node.ObjectNode;
53
import com.semantiq.server.DTO.VoteDto;
64
import com.semantiq.server.entity.BotData;
75
import com.semantiq.server.entity.ChatBot;
@@ -14,8 +12,6 @@
1412
import org.springframework.web.bind.annotation.*;
1513

1614
import java.security.InvalidParameterException;
17-
import java.time.LocalDate;
18-
import java.util.ArrayList;
1915
import java.util.List;
2016
import java.util.Map;
2117

@@ -33,8 +29,8 @@ public BotDataController(BotDataService botDataService, ChatBotService chatBotSe
3329
this.chatService = chatService;
3430
}
3531

36-
@PostMapping("/voteBot/{chatBotId}/{vote}")
37-
public ResponseEntity<?> voteBot(@PathVariable int chatBotId, @PathVariable String vote, @PathVariable int chatId){
32+
@PostMapping("/{chatBotId}/chats/{chatId}/{vote}")
33+
public ResponseEntity<?> voteBot(@PathVariable int chatBotId, @PathVariable int vote, @PathVariable int chatId){
3834
ChatBot chatBot = chatBotService.findChatBotById(chatBotId);
3935
if (chatBot == null) {
4036
return new ResponseEntity<>("ChatBot not found", HttpStatus.NOT_FOUND);

server/src/main/java/com/semantiq/server/service/BotDataService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public void saveBotData(BotData botData) {
2424
botDataRepo.save(botData);
2525
}
2626

27-
public void voteBot(BotData botData, String vote){
27+
public void voteBot(BotData botData, int vote){
2828
int newCount;
29-
if(vote == "1"){
29+
if(vote == 1){
3030
newCount = botData.getCountPos() + 1;
3131
botData.setCountPos(newCount);
3232
}
33-
else if (vote == "-1"){
33+
else if (vote == -1){
3434
newCount = botData.getCountNeg() + 1;
3535
botData.setCountNeg(newCount);
3636
}

server/src/main/java/com/semantiq/server/service/ChatService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ public List<Chat> findAllChatsForBot(int botId) {
2626
return chatRepo.findAllByBotId(botId);
2727
}
2828

29-
public void setVote(int chatId, String vote) {
29+
public void setVote(int chatId, int vote) {
3030
Chat chat = findChatById(chatId);
31-
if (vote.equals("1")) chat.setVote("1");
32-
else if (vote.equals("-1")) chat.setVote("-1");
31+
chat.setVote(String.valueOf(vote)); // Convert int to String
3332

3433
chatRepo.save(chat);
3534
}

0 commit comments

Comments
 (0)