-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/145 import export cart (#195)
add ability to import and export cart this is done by providing buttons attached to the cart item on any page. The export will generate a JSON file and the import will handle this JSON file. additionally -- the reset button from the 1-click buy feature has been moved over to this component. If the cart has been modified -- exporting is not allow and the reset button is show. Otherwise, the reset button is hidden and the export button is visible additionally: * move download file function to utilits * move dateString function to utilities * update date format for cart and downloads to be yy-mm-dd * move re-usable static method to component and re-name component to be for button types * update download_helper and tests for previous changes * fix potential failure mode of Player and add test * update Player 1-click feature with a small fix and add test
- Loading branch information
Showing
10 changed files
with
645 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import Logger from "../logger"; | ||
|
||
import { createButton } from "../components/buttons.js"; | ||
import { | ||
downloadFile, | ||
dateString, | ||
loadJsonFile, | ||
addAlbumToCart | ||
} from "../utilities"; | ||
import { createShoppingCartItem } from "../components/shoppingCart.js"; | ||
|
||
export default class Cart { | ||
constructor() { | ||
this.log = new Logger(); | ||
|
||
// re-import | ||
this.createButton = createButton; | ||
this.loadJsonFile = loadJsonFile; | ||
this.addAlbumToCart = addAlbumToCart; | ||
this.createShoppingCartItem = createShoppingCartItem; | ||
this.downloadFile = downloadFile; | ||
this.reloadWindow = () => location.reload(); | ||
} | ||
|
||
init() { | ||
this.log.info("cart init"); | ||
|
||
const importCartButton = this.createButton({ | ||
className: "buttonLink", | ||
innerText: "import", | ||
buttonClicked: async () => { | ||
try { | ||
const { tracks_export } = await this.loadJsonFile(); | ||
const promises = tracks_export.map(track => | ||
this.addAlbumToCart( | ||
track.item_id, | ||
track.unit_price, | ||
track.item_type | ||
).then(response => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const cartItem = this.createShoppingCartItem({ | ||
itemId: track.item_id, | ||
itemName: track.item_title, | ||
itemPrice: track.unit_price, | ||
itemCurrency: track.currency | ||
}); | ||
|
||
document.querySelector("#item_list").append(cartItem); | ||
}) | ||
); | ||
|
||
await Promise.all(promises).then(results => { | ||
if (!results || results.length < 1) { | ||
return; | ||
} | ||
this.reloadWindow(); | ||
}); | ||
} catch (error) { | ||
this.log.error("Error loading JSON:", error); | ||
} | ||
} | ||
}); | ||
document.querySelector("#sidecartReveal").prepend(importCartButton); | ||
|
||
const exportCartButton = this.createButton({ | ||
className: "buttonLink", | ||
innerText: "export", | ||
buttonClicked: () => { | ||
const { items } = JSON.parse( | ||
document.querySelector("[data-cart]").getAttribute("data-cart") | ||
); | ||
if (items.length < 1) { | ||
this.log.error("error trying to export cart with length of 0"); | ||
return; | ||
} | ||
|
||
const cart_id = items[0].cart_id; | ||
const date = dateString(); | ||
const tracks_export = items | ||
.filter(item => item.item_type === "a" || item.item_type === "t") | ||
.map( | ||
({ | ||
band_name, | ||
item_id, | ||
item_title, | ||
unit_price, | ||
url, | ||
currency, | ||
item_type | ||
}) => ({ | ||
band_name, | ||
item_id, | ||
item_title, | ||
unit_price, | ||
url, | ||
currency, | ||
item_type | ||
}) | ||
); | ||
if (tracks_export.length < 1) return; | ||
|
||
const filename = `${date}_${cart_id}_bes_cart_export.json`; | ||
const data = JSON.stringify({ date, cart_id, tracks_export }, null, 2); | ||
this.downloadFile(filename, data); | ||
} | ||
}); | ||
document.querySelector("#sidecartReveal").append(exportCartButton); | ||
|
||
const cartRefreshButton = this.createButton({ | ||
className: "buttonLink", | ||
innerText: "⟳", | ||
buttonClicked: () => this.reloadWindow() | ||
}); | ||
cartRefreshButton.style.display = "none"; | ||
document.querySelector("#sidecartReveal").append(cartRefreshButton); | ||
|
||
const observer = new MutationObserver(() => { | ||
const item_list = document.querySelectorAll("#item_list .item"); | ||
const cartDataElement = document.querySelector("[data-cart]"); | ||
|
||
if (!cartDataElement) { | ||
return; | ||
} | ||
const actual_cart = JSON.parse(cartDataElement.getAttribute("data-cart")) | ||
.items; | ||
|
||
cartRefreshButton.style.display = | ||
item_list.length === actual_cart.length ? "none" : "block"; | ||
|
||
exportCartButton.style.display = | ||
item_list.length === actual_cart.length ? "block" : "none"; | ||
}); | ||
|
||
const itemList = document.getElementById("item_list"); | ||
if (itemList) { | ||
observer.observe(itemList, { | ||
childList: true | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.