As a user, when the page loads, when I click on a category button, then on a type button, I see all of the products within that type, and I can return to the previous page when I click on the Back button.
AC
Given a user navigates to the page
And clicks on a category card's button
And clicks on a type card's button
Then they should see all products from db/products.json that correspond to that type and category printed on the page as cards
And when they click on the Back button
Then they should return to the Types page
Dev Notes
in index.html, in the the products-page div, add a button with the id toTypesBtn with the text "Back"
in javascripts/helpers/data, create productsData.js
productsData.js will create the functions:
loadProductsFromType, which:
takes the parameter typeId
creates a newPromise() with resolve, reject parameters
calls axios.get() to
get the data from products.json with a
.then() method that
takes the parameter resp
creates a new variable called allProducts which equals resp.data.products
creates a new variable called matchingProducts that calls .filter() on allProducts, takes the parameter product, and returns true if product.typeId === typeId
and finally calls resolve(matchingProducts), and a
.catch() method that takes the parameter err and that calls reject(err) in the body
getProductsForEachType, which:
takes the parameter types
creates a new Promise() with resolve, reject parameters
calls axios.get() to
get the data from products.json with a
.then() method that
takes the parameter resp
creates a new variable called products which equals resp.data.products
creates a new variable called typesWithProducts that calls .map() on types, takes the parameter type, creates a new variable const newType = type to bypass the linter, creates a variable called matchingProducts that calls .filter() on products, takes the parameter product, and returns true if product.typeId === type.id, assigns a new key value pair to newType with newType.products = matchingProducts, and returns newType
and finally calls resolve(typesWithProducts)
.catch() method that takes the parameter err and that calls reject(err) in the body
export loadProductsFromType and getProductsFromEachType
in javascripts/components, create a new directory called products, which contains the files products.js and products.scss
- products.js should:
import $ from jquery, import productsData from productsData.js, import util from util.js
include the following functions:
initProducts which:
takes the parameter typeId
calls productsData.loadProductsFromType which:
passes the parameter typeId
calls .then() on success, which passes the parameter types and calls writeProducts(products) in the body
calls .catch(err => console.error(err) on failure
writeProducts which:
takes the parameter products
calls bindEvents()
filters through products parameter and creates a card for each product, and will print the product card to the page using util.printToDom('products', domString)
bindEvents()which:
adds a click eventListener to the id toTypesBtn which adds the .hide class to the #products-page and #categories-page
export initProducts
-types.js should:
contain the following functions:
bindEvents() which:
searches the DOM for all buttons with the className see-products, assign them to the variable productButtons and loops through productButtons, adding a click eventListener to each button, which calls seeProductDiv
seeProductDiv that will:
pass the parameter e, find the closest e.target.id of the type .card class, and assign it to the new variable typeId
add the .hide class to the divs with the ids categories-page and types-page
remove the .hide class from the div with the id products-page, and
call `products.initProducts(typeId)
writeTypes:
add a button with the class see-products
initTypes:
replace the .then() statement to include the following two .then() statements:
the first .then() will take the parameter resp and will call productsData.getProductsForEachType(resp.data.types) which returns the specific types for each category
the second .then() will take the parameter typesWithProducts and will call writeTypes, passing in typesWithProducts
User Story
As a user, when the page loads, when I click on a category button, then on a type button, I see all of the products within that type, and I can return to the previous page when I click on the Back button.
AC
Given a user navigates to the page And clicks on a category card's button And clicks on a type card's button Then they should see all products from
db/products.json
that correspond to that type and category printed on the page as cards And when they click on the Back button Then they should return to the Types pageDev Notes
index.html
, in the theproducts-page
div, add a button with the idtoTypesBtn
with the text "Back"javascripts/helpers/data
, createproductsData.js
productsData.js
will create the functions:loadProductsFromType
, which:typeId
newPromise()
withresolve, reject
parametersaxios.get()
toproducts.json
with a.then()
method thatresp
allProducts
which equalsresp.data.products
matchingProducts
that calls.filter()
onallProducts
, takes the parameterproduct
, and returns true ifproduct.typeId === typeId
resolve(matchingProducts)
, and a.catch()
method that takes the parametererr
and that callsreject(err)
in the bodygetProductsForEachType
, which:types
new Promise()
withresolve, reject
parametersaxios.get()
toproducts.json
with a.then()
method thatresp
products
which equalsresp.data.products
typesWithProducts
that calls.map()
ontypes
, takes the parametertype
, creates a new variableconst newType = type
to bypass the linter, creates a variable calledmatchingProducts
that calls.filter()
onproducts
, takes the parameterproduct
, and returns true ifproduct.typeId === type.id
, assigns a new key value pair tonewType
withnewType.products = matchingProducts
, and returnsnewType
resolve(typesWithProducts)
.catch()
method that takes the parametererr
and that callsreject(err)
in the bodyloadProductsFromType
andgetProductsFromEachType
javascripts/components
, create a new directory calledproducts
, which contains the filesproducts.js
andproducts.scss
-
products.js
should:$
fromjquery
, importproductsData
fromproductsData.js
, importutil
fromutil.js
initProducts
which:typeId
productsData.loadProductsFromType
which:typeId
.then()
on success, which passes the parametertypes
and callswriteProducts(products)
in the body.catch(err => console.error(err)
on failurewriteProducts
which:products
bindEvents()
products
parameter and creates a card for each product, and will print the product card to the page usingutil.printToDom('products', domString)
bindEvents()
which:toTypesBtn
which adds the.hide
class to the#products-page
and#categories-page
initProducts
-
types.js
should:bindEvents()
which:see-products
, assign them to the variableproductButtons
and loops throughproductButtons
, adding aclick
eventListener to each button, which callsseeProductDiv
seeProductDiv
that will:e
, find the closeste.target.id
of the type.card
class, and assign it to the new variabletypeId
.hide
class to the divs with the idscategories-page
andtypes-page
.hide
class from the div with the idproducts-page
, andwriteTypes
:see-products
initTypes
:.then()
statement to include the following two.then()
statements:.then()
will take the parameterresp
and will callproductsData.getProductsForEachType(resp.data.types)
which returns the specific types for each category.then()
will take the parametertypesWithProducts
and will callwriteTypes
, passing intypesWithProducts