├── README.md ├── app.js ├── index.html └── style.css /README.md: -------------------------------------------------------------------------------- 1 | # beer-generator-javascript 2 | 3 | Random beer Generator using vanilla JavaScript API fetch 4 | 5 | View the full walkthrough [here](https://www.youtube.com/channel/UC5DNytAJ6_FISueUfzZCVsw) 6 | 7 | I have kept the styling at a bare miniumum for you to go wild and make it your own. Please tag me as I would LOVE to see your game!!! 8 | 9 | After creating my Random Beer generator in React, I decided to take one step back and create it using super simple vanilla JavaScript with HTML and CSS. This was mainly to show those who have never used React before how to make API get requests, and also to showcase the use of fetch(). 10 | 11 | Thanks to punkapi.com for their BrewDog catalogue API. 12 | 13 | ### Inbuilt JavaScript functions used: 14 | * addEventListener() 15 | * querySelector() 16 | * e.preventDefault() 17 | * fetch() 18 | 19 | 20 | ### MIT Licence 21 | 22 | Copyright (c) 2020 Ania Kubow 23 | 24 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 25 | 26 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 27 | 28 | *Translation: Ofcourse you can use this for you project! Just make sure to say where you got this from :) 29 | 30 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', () => { 2 | const startBtn = document.querySelector('.beer-button') 3 | const randomBeer = document.querySelector('.random-beer') 4 | const descriptionDisplay = document.querySelector('.description') 5 | 6 | function getData(e) { 7 | e.preventDefault() 8 | 9 | fetch('https://api.punkapi.com/v2/beers/random') 10 | .then(response => { 11 | return response.json() 12 | }) 13 | .then(data => { 14 | console.log(data) 15 | const name = data[0].name 16 | const description = data[0].description 17 | const {volume} = data[0] 18 | const volumeValue = volume.value 19 | const volumeUnit = volume.unit 20 | 21 | randomBeer.innerHTML = name + ' ' + volumeValue + volumeUnit 22 | descriptionDisplay.innerHTML = description 23 | }) 24 | } 25 | 26 | startBtn.addEventListener('click', getData) 27 | }) 28 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Beer Generator JavaScript 6 | 7 | 8 | 9 | 10 | 11 |
12 |

Click me for a Random Beer!

13 |
14 | 15 | 16 | 17 |
18 |

19 |
20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | //Special thanks to Marifer Villarroel! 2 | 3 | html, 4 | body { 5 | height: 100vh; 6 | background-color: #FEBC58; 7 | } 8 | 9 | .container { 10 | height: 100vh; 11 | display: flex; 12 | flex-direction: column; 13 | align-items: center; 14 | text-align: center; 15 | background-color: #FEBC58; 16 | } 17 | 18 | .random-beer { 19 | margin-top: 150px; 20 | color: white; 21 | font-size: 30px; 22 | font-family: helvetica; 23 | } 24 | 25 | .description { 26 | max-width: 600px; 27 | color: #f7ecb7; 28 | font-family: helvetica; 29 | font-szie: 20px; 30 | } 31 | 32 | .beer-button { 33 | display: inline-flex; 34 | } 35 | 36 | .beer-button a{ 37 | display: inline-block; 38 | text-decoration: none; 39 | background-color: #FEBC58; 40 | box-shadow: -8px -8px 12px 0 rgba(0, 0, 0, 0.3), 41 | 12px 12px 16px rgba(255, 255, 255, 0.25); 42 | border-radius: 50%; 43 | width: 80px; 44 | height: 80px; 45 | transition: 0.5s; 46 | display: flex; 47 | align-items: center; 48 | justify-content: center; 49 | } 50 | 51 | .beer-button a:hover { 52 | box-shadow: inset 6px 6px 10px 0 rgba(0, 0, 0, 0.2), 53 | inset -6px -6px 10px 0 rgba(255, 255, 255, 0.5), 54 | } 55 | 56 | .beer-button .fab { 57 | padding-left: 3px; 58 | } 59 | --------------------------------------------------------------------------------