Today we will build a Christmas list, just like you did back in the Intro to Ruby module, but this time it'll have a real front-end!
We will be creating a form via which we can upload gifts to our list. We will also be using an API to get gift ideas we can add to our list.
The Bootstrap stylesheet is already linked in the index.html
file for you so feel free to use their pre-made classes or have a go at making your own in style.css
!
Note: there will be no rake
for this challenge! To see if your page is working, you'll have to test it out yourself in the browser.
Let's begin by creating our gift form. It should have the following inputs:
- Name
- Price
Use a little CSS and/or Bootstrap to make it look neat and center it in the middle of the page. Don't worry about spending too long on this; you can come back to it at the end if you want to make it look prettier 🖌️
Let's create a list and two or three hard-coded items. We will remove these shortly so we can add the items via the form; they're just there for now so we can style our list :)
Let's make our form work!
At which moment do we want to add an item to our list? When we submit our form, of course!
First, select the form. Then, listen out for form submission. For now, we can just console.log('Form submitted!')
when we hear it.
Note: Remember that the default behaviour of a form is that it refreshes the page unless we tell it not to!
Before we start thinking about adding things to the list, let's start off with grabbing the name and price from the form's inputs.
Remember, you first need to select each input and then you can access its value.
To check if you're accessing the right elements, you can use console.log()
at each stage.
Now that we have the information from the form, lets select our list an add our gift to the end of it.
Sometimes it's hard to think of gifts for everyone. So, let's use the Fake Store API to come up wiht some ideas! Take a minute to read the documentation before beginning.
For our 'ideas' form, we want to have a dropdown with the following options:
- Electronics
- Jewelry
- Men's clothing
- Women's clothing
Important: It doesn't matter if you have the text displayed in the options capitalised or not, but the value
of each one must be written the same as they are here
Follow the same steps as you did for your previous form (HTML, basic CSS and listening out for a submission).
We will be making a fetch
request to make a call to the API.
As there is an endpoint for each category, we will want to make sure that we can access any endpoint using the same code.
Below is some code to get you started. Don't forget that you first need to grab the user's chosen category from the input!
Below is some code that makes a call to the endpoint for the "jewelery" category. You can use this as a starting point to help you figure out how to dyanmically make calls to the API depending on the user's selected category.
fetch('https://fakestoreapi.com/products/category/jewelery')
.then(response => response.json())
.then(data => console.log(data))
Use console.log()
to see what we got back from the API.
Look carefully at what the API has given you - which information is relevant to our list? How do we access it?
Let's display the gift options in a separate list so the user can see which choices are available to them.
In a moment we'll want to make it so that we can select ideas and add them to our main list, so you can go ahead and create an "Add" button for each one.
Now, when a click is heard on the "Add" button of each idea, let's remove that item from the ideas list and add it the main gift list.
Already bought a gift? Great! So let's cross it off on our list.
For both the items we add via our form and also the ones that come from our ideas list, listen out for a click. Upon clicking, we want to cross the item off.
🤔 Bonus: Can you make it so that when we hover over an item, the mouse turns to a 👆🏽?
Notice how when we refresh the page, we lose the items we added to our list. Can you store the items in localStorage as we add them and load them up when we refresh?
Have a go at implementing Tom Select to improve the appearance of the dropdown list.