-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Awesome Book: Using modules with ES6 #1
Open
miltonHenschel
wants to merge
12
commits into
main
Choose a base branch
from
1-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a7c7de
Setup add/remove buttons
miltonHenschel 9386dab
Create book class in book.js and import it to index.js
miltonHenschel dd3c306
Create UI class in ui.js and export to index.js
miltonHenschel 0393385
Create LocalStore class in localStore.js and export to index.js
miltonHenschel b4f7f5d
Create MenuNav class in menuNav.js and export to index.js
miltonHenschel 05df73d
Import time and date library from luxon.js and export to index.js
miltonHenschel ca39b56
Fix linter errors from luxon.js
miltonHenschel 045eea1
Updae luxon.js to use ES6
miltonHenschel cd46796
remove link to btnAddClick js file from html
miltonHenschel ec4fd87
Refactor UI and LocalStore to use arrow functions
miltonHenschel acd155e
Add media query for desktop version
miltonHenschel 166d63e
Fix css linter error
miltonHenschel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,41 @@ | ||
'use script'; | ||
|
||
import Book from '../modules/book.js'; | ||
import UI from '../modules/ui.js'; | ||
import LocalStore from '../modules/localStore.js'; | ||
import menuNav from '../modules/menuNav.js'; | ||
import { DateTime } from '../modules/luxon.js'; | ||
|
||
// "max-classes-per-file": "off" | ||
|
||
// DECLARE VARIABLES | ||
const titleInput = document.querySelector('.title-book-add'); | ||
const authorInput = document.querySelector('.author-book-add'); | ||
const btnAdd = document.querySelector('.btn-add'); | ||
// DECLARE GLOBAL VARIABLES | ||
|
||
const bookDisplay = document.querySelector('.books-display'); | ||
const bookDetail = document.querySelector('.books-detail'); | ||
const errorMsg = document.querySelector('.error-message'); | ||
const navList = document.querySelector('#nav-list'); | ||
const navAdd = document.querySelector('#nav-add'); | ||
const navContact = document.querySelector('#nav-contact'); | ||
const page = document.querySelector('.section'); | ||
const form = document.querySelector('.add-book-form'); | ||
const contact = document.querySelector('#contact'); | ||
const timeInfo = document.querySelector('#time-info'); | ||
let newId = 0; | ||
|
||
// DECLARE MAIN CLASS | ||
class Book { | ||
constructor(title, author, id) { | ||
this.title = title; | ||
this.author = author; | ||
this.id = id; | ||
} | ||
} | ||
|
||
// LOCAL STORAGE | ||
|
||
class LocalStore { | ||
static getBooks() { | ||
let books; | ||
const localBook = localStorage.getItem('local'); | ||
if (!localBook) { | ||
books = []; | ||
} else { | ||
books = JSON.parse(localBook); | ||
} | ||
return books; | ||
} | ||
|
||
static addBooks(newBook) { | ||
const books = LocalStore.getBooks(); | ||
if (books) { | ||
books.push(newBook); | ||
localStorage.setItem('local', JSON.stringify(books)); | ||
} | ||
} | ||
|
||
// static deleteBooks() | ||
} | ||
|
||
class UI { | ||
static showBooks() { | ||
const books = LocalStore.getBooks(); | ||
books.forEach((newBook) => { | ||
UI.addBookToList(newBook); | ||
}); | ||
} | ||
const titleInput = document.querySelector('.title-book-add'); | ||
const authorInput = document.querySelector('.author-book-add'); | ||
const btnAdd = document.querySelector('.btn-add'); | ||
|
||
static addBookToList(newBook) { | ||
newBook.id = newId; | ||
const bookInfo = ` | ||
<div id="${newId}"> | ||
<p class="book-position">"<span class="">${newBook.title}</span>" by <span class="">${newBook.author}</span></p> | ||
<button id="${newId}" class="btn-remove">Remove</button> | ||
</div> | ||
`; | ||
bookDetail.innerHTML += bookInfo; | ||
newId += 1; | ||
} | ||
// CLICK ON REMOVE BUTTON | ||
|
||
static deleteBookFromList(e) { | ||
e.parentElement.remove(); | ||
bookDisplay.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
if (e.target.className === 'btn-remove') { | ||
const { id } = e.target; | ||
let books = LocalStore.getBooks(); | ||
books = books.filter((bk) => JSON.stringify(bk.id) !== id); | ||
localStorage.setItem('local', JSON.stringify(books)); // LOCAL STORAGE | ||
UI.deleteBookFromList(e.target); | ||
// window.location.reload(); | ||
} | ||
} | ||
}); | ||
|
||
// ADD BOOKS | ||
// CLICK ON ADD BUTTON | ||
|
||
btnAdd.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
// window.location.reload(); | ||
const books = LocalStore.getBooks(); | ||
const newTitle = titleInput.value; | ||
const newAuthor = authorInput.value; | ||
|
@@ -102,73 +57,16 @@ btnAdd.addEventListener('click', (e) => { | |
form.reset(); | ||
}); | ||
|
||
// REMOVE BOOKS | ||
|
||
bookDisplay.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
if (e.target.className === 'btn-remove') { | ||
const { id } = e.target; | ||
let books = LocalStore.getBooks(); | ||
books = books.filter((bk) => JSON.stringify(bk.id) !== id); | ||
localStorage.setItem('local', JSON.stringify(books)); // LOCAL STORAGE | ||
UI.deleteBookFromList(e.target); | ||
} | ||
}); | ||
|
||
// MENU NAVIGATION | ||
|
||
navList.addEventListener('click', () => { | ||
page.style.display = 'block'; | ||
form.style.display = 'none'; | ||
contact.style.display = 'none'; | ||
}); | ||
|
||
navAdd.addEventListener('click', () => { | ||
page.style.display = 'none'; | ||
form.style.display = 'block'; | ||
contact.style.display = 'none'; | ||
}); | ||
// GENERAL | ||
|
||
navContact.addEventListener('click', () => { | ||
page.style.display = 'none'; | ||
form.style.display = 'none'; | ||
contact.style.display = 'block'; | ||
}); | ||
window.addEventListener('DOMContentLoaded', UI.showBooks); | ||
window.addEventListener('DOMContentLoaded', menuNav); | ||
|
||
// TIME DISPLAY | ||
function timeDisplay() { | ||
const myDate = new Date(); | ||
|
||
const daysList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
const monthsList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'August', 'October', 'November', 'December']; | ||
|
||
const date = myDate.getDate(); | ||
const month = monthsList[myDate.getMonth()]; | ||
const year = myDate.getFullYear(); | ||
const day = daysList[myDate.getDay()]; | ||
|
||
const today = `${day}, ${month} ${date}, ${year},`; | ||
|
||
let amOrPm; | ||
const twelveHours = () => { | ||
if (myDate.getHours() > 12) { | ||
amOrPm = 'PM'; | ||
const twentyFourHourTime = myDate.getHours(); | ||
const conversion = twentyFourHourTime - 12; | ||
return `${conversion}`; | ||
} | ||
amOrPm = 'AM'; | ||
return `${myDate.getHours()}`; | ||
}; | ||
const hours = twelveHours(); | ||
const minutes = myDate.getMinutes(); | ||
|
||
const currentTime = `${hours}:${minutes} ${amOrPm}`; | ||
|
||
timeInfo.innerHTML = `${today} ${currentTime}`; | ||
} | ||
|
||
// GENERAL | ||
const timeDisplay = () => { | ||
const timeInfo = document.querySelector('#time-info'); | ||
timeInfo.innerHTML = `${DateTime.now().toLocaleString(DateTime.DATETIME_MED)}`; | ||
}; | ||
Comment on lines
+67
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
document.addEventListener('DOMContentLoaded', UI.showBooks); | ||
document.addEventListener('DOMContentLoaded', timeDisplay); | ||
window.addEventListener('DOMContentLoaded', timeDisplay); |
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 @@ | ||
class Book { | ||
constructor(title, author, id) { | ||
this.title = title; | ||
this.author = author; | ||
this.id = id; | ||
} | ||
} | ||
|
||
export default Book; |
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,24 @@ | ||
class LocalStore { | ||
static getBooks = () => { | ||
let books; | ||
const localBook = localStorage.getItem('local'); | ||
if (!localBook) { | ||
books = []; | ||
} else { | ||
books = JSON.parse(localBook); | ||
} | ||
return books; | ||
} | ||
|
||
static addBooks = (newBook) => { | ||
const books = LocalStore.getBooks(); | ||
if (books) { | ||
books.push(newBook); | ||
localStorage.setItem('local', JSON.stringify(books)); | ||
} | ||
} | ||
|
||
// static deleteBooks() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
export default LocalStore; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.