-
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.
Merge pull request #85 from akhilome/gh-pages
Consume API endpoints from frontend
- Loading branch information
Showing
25 changed files
with
662 additions
and
180 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
'extends': 'airbnb-base', | ||
'env': { | ||
'browser': true, | ||
}, | ||
'rules': { | ||
'no-undef': 'off', | ||
'no-console': 'off' | ||
} | ||
}; |
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,32 @@ | ||
const foodForm = document.querySelector('form'); | ||
|
||
function addNewFood(e) { | ||
e.preventDefault(); | ||
flashMessage('Adding food to menu ...'); | ||
const formData = { | ||
foodName: foodForm['food-item'].value, | ||
foodImage: foodForm['food-image'].value, | ||
price: foodForm['food-price'].value, | ||
}; | ||
|
||
fetch('https://kiakiafood.herokuapp.com/api/v1/menu', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-auth': token, | ||
}, | ||
body: JSON.stringify(formData), | ||
}).then(data => data.json()) | ||
.then((response) => { | ||
if (response.status !== 'success') return flashMessage(response.message, 'error'); | ||
|
||
window.location = 'admin-menu.html'; | ||
return flashMessage('Food added!', 'success'); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
flashMessage('Something went wrong while new food item', 'error'); | ||
}); | ||
} | ||
|
||
foodForm.addEventListener('submit', addNewFood); |
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,9 @@ | ||
// Admins should always default to the admin-menu page | ||
try { | ||
const { userStatus } = (jwt_decode(localStorage.getItem('kiakiafoodToken'))); | ||
if (userStatus === 'admin') { | ||
window.location = 'admin-menu.html'; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} |
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,67 @@ | ||
function adminOrderCardBluePrint(orderId, customerName, foodItems = [], date, status) { | ||
const formattedNames = foodItems | ||
.map(foodName => `<p>${foodName}</p>`).join(''); | ||
|
||
let actionButtons; | ||
|
||
switch (status) { | ||
case 'processing': | ||
actionButtons = '<button class="small done">mark done</button>'; | ||
break; | ||
case 'cancelled': | ||
actionButtons = '<button class="small disabled">handled</button>'; | ||
break; | ||
case 'complete': | ||
actionButtons = '<button class="small disabled">handled</button>'; | ||
break; | ||
default: | ||
actionButtons = '<button class="small accept">accept</button><button class="small reject">reject</button>'; | ||
} | ||
|
||
return ` | ||
<div data-orderid="${orderId}" class="order-card"> | ||
<div class="order-card_food-items"> | ||
<h3>Food Item(s)</h3> | ||
<div class="order-card_food-items_names">${formattedNames}</div> | ||
</div> | ||
<div class="order-card_date"> | ||
<p><strong>date:</strong></p> | ||
<p>${date.split('T')[0].split('-').reverse().join('/')}</p> | ||
</div> | ||
<div class="order-card_price"> | ||
<p><strong>customer:</strong></p> | ||
<p>${customerName}</p> | ||
</div> | ||
<div class="order-card_admin-actions">${actionButtons}</div> | ||
</div> | ||
`; | ||
} | ||
|
||
const url = 'https://kiakiafood.herokuapp.com/api/v1/orders'; | ||
|
||
fetch(url, { | ||
headers: { 'x-auth': token }, | ||
}).then(data => data.json()) | ||
.then((response) => { | ||
const { orders } = response; | ||
if (!orders.length) { | ||
document.querySelector('.admin-orders').innerHTML = '<p>No orders have been made yet!</p>'; | ||
} else { | ||
const allOrders = orders | ||
.map(order => adminOrderCardBluePrint( | ||
order.id, | ||
order.author, | ||
order.items, | ||
order.date, | ||
order.status, | ||
)) | ||
.join(''); | ||
|
||
document.querySelector('.admin-orders').innerHTML = allOrders; | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
flashMessage('An error occured while fetching the orders', 'error'); | ||
document.querySelector('.admin-orders').innerHTML = '<div></div>'; | ||
}); |
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,10 @@ | ||
const token = localStorage.getItem('kiakiafoodToken'); | ||
if (!token) { | ||
window.location = 'sign-in.html'; | ||
} | ||
|
||
try { | ||
jwt_decode(token); | ||
} catch (error) { | ||
window.location = 'sign-in.html'; | ||
} |
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,5 @@ | ||
const decoded = jwt_decode(token); | ||
|
||
if (decoded.userStatus !== 'admin') { | ||
window.location = 'index.html'; | ||
} |
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,25 +1,36 @@ | ||
// Get all buttons | ||
const buyButtons = document.querySelectorAll('.food-details__action > button'); | ||
function addToCart(e) { | ||
if (!e.target.matches('button')) return; // event delegation | ||
const button = e.target; | ||
|
||
function addToCart() { | ||
if(!localStorage.getItem('cart')) localStorage.setItem('cart', '{}'); | ||
const cart = JSON.parse(localStorage.getItem('cart')); | ||
const foodDetails = this.parentNode.previousElementSibling; | ||
// Prevent unauthenticated customer from adding to cart | ||
try { | ||
const token = localStorage.getItem('kiakiafoodToken'); | ||
jwt_decode(token); | ||
if (!token) throw new Error(); | ||
} catch (error) { | ||
window.location = 'sign-in.html'; | ||
return; | ||
} | ||
|
||
|
||
const cart = JSON.parse(localStorage.getItem('cart')) || {}; | ||
|
||
const foodDetails = button.parentNode.previousElementSibling; | ||
const foodImage = foodDetails.parentNode.previousElementSibling.src; | ||
const foodId = foodDetails.dataset.foodid | ||
const foodId = foodDetails.dataset.foodid; | ||
const foodName = foodDetails.querySelector('h4').innerText; | ||
const foodPrice = foodDetails.querySelector('p').innerText; | ||
if(!cart.hasOwnProperty(foodId)) { | ||
cart[foodId] = { foodName, foodPrice, foodImage } | ||
|
||
if (!Object.keys(cart).includes(foodId)) { | ||
cart[foodId] = { foodName, foodPrice, foodImage }; | ||
} else { | ||
return flashMessage('Item already in cart', 'error'); | ||
flashMessage('Item already in cart', 'error'); | ||
return; | ||
} | ||
|
||
localStorage.setItem('cart', JSON.stringify(cart)); | ||
flashMessage('Item added to cart', 'success'); | ||
} | ||
|
||
|
||
// Event Listeners | ||
buyButtons.forEach(button => button.addEventListener('click', addToCart)); | ||
document.querySelector('section.food-menu').addEventListener('click', addToCart); |
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,66 @@ | ||
function orderCardBluePrint(foodNames = [], orderPrice, date, status) { | ||
const formattedNames = foodNames | ||
.map(foodName => `<p>${foodName}</p>`).join(''); | ||
|
||
let orderStatus; | ||
|
||
switch (status) { | ||
case 'processing': | ||
orderStatus = 'progress'; | ||
break; | ||
case 'cancelled': | ||
orderStatus = 'rejected'; | ||
break; | ||
case 'complete': | ||
orderStatus = 'completed'; | ||
break; | ||
default: | ||
orderStatus = 'new'; | ||
} | ||
|
||
return ` | ||
<div class="order-card"> | ||
<div class="order-card_food-items"> | ||
<h3>Food Item(s)</h3> | ||
<div class="order-card_food-items_names">${formattedNames}</div> | ||
</div> | ||
<div class="order-card_price"> | ||
<p><strong>price:</strong></p> | ||
<p>₦${orderPrice.toLocaleString()}</p> | ||
</div> | ||
<div class="order-card_date"> | ||
<p><strong>date:</strong></p> | ||
<p>${date.split('T')[0].split('-').reverse().join('/')}</p> | ||
</div> | ||
<div class="order-status-${orderStatus}"></div> | ||
</div> | ||
`; | ||
} | ||
|
||
let decodedUserId; | ||
try { | ||
decodedUserId = (jwt_decode(token)).userId; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
const url = `https://kiakiafood.herokuapp.com/api/v1/users/${decodedUserId}/orders`; | ||
|
||
fetch(url, { | ||
headers: { 'x-auth': token }, | ||
}).then(data => data.json()) | ||
.then((response) => { | ||
if (!response.orders.length) { | ||
document.querySelector('.order-history').innerHTML = '<p>You have not made any orders yet!</p>'; | ||
} else { | ||
const allUserOrders = response.orders | ||
.map(order => orderCardBluePrint(order.items, order.price, order.date, order.status)) | ||
.join(''); | ||
document.querySelector('.order-history').innerHTML = allUserOrders; | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
flashMessage('An error occured while fetching your order history', 'error'); | ||
document.querySelector('.order-history').innerHTML = '<div></div>'; | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.