From 1fc9d613252448677ca2ca46dfa6ddd815a024ff Mon Sep 17 00:00:00 2001 From: ajaythapliyal Date: Wed, 22 Feb 2023 10:02:05 +0530 Subject: [PATCH] shows error message when request req fails --- frontend/src/assets/alert.svg | 3 +++ .../src/conversation/ConversationBubble.tsx | 20 +++++++++++++++++-- frontend/src/conversation/conversationApi.ts | 13 ++++++++++-- .../src/conversation/conversationModels.ts | 2 +- .../src/conversation/conversationSlice.ts | 6 +++++- frontend/tailwind.config.cjs | 3 +++ 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 frontend/src/assets/alert.svg diff --git a/frontend/src/assets/alert.svg b/frontend/src/assets/alert.svg new file mode 100644 index 0000000..05c7634 --- /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 6fa9913..1283c79 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 c58532b..2030284 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 e2b55e4..c30977d 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 82e62ce..bd6929e 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 8596bc5..73cc3ed 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', }, }, },