Skip to content

Commit

Permalink
initial settings enable handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed May 2, 2024
1 parent 0efe0c5 commit ade37d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import LogoutIcon from '@mui/icons-material/Logout';
import { BasicUserInfo, useAuthContext } from "@asgardeo/auth-react";
import { getNotification } from "./Notifications/get-notification";
import GetBilling from "../pages/billing";
import { getBilling } from "./Billing/billing";
import { getBilling, getUpgrade } from "./Billing/billing";
import { AddCard } from "@mui/icons-material";
import { getConfig } from "../util/getConfig";

Expand All @@ -44,6 +44,7 @@ export default function MenuListComposition(props: {
const [isUpgraded, setisUpgraded] = React.useState(false);

const handleToggle = () => {
checkUpgrade();
setUserMenuOpen((prevOpen) => !prevOpen);
};

Expand Down Expand Up @@ -85,6 +86,30 @@ 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")) {
setisUpgraded(true);
}
console.error('Error during upgrade process:', error);
}
}
getUpgradeDetail();
};

return (
<><div className="user-menu-div">
<button className="menu-btn" onClick={handleToggle}>
Expand Down Expand Up @@ -174,7 +199,7 @@ export default function MenuListComposition(props: {
enabled={enabled} email={email} setEnabled={setEnabled} setEmail={setEmail} />
</div>
<div>
<GetBilling isUpgraded={isUpgraded} setisUpgraded={setisUpgraded} user={user} handleBilling={handleBilling} isOpen={billingOpen} setIsOpen={setBillingOpen} />
<GetBilling isUpgraded={isUpgraded} setisUpgraded={setisUpgraded} user={user} handleBilling={handleBilling} checkUpgrade={checkUpgrade} isOpen={billingOpen} setIsOpen={setBillingOpen} />
</div>
</>
);
Expand Down
27 changes: 2 additions & 25 deletions petcare-sample/b2c/web-app/petdesk/web/react/src/pages/billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ interface BillingProps {
isOpen: boolean;
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
handleBilling: () => Promise<AxiosResponse<BillingInfo, any>>;
checkUpgrade: () => void;
}

export default function GetBilling(props: BillingProps) {
const { user, isUpgraded, setisUpgraded, isOpen, setIsOpen, handleBilling } = props;
const { user, isUpgraded, setisUpgraded, isOpen, setIsOpen, handleBilling, checkUpgrade } = props;
const { getAccessToken } = useAuthContext();

const [cardName, setCardName] = useState("");
Expand Down Expand Up @@ -95,30 +96,6 @@ export default function GetBilling(props: BillingProps) {
setUpgrade();
};

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")) {
setisUpgraded(true);
}
console.error('Error during upgrade process:', error);
}
}
getUpgradeDetail();
};

useEffect(() => {
//if session strage has billing true, then open the billing dialog
const billingExists = sessionStorage.getItem("billing") !== null;
Expand Down

0 comments on commit ade37d3

Please sign in to comment.