From a888f38afb3bcce313de39bc5795fc48f3faad78 Mon Sep 17 00:00:00 2001 From: ajaythapliyal Date: Mon, 6 Mar 2023 23:02:15 +0530 Subject: [PATCH] wires up the feedback event handler with redux storie --- frontend/src/conversation/Conversation.tsx | 17 ++++++++++-- .../src/conversation/ConversationBubble.tsx | 27 ++++++++----------- .../src/conversation/conversationSlice.ts | 12 ++++++++- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/frontend/src/conversation/Conversation.tsx b/frontend/src/conversation/Conversation.tsx index 40a5fdc7..de8ace00 100644 --- a/frontend/src/conversation/Conversation.tsx +++ b/frontend/src/conversation/Conversation.tsx @@ -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) + } > ); } diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 55f9fdd4..7bad1663 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -12,17 +12,17 @@ const ConversationBubble = forwardRef< type: MESSAGE_TYPE; className?: string; feedback?: FEEDBACK; - handleFeedback?: () => Promise; + handleFeedback?: (feedback: FEEDBACK) => void; } >(function ConversationBubble( { message, type, className, feedback, handleFeedback }, ref, ) { const [showFeedback, setShowFeedback] = useState(false); - const [overriddenFeedback, setOverriddenFeedback] = useState< - FEEDBACK | undefined - >(undefined); - const effectiveFeedback = overriddenFeedback ?? feedback; + // const [overriddenFeedback, setOverriddenFeedback] = useState< + // FEEDBACK | undefined + // >(undefined); + // const effectiveFeedback = overriddenFeedback ?? feedback; let bubble; if (type === 'QUESTION') { @@ -57,7 +57,7 @@ const ConversationBubble = forwardRef<
setOverriddenFeedback('LIKE')} + onClick={() => handleFeedback?.('LIKE')} >
setOverriddenFeedback('DISLIKE')} + onClick={() => handleFeedback?.('DISLIKE')} >
diff --git a/frontend/src/conversation/conversationSlice.ts b/frontend/src/conversation/conversationSlice.ts index 6701bc10..c728b9e0 100644 --- a/frontend/src/conversation/conversationSlice.ts +++ b/frontend/src/conversation/conversationSlice.ts @@ -30,6 +30,16 @@ export const conversationSlice = createSlice({ addQuery(state, action: PayloadAction) { state.queries.push(action.payload); }, + updateQuery( + state, + action: PayloadAction<{ index: number; query: Partial }>, + ) { + const index = action.payload.index; + state.queries[index] = { + ...state.queries[index], + ...action.payload.query, + }; + }, }, extraReducers(builder) { builder @@ -55,5 +65,5 @@ export const selectQueries = (state: RootState) => state.conversation.queries; export const selectStatus = (state: RootState) => state.conversation.status; -export const { addQuery } = conversationSlice.actions; +export const { addQuery, updateQuery } = conversationSlice.actions; export default conversationSlice.reducer;