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
{ | |
"name": "nifty-npm-tips", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { }, | |
"devDependencies": { }, | |
"scripts": { | |
"start": "react-scripts start", | |
"build": "react-scripts build", | |
"test": "react-scripts test", |
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
// get books | |
const getBookByName = (name) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ name: 'Art of War', author: 'Sun Tsu', ISBN: 9780140439199 }); | |
}, 500); | |
}); | |
}; | |
// check if that book exists |
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
const beingAsync = async () => { | |
const book = await getBookByName('Art of War'); | |
const { stock } = await checkIfBookExists(book.ISBN); | |
if (stock > 0) { | |
const response = getTheBookPrice(book.ISBN); | |
console.log(`Book price is $${response.offers.booksrun.new.price}!`); | |
} else { | |
console.log('Book not found!'); | |
} | |
}; |
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
getBookByName('Art of War').then((book) => { | |
if (book) { | |
checkIfBookExists(book.ISBN).then((data) => { | |
if (data.stock > 0) { | |
getTheBookPrice(data.ISBN).then((response) => { | |
console.log(response.offers.booksrun.new.price); // uff 😅 | |
}); | |
} | |
}); | |
} else { |
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
// hit BooksRun API - To get the new, used and rent price | |
getTheBookPrice = (ISBN) => { | |
return new Promise((resolve, reject) => { | |
const URL = `https://booksrun.com/API/v3/price/buy/${ISBN}?key=${process.env.API_KEY}`; | |
fetch(URL) | |
.then((response) => { | |
resolve(response.json()); | |
}) | |
.catch((err) => { |
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
getBookByName('Art of War').then((book) => { | |
if (book) { | |
checkIfBookExists(book.ISBN).then((data) => { | |
console.log(data.stock > 0); | |
}); | |
} else { | |
console.log('Book not found!'); | |
} | |
}); |
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
// check if that book exists | |
checkIfBookExists = (ISBN) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ stock: 10, ISBN: 9780140439199 }); | |
}, 500); | |
}); | |
}; |
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
getBookByName('Art of War').then((book) => { | |
if (book) { | |
console.log('Found the book!'); | |
} else { | |
console.log('Book not found!'); | |
} | |
}); |
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
// get books | |
const getBookByName = (name) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ name: 'Art of War', author: 'Sun Tsu', ISBN: 9780140439199 }); | |
}, 500); | |
}); | |
}; |
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
function useTheme(theme) { | |
useLayoutEffect( | |
() => { | |
// Iterate through each value in theme object | |
for (const key in theme) { | |
// Update css variables in document's root element | |
document.documentElement.style.setProperty(`--${key}`, theme[key]); | |
} | |
}, | |
[theme] // Only call again if theme object reference changes |
NewerOlder