-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
131 lines (103 loc) · 3.48 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as model from './model.js';
import { MODAL_CLOSE_SEC } from './config.js';
import recipeView from './views/recipeView.js';
import searchView from './views/searchView.js';
import resultsView from './views/resultsView.js';
import paginationView from './views/paginationView.js';
import bookmarksView from './views/bookmarksView.js';
import addRecipeView from './views/addRecipeView.js';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { async } from 'regenerator-runtime';
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 result
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 (err) {
recipeView.renderError();
console.error(err);
}
};
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 (err) {
console.log(err);
}
};
const controlPagination = function (goToPage) {
// 1) Render NEW results
resultsView.render(model.getSearchResultsPage(goToPage));
// 2) Render NEW 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.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 = async function (newRecipe) {
try {
// Show loading spinner
addRecipeView.renderSpinner();
// Upload the new recipe data
await model.uploadRecipe(newRecipe);
console.log(model.state.recipe);
// Render recipe
recipeView.render(model.state.recipe);
// Success message
addRecipeView.renderMessage();
// Render bookmark view
bookmarksView.render(model.state.bookmarks);
// Change ID in URL
window.history.pushState(null, '', `#${model.state.recipe.id}`);
// Close form window
setTimeout(function () {
addRecipeView.toggleWindow();
}, MODAL_CLOSE_SEC * 1000);
} catch (err) {
console.error('💥', err);
addRecipeView.renderError(err.message);
}
};
const init = function () {
bookmarksView.addHandlerRender(controlBookmarks);
recipeView.addHandlerRender(controlRecipes);
recipeView.addHandlerUpdateServings(controlServings);
recipeView.addHandlerAddBookmark(controlAddBookmark);
searchView.addHandlerSearch(controlSearchResults);
paginationView.addHandlerClick(controlPagination);
addRecipeView.addHandlerUpload(controlAddRecipe);
};
init();