W03 Team Activity: Dynamic Header and Footer
Overview
This activity will finish the process of converting the SleepOutside site to a dynamic site.
Activity Instructions
Complete the following assignment as a team. Designate one team member as the "main driver" and collaborate on their copy of the code. Everyone on the team should be actively engaged in writing the code and contributing to the solution. Once the solution is working, make sure that everyone on the team gets a copy of the code. Each week let someone else be the "main driver" of the coding.
There are many spots where code examples have been given. To get the most out of this activity, do not look at the examples until your group has given that section a try. Then after you look at the example, resist the temptation to copy/paste. Use the examples to get correction, or help you get unstuck.
Core Requirements
Trello
- In the synchronous meeting, the driver should visit the team's copy of the project Trello board.
- Add each of the attending team members to the
Team Activity 03: Dynamic header and footer
card. - Move it to the
"Doing"
list in Trello. - Read the details of the card together.
GitHub Branch
- Pull any changes from the team GitHub project before proceeding.
- Create a new branch named
driverinitials--team3
where driverinitials is the driver's initials.
The Header and Footer
The code for the Header and Footer of our site should be same or nearly the same on all pages of a site. We currently have 4 copies, one on each .html file in the site. If we need to make a change to the header we will have to visit each page to keep them the same. That is a bother with 4 pages and it becomes unmanageable with larger sites. Just as it was for the product list, templating can be a solution here as well.
There is a difference this time in that there is only one copy of our template, and there is little information that needs to be updated. The header and footer are mostly static. Instead of embedding the template in JavaScript like we did with the product listing, this time we will pull the template HTML into a separate file that we can load in with AJAX.
- Create another directory called
partials
(make sure you put this in thesrc/public
directory). - Add two new files to the partials directory:
header.html
andfooter.html
. - Copy the HTML from the inside of the header and footer elements from the
src/index.html
page and place that HTML into its respective file. - Add an id attribute to the header and footer elements in the index.html file.
Example:
header.html
(Do not copy this code as it may not match up with your work!)<div class="logo"> <img src="/images/noun_Tent_2517.svg" alt="tent image for logo"> <a href="index.html"> Sleep<span class="highlight">Outside</span></a> </div> <div class="cart"> <a href="cart.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> ...all the SVG code ... </svg> </a> </div>
Example:
index.html
(Do not copy this code as it may not match up with your work!)<header class="divider" id="main-header"> </header>
-
To load the templates we can start with the
renderListWithTemplate
function we wrote last week, but will need to make some changes. We are only rendering one template instead of a list. We also need to write a function to request the HTML file with the header and footer.renderListWithTemplate
function and paste it into theutils.mjs
module.
• Rename it torenderWithTemplate
. - Modify the new function by removing the loop and instead of passing in a
list
, pass indata
. - The header should be mostly static HTML. However, if your group has completed the cart
icon cards (animation or number of items), you may need to add back that functionality. This
is a great use case for a callback function, but the callback will not always be
necessary.
• Add an if statement inrenderWithTemplate
that checks for the existence of the callback parameter, if it exists then call it.Example:
utils.mjs
export function renderWithTemplate(template, parent, data, callback) { parentElement.insertAdjacentHTML("afterbegin", template); if(callback) { callback(data); } }
- Create another function in utils.mjs called
loadHeaderFooter
. It needs to be able to do the following:- Load the header and footer templates in from our partials (
loadTemplate
). - Grab the header and footer elements out of the DOM.
- Render the header and footer (
renderWithTemplate
).
- Load the header and footer templates in from our partials (
- In this function make a fetch request to the provided
path
parameter, then process the response as text usingconvertToText
. - Create a new template element using the document createElement method and set the results
of the fetch into the innerHTML of that new element. Return the element from the function.
Example:
utils.mjs
export async function loadTemplate(path) { const html = await fetch(path).then(convertToText); const template = document.createElement('template'); template.innerHTML = html; return template; }
- Update
main.js
. Import inloadHeaderFooter
and then use that function to load the header and footer intosrc/index.html
. - Do the same thing for
product_pages/index.html
,cart/index.html
, andcart/checkout.html
(you will probably need to create acheckout.js
file for this).
Stretch Activity
The cart was already a dynamic page. It read the list of items out of localStorage and then built the HTML to display the items. Update the shopping cart to match the rest of the application.
Refactor the Shopping Cart
- Create a
ShoppingCart.mjs
module similar to what we did forProductList
. - Convert it to use a
template
like we did for theProductList
.
Instructor's Solution
As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one.
Do NOT open the solution until you have worked through this activity as a team for at least one hour. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code.
Finally - Make a Pull Request
After you have completed what you can, reviewed the instructor's solution, and gotten your code working:
- the driver should commit and push the changes,
- the driver should submit a pull request for this branch,
- the team reviews the pull request, closes it, and merges the branch back into the Main, and
- finally, someone moves the Trello card to "Done".
Submission
Return to I-Learn to report on your work.