Created
February 23, 2018 19:13
-
-
Save rich-at-thinkful/c82756e673451e0e651ef8f148aef336 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const STORE = { | |
items: [ | |
{ name: 'apples', checked: false, createdAt: new Date(Date.now() - 100000) }, | |
{ name: 'oranges', checked: false, createdAt: new Date(Date.now() - 200000) }, | |
{ name: 'milk', checked: true, createdAt: new Date(Date.now() - 400000) }, | |
{ name: 'bread', checked: false, createdAt: new Date(Date.now() - 300000) } | |
], | |
sortBy: 'alpha' | |
}; | |
function getTimeString(time) { | |
return strftime('%b-%d %H:%M', time); | |
} | |
function generateItemElement(item, itemIndex) { | |
return ` | |
<li class="js-item-index-element" data-item-index="${itemIndex}"> | |
<span class="shopping-item js-shopping-item ${item.checked ? 'shopping-item__checked' : ''}">${item.name}</span> | |
<span class="timestamp">${getTimeString(item.createdAt)}</span> | |
<div class="shopping-item-controls"> | |
<button class="shopping-item-toggle js-item-toggle"> | |
<span class="button-label">check</span> | |
</button> | |
<button class="shopping-item-delete js-item-delete"> | |
<span class="button-label">delete</span> | |
</button> | |
</div> | |
</li>`; | |
} | |
function generateShoppingItemsString(shoppingList) { | |
console.log('Generating shopping list element'); | |
const items = shoppingList.map((item, index) => generateItemElement(item, index)); | |
return items.join(''); | |
} | |
function renderShoppingList() { | |
// render the shopping list in the DOM | |
console.log('`renderShoppingList` ran'); | |
let filteredItems = [ ...STORE.items ]; | |
switch(STORE.sortBy) { | |
case 'alpha': | |
filteredItems = filteredItems.sort((a, b) => a.name > b.name); | |
break; | |
case 'time': | |
filteredItems = filteredItems.sort((a, b) => a.createdAt > b.createdAt); | |
} | |
const shoppingListItemsString = generateShoppingItemsString(filteredItems); | |
// insert that HTML into the DOM | |
$('.js-shopping-list').html(shoppingListItemsString); | |
} | |
function addItemToShoppingList(itemName) { | |
console.log(`Adding "${itemName}" to shopping list`); | |
STORE.items.push({ name: itemName, checked: false, createdAt: new Date() }); | |
} | |
function handleNewItemSubmit() { | |
$('#js-shopping-list-form').submit(function (event) { | |
event.preventDefault(); | |
console.log('`handleNewItemSubmit` ran'); | |
const newItemName = $('.js-shopping-list-entry').val(); | |
$('.js-shopping-list-entry').val(''); | |
addItemToShoppingList(newItemName); | |
renderShoppingList(); | |
}); | |
} | |
function toggleCheckedForListItem(itemIndex) { | |
console.log('Toggling checked property for item at index ' + itemIndex); | |
STORE.items[itemIndex].checked = !STORE.items[itemIndex].checked; | |
} | |
function getItemIndexFromElement(item) { | |
const itemIndexString = $(item) | |
.closest('.js-item-index-element') | |
.attr('data-item-index'); | |
return parseInt(itemIndexString, 10); | |
} | |
function handleItemCheckClicked() { | |
$('.js-shopping-list').on('click', '.js-item-toggle', event => { | |
console.log('`handleItemCheckClicked` ran'); | |
const itemIndex = getItemIndexFromElement(event.currentTarget); | |
toggleCheckedForListItem(itemIndex); | |
renderShoppingList(); | |
}); | |
} | |
function deleteItem(index) { | |
STORE.items.splice(index, 1); | |
} | |
function handleDeleteItemClicked() { | |
// this function will be responsible for when users want to delete a shopping list | |
// item | |
$('.js-shopping-list').on('click', '.js-item-delete', event => { | |
console.log('`handleDeleteItemClicked` ran'); | |
const itemIndex = getItemIndexFromElement(event.currentTarget); | |
deleteItem(itemIndex); | |
renderShoppingList(); | |
}); | |
} | |
function changeSortBy(sortBy) { | |
STORE.sortBy = sortBy; | |
} | |
function handleSortChange() { | |
$('#sort-options').change(event => { | |
// get DOM info about action | |
const option = $(event.currentTarget).find('option:selected').val(); | |
// change store | |
changeSortBy(option); | |
// render | |
renderShoppingList(); | |
}); | |
} | |
// this function will be our callback when the page loads. it's responsible for | |
// initially rendering the shopping list, and activating our individual functions | |
// that handle new item submission and user clicks on the "check" and "delete" buttons | |
// for individual shopping list items. | |
function handleShoppingList() { | |
renderShoppingList(); | |
handleNewItemSubmit(); | |
handleItemCheckClicked(); | |
handleDeleteItemClicked(); | |
handleSortChange(); | |
} | |
// when the page loads, call `handleShoppingList` | |
$(handleShoppingList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment