Newer
Older
import { Button } from "react-bootstrap";
import { GlobalContext } from "../../contexts/GlobalContext";
import { BasketContext } from "../../contexts/BasketContext";
import axios from "axios";
import { getShoppingIcon } from "../../utils/styling";
export function loadBasket(basketContext, api_host, isAuthenticated){
//alert('loadBasket: authenticated = '+isAuthenticated)
if (!isAuthenticated) {
return
}
Hugh Dickinson
committed
const profileUrl = api_host + "accounts/user-profiles/";
.get(profileUrl, {withCredentials: true})
.then((response) => {
console.log(response.data)
const userProfileUrl = profileUrl + response.data.results[0].user_name + "/";
Hugh Dickinson
committed
axios
.get(userProfileUrl, {withCredentials: true})
.then((response) => {
console.log("get", response);
// load the shopping_cart into the basketContext
//alert('loaded basket: '+response.data.shopping_cart.length)
basketContext.setDatasets(response.data.shopping_cart)
})
.catch((error) => {
console.log(error);
});
})
.catch((error) => {
Hugh Dickinson
committed
console.log(error);
});
}
export function LoadBasketButton(props) {
const { api_host, isAuthenticated } = useContext(GlobalContext);
const basketContext = useContext(BasketContext);
// fake authentication when in 'development' mode.
//let authenticated = isAuthenticated || (process.env.NODE_ENV === "development")
let authenticated = isAuthenticated
Hugh Dickinson
committed
return (
<Button
type="button"
variant="primary"
onClick={() => loadBasket(basketContext, api_host, authenticated)}
{getShoppingIcon("cart")} Load Basket</Button>
Hugh Dickinson
committed
);
}
else{
return (<>
<Button variant="warning" disabled {...props}>
Log In to Enable Data Selection
</Button>
</>
);
}