Skip to content
Snippets Groups Projects
Commit 0aa34ccc authored by Nico Vermaas's avatar Nico Vermaas
Browse files

Merge branch 'dev-nico' into 'master'

fix 'empty shopping basket' functionality

See merge request astron-sdc/esap-gui!55
parents fc7ad00a 593f5250
No related branches found
No related tags found
2 merge requests!57Master,!55fix 'empty shopping basket' functionality
Pipeline #16802 passed
...@@ -25,11 +25,7 @@ export default function AddToCheckout(props) { ...@@ -25,11 +25,7 @@ export default function AddToCheckout(props) {
console.log('removeFromBasket: '+[removeFromBasketItem, basketContext]); console.log('removeFromBasket: '+[removeFromBasketItem, basketContext]);
} }
// fake authentication when in 'development' mode. if (isAuthenticated){
// let authenticated = isAuthenticated || (process.env.NODE_ENV === "development")
let authenticated = isAuthenticated
if (authenticated){
//let datasets_in_basket = basketContext.datasets //let datasets_in_basket = basketContext.datasets
//console.log('datasets_in_basket: '+datasets_in_basket) //console.log('datasets_in_basket: '+datasets_in_basket)
return ( return (
......
...@@ -9,7 +9,7 @@ import { saveBasket } from "./SaveBasketButton" ...@@ -9,7 +9,7 @@ import { saveBasket } from "./SaveBasketButton"
export default function EmptyBasketButton(props) { export default function EmptyBasketButton(props) {
const { api_host, isAuthenticated, isTokenValid, loginAgain } = useContext(GlobalContext); const { api_host, isAuthenticated, isTokenValid, loginAgain } = useContext(GlobalContext);
const basketContext = useContext(BasketContext); const basketContext = useContext(BasketContext);
const { setHasChanged } = useContext(BasketContext); const { setHasChanged, refresh, setRefresh } = useContext(BasketContext);
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const handleClose = () => setShow(false); const handleClose = () => setShow(false);
...@@ -18,15 +18,16 @@ export default function EmptyBasketButton(props) { ...@@ -18,15 +18,16 @@ export default function EmptyBasketButton(props) {
let history = useHistory() let history = useHistory()
function emptyBasket(basketContext , api_host, isTokenValid, loginAgain, history){ function emptyBasket(basketContext , api_host, isTokenValid, loginAgain, history){
basketContext.setDatasets([]) basketContext.clear()
setHasChanged(true) setHasChanged(true)
setShow(false) setShow(false)
saveBasket(basketContext , api_host, isTokenValid, loginAgain, history) setRefresh(!refresh)
} }
if (isAuthenticated) { if (isAuthenticated) {
if (basketContext.datasets!==[]) { //alert(basketContext.datasets)
if (basketContext.datasets.length>0) {
return ( return (
<> <>
<Button <Button
...@@ -41,16 +42,16 @@ export default function EmptyBasketButton(props) { ...@@ -41,16 +42,16 @@ export default function EmptyBasketButton(props) {
<Modal.Header closeButton> <Modal.Header closeButton>
<Modal.Title>{getTrashIcon('black')}{' '}Empty Basket</Modal.Title> <Modal.Title>{getTrashIcon('black')}{' '}Empty Basket</Modal.Title>
</Modal.Header> </Modal.Header>
<Modal.Body>Are you sure you want to empty the shopping basket?</Modal.Body> <Modal.Body><h5>Are you sure you want to empty the basket?</h5>
<i>(don't forget to click 'Save Basket' afterward)</i>
</Modal.Body>
<Modal.Footer> <Modal.Footer>
<Button variant="success" <Button variant="success"
onClick={() => emptyBasket(basketContext, api_host, isTokenValid, loginAgain, history)} onClick={() => emptyBasket(basketContext, api_host, isTokenValid, loginAgain, history)}
> >
{getOKIcon()}{' '}OK {getOKIcon()}{' '}OK
</Button> </Button>
<Button variant="warning" onClick={handleClose}>
Cancel
</Button>
</Modal.Footer> </Modal.Footer>
</Modal> </Modal>
</> </>
......
...@@ -49,22 +49,12 @@ function renderRow(item) { ...@@ -49,22 +49,12 @@ function renderRow(item) {
} }
} }
export default function MyBasketPage() { function renderBasketPage(items) {
const { api_host, isAuthenticated } = useContext(GlobalContext);
const basketContext = useContext(BasketContext);
const { preview } = useContext(QueryContext);
// work on a local copy of datasets, to be able (un)(re)select items before saving
const [items, setItems] = useState(basketContext.datasets);
if (!items) { if (!items) {
return null return null
} }
//if (items.length === basketContext.datasets.length) {
// basketContext.changed(false)
//}
// parse the items and build a line to display // parse the items and build a line to display
let my_list = items.map((item, index) => { let my_list = items.map((item, index) => {
let id = `${index}` let id = `${index}`
...@@ -83,7 +73,7 @@ export default function MyBasketPage() { ...@@ -83,7 +73,7 @@ export default function MyBasketPage() {
<> <>
<Container fluid> <Container fluid>
&nbsp; &nbsp;
<h3>Data Checkout &nbsp;&nbsp; <EmptyBasketButton/> <APIButton/></h3> <h3>Data Shopping Basket &nbsp;&nbsp; <EmptyBasketButton/> <APIButton/></h3>
<Table className="mt-3" responsive> <Table className="mt-3" responsive>
<thead> <thead>
...@@ -105,3 +95,20 @@ export default function MyBasketPage() { ...@@ -105,3 +95,20 @@ export default function MyBasketPage() {
</> </>
); );
} }
export default function MyBasketPage() {
const { api_host, isAuthenticated } = useContext(GlobalContext);
const basketContext = useContext(BasketContext);
const { refresh } = useContext(BasketContext);
const { preview } = useContext(QueryContext);
// work on a local copy of datasets, to be able (un)(re)select items before saving
const [items, setItems] = useState(basketContext.datasets);
useEffect(() => {
//alert('refresh')
setItems(basketContext.datasets)
},[refresh])
return renderBasketPage(items)
}
...@@ -9,7 +9,7 @@ import { getShoppingIcon } from "../../utils/styling"; ...@@ -9,7 +9,7 @@ import { getShoppingIcon } from "../../utils/styling";
export function saveBasket(basketContext, api_host, isTokenValid, loginAgain, history){ export function saveBasket(basketContext, api_host, isTokenValid, loginAgain, history){
const payload = {shopping_cart: basketContext.datasets}; const payload = {shopping_cart: basketContext.datasets};
console.log('saveBasket()') console.log('saveBasket() '+payload)
const profileUrl = api_host + "accounts/user-profiles/"; const profileUrl = api_host + "accounts/user-profiles/";
// check if he token is still valid // check if he token is still valid
......
...@@ -6,6 +6,7 @@ export function BasketContextProvider({ children }) { ...@@ -6,6 +6,7 @@ export function BasketContextProvider({ children }) {
const [datasets, setDatasets] = useState([]); const [datasets, setDatasets] = useState([]);
const [hasChanged, setHasChanged] = useState(false); const [hasChanged, setHasChanged] = useState(false);
const [refresh, setRefresh] = useState(false);
function handleAddDataset(dataset) { function handleAddDataset(dataset) {
setDatasets([...datasets, dataset]); setDatasets([...datasets, dataset]);
...@@ -20,16 +21,22 @@ export function BasketContextProvider({ children }) { ...@@ -20,16 +21,22 @@ export function BasketContextProvider({ children }) {
setHasChanged(true) setHasChanged(true)
} }
function handleClearDatasets() {
const copy = []
setDatasets(copy);
setHasChanged(true)
}
return ( return (
<BasketContext.Provider <BasketContext.Provider
value={{ value={{
datasets, datasets, setDatasets,
setDatasets, hasChanged, setHasChanged,
hasChanged, refresh, setRefresh,
setHasChanged,
add: handleAddDataset, add: handleAddDataset,
remove: handleRemoveDataset, remove: handleRemoveDataset,
changed: setHasChanged clear: handleClearDatasets,
changed: setHasChanged,
}} }}
> >
{children} {children}
......
...@@ -87,7 +87,7 @@ export default function Routes() { ...@@ -87,7 +87,7 @@ export default function Routes() {
</Switch> </Switch>
<footer><small>esap-gui version 27 aug 2021 - 11:00</small></footer> <footer><small>esap-gui version 27 aug 2021 - 14:00</small></footer>
</Router> </Router>
); );
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment