fix(authContext.tsx): remove unused hasApiKey state and function to improve code readability
feat(authContext.tsx): add storeApiKey function to store API key in cookies and update apiKey state fix(StoreApiKeyModal/index.tsx): update usage of setApiKey to storeApiKey from AuthContext feat(StorePage/index.tsx): add error handling for missing or invalid API key and display appropriate error message
This commit is contained in:
parent
df267c065a
commit
6577d9c52c
4 changed files with 25 additions and 14 deletions
|
|
@ -21,7 +21,7 @@ const initialValue: AuthContextType = {
|
||||||
setAutoLogin: () => {},
|
setAutoLogin: () => {},
|
||||||
setApiKey: () => {},
|
setApiKey: () => {},
|
||||||
apiKey: null,
|
apiKey: null,
|
||||||
hasApiKey: false,
|
storeApiKey: () => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthContextType>(initialValue);
|
export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||||
|
|
@ -42,7 +42,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [apiKey, setApiKey] = useState<string | null>(
|
const [apiKey, setApiKey] = useState<string | null>(
|
||||||
cookies.get("apikey_tkn_lflw")
|
cookies.get("apikey_tkn_lflw")
|
||||||
);
|
);
|
||||||
const [hasApiKey, setHasApiKey] = useState<boolean>(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const storedAccessToken = cookies.get("access_tkn_lflw");
|
const storedAccessToken = cookies.get("access_tkn_lflw");
|
||||||
|
|
@ -98,10 +97,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasApiKeyCheck() {
|
|
||||||
return hasApiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
function login(newAccessToken: string, refreshToken: string) {
|
function login(newAccessToken: string, refreshToken: string) {
|
||||||
cookies.set("access_tkn_lflw", newAccessToken, { path: "/" });
|
cookies.set("access_tkn_lflw", newAccessToken, { path: "/" });
|
||||||
cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" });
|
cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" });
|
||||||
|
|
@ -113,6 +108,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
function logout() {
|
function logout() {
|
||||||
cookies.remove("access_tkn_lflw", { path: "/" });
|
cookies.remove("access_tkn_lflw", { path: "/" });
|
||||||
cookies.remove("refresh_tkn_lflw", { path: "/" });
|
cookies.remove("refresh_tkn_lflw", { path: "/" });
|
||||||
|
cookies.remove("apikey_tkn_lflw", { path: "/" });
|
||||||
setIsAdmin(false);
|
setIsAdmin(false);
|
||||||
setUserData(null);
|
setUserData(null);
|
||||||
setAccessToken(null);
|
setAccessToken(null);
|
||||||
|
|
@ -120,8 +116,9 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function apikey(apikey: string) {
|
function storeApiKey(apikey: string) {
|
||||||
cookies.set("apikey_tkn_lflw", apikey, { path: "/" });
|
cookies.set("apikey_tkn_lflw", apikey, { path: "/" });
|
||||||
|
setApiKey(apikey);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -143,7 +140,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
autoLogin,
|
autoLogin,
|
||||||
setApiKey,
|
setApiKey,
|
||||||
apiKey,
|
apiKey,
|
||||||
hasApiKey,
|
storeApiKey,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ export default function StoreApiKeyModal({
|
||||||
useState<ApiKeyInputType>(CONTROL_NEW_API_KEY);
|
useState<ApiKeyInputType>(CONTROL_NEW_API_KEY);
|
||||||
const { setSuccessData, setErrorData } = useContext(alertContext);
|
const { setSuccessData, setErrorData } = useContext(alertContext);
|
||||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||||
const { setApiKey, apiKey } = useContext(AuthContext);
|
const { storeApiKey } = useContext(AuthContext);
|
||||||
|
|
||||||
function handleInput({
|
function handleInput({
|
||||||
target: { name, value },
|
target: { name, value },
|
||||||
|
|
@ -51,7 +51,7 @@ export default function StoreApiKeyModal({
|
||||||
setSuccessData({
|
setSuccessData({
|
||||||
title: "Success! Your API Key has been saved.",
|
title: "Success! Your API Key has been saved.",
|
||||||
});
|
});
|
||||||
setApiKey(inputState["apikey"]);
|
storeApiKey(inputState["apikey"]);
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ export default function StorePage(): JSX.Element {
|
||||||
const [filteredCategories, setFilteredCategories] = useState(new Set());
|
const [filteredCategories, setFilteredCategories] = useState(new Set());
|
||||||
const [inputText, setInputText] = useState<string>("");
|
const [inputText, setInputText] = useState<string>("");
|
||||||
const [searchData, setSearchData] = useState(data);
|
const [searchData, setSearchData] = useState(data);
|
||||||
|
const [errorApiKey, setErrorApiKey] = useState(false);
|
||||||
const { setErrorData } = useContext(alertContext);
|
const { setErrorData } = useContext(alertContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -49,9 +50,11 @@ export default function StorePage(): JSX.Element {
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
console.log(res);
|
console.log(res);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
setErrorApiKey(false);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
setErrorApiKey(true);
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: "Error to get components.",
|
title: "Error to get components.",
|
||||||
list: [err["response"]["data"]["detail"]],
|
list: [err["response"]["data"]["detail"]],
|
||||||
|
|
@ -68,6 +71,11 @@ export default function StorePage(): JSX.Element {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const loadingWithApiKey = apiKey && loading;
|
||||||
|
const noApiKey = !apiKey;
|
||||||
|
const errorMessage = errorApiKey && !loading;
|
||||||
|
const renderComponents = !loading && !errorApiKey && apiKey;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Header />
|
<Header />
|
||||||
|
|
@ -94,7 +102,7 @@ export default function StorePage(): JSX.Element {
|
||||||
<span className="community-page-description-text">
|
<span className="community-page-description-text">
|
||||||
Search flows and components from the community.
|
Search flows and components from the community.
|
||||||
</span>
|
</span>
|
||||||
{!loading && apiKey && (
|
{renderComponents && (
|
||||||
<div className="flex w-full flex-col gap-4 p-4">
|
<div className="flex w-full flex-col gap-4 p-4">
|
||||||
<div className="flex items-center justify-center gap-4">
|
<div className="flex items-center justify-center gap-4">
|
||||||
<div className="flex w-[13%] items-center justify-center gap-3 text-sm">
|
<div className="flex w-[13%] items-center justify-center gap-3 text-sm">
|
||||||
|
|
@ -192,15 +200,21 @@ export default function StorePage(): JSX.Element {
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!apiKey && (
|
{noApiKey && (
|
||||||
<div className="flex w-full flex-col gap-4 p-4">
|
<div className="flex w-full flex-col gap-4 p-4">
|
||||||
Try add an API Key :)
|
Try add an API Key :)
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{apiKey && loading && (
|
{loadingWithApiKey && (
|
||||||
<div className="flex w-full flex-col gap-4 p-4">Loading...</div>
|
<div className="flex w-full flex-col gap-4 p-4">Loading...</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{errorMessage && (
|
||||||
|
<div className="flex w-full flex-col gap-4 p-4">
|
||||||
|
An error has occurred. Please check your API key.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -16,5 +16,5 @@ export type AuthContextType = {
|
||||||
setAutoLogin: (autoLogin: boolean) => void;
|
setAutoLogin: (autoLogin: boolean) => void;
|
||||||
apiKey: string | null;
|
apiKey: string | null;
|
||||||
setApiKey: (apiKey: string | null) => void;
|
setApiKey: (apiKey: string | null) => void;
|
||||||
hasApiKey: boolean;
|
storeApiKey: (apiKey: string) => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue