@@ -2,10 +2,12 @@ import 'core-js/stable';
22import 'regenerator-runtime/runtime' ;
33
44import * as model from './model.js' ;
5+ import { getHash } from './helpers.js' ;
56import recipeView from './views/recipeView.js' ;
67import searchView from './views/searchView.js' ;
78import resultsView from './views/resultsView.js' ;
89import paginationView from './views/paginationView.js' ;
10+ import bookmarksView from './views/bookmarksView.js' ;
911
1012// https://forkify-api.herokuapp.com/v2
1113
@@ -15,14 +17,18 @@ const controlRecipe = async function ()
1517{
1618 try
1719 {
18- // Get Hash
19- const id = window . location . hash . slice ( 1 ) ;
20+ const id = getHash ( ) ;
2021
2122 if ( ! id ) return ;
2223
2324 // Render Spinner while the Data is being Fetched
2425 recipeView . renderSpinner ( ) ;
2526
27+ // Update Selected Recipe from Search Results
28+ resultsView . update ( model . getSearchResultsPage ( ) ) ;
29+ // Update Bookmarks
30+ bookmarksView . update ( model . state . bookmarks ) ;
31+
2632 // Fetching Data
2733 await model . loadRecipe ( id ) ;
2834 const { recipe } = model . state ;
@@ -41,6 +47,9 @@ const controlSearchResults = async function ()
4147{
4248 try
4349 {
50+ // Render Spinner while the Search is being Fetched
51+ resultsView . renderSpinner ( ) ;
52+
4453 // Get Search Query
4554 const query = searchView . getQuery ( ) ;
4655
@@ -51,7 +60,7 @@ const controlSearchResults = async function ()
5160 console . log ( model . state . search ) ;
5261
5362 // Render Search Results
54- resultsView . render ( model . getSearchResultsPage ( 1 ) ) ;
63+ resultsView . render ( model . getSearchResultsPage ( ) ) ;
5564 // Render Initial Pagination Buttons
5665 paginationView . render ( model . state . search ) ;
5766 }
@@ -70,9 +79,45 @@ const controlPagination = function (goToPage)
7079 paginationView . render ( model . state . search ) ;
7180} ;
7281
82+ const controlServings = function ( newServings )
83+ {
84+ // Update Recipe Servings
85+ model . updateServings ( newServings ) ;
86+
87+ // Update Recipe
88+ recipeView . update ( model . state . recipe ) ;
89+ } ;
90+
91+ const controlBookmark = function ( )
92+ {
93+ // Add/Remove Bookmark
94+ model . state . recipe . bookmarked ? model . delBookmark ( model . state . recipe . id ) : model . addBookmark ( model . state . recipe ) ;
95+
96+ // Update Recipe
97+ console . log ( model . state . recipe ) ;
98+ recipeView . update ( model . state . recipe ) ;
99+
100+ // Render Bookmarks
101+ bookmarksView . render ( model . state . bookmarks ) ;
102+ } ;
103+
104+ const controlInitBookmarks = function ( )
105+ {
106+ bookmarksView . render ( model . state . bookmarks ) ;
107+ } ;
108+
109+ const clearBookmarks = function ( )
110+ {
111+ localStorage . clear ( 'bookmarks' ) ;
112+ } ;
113+ // clearBookmarks()
114+
73115const init = function ( )
74116{
117+ bookmarksView . addHandlerRender ( controlInitBookmarks ) ;
75118 recipeView . addHandlerRender ( controlRecipe ) ;
119+ recipeView . addHandlerUpdateServings ( controlServings ) ;
120+ recipeView . addHandlerBookmark ( controlBookmark ) ;
76121 searchView . addHandlerSearch ( controlSearchResults ) ;
77122 paginationView . addHandlerPagination ( controlPagination ) ;
78123} ;
0 commit comments