-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
74 lines (60 loc) · 1.99 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
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 paginationView from "./views/PaginationView.js";
import bookmarksView from './views/BookmarksView.js';
const controlRecipe = async function (id) {
try {
recipeView.renderSpinner();
await model.loadRecipe(id);
recipeView.render(model.state.recipe);
resultsView.update(model.getSearchResultsPage());
bookmarksView.update(model.state.bookmarks);
} catch (e) {
console.error(e);
recipeView.renderError();
}
};
const controlSearchResults = async function () {
try {
resultsView.renderSpinner();
const query = searchView.getQuery();
if (!query) return;
await model.loadSearchResults(query);
paginationView.render(model.state.search);
resultsView.render(model.getSearchResultsPage());
} catch (e) {
console.error(e);
resultsView.renderError();
}
};
const controlPagination = function (btn) {
resultsView.render(model.getSearchResultsPage(Number(btn.dataset.target)));
paginationView.render(model.state.search);
};
const controlServings = function (newServings) {
model.updateServings(newServings);
recipeView.update(model.state.recipe);
};
const controlAddBookmark = function () {
if (model.state.recipe.bookmarked) {
model.deleteBookmark(model.state.recipe.id);
} else {
model.addBookmark(model.state.recipe);
}
recipeView.update(model.state.recipe);
bookmarksView.render(model.state.bookmarks);
}
const controlBookmarks = function () {
bookmarksView.render(model.state.bookmarks);
}
const init = function () {
recipeView.addRenderHandler(controlRecipe);
bookmarksView.addRenderHandler(controlBookmarks);
recipeView.addServingsUpdateHandler(controlServings);
recipeView.addBookmarkHandler(controlAddBookmark);
searchView.addSearchHandler(controlSearchResults);
paginationView.addClickHandler(controlPagination);
};
init();