Refactored API Key state to update components when updating API key
This commit is contained in:
parent
7f55da1df9
commit
ebae35bf66
1 changed files with 17 additions and 16 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useContext, useEffect, useState } from "react";
|
||||||
import { checkHasApiKey, checkHasStore } from "../controllers/API";
|
import { checkHasApiKey, checkHasStore } from "../controllers/API";
|
||||||
import { storeContextType } from "../types/contexts/store";
|
import { storeContextType } from "../types/contexts/store";
|
||||||
|
import { AuthContext } from "./authContext";
|
||||||
|
|
||||||
//store context to share user components and update them
|
//store context to share user components and update them
|
||||||
const initialValue = {
|
const initialValue = {
|
||||||
|
|
@ -21,6 +22,7 @@ export function StoreProvider({ children }) {
|
||||||
const [hasApiKey, setHasApiKey] = useState(true);
|
const [hasApiKey, setHasApiKey] = useState(true);
|
||||||
const [validApiKey, setValidApiKey] = useState(false);
|
const [validApiKey, setValidApiKey] = useState(false);
|
||||||
const [storeChecked, setStoreChecked] = useState(false);
|
const [storeChecked, setStoreChecked] = useState(false);
|
||||||
|
const { apiKey } = useContext(AuthContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchStoreData = async () => {
|
const fetchStoreData = async () => {
|
||||||
|
|
@ -37,11 +39,9 @@ export function StoreProvider({ children }) {
|
||||||
fetchStoreData();
|
fetchStoreData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
const fetchApiData = async () => {
|
||||||
setLoadingApiKey(true);
|
setLoadingApiKey(true);
|
||||||
const fetchStoreData = async () => {
|
|
||||||
try {
|
try {
|
||||||
if (storeChecked) return;
|
|
||||||
const res = await checkHasApiKey();
|
const res = await checkHasApiKey();
|
||||||
console.log(res);
|
console.log(res);
|
||||||
setHasApiKey(res?.has_api_key ?? false);
|
setHasApiKey(res?.has_api_key ?? false);
|
||||||
|
|
@ -52,8 +52,9 @@ export function StoreProvider({ children }) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchStoreData();
|
useEffect(() => {
|
||||||
}, [storeChecked, validApiKey]);
|
fetchApiData();
|
||||||
|
}, [storeChecked, validApiKey, apiKey]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StoreContext.Provider
|
<StoreContext.Provider
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue