-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use fetch when adding item to cart rather than submit form
- Loading branch information
1 parent
07d8648
commit f02da36
Showing
5 changed files
with
123 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
.env | ||
/client/node_modules | ||
/src/public/js | ||
/client/node_modules |
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 |
---|---|---|
|
@@ -2,5 +2,5 @@ package models | |
|
||
type CartItem struct { | ||
ID int | ||
Quantity int | ||
Quantity int `json:"quantity"` | ||
} |
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,92 @@ | ||
/* | ||
primary, | ||
success, | ||
warning, | ||
danger | ||
*/ | ||
|
||
/* | ||
element: string; | ||
className: string, | ||
attributes: { | ||
key: string; | ||
value: string; | ||
}[]; | ||
*/ | ||
const createElement = (options) => { | ||
if (!options.element) return null; | ||
|
||
const element = document.createElement(options.element); | ||
|
||
if (options.className) { | ||
element.className = options.className; | ||
} | ||
|
||
if (options.attributes && options.attributes.length > 0) { | ||
options.attributes.forEach((attribute) => { | ||
element.setAttribute(attribute.key, attribute.value); | ||
}); | ||
} | ||
|
||
if (options.text) { | ||
element.textContent = options.text; | ||
} | ||
|
||
return element; | ||
}; | ||
|
||
const appendAlert = (text, severity) => { | ||
if (!text) return; | ||
|
||
const alertContainer = document.getElementById('alert-container'); | ||
|
||
Array.from(alertContainer.children).forEach((child) => { | ||
alertContainer.removeChild(child); | ||
}); | ||
|
||
const alertElement = createElement({ | ||
element: 'div', | ||
className: 'uk-alert-' + severity, | ||
attributes: [{ key: 'uk-alert', value: '' }], | ||
}); | ||
|
||
alertElement.appendChild( | ||
createElement({ | ||
element: 'a', | ||
className: 'uk-alert-close', | ||
attributes: [{ key: 'uk-close', value: '' }], | ||
}) | ||
); | ||
|
||
alertElement.appendChild(createElement({ text, element: 'p' })); | ||
|
||
alertContainer.appendChild(alertElement); | ||
}; | ||
|
||
const addToCart = async (productId) => { | ||
const addToCartButton = document.getElementById('add-to-cart'); | ||
|
||
addToCartButton.setAttribute('disabled', true); | ||
|
||
try { | ||
const quantityInput = document.querySelector('[name=quantity]'); | ||
|
||
const quantity = parseInt(quantityInput.value); | ||
|
||
const res = await fetch('/add-to-cart/' + productId, { | ||
method: 'POST', | ||
body: JSON.stringify({ quantity }), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const data = await res.json(); | ||
|
||
appendAlert(data.message, data.success ? 'success' : 'danger'); | ||
} catch (err) { | ||
return; | ||
} | ||
|
||
addToCartButton.removeAttribute('disabled'); | ||
}; |
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