Skip to content

Commit

Permalink
Add to cart + fix genre and type potentially empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsDyze committed Sep 20, 2024
1 parent c483f3f commit 40b9c1c
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/assets/add-to-cart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions public/assets/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,35 @@ function toggleCart()
document.getElementById('cart-nav').classList.toggle('active')
document.getElementById('cart-backdrop').classList.toggle('active')

}

function createCookie(name,value,days) {
let expires;
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 *1000));
expires = "; expires=" + date.toLocaleString();
} else {
let expires = "";
}
document.cookie = name + "=" + JSON.stringify(value) + expires + "; path=/";
}

function readCookie(name) {
let nameEQ = name + "=";
let ca = document.cookie.split(';');
for(let i=0;i < ca.length;i++) {
let c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return JSON.parse(c.substring(nameEQ.length,c.length));
}
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}
4 changes: 2 additions & 2 deletions src/Controllers/Manage/ManageDVDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ private function postToDvdModel(DVDModel $model)
$model->Quantity = $_POST["Quantity"];
$model->Price = $_POST["Price"];
$model->Year = $_POST["Year"];
$model->TypeId = !is_null($_POST["TypeId"]) ? intval($_POST["TypeId"]) : null;
$model->GenreId = !is_null($_POST["GenreId"]) ? intval($_POST["GenreId"]) : null;
$model->TypeId = isset($_POST["TypeId"]) ? intval($_POST["TypeId"]) : null;
$model->GenreId = isset($_POST["GenreId"]) ? intval($_POST["GenreId"]) : null;
$model->Image = $_POST["Image"];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<select name="<?php echo $this->name; ?>"
<?php echo $this->required?"required":""; ?>
<?php echo $this->readOnly?"readOnly":""; ?>>
<option disabled selected>Select a value</option>
<option disabled selected value="null">Select a value</option>
<?php foreach ($this->availableValues as $item): ?>
<option value="<?php echo $item->Id; ?>" <?php echo $item->Id == $this->value ? "selected" : ""; ?>>
<?php echo $item->Name; ?>
Expand Down
33 changes: 33 additions & 0 deletions src/Views/DVD/DVDView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function addToCart(pId, pName)
{
let cart = readCookie("cart");

if(cart && cart.articles)
{
let existingArticle = cart.articles.find(x => x.id === pId);
if(existingArticle !== undefined)
{
existingArticle.quantity++;
}
else
{
cart.articles.push({
id: pId,
name: pName,
quantity: 1
})
}
}
else
{
cart = {
articles: [{
id: pId,
name: pName,
quantity: 1
}]
}
}

createCookie("cart", cart, 30);
}
2 changes: 1 addition & 1 deletion src/Views/DVD/DVDView.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function render(): void
{
$layoutData = new LayoutViewModel();
$layoutData -> pageSubTitle = $this->subTitle;
parent::renderLayout($layoutData, $this->data);
parent::renderLayout($layoutData, $this->data, true);
}
}
}
13 changes: 13 additions & 0 deletions src/Views/DVD/DVDView.style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
}

.product-detail {
position: relative;
margin: auto;
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -37,4 +38,16 @@
.details h3 {
text-align: left;
color: white;
}

.button {
position: absolute;
right: 0;
top: 5px;
padding: 10px 20px;
border: none;
border-radius: 4px;
background-color: #27994e;
color: white;
cursor: pointer;
}
5 changes: 5 additions & 0 deletions src/Views/DVD/DVDView.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
echo $data->dvd->LocalTitle;
?>
</h2>
<div class="button" onclick="addToCart(<?php echo $data->dvd->Id; ?>, '<?php echo $data->dvd->LocalTitle; ?>')">
<img alt="cart icon" src="/assets/add-to-cart.svg" width="32px" height="32px"/>
</div>
<div class="product-detail">
<div class="summary">
<img src="<?php echo $data->dvd->Image; ?>" class="img-preview"/>
Expand Down Expand Up @@ -47,5 +50,7 @@
<?php echo $data->dvd->Note; ?>
</div>
</div>


</div>

1 change: 1 addition & 0 deletions src/Views/Layout/LayoutView.style.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ header h1 a {
}

main {
position: relative;
display: flex;
flex: 9;
flex-direction: column;
Expand Down

0 comments on commit 40b9c1c

Please sign in to comment.