Today we will
In a future lab you will design and implement your own web service to return the JSON data used in this lab.
There are a few options to get local servers set up, which we'll need for running our own PHP web services and fetching local files. If you use Windows 10, we recommend getting Ubuntu Bash and have a tutorial on getting it with PHP installed here. If you are using other versions of Windows you can download and use Cygwin.
The other option we'll be using is MAMP as our local server. Click to download the free regular version (not the pro version)
Following your exciting (and sometimes challenging) quarter in CSE 154, you find your first web-dev opportunity in the real world. A new local café called Mowgli's Café is joining the online-delivery craze, aiming to provide customers with fresh coffee and breakfast at the click of a button. They've just hired you to create their website.
A designer with little web development background has created a PNG using Adobe Illustrator for the site design the manager wants, as well as specifications for desired behavior. The owners hired a small team of developers to start the project, but for personal reasons they had to drop the contract rather quickly leaving you to pick up the with the codebase as it is. They told you to find the starter files here.
Note: This scenario is very similar to web projects done by developer "contractors" hired on a project-basis like this....
First, the manager asks you to implement the menu, listing all of the current items offered by the café and their prices.
They provided you with a brief document which describes an API that returns all of the data you need to populate the menu.
They ask that you maintain the organization in the JSON format as much as possible, grouping the different categories and subcategories nicely so that customers can easily navigate the menu page. The designer has provided you another set of screenshots to guide you, provided on the slide below. Additional instructions and hints are provided on following slides below.
There are a lot of pieces in this task, so taking the time to develop a clear plan-of-action will save you a great deal of hassle and debugging later on. First, note that the menu the manager provided you through the API in JSON format will most likely be updated in the future depending on supply and demand. You know to avoid hardcoding values in the HTML, but rather use JS to generate the menu when the page loads. This means that the HTML given to you will most likely not change at all here. Fortunately there is already a script tag at the top that points to a .js file with some behavior already written.
Recall that you can use AJAX with JS to request data from the server and process/return the requested data with a web service. Since the data is conveniently given to you in JSON format, this should be fairly straightforward.
Your next step is to look at the API documentation to understand the JSON that is returned by the service. Then look at the screenshots and see what data can map to the elements that will be on the page.
/**** TO DO:
. Now it's your turn to make this work!
Once you get the fetch working, ensure that all parts of the page work. When you click on a radio button, does the total update and is it correct? What happens when you click "Submit your order"? What other things do you think you can test to ensure this page is functioning correctly?
When you find a problem, use the Chrome inspector to help you isolate what the problem is. Some helpful hints are:
The manager is very happy at the progress you've made and seeing everything start to come together. Now that you have a fairly solid foundation to populate the menu, it's time to work on processing user input on the page using JS.
Ensure that a customer may choose no more than 1 drink item and 1 bakery item (a customer may choose 1 of each). If the customer chooses a drink, the "sizes" section of the menu should have an option set to 12oz by default (otherwise no size should be checked if the user hasn't selected a drink item).
Whenever a change is made to the selected items on the menu, the price displayed at the bottom of the page, listed as "Your total (before tip and 8% tax)", should be updated. The value for the "final cost" displayed at the very bottom of the page should be equal to (price * 1.08 + tip), where "price" is the price before tip and tax.
For the user's order information at the bottom of the page, they should only be able to enter numbers for the tip, and shouldn't be able to enter negative values. The value they enter for "State" should be in the format of a 2-letter state code (e.g., WA for Washington).
Your manager now asks you to implement a feature to save the customer's last order so it will be easy for them to come back the next time and order again. The order information will contain the drink (if any), size of the drink (if a drink is chosen), baked good (if any), tip, and total cost.
Now whenever the user clicks "Submit Your order" you should use your
window.localStorage
to save all of the information the user has entered onto
the page: which drink, the size, the baked good, as well as the tip, address, city
state and zip code. You do not need to store the total cost, that can be regenerated
when you load the information back in from storage.
Next, when the page is loaded, reload this information from local storage and update the fields and radio buttons accordingly. Finally once all of the information is reloaded, you can recalculate the cost.
You will find the documentation on localStorage helpful in knowing how to set and get an item.
Be sure to take a look at the application tab in your developer tools to see that your localStorage is being set correctly.
You can find one solution here.