🔧 fix(storeContext.tsx): add missing import for checkHasApiKey function in API controller

🔧 fix(API/index.ts): add checkHasApiKey function to check if store has an API key
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-07 21:10:13 -03:00
commit 5affcdf987
2 changed files with 27 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import { createContext, useEffect, useState } from "react";
import { checkHasStore } from "../controllers/API";
import { checkHasApiKey, checkHasStore } from "../controllers/API";
import { storeContextType } from "../types/contexts/store";
//store context to share user components and update them
@ -27,7 +27,6 @@ export function StoreProvider({ children }) {
if (storeChecked) return;
const res = await checkHasStore();
setHasStore(res?.enabled ?? false);
setHasApiKey(res?.has_api_key ?? false);
setStoreChecked(true);
} catch (e) {
console.log(e);
@ -37,6 +36,20 @@ export function StoreProvider({ children }) {
fetchStoreData();
}, []);
useEffect(() => {
const fetchStoreData = async () => {
try {
if (storeChecked) return;
const res = await checkHasApiKey();
setHasApiKey(res?.has_api_key ?? false);
} catch (e) {
console.log(e);
}
};
fetchStoreData();
}, [storeChecked]);
return (
<StoreContext.Provider
value={{

View file

@ -729,6 +729,18 @@ export async function searchComponent(
}
}
export async function checkHasApiKey() {
try {
const res = await api.get(`${BASE_URL_API}/store/check/api_key`);
if (res?.status === 200) {
return res.data;
}
} catch (error) {
console.log("Error:", error);
throw error;
}
}
export async function checkHasStore() {
try {
const res = await api.get(`${BASE_URL_API}store/check/`);