As a user, when the page loads, when I click on a category button, I see all of the types within that category, 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
Then they should see all types from db/types.json printed on the page as cards
And when they click on the Back button
Then they should return to the Categories page
Dev Notes
in index.html, in the types-page div, add a button with the id toCategoriesBtn with the text "Back"
in javascripts/helpers/data, create typesData.js
typesData.js will create the functions
loadTypesFromCategory, which:
takes the parameter categoryId
creates a new Promise() with resolve, reject parameters
calls axios.get() to
get the data from types.json with a
.then() method that
takes the parameter resp
creates a new variable called allTypes which equals resp.data.types
creates a new variable called matchingTypes that calls .filter() on allTypes, takes the parameter type, and returns true if type.categoryId === categoryId
and finally calls resolve(matchingTypes), and a
.catch() method that takes the parameter err and that calls reject(err) in the body
getTypesForEachCategory, which:
takes the parameter categories
creates a new Promise() with resolve, reject parameters
calls axios.get() to
get the data from types.json with a
.then() method that
takes the parameter resp
creates a new variable called types which equals resp.data.types
creates a new variable called categoriesWithTypes that calls .map() on categories, takes the parameter category, creates a new variable const newCategory = category to bypass the linter, creates a variable called matchingTypes that calls .filter() on types, takes the parameter type, and returns true if type.categoryId === category.id, assigns a new key value pair to newCategory with newCategory.types = matchingTypes, and returns newCategory
and finally calls resolve(categoriesWithTypes)
.catch() method that takes the parameter err and that calls reject(err) in the body
export loadTypesFromCategory and getTypesFromEachCategory
in javascripts/components, create a new directory called types, which contains the files types.js and types.scss
- types.js should:
import $ from jquery, import typesData from typesData.js, import util from util.js
include the following functions:
initTypes which:
takes the parameter categoryId
calls typesData.loadTypesFromCategory which:
passes the parameter categoryId
calls .then() on success, which passes the parameter types and calls writeTypes(types) in the body
calls .catch(err => console.error(err) on failure
writeTypes which:
takes the parameter types
filters through types parameter and creates a card for each type that will print the type cards to the page using `util.printToDom('types', domString)
`bindEvents()~ which:
adds a click eventListener to the id toCategoriesBtn which adds the .hide class to the types-page and products page and removes the.hideclass from thecategories-page`
export initTypes
- categories.js should:
import types from types.js, and typesData from typesData.json
contain the following functions:
bindEvents() will search the DOM for all buttons with the className see-types, assign them to the variable allButtons and will loop through allButtons adding a click eventListener to each button, which calls seeTypeDiv
seeTypeDiv() will
pass a parameter e, find the closest e.target.id of the category .card class, and assign it to the new variable categoryId
add the hide class to the divs with the ids categories-page and products-page
remove the hide class from the divs with the id types-page, and
call types.initTypes(categoryId)
writeCategories:
add a button with the class see-types
call bindEvents()
initCategories:
replace the .then() statement to include the following two .then() statements:
the first .then() will take the parameter resp and will call typesData.getTypesForEachCategory(resp.data.categories) which returns the specific types for each category
the second .then() will take the parameter categoriesWithTypes and will call writeCategories, passing in categoriesWithTypes
User Story
As a user, when the page loads, when I click on a category button, I see all of the types within that category, 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 Then they should see all types from
db/types.json
printed on the page as cards And when they click on the Back button Then they should return to the Categories pageDev Notes
index.html
, in thetypes-page
div, add a button with the idtoCategoriesBtn
with the text "Back"javascripts/helpers/data
, createtypesData.js
typesData.js
will create the functionsloadTypesFromCategory
, which:categoryId
new Promise()
withresolve, reject
parametersaxios.get()
totypes.json
with a.then()
method thatresp
allTypes
which equalsresp.data.types
matchingTypes
that calls.filter()
onallTypes
, takes the parametertype
, and returns true iftype.categoryId === categoryId
resolve(matchingTypes)
, and a.catch()
method that takes the parametererr
and that callsreject(err)
in the bodygetTypesForEachCategory
, which:categories
new Promise()
withresolve, reject
parametersaxios.get()
totypes.json
with a.then()
method thatresp
types
which equalsresp.data.types
categoriesWithTypes
that calls.map()
oncategories
, takes the parametercategory
, creates a new variableconst newCategory = category
to bypass the linter, creates a variable calledmatchingTypes
that calls.filter()
ontypes
, takes the parametertype
, and returns true iftype.categoryId === category.id
, assigns a new key value pair tonewCategory
withnewCategory.types = matchingTypes
, and returnsnewCategory
resolve(categoriesWithTypes)
.catch()
method that takes the parametererr
and that callsreject(err)
in the bodyloadTypesFromCategory
andgetTypesFromEachCategory
javascripts/components
, create a new directory calledtypes
, which contains the filestypes.js
andtypes.scss
-
types.js
should:$
fromjquery
, importtypesData
fromtypesData.js
, importutil
fromutil.js
initTypes
which:categoryId
typesData.loadTypesFromCategory
which:categoryId
.then()
on success, which passes the parametertypes
and callswriteTypes(types)
in the body.catch(err => console.error(err)
on failurewriteTypes
which:types
types
parameter and creates a card for each type that will print the type cards to the page using `util.printToDom('types', domString)toCategoriesBtn
which adds the.hide
class to thetypes-page and products page and removes the
.hideclass from the
categories-page`initTypes
- categories.js should:
types
fromtypes.js
, andtypesData
fromtypesData.json
bindEvents()
will search the DOM for all buttons with the classNamesee-types
, assign them to the variableallButtons
and will loop throughallButtons
adding aclick
eventListener to each button, which callsseeTypeDiv
seeTypeDiv()
wille
, find the closeste.target.id
of the category.card
class, and assign it to the new variablecategoryId
hide
class to the divs with the idscategories-page
andproducts-page
hide
class from the divs with the idtypes-page
, andtypes.initTypes(categoryId)
writeCategories
:see-types
bindEvents()
initCategories
:.then()
statement to include the following two.then()
statements:.then()
will take the parameterresp
and will calltypesData.getTypesForEachCategory(resp.data.categories)
which returns the specific types for each category.then()
will take the parametercategoriesWithTypes
and will callwriteCategories
, passing incategoriesWithTypes