munch-ease-frontend/src/data.js

130 lines
4.3 KiB
JavaScript
Raw Normal View History

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-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();
for (const meal of meals) {
2024-04-28 02:13:49 +00:00
meal.meal_date = new Date(meal.meal_date);
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-04-28 02:13:49 +00:00
async isLoggedIn() {
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",
});
user = await response.json();
}
return user !== null;
},
async login(username) {
const response = await fetch(BASE_URL + "/auth/login", {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ username }),
});
user = await response.json();
return user;
2024-05-04 02:24:22 +00:00
},
async deleteRecipe(id) {
var response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`, {
method: "DELETE",
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-01-13 02:00:15 +00:00
}