wires up the feedback event handler with redux storie

This commit is contained in:
ajaythapliyal
2023-03-06 23:02:15 +05:30
parent 07e51dc8c6
commit a888f38afb
3 changed files with 37 additions and 19 deletions

View File

@@ -8,10 +8,12 @@ import {
fetchAnswer,
selectQueries,
selectStatus,
updateQuery,
} from './conversationSlice';
import Send from './../assets/send.svg';
import Spinner from './../assets/spinner.svg';
import { Query } from './conversationModels';
import { FEEDBACK, Query } from './conversationModels';
import { sendFeedback } from './conversationApi';
export default function Conversation() {
const queries = useSelector(selectQueries);
@@ -22,7 +24,7 @@ export default function Conversation() {
useEffect(
() => endMessageRef?.current?.scrollIntoView({ behavior: 'smooth' }),
[queries],
[queries.length, queries[queries.length - 1]],
);
const handleQuestion = (question: string) => {
@@ -30,6 +32,14 @@ export default function Conversation() {
dispatch(fetchAnswer({ question }));
};
const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => {
const prevFeedback = query.feedback;
dispatch(updateQuery({ index, query: { feedback } }));
sendFeedback(query.prompt, query.response!, feedback).catch(() =>
dispatch(updateQuery({ index, query: { feedback: prevFeedback } })),
);
};
const prepResponseView = (query: Query, index: number) => {
let responseView;
if (query.error) {
@@ -51,6 +61,9 @@ export default function Conversation() {
message={query.response}
type={'ANSWER'}
feedback={query.feedback}
handleFeedback={(feedback: FEEDBACK) =>
handleFeedback(query, feedback, index)
}
></ConversationBubble>
);
}