From 3ab08c29c3f77d8a6c0bbe1286d47b9829963139 Mon Sep 17 00:00:00 2001 From: Le Khac Thanh Tung Date: Wed, 24 Jan 2024 17:40:50 +0700 Subject: [PATCH] handle state after sign --- .../extension/src/pages/chatbot/index.tsx | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/extension/src/pages/chatbot/index.tsx b/packages/extension/src/pages/chatbot/index.tsx index cc776a6f..f2e742ef 100644 --- a/packages/extension/src/pages/chatbot/index.tsx +++ b/packages/extension/src/pages/chatbot/index.tsx @@ -8,8 +8,9 @@ import { useClientTestnet } from './use-client-testnet'; import { OptionEnum, StatusEnum } from './enum'; import { Dropdown } from './dropdown'; import { Messages } from './messages'; +import { useHistory, useLocation } from 'react-router'; -const BACKEND_URL = 'http://127.0.0.1:5000'; +const BACKEND_URL = 'https://oraidex-tools.fly.dev'; const initialState = { messages: JSON.parse(localStorage.getItem('messages')) || [], @@ -73,6 +74,10 @@ export const ChatbotPage: FunctionComponent = observer(() => { const accountOrai = accountStore.getAccount(ChainIdEnum.Oraichain); const client = useClientTestnet(accountOrai); + // router + const history = useHistory(); // navigate back to chatbot after sign success + const location = useLocation(); // active useEffect to reload messages + const messagesEndRef = useRef(null); const [ @@ -86,7 +91,7 @@ export const ChatbotPage: FunctionComponent = observer(() => { dispatch({ type: 'reload_messages', }); - }, []); + }, [location.pathname]); const testChatUI = () => { dispatch({ @@ -165,14 +170,20 @@ export const ChatbotPage: FunctionComponent = observer(() => { amount ); const { transactionHash } = result; - dispatch({ - type: 'chat', - payload: { - isUser: false, - msg: `${answer}. Go to here https://testnet.scan.orai.io/txs/${transactionHash} to see transaction`, - }, - }); - console.log(result); + const messages = JSON.parse(localStorage.getItem('messages')); + // because when execute it will redirect to signing page so the component will be unmount so we need to save messages to localstorage (temp solution) + localStorage.setItem( + 'messages', + JSON.stringify([ + ...messages, + { + isUser: false, + msg: `${answer}. Go to here https://testnet.scan.orai.io/txs/${transactionHash} to see transaction`, + }, + ]) + ); + // navigate to chatbot page + history.push('/chatbot'); } catch (err) { console.log(err); }