-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
120 lines (100 loc) · 2.76 KB
/
model.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
import * as config from "./config.js";
import * as helpers from "./helpers.js";
export const state = {
recipe: {},
search: {
currentPage: 1,
query: "",
results: [],
resultsPerPage: config.RES_PER_PAGE,
},
bookmarks: [],
};
export const loadRecipe = async function (id) {
try {
const data =
await Promise
.race([
helpers.getJSON(`${config.API_URL}/${id}?key="${config.API_KEY}"`),
helpers.timeout(config.TIMEOUT_SEC),
]);
const { recipe } = data.data;
state.recipe = {
id: recipe.id,
title: recipe.title,
imageURL: recipe.image_url,
cookingTime: recipe.cooking_time,
servings: recipe.servings,
ingredients: recipe.ingredients,
publisher: recipe.publisher,
sourceURL: recipe.source_url,
};
if (state.bookmarks.some((cur) => state.recipe.id === cur.id)) {
state.recipe.bookmarked = true;
}
} catch (e) {
throw e;
}
};
export const loadSearchResults = async function (query) {
try {
const data =
await Promise
.race([
helpers.getJSON(`${config.API_URL}?search=${query}&key=${config.API_KEY}`),
helpers.timeout(config.TIMEOUT_SEC),
]);
state.search.query = query;
state.search.currentPage = 1;
state.search.results = [];
data.data.recipes
.map((recipe) => {
state.search.results.push({
id: recipe.id,
title: recipe.title,
imageURL: recipe.image_url,
publisher: recipe.publisher,
});
});
} catch (e) {
throw e;
}
};
export const getSearchResultsPage = function (page = state.search.currentPage) {
state.search.currentPage = page;
const min = (page - 1) * state.search.resultsPerPage;
const max = page * state.search.resultsPerPage;
return state.search.results.slice(min, max);
};
export const updateServings = function (newServings) {
state.recipe.ingredients
.forEach((cur) => {
if (cur.quantity) {
cur.quantity = (cur.quantity * (newServings / state.recipe.servings)).toFixed(2);
}
});
state.recipe.servings = newServings;
}
export const addBookmark = function (recipe) {
if (state.recipe.id === recipe.id) {
state.bookmarks.push(recipe);
state.recipe.bookmarked = true;
persistBookmarks();
}
}
export const deleteBookmark = function (id) {
if (state.recipe.id === id) {
const index = state.bookmarks.findIndex((cur) => cur.id === id);
state.bookmarks.splice(index, 1);
state.recipe.bookmarked = false;
persistBookmarks();
}
}
const persistBookmarks = function () {
localStorage.setItem('bookmarks', JSON.stringify(state.bookmarks));
}
const init = function () {
const storedBookmarks = JSON.parse(localStorage.getItem('bookmarks'));
if (storedBookmarks) state.bookmarks = storedBookmarks;
}
init();