diff --git a/frontend/src/assets/alert.svg b/frontend/src/assets/alert.svg new file mode 100644 index 00000000..05c7634b --- /dev/null +++ b/frontend/src/assets/alert.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 6fa99131..1283c798 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -1,6 +1,7 @@ import { forwardRef } from 'react'; import Avatar from '../Avatar'; import { MESSAGE_TYPE } from './conversationModels'; +import Alert from './../assets/alert.svg'; const ConversationBubble = forwardRef< HTMLDivElement, @@ -14,11 +15,26 @@ const ConversationBubble = forwardRef<
-

{message}

+
+ {type === 'ERROR' && ( + alert + )} + {message} +
); }); diff --git a/frontend/src/conversation/conversationApi.ts b/frontend/src/conversation/conversationApi.ts index c58532b6..20302847 100644 --- a/frontend/src/conversation/conversationApi.ts +++ b/frontend/src/conversation/conversationApi.ts @@ -5,7 +5,7 @@ export function fetchAnswerApi( apiKey: string, ): Promise { // a mock answer generator, this is going to be replaced with real http call - return new Promise((resolve) => { + return new Promise((resolve, reject) => { setTimeout(() => { let result = ''; const characters = @@ -18,7 +18,16 @@ export function fetchAnswerApi( ); counter += 1; } - resolve({ answer: result, query: question, result }); + const randNum = getRandomInt(0, 10); + randNum < 5 + ? reject() + : resolve({ answer: result, query: question, result }); }, 3000); }); } + +function getRandomInt(min: number, max: number) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive +} diff --git a/frontend/src/conversation/conversationModels.ts b/frontend/src/conversation/conversationModels.ts index e2b55e44..c30977d1 100644 --- a/frontend/src/conversation/conversationModels.ts +++ b/frontend/src/conversation/conversationModels.ts @@ -1,4 +1,4 @@ -export type MESSAGE_TYPE = 'QUESTION' | 'ANSWER'; +export type MESSAGE_TYPE = 'QUESTION' | 'ANSWER' | 'ERROR'; export type Status = 'idle' | 'loading' | 'failed'; export interface Message { diff --git a/frontend/src/conversation/conversationSlice.ts b/frontend/src/conversation/conversationSlice.ts index 82e62ceb..bd6929e8 100644 --- a/frontend/src/conversation/conversationSlice.ts +++ b/frontend/src/conversation/conversationSlice.ts @@ -38,8 +38,12 @@ export const conversationSlice = createSlice({ type: 'ANSWER', }); }) - .addCase(fetchAnswer.rejected, (state) => { + .addCase(fetchAnswer.rejected, (state, action) => { state.status = 'failed'; + state.conversation.push({ + text: 'Something went wrong. Please try again later.', + type: 'ERROR', + }); }); }, }); diff --git a/frontend/tailwind.config.cjs b/frontend/tailwind.config.cjs index 8596bc5d..73cc3eda 100644 --- a/frontend/tailwind.config.cjs +++ b/frontend/tailwind.config.cjs @@ -12,6 +12,9 @@ module.exports = { jet: '#343541', 'gray-alpha': 'rgba(0,0,0, .1)', 'gray-1000': '#F6F6F6', + 'red-1000': 'rgb(254, 202, 202)', + 'red-2000' : '#F44336', + 'red-3000': '#621B16', }, }, },