2024-05-12 03:30:42 +00:00
|
|
|
const BASE_URL = window.location.href.replace(/^(http:\/\/[^/:]+).*/, "$1:8000")
|
2024-01-13 02:00:15 +00:00
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
function fixMealDates(meals) {
|
|
|
|
|
for (const meal of meals) {
|
|
|
|
|
meal.meal_date = new Date(meal.meal_date);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 02:13:49 +00:00
|
|
|
let user = null;
|
2024-01-13 02:00:15 +00:00
|
|
|
export default {
|
2024-01-17 09:17:22 +00:00
|
|
|
async getMeals(from, to) {
|
2024-01-17 11:31:45 +00:00
|
|
|
const response = await fetch(BASE_URL + "/meals?from=" + from.toISOString() + "&to=" + to.toISOString());
|
|
|
|
|
const meals = await response.json();
|
2024-05-18 07:05:28 +00:00
|
|
|
fixMealDates(meals);
|
2024-01-17 11:31:45 +00:00
|
|
|
|
2024-05-12 03:30:42 +00:00
|
|
|
return meals.sort((a, b) => a.meal_date - b.meal_date);
|
2024-01-13 02:00:15 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async getMeal(id) {
|
|
|
|
|
var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`);
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 02:00:15 +00:00
|
|
|
},
|
2024-01-17 11:57:44 +00:00
|
|
|
async deleteMeal(id) {
|
|
|
|
|
var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`, {
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
});
|
|
|
|
|
return await response.json();
|
|
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async searchRecipes(query) {
|
|
|
|
|
const response = await fetch(BASE_URL + "/recipes?q=" + encodeURIComponent(query));
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 02:00:15 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async parseRecipe(url) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/recipes/parse?url=${encodeURIComponent(url)}`);
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 02:00:15 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async getRecipe(id) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`);
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 07:00:55 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async parseProduct(ingredient, url) {
|
2024-01-13 02:00:15 +00:00
|
|
|
const body = {
|
|
|
|
|
url, tags: [ingredient.name, ingredient.line],
|
2024-01-17 09:17:22 +00:00
|
|
|
};
|
2024-01-13 03:22:10 +00:00
|
|
|
|
2024-01-17 09:17:22 +00:00
|
|
|
const response = await fetch(BASE_URL + "/products", {
|
2024-01-13 02:00:15 +00:00
|
|
|
method: "POST",
|
2024-04-28 02:13:49 +00:00
|
|
|
credentials: "include",
|
2024-01-13 02:00:15 +00:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(body),
|
2024-01-17 09:17:22 +00:00
|
|
|
});
|
|
|
|
|
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 02:00:15 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async parseIngredients(lines) {
|
2024-01-13 02:00:15 +00:00
|
|
|
const params = lines.map(line => "ingredients=" + encodeURIComponent(line)).join("&");
|
2024-01-17 09:17:22 +00:00
|
|
|
const response = await fetch(BASE_URL + "/recipes/ingredients/parse?" + params);
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-13 03:22:10 +00:00
|
|
|
},
|
2024-01-17 09:17:22 +00:00
|
|
|
async saveRecipe(recipe) {
|
|
|
|
|
const response = await fetch(BASE_URL + "/recipes", {
|
2024-01-13 03:22:10 +00:00
|
|
|
method: "POST",
|
2024-04-28 02:13:49 +00:00
|
|
|
credentials: "include",
|
2024-01-13 03:22:10 +00:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(recipe),
|
2024-01-17 09:17:22 +00:00
|
|
|
});
|
|
|
|
|
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-17 09:17:22 +00:00
|
|
|
},
|
|
|
|
|
async saveMeal(meal) {
|
2024-05-02 11:25:01 +00:00
|
|
|
if (meal.id) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/meals/${encodeURIComponent(meal.id)}`, {
|
|
|
|
|
method: "PUT",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(meal),
|
|
|
|
|
});
|
|
|
|
|
return await response.json();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 09:17:22 +00:00
|
|
|
const response = await fetch(BASE_URL + "/meals", {
|
|
|
|
|
method: "POST",
|
2024-04-28 02:13:49 +00:00
|
|
|
credentials: "include",
|
2024-01-17 09:17:22 +00:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(meal),
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-17 11:31:45 +00:00
|
|
|
return await response.json();
|
2024-01-17 09:17:22 +00:00
|
|
|
},
|
2024-05-12 06:32:08 +00:00
|
|
|
async currentUser() {
|
|
|
|
|
if (user) {
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 02:13:49 +00:00
|
|
|
var cookie = decodeURIComponent(document.cookie).split(";").find(cookie => cookie.trimStart().startsWith("user_id="));
|
|
|
|
|
if (cookie) {
|
|
|
|
|
const response = await fetch(BASE_URL + "/auth/refresh", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-12 06:32:08 +00:00
|
|
|
if (response.ok) {
|
|
|
|
|
user = await response.json();
|
|
|
|
|
}
|
2024-04-28 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-12 06:32:08 +00:00
|
|
|
return user;
|
2024-04-28 02:13:49 +00:00
|
|
|
},
|
|
|
|
|
async login(username) {
|
|
|
|
|
const response = await fetch(BASE_URL + "/auth/login", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ username }),
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-12 06:32:08 +00:00
|
|
|
if (response.ok) {
|
|
|
|
|
user = await response.json();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 02:13:49 +00:00
|
|
|
return user;
|
2024-05-04 02:24:22 +00:00
|
|
|
},
|
|
|
|
|
async deleteRecipe(id) {
|
|
|
|
|
var response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`, {
|
|
|
|
|
method: "DELETE",
|
2024-05-04 02:39:30 +00:00
|
|
|
credentials: "include",
|
2024-05-04 02:24:22 +00:00
|
|
|
});
|
|
|
|
|
return await response.json();
|
2024-05-04 04:22:37 +00:00
|
|
|
},
|
|
|
|
|
async searchPerson(name) {
|
|
|
|
|
const response = await fetch(BASE_URL + "/persons?q=" + encodeURIComponent(name));
|
|
|
|
|
return await response.json();
|
|
|
|
|
},
|
2024-05-18 04:46:27 +00:00
|
|
|
async getMyShoppingList() {
|
2024-05-18 07:05:28 +00:00
|
|
|
const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", { credentials: "include" });
|
2024-05-18 04:46:27 +00:00
|
|
|
return await response.json();
|
|
|
|
|
},
|
|
|
|
|
async saveMyShoppingList(list) {
|
2024-05-18 07:05:28 +00:00
|
|
|
const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", {
|
2024-05-18 04:46:27 +00:00
|
|
|
method: "POST",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(list),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
|
},
|
|
|
|
|
async getShoppingList(id) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/shopping/${encodeURIComponent(id)}`);
|
|
|
|
|
return await response.json();
|
|
|
|
|
},
|
|
|
|
|
async getCurrentShoppingList() {
|
|
|
|
|
const response = await fetch(BASE_URL + "/shopping/current");
|
2024-05-18 07:05:28 +00:00
|
|
|
const lst = await response.json();
|
|
|
|
|
fixMealDates(lst.requests.map(request => request.meal).filter(meal => meal));
|
|
|
|
|
return lst;
|
|
|
|
|
},
|
|
|
|
|
async requestMeal(meal_id) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/shopping/current/meals/me`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ meal_id }),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
fixMealDates([result.meal]);
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
async unrequestMeal(meal_id) {
|
|
|
|
|
const response = await fetch(BASE_URL + `/shopping/current/meals/${encodeURIComponent(meal_id)}`, {
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
credentials: "include",
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-18 04:46:27 +00:00
|
|
|
return await response.json();
|
|
|
|
|
}
|
2024-01-13 02:00:15 +00:00
|
|
|
}
|