From eefac9ef4859da1af959227703148012a75724ee Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Tue, 4 Jun 2024 09:20:55 -0300 Subject: [PATCH] Added Store API Key configuration on General Settings page --- src/frontend/src/constants/constants.ts | 1 + .../SettingsPage/pages/GeneralPage/index.tsx | 105 +++++++++++++++++- src/frontend/src/types/components/index.ts | 1 + 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/frontend/src/constants/constants.ts b/src/frontend/src/constants/constants.ts index 47563b9c3..b0a0b55c5 100644 --- a/src/frontend/src/constants/constants.ts +++ b/src/frontend/src/constants/constants.ts @@ -590,6 +590,7 @@ export const CONTROL_PATCH_USER_STATE = { password: "", cnfPassword: "", gradient: "", + apikey: "", }; export const CONTROL_LOGIN_STATE = { diff --git a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx index 5c53e0aa8..d10c450c1 100644 --- a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx +++ b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx @@ -14,14 +14,25 @@ import { CardTitle, } from "../../../../components/ui/card"; import { + API_ERROR_ALERT, + API_SUCCESS_ALERT, EDIT_PASSWORD_ALERT_LIST, EDIT_PASSWORD_ERROR_ALERT, SAVE_ERROR_ALERT, SAVE_SUCCESS_ALERT, } from "../../../../constants/alerts_constants"; -import { CONTROL_PATCH_USER_STATE } from "../../../../constants/constants"; +import { + CONTROL_PATCH_USER_STATE, + INSERT_API_KEY, + INVALID_API_KEY, + NO_API_KEY, +} from "../../../../constants/constants"; import { AuthContext } from "../../../../contexts/authContext"; -import { resetPassword, updateUser } from "../../../../controllers/API"; +import { + addApiKeyStore, + resetPassword, + updateUser, +} from "../../../../controllers/API"; import useAlertStore from "../../../../stores/alertStore"; import useFlowsManagerStore from "../../../../stores/flowsManagerStore"; import { @@ -29,6 +40,7 @@ import { patchUserInputStateType, } from "../../../../types/components"; import { gradients } from "../../../../utils/styleUtils"; +import { useStoreStore } from "../../../../stores/storeStore"; export default function GeneralPage() { const setCurrentFlowId = useFlowsManagerStore( @@ -48,7 +60,16 @@ export default function GeneralPage() { const setSuccessData = useAlertStore((state) => state.setSuccessData); const setErrorData = useAlertStore((state) => state.setErrorData); const { userData, setUserData } = useContext(AuthContext); - const { password, cnfPassword, gradient } = inputState; + const hasStore = useStoreStore((state) => state.hasStore); + const { storeApiKey } = useContext(AuthContext); + + const validApiKey = useStoreStore((state) => state.validApiKey); + const hasApiKey = useStoreStore((state) => state.hasApiKey); + const setHasApiKey = useStoreStore((state) => state.updateHasApiKey); + const loadingApiKey = useStoreStore((state) => state.loadingApiKey); + const setValidApiKey = useStoreStore((state) => state.updateValidApiKey); + const setLoadingApiKey = useStoreStore((state) => state.updateLoadingApiKey); + const { password, cnfPassword, gradient, apikey } = inputState; async function handlePatchPassword() { if (password !== cnfPassword) { @@ -90,11 +111,38 @@ export default function GeneralPage() { } } + const handleSaveKey = () => { + if (apikey) { + addApiKeyStore(apikey).then( + () => { + setSuccessData({ + title: API_SUCCESS_ALERT, + }); + storeApiKey(apikey); + setHasApiKey(true); + setValidApiKey(true); + setLoadingApiKey(false); + handleInput({ target: { name: "apikey", value: "" } }); + }, + (error) => { + setErrorData({ + title: API_ERROR_ALERT, + list: [error["response"]["data"]["detail"]], + }); + setHasApiKey(false); + setValidApiKey(false); + setLoadingApiKey(false); + }, + ); + } + }; + function handleInput({ target: { name, value }, }: inputHandlerEventType): void { setInputState((prev) => ({ ...prev, [name]: value })); } + return (
@@ -217,6 +265,57 @@ export default function GeneralPage() { )} + {hasStore && ( + { + event.preventDefault(); + handleSaveKey(); + }} + > + + + API Key + + {(hasApiKey && !validApiKey + ? INVALID_API_KEY + : !hasApiKey + ? NO_API_KEY + : "") + INSERT_API_KEY} + + + +
+ + { + handleInput({ target: { name: "apikey", value } }); + }} + value={apikey} + isForm + password={true} + placeholder="Insert your API Key" + className="w-full" + /> + + Please enter your API Key + + +
+
+ + + + + +
+
+ )}
); diff --git a/src/frontend/src/types/components/index.ts b/src/frontend/src/types/components/index.ts index b955003de..199cbb1f2 100644 --- a/src/frontend/src/types/components/index.ts +++ b/src/frontend/src/types/components/index.ts @@ -379,6 +379,7 @@ export type patchUserInputStateType = { password: string; cnfPassword: string; gradient: string; + apikey: string; }; export type UserInputType = {