Skip to content
Snippets Groups Projects
SaveBasketButton.js 2.65 KiB
Newer Older
Klaas Kliffen's avatar
Klaas Kliffen committed
import axios from "axios";
import React, { useContext } from "react";
import { Button } from "react-bootstrap";
Klaas Kliffen's avatar
Klaas Kliffen committed
import { useHistory } from "react-router-dom";
import { BasketContext } from "../../contexts/BasketContext";
Klaas Kliffen's avatar
Klaas Kliffen committed
import { GlobalContext } from "../../contexts/GlobalContext";
import { getShoppingIcon } from "../../utils/styling";
Klaas Kliffen's avatar
Klaas Kliffen committed
export function saveBasket(basketContext, api_host, isTokenValid, loginAgain, history) {
    const payload = { shopping_cart: basketContext.datasets };
    console.log('saveBasket() ' + payload)
    const profileUrl = api_host + "accounts/user-profiles/";

    // check if he token is still valid
    let token_is_valid = isTokenValid()
Klaas Kliffen's avatar
Klaas Kliffen committed
    console.log('token valid: ' + token_is_valid)

    // if the token is not valid, then refresh it by logging in again
    if (token_is_valid < 0) {

        console.log('token no longer valid, retrying login...')
        loginAgain(history)
        //saveBasket(basketContext, api_host, isTokenValid, history)

        return
    }

        .get(profileUrl, {
            withCredentials: true,
        })
        .then((response) => {
            // build the userProfileUrl based on the user_name in the original id_token
            console.log(response.data)
            const userProfileUrl = profileUrl + response.data.results[0].user_name + "/";
            // send the payload to the userProfile
            axios
Klaas Kliffen's avatar
Klaas Kliffen committed
                .patch(userProfileUrl, payload, { withCredentials: true })
                .then((response) => {
                    console.log("patch", response);
                    basketContext.setHasChanged(false)
                })
                .catch((error) => {
                    console.log(error);
                });
        })
        .catch((error) => {
        });
}

export default function SaveBasketButton(props) {
Klaas Kliffen's avatar
Klaas Kliffen committed
    const { api_host, isAuthenticated, isTokenValid, loginAgain } = useContext(GlobalContext);
    const basketContext = useContext(BasketContext);
    const { hasChanged } = useContext(BasketContext);
Klaas Kliffen's avatar
Klaas Kliffen committed
    let history = useHistory()
Klaas Kliffen's avatar
Klaas Kliffen committed
    // only show the 'save basket' button when a user is logged in and something in the basket has changed
    if (isAuthenticated) {
        if (hasChanged) {
            return (
                <Button
                    type="button"
                    variant="primary"
                    onClick={() => saveBasket(basketContext, api_host, isTokenValid, loginAgain, history)}
                    {...props}>
                    {getShoppingIcon("save_cart")} Save Basket</Button>
            )
        } else {
            return null
        }
    }
    else {
        return null
Klaas Kliffen's avatar
Klaas Kliffen committed
    }