diff --git a/src/frontend/src/contexts/authContext.tsx b/src/frontend/src/contexts/authContext.tsx index 4e0d2f591..15dd16676 100644 --- a/src/frontend/src/contexts/authContext.tsx +++ b/src/frontend/src/contexts/authContext.tsx @@ -19,6 +19,9 @@ const initialValue: AuthContextType = { authenticationErrorCount: 0, autoLogin: false, setAutoLogin: () => {}, + setApiKey: () => {}, + apiKey: null, + hasApiKey: false, }; export const AuthContext = createContext(initialValue); @@ -36,6 +39,11 @@ export function AuthProvider({ children }): React.ReactElement { const [userData, setUserData] = useState(null); const [autoLogin, setAutoLogin] = useState(false); const { setLoading } = useContext(alertContext); + const [apiKey, setApiKey] = useState( + cookies.get("apikey_tkn_lflw") + ); + const [hasApiKey, setHasApiKey] = useState(false); + useEffect(() => { const storedAccessToken = cookies.get("access_tkn_lflw"); if (storedAccessToken) { @@ -43,6 +51,13 @@ export function AuthProvider({ children }): React.ReactElement { } }, []); + useEffect(() => { + const apiKey = cookies.get("apikey_tkn_lflw"); + if (apiKey) { + setApiKey(apiKey); + } + }, []); + useEffect(() => { const isLoginPage = location.pathname.includes("login"); @@ -83,6 +98,10 @@ export function AuthProvider({ children }): React.ReactElement { return auth; } + function hasApiKeyCheck() { + return hasApiKey; + } + function login(newAccessToken: string, refreshToken: string) { cookies.set("access_tkn_lflw", newAccessToken, { path: "/" }); cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" }); @@ -101,6 +120,10 @@ export function AuthProvider({ children }): React.ReactElement { setIsAuthenticated(false); } + function apikey(apikey: string) { + cookies.set("apikey_tkn_lflw", apikey, { path: "/" }); + } + return ( // !! to convert string to boolean {children} diff --git a/src/frontend/src/modals/StoreApiKeyModal/index.tsx b/src/frontend/src/modals/StoreApiKeyModal/index.tsx index bfbf3a55b..5fbeac59e 100644 --- a/src/frontend/src/modals/StoreApiKeyModal/index.tsx +++ b/src/frontend/src/modals/StoreApiKeyModal/index.tsx @@ -5,6 +5,7 @@ import { Button } from "../../components/ui/button"; import { Input } from "../../components/ui/input"; import { CONTROL_NEW_API_KEY } from "../../constants/constants"; import { alertContext } from "../../contexts/alertContext"; +import { AuthContext } from "../../contexts/authContext"; import { addApiKeyStore } from "../../controllers/API"; import { ApiKeyInputType, @@ -23,6 +24,7 @@ export default function StoreApiKeyModal({ useState(CONTROL_NEW_API_KEY); const { setSuccessData, setErrorData } = useContext(alertContext); const inputRef = useRef(null); + const { setApiKey, apiKey } = useContext(AuthContext); function handleInput({ target: { name, value }, @@ -49,6 +51,7 @@ export default function StoreApiKeyModal({ setSuccessData({ title: "Success! Your API Key has been saved.", }); + setApiKey(inputState["apikey"]); setOpen(false); }, (error) => { diff --git a/src/frontend/src/pages/StorePage/index.tsx b/src/frontend/src/pages/StorePage/index.tsx index 907bb039e..612600f83 100644 --- a/src/frontend/src/pages/StorePage/index.tsx +++ b/src/frontend/src/pages/StorePage/index.tsx @@ -15,6 +15,7 @@ import { } from "../../components/ui/select"; import { Switch } from "../../components/ui/switch"; import { alertContext } from "../../contexts/alertContext"; +import { AuthContext } from "../../contexts/authContext"; import { TabsContext } from "../../contexts/tabsContext"; import { getStoreComponents, searchComponent } from "../../controllers/API"; import StoreApiKeyModal from "../../modals/StoreApiKeyModal"; @@ -24,6 +25,8 @@ import { MarketCardComponent } from "./components/market-card"; export default function StorePage(): JSX.Element { const { setTabId } = useContext(TabsContext); + const { setApiKey, apiKey } = useContext(AuthContext); + // set null id useEffect(() => { setTabId(""); @@ -91,7 +94,7 @@ export default function StorePage(): JSX.Element { Search flows and components from the community. - {!loading && ( + {!loading && apiKey && (
@@ -188,6 +191,16 @@ export default function StorePage(): JSX.Element {
)} + + {!apiKey && ( +
+ Try add an API Key :) +
+ )} + + {apiKey && loading && ( +
Loading...
+ )}
); diff --git a/src/frontend/src/types/contexts/auth.ts b/src/frontend/src/types/contexts/auth.ts index 4946efa44..602573525 100644 --- a/src/frontend/src/types/contexts/auth.ts +++ b/src/frontend/src/types/contexts/auth.ts @@ -14,4 +14,7 @@ export type AuthContextType = { authenticationErrorCount: number; autoLogin: boolean; setAutoLogin: (autoLogin: boolean) => void; + apiKey: string | null; + setApiKey: (apiKey: string | null) => void; + hasApiKey: boolean; };