diff --git a/petcare-sample/b2c/web-app/petdesk/web/react/src/components/UserMenu.tsx b/petcare-sample/b2c/web-app/petdesk/web/react/src/components/UserMenu.tsx index a6e219eb6..ffbe18652 100644 --- a/petcare-sample/b2c/web-app/petdesk/web/react/src/components/UserMenu.tsx +++ b/petcare-sample/b2c/web-app/petdesk/web/react/src/components/UserMenu.tsx @@ -33,18 +33,18 @@ export default function MenuListComposition(props: { user: BasicUserInfo; signout: (callback?: (response: boolean) => void) => Promise; setIsBillingWariningOpen: React.Dispatch>; + isUpgraded: boolean; + setisUpgraded: React.Dispatch>; }) { - const { user, signout, setIsBillingWariningOpen } = props; + const { isUpgraded, setisUpgraded, user, signout, setIsBillingWariningOpen } = props; const [userMenuOpen, setUserMenuOpen] = React.useState(false); const [settingsOpen, setSettingsOpen] = React.useState(false); const [billingOpen, setBillingOpen] = React.useState(false); const { getAccessToken } = useAuthContext(); const [enabled, setEnabled] = React.useState(false); const [email, setEmail] = React.useState(user.email); - const [isUpgraded, setisUpgraded] = React.useState(false); const handleToggle = () => { - checkUpgrade(); setUserMenuOpen((prevOpen) => !prevOpen); }; @@ -86,30 +86,6 @@ export default function MenuListComposition(props: { }); } - const checkUpgrade = () => { - async function getUpgradeDetail() { - const accessToken = await getAccessToken(); - try { - const response = await getUpgrade(accessToken); - if (response.data instanceof Object) { - if (response.data.isUpgraded) { - setisUpgraded(true); - } else { - setisUpgraded(false); - } - } - } catch (error) { - setisUpgraded(false); - //if error is Error: Network Error set isUpgraded to true - if (error.toString().includes("Error: Network Error") || error.toString().includes("status code 503")){ - setisUpgraded(true); - } - console.error('Error during upgrade process:', error); - } - } - getUpgradeDetail(); - }; - return ( <>
- +
); diff --git a/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/billing.tsx b/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/billing.tsx index d24f546f7..a3f6c8cbd 100644 --- a/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/billing.tsx +++ b/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/billing.tsx @@ -32,11 +32,10 @@ interface BillingProps { isOpen: boolean; setIsOpen: React.Dispatch>; handleBilling: () => Promise>; - checkUpgrade: () => void; } export default function GetBilling(props: BillingProps) { - const { user, isUpgraded, setisUpgraded, isOpen, setIsOpen, handleBilling, checkUpgrade } = props; + const { user, isUpgraded, setisUpgraded, isOpen, setIsOpen, handleBilling } = props; const { getAccessToken } = useAuthContext(); const [cardName, setCardName] = useState(""); @@ -91,9 +90,12 @@ export default function GetBilling(props: BillingProps) { expiryDate: expiryDate, securityCode: securityCode }; - await postBilling(accessToken, payload); - await postUpgrade(accessToken, user); - setisUpgraded(true); + await postUpgrade(accessToken, user).then(async (result) => { + await postBilling(accessToken, payload); + setisUpgraded(true); + }).catch((error) => { + console.log("Upgrade error: ", error); + }); setIsOpen(false); } } @@ -104,7 +106,6 @@ export default function GetBilling(props: BillingProps) { //if session strage has billing true, then open the billing dialog const billingExists = sessionStorage.getItem("billing") !== null; if (billingExists) { - checkUpgrade(); const billing = sessionStorage.getItem("billing"); if (billing === "true") { setBillingInfo(); diff --git a/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/home.tsx b/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/home.tsx index 9490beede..5da2466f8 100644 --- a/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/home.tsx +++ b/petcare-sample/b2c/web-app/petdesk/web/react/src/pages/home.tsx @@ -35,6 +35,7 @@ import PetCard from "./Pets/PetCard"; import { getPets } from "../components/getPetList/get-pets"; import MenuListComposition from "../components/UserMenu"; import { getConfig } from "../util/getConfig"; +import { getUpgrade } from "../components/Billing/billing"; interface DerivedState { authenticateResponse: BasicUserInfo, @@ -75,6 +76,7 @@ export const HomePage: FunctionComponent = (): ReactElement => { const [pet, setPet] = useState(null); const [thumbnail, setThumbnail] = useState(null); const { getAccessToken } = useAuthContext(); + const [isUpgraded, setisUpgraded] = React.useState(false); const search = useLocation().search; const stateParam = new URLSearchParams(search).get('state'); @@ -128,6 +130,30 @@ export const HomePage: FunctionComponent = (): ReactElement => { }); } + const checkUpgrade = () => { + async function getUpgradeDetail() { + const accessToken = await getAccessToken(); + try { + const response = await getUpgrade(accessToken); + if (response.data instanceof Object) { + if (response.data.isUpgraded) { + setisUpgraded(true); + } else { + setisUpgraded(false); + } + } + } catch (error) { + setisUpgraded(false); + //if error is Error: Network Error set isUpgraded to true + if (error.toString().includes("Error: Network Error") || error.toString().includes("status code 503")){ + setisUpgraded(true); + } + console.error('Error during upgrade process:', error); + } + } + getUpgradeDetail(); + }; + useEffect(() => { if (!isAddPetOpen && state.isAuthenticated) { getPetList(); @@ -137,6 +163,7 @@ export const HomePage: FunctionComponent = (): ReactElement => { useEffect(() => { if(state.isAuthenticated){ + checkUpgrade(); getPetList(); } }, [state.isAuthenticated]); @@ -252,7 +279,7 @@ export const HomePage: FunctionComponent = (): ReactElement => { <>