Skip to content

Commit

Permalink
handle upgrade call
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed May 5, 2024
1 parent b710b86 commit 51033a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ export default function MenuListComposition(props: {
user: BasicUserInfo;
signout: (callback?: (response: boolean) => void) => Promise<boolean>;
setIsBillingWariningOpen: React.Dispatch<React.SetStateAction<boolean>>;
isUpgraded: boolean;
setisUpgraded: React.Dispatch<React.SetStateAction<boolean>>;
}) {
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);
};

Expand Down Expand Up @@ -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 (
<><div className="user-menu-div">
<button className="menu-btn" onClick={handleToggle}>
Expand Down Expand Up @@ -199,7 +175,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} checkUpgrade={checkUpgrade} isOpen={billingOpen} setIsOpen={setBillingOpen} />
<GetBilling isUpgraded={isUpgraded} setisUpgraded={setisUpgraded} user={user} handleBilling={handleBilling} isOpen={billingOpen} setIsOpen={setBillingOpen} />
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ 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, checkUpgrade } = props;
const { user, isUpgraded, setisUpgraded, isOpen, setIsOpen, handleBilling } = props;
const { getAccessToken } = useAuthContext();

const [cardName, setCardName] = useState("");
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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();
Expand Down
29 changes: 28 additions & 1 deletion petcare-sample/b2c/web-app/petdesk/web/react/src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,6 +76,7 @@ export const HomePage: FunctionComponent = (): ReactElement => {
const [pet, setPet] = useState<Pet | null>(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');
Expand Down Expand Up @@ -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();
Expand All @@ -137,6 +163,7 @@ export const HomePage: FunctionComponent = (): ReactElement => {

useEffect(() => {
if(state.isAuthenticated){
checkUpgrade();
getPetList();
}
}, [state.isAuthenticated]);
Expand Down Expand Up @@ -252,7 +279,7 @@ export const HomePage: FunctionComponent = (): ReactElement => {
<><nav className="header">
<div>
{user && (
<MenuListComposition setIsBillingWariningOpen={setIsBillingWariningOpen} user={user} signout={signOut} />
<MenuListComposition isUpgraded={isUpgraded} setisUpgraded={setisUpgraded} setIsBillingWariningOpen={setIsBillingWariningOpen} user={user} signout={signOut} />
)}
</div>
<div className="app-title-style">
Expand Down

0 comments on commit 51033a7

Please sign in to comment.