-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
159 lines (132 loc) Β· 4.16 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { async } from 'regenerator-runtime';
import { API_URL, RES_PER_PAGE, KEY } from './config.js';
// import { getJSON, sendJSON } from './helpers.js';
import { AJAX } from './helpers.js';
export const state = {
recipe: {},
search: {
query: '',
results: [],
page: 1,
resultsPerPage: RES_PER_PAGE,
},
bookmarks: [],
};
const createRecipeObject = function (data) {
const { recipe } = data.data;
return {
id: recipe.id,
title: recipe.title,
publisher: recipe.publisher,
sourceUrl: recipe.source_url,
image: recipe.image_url,
servings: recipe.servings,
cookingTime: recipe.cooking_time,
ingredients: recipe.ingredients,
...(recipe.key && { key: recipe.key }),
};
};
export const loadRecipe = async function (id) {
try {
const data = await AJAX(`${API_URL}${id}?key=${KEY}`);
state.recipe = createRecipeObject(data);
if (state.bookmarks.some(bookmark => bookmark.id === id))
state.recipe.bookmarked = true;
else state.recipe.bookmarked = false;
console.log(state.recipe);
} catch (err) {
// Temp error handling
console.error(`${err} π₯π₯π₯π₯`);
throw err;
}
};
export const loadSearchResults = async function (query) {
try {
state.search.query = query;
const data = await AJAX(`${API_URL}?search=${query}&key=${KEY}`);
console.log(data);
state.search.results = data.data.recipes.map(rec => {
return {
id: rec.id,
title: rec.title,
publisher: rec.publisher,
image: rec.image_url,
...(rec.key && { key: rec.key }),
};
});
state.search.page = 1;
} catch (err) {
console.error(`${err} π₯π₯π₯π₯`);
throw err;
}
};
export const getSearchResultsPage = function (page = state.search.page) {
state.search.page = page;
const start = (page - 1) * state.search.resultsPerPage; // 0
const end = page * state.search.resultsPerPage; // 9
return state.search.results.slice(start, end);
};
export const updateServings = function (newServings) {
state.recipe.ingredients.forEach(ing => {
ing.quantity = (ing.quantity * newServings) / state.recipe.servings;
// newQt = oldQt * newServings / oldServings // 2 * 8 / 4 = 4
});
state.recipe.servings = newServings;
};
const persistBookmarks = function () {
localStorage.setItem('bookmarks', JSON.stringify(state.bookmarks));
};
export const addBookmark = function (recipe) {
// Add bookmark
state.bookmarks.push(recipe);
// Mark current recipe as bookmarked
if (recipe.id === state.recipe.id) state.recipe.bookmarked = true;
persistBookmarks();
};
export const deleteBookmark = function (id) {
// Delete bookmark
const index = state.bookmarks.findIndex(el => el.id === id);
state.bookmarks.splice(index, 1);
// Mark current recipe as NOT bookmarked
if (id === state.recipe.id) state.recipe.bookmarked = false;
persistBookmarks();
};
const init = function () {
const storage = localStorage.getItem('bookmarks');
if (storage) state.bookmarks = JSON.parse(storage);
};
init();
const clearBookmarks = function () {
localStorage.clear('bookmarks');
};
// clearBookmarks();
export const uploadRecipe = async function (newRecipe) {
try {
const ingredients = Object.entries(newRecipe)
.filter(entry => entry[0].startsWith('ingredient') && entry[1] !== '')
.map(ing => {
const ingArr = ing[1].split(',').map(el => el.trim());
// const ingArr = ing[1].replaceAll(' ', '').split(',');
if (ingArr.length !== 3)
throw new Error(
'Wrong ingredient fromat! Please use the correct format :)'
);
const [quantity, unit, description] = ingArr;
return { quantity: quantity ? +quantity : null, unit, description };
});
const recipe = {
title: newRecipe.title,
source_url: newRecipe.sourceUrl,
image_url: newRecipe.image,
publisher: newRecipe.publisher,
cooking_time: +newRecipe.cookingTime,
servings: +newRecipe.servings,
ingredients,
};
const data = await AJAX(`${API_URL}?key=${KEY}`, recipe);
state.recipe = createRecipeObject(data);
addBookmark(state.recipe);
} catch (err) {
throw err;
}
};