From 948c14a6eb7c7771900b3ca5b32c6243cb58871f Mon Sep 17 00:00:00 2001 From: Mark Anthony Llego Date: Mon, 6 Mar 2023 22:06:48 +0800 Subject: [PATCH] Bug Fix #21 (#22) * bug fix #21 * changes * update setApiFree function in useEffect --- src/components/ApiMenu/ApiMenu.tsx | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/components/ApiMenu/ApiMenu.tsx b/src/components/ApiMenu/ApiMenu.tsx index c5e3f17..1c23720 100644 --- a/src/components/ApiMenu/ApiMenu.tsx +++ b/src/components/ApiMenu/ApiMenu.tsx @@ -16,17 +16,19 @@ const ApiMenu = ({ const apiFree = useStore((state) => state.apiFree); const setApiFree = useStore((state) => state.setApiFree); + const [_apiFree, _setApiFree] = useState(apiFree); const [_apiKey, _setApiKey] = useState(apiKey || ''); const [error, setError] = useState(false); const handleSave = async () => { - if (apiFree === true) { + if (_apiFree === true) { + setApiFree(true); setIsModalOpen(false); } else { const valid = await validateApiKey(_apiKey); - if (valid) { setApiKey(_apiKey); + setApiFree(false); setError(false); setIsModalOpen(false); } else { @@ -40,9 +42,10 @@ const ApiMenu = ({ useEffect(() => { if (apiKey) { - setApiFree(false); - _setApiKey(apiKey); - } + setApiFree(false); + _setApiFree(false); + _setApiKey(apiKey); + } }, []); return isModalOpen ? ( @@ -55,9 +58,9 @@ const ApiMenu = ({
setApiFree(true)} + onChange={() => _setApiFree(true)} />
-
setApiFree(false)} + onChange={() => _setApiFree(false)} />
- {apiFree === false && ( + {_apiFree === false && ( <>
@@ -91,7 +93,7 @@ const ApiMenu = ({
{ _setApiKey(e.target.value); @@ -100,7 +102,8 @@ const ApiMenu = ({
{error && (
- Error: Invalid API key or network blocked. Please check your API key and network settings for OpenAI API. + Error: Invalid API key or network blocked. Please check your API + key and network settings for OpenAI API.
)}