Newer
Older
Hugh Dickinson
committed
import React, { useContext, useState, useRef } from "react";
import { Button } from "react-bootstrap";
import { GlobalContext } from "../../contexts/GlobalContext";
import { BasketContext } from "../../contexts/BasketContext";
import axios from "axios";
Hugh Dickinson
committed
export default function SaveBasket(props) {
const { api_host, isAuthenticated, sessionid } = useContext(GlobalContext);
const basketContext = useContext(BasketContext);
function saveBasket(basketData){
const payload = {shopping_cart: basketData};
console.log(payload);
Hugh Dickinson
committed
const profileUrl = api_host + "accounts/user-profiles/";
Hugh Dickinson
committed
.get(profileUrl, {withCredentials: true})
.then((response) => {
const userProfileUrl = profileUrl + response.data.results[0].user_name + "/";
Hugh Dickinson
committed
axios
.patch(userProfileUrl, payload, {withCredentials: true})
.then((response) => {
console.log("patch", response);
})
.catch((error) => {
console.log(error);
});
})
.catch((error) => {
console.log(error);
});
}
Hugh Dickinson
committed
if(isAuthenticated){
return (
<Button
type="button"
variant="primary"
onClick={() => saveBasket(basketContext.datasets)}
{...props}
>Save Data Selection</Button>
);
}
else{
return (<>
<Button variant="warning" disabled {...props}>
Log In to Enable Data Selection
</Button>
</>
);
}