import * as model from './model.js'; import recipeView from './views/recipeView.js'; import searchView from './views/searchView.js'; import resultsView from './views/resultsView.js'; import bookmarksView from './views/bookmarksView.js'; import paginationView from './views/paginationView.js'; import addRecipeView from './views/addRecipeView.js'; import 'core-js/stable'; import 'regenerator-runtime/runtime'; // if (module.hot) { // module.hot.accept(); // } const controlRecipes = async function () { try { const id = window.location.hash.slice(1); if (!id) return; recipeView.renderSpinner(); // 0) Update results view to mark selected search results resultsView.update(model.getSearchResultsPage()); // 1) Updating bookmarks view bookmarksView.update(model.state.bookmarks); // 2)Loading recipe await model.loadRecipe(id); // 3) Rendering recipe recipeView.render(model.state.recipe); } catch (error) { recipeView.renderError(error); } }; const controlSearchResults = async function () { try { resultsView.renderSpinner(); // 1) Get search query const query = searchView.getQuery(); if (!query) return; // 2) Load search results await model.loadSearchResults(query); // 3) Render results resultsView.render(model.getSearchResultsPage()); // 4) Render initial pagination buttons paginationView.render(model.state.search); } catch (error) { console.error(error); } }; const controlPagination = goToPage => { // 1) Render new results resultsView.render(model.getSearchResultsPage(goToPage)); // 2) Render new initial pagination buttons paginationView.render(model.state.search); }; const controlServings = function (newServings) { // Update the recipe servings (in state) model.updateServings(newServings); // Update the recipe view // recipeView.render(model.state.recipe); recipeView.update(model.state.recipe); }; const controlAddBookmark = function () { // 1) Add/remove bookmark if (!model.state.recipe.bookmarked) model.addBookmark(model.state.recipe); else model.deleteBookmark(model.state.recipe.id); // 2) Update recipe view recipeView.update(model.state.recipe); // 3) Render bookmarks bookmarksView.render(model.state.bookmarks); }; const controlBookmarks = function () { bookmarksView.render(model.state.bookmarks); }; const controlAddRecipe = function (newRecipe) { console.log(newRecipe); // Upload the new recipe data }; const init = function () { bookmarksView.addHandlerRender(controlBookmarks); recipeView.addHandleRender(controlRecipes); recipeView.addHandlerUpdateServings(controlServings); recipeView.addHandlerAddBookmark(controlAddBookmark); searchView.addHandlerSearch(controlSearchResults); paginationView.addHandlerClick(controlPagination); addRecipeView.addHandlerUpload(controlAddRecipe); }; init();