diff --git a/src/data.js b/src/data.js deleted file mode 100644 index 6b91d88..0000000 --- a/src/data.js +++ /dev/null @@ -1,390 +0,0 @@ -const BASE_URL = window.location.href.replace(/^(https?:\/\/[^/]+).*/, '$1/api') - -const datesToFix = { - Meal: { fields: ['suggested_date', 'purchase_date', 'consumed_date'] }, - CurrentShoppingList: { - dependants: (l) => ({ - ShoppingListItem: [l.outstanding_items, l.requested_meals, l.purchased_items], - Meal: Object.values(l.meals_lookup), - Recipe: Object.values(l.recipes_lookup), - Ingredient: Object.values(l.ingredients_lookup), - ShoppingList: Object.values(l.shopping_list_lookup), - }), - }, - PurchasedShoppingList: { - dependants: (l) => ({ - ShoppingListItem: l.items, - Meal: Object.values(l.meals_lookup), - Recipe: Object.values(l.recipes_lookup), - Ingredient: Object.values(l.ingredients_lookup), - ShoppingList: l.list, - }), - }, - ShoppingList: { fields: ['created_date'], dependants: (l) => ({ ShoppingListItem: l.items }) }, - ShoppingListItem: { - fields: ['created_date'], - dependants: (r) => ({ - Meal: r.meal, - Recipe: r.recipe, - Ingredient: r.ingredient, - ShoppingList: r.list, - }), - }, - Recipe: { fields: ['date_created', 'date_hidden'] }, -} - -const fixDates = (obj, type) => { - if (!obj) return - - if (Array.isArray(obj)) { - for (const item of obj) { - fixDates(item, type) - } - } - - const toFix = datesToFix[type] - if (!toFix) return - - if (toFix.fields) { - for (const field of toFix.fields) { - if (obj[field]) { - obj[field] = new Date(obj[field]) - } - } - } - - if (toFix.dependants) { - for (const [key, value] of Object.entries(toFix.dependants(obj))) { - if (Array.isArray(value)) { - for (const item of value) { - fixDates(item, key) - } - } else { - fixDates(value, key) - } - } - } -} - -const setPurchasedShoppingListReferences = (purchasedShoppingList) => { - if (!purchasedShoppingList) return - const { ingredients_lookup, meals_lookup, recipes_lookup } = purchasedShoppingList - const shopping_list_lookup = { [purchasedShoppingList.list.id]: purchasedShoppingList.list } - setShoppingListItemReferences( - purchasedShoppingList.list.items, - ingredients_lookup, - meals_lookup, - recipes_lookup, - shopping_list_lookup - ) -} - -const setCurrentShoppingListReferences = (currentShoppingList) => { - if (!currentShoppingList) return - const { ingredients_lookup, meals_lookup, recipes_lookup, shopping_list_lookup } = - currentShoppingList - const allShoppingListItems = [ - ...currentShoppingList.outstanding_items, - ...currentShoppingList.requested_meals, - ...currentShoppingList.purchased_items, - ] - - setShoppingListItemReferences( - allShoppingListItems, - ingredients_lookup, - meals_lookup, - recipes_lookup, - shopping_list_lookup - ) -} - -const setShoppingListItemReferences = ( - shoppingListItems, - ingredients_lookup, - meals_lookup, - recipes_lookup, - shopping_list_lookup -) => { - if (!shoppingListItems) return - - for (const item of shoppingListItems) { - if (item.ingredient_id) { - item.ingredient = ingredients_lookup[item.ingredient_id] - } - if (item.meal_id) { - item.meal = meals_lookup[item.meal_id] - } - if (item.list_id) { - item.list = shopping_list_lookup[item.list_id] - } - if (item.recipe_id) { - item.recipe = recipes_lookup[item.recipe_id] - } - } -} - -let user = null -export default { - async markMealConsumed(meal_id) { - const response = await fetch(BASE_URL + `/meals/${encodeURIComponent(meal_id)}/consumed`, { - method: 'POST', - credentials: 'include', - }) - - const meal = await response.json() - fixDates(meal, 'Meal') - - return meal - }, - async getUpcomingMeals(from, to) { - const response = await fetch( - BASE_URL + '/meals/upcoming?from=' + from.toISOString() + '&to=' + to.toISOString() - ) - const meals = await response.json() - fixDates(meals, 'Meal') - - return meals.sort((a, b) => a.suggested_date - b.suggested_date) - }, - async getMeal(id) { - var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`) - const meal = await response.json() - fixDates(meal, 'Meal') - - return meal - }, - async deleteMeal(id) { - var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`, { - method: 'DELETE', - }) - - return await response.json() - }, - async searchRecipes(query) { - const response = await fetch(BASE_URL + '/recipes?q=' + encodeURIComponent(query)) - const recipes = await response.json() - fixDates(recipes, 'Recipe') - - return recipes - }, - async parseRecipe(url) { - const response = await fetch(BASE_URL + `/recipes/parse?url=${encodeURIComponent(url)}`, { - credentials: 'include', - }) - const recipe = await response.json() - fixDates(recipe, 'Recipe') - - return recipe - }, - async getRecipe(id) { - const response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`) - const recipe = await response.json() - fixDates(recipe, 'Recipe') - - return recipe - }, - async parseProduct(ingredient, url) { - const body = { - url, - tags: [ingredient.name, ingredient.line], - } - - const response = await fetch(BASE_URL + '/products', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(body), - }) - - return await response.json() - }, - async parseIngredients(lines) { - const params = lines.map((line) => 'ingredients=' + encodeURIComponent(line)).join('&') - const response = await fetch(BASE_URL + '/recipes/ingredients/parse?' + params) - return await response.json() - }, - async saveRecipe(recipe) { - const response = await fetch(BASE_URL + '/recipes', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(recipe), - }) - - const saved = await response.json() - fixDates(saved, 'Recipe') - - return saved - }, - async saveMeal(meal) { - let response = null - if (meal.id >= 0) { - response = await fetch(BASE_URL + `/meals/${encodeURIComponent(meal.id)}`, { - method: 'PUT', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(meal), - }) - } else { - response = await fetch(BASE_URL + '/meals', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(meal), - }) - } - - const saved = await response.json() - fixDates(saved, 'Meal') - - return saved - }, - async currentUser() { - if (user) { - return user - } - - 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', - }) - - if (response.ok) { - user = await response.json() - } - } - - return user - }, - async login(username) { - const response = await fetch(BASE_URL + '/auth/login', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ username }), - }) - - if (response.ok) { - user = await response.json() - } - - return user - }, - async deleteRecipe(id) { - var response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`, { - method: 'DELETE', - credentials: 'include', - }) - - const recipe = await response.json() - fixDates(recipe, 'Recipe') - return recipe - }, - async searchPerson(name) { - const response = await fetch(BASE_URL + '/persons?q=' + encodeURIComponent(name)) - return await response.json() - }, - async getMyShoppingList() { - const response = await fetch(BASE_URL + '/shopping/current/me/ingredients', { - credentials: 'include', - }) - const ingredients = await response.json() - fixDates(ingredients, 'Ingredient') - - return ingredients - }, - async saveMyShoppingList(list) { - const response = await fetch(BASE_URL + '/shopping/current/me/ingredients', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(list), - }) - - const ingredients = await response.json() - fixDates(ingredients, 'Ingredient') - - return ingredients - }, - async getShoppingList(id) { - const response = await fetch(BASE_URL + `/shopping/${encodeURIComponent(id)}`) - - const purchasedShoppingList = await response.json() - fixDates(purchasedShoppingList, 'PurchasedShoppingList') - setPurchasedShoppingListReferences(purchasedShoppingList) - - return purchasedShoppingList.list - }, - async getCurrentShoppingList() { - const response = await fetch(BASE_URL + '/shopping/current') - - const lst = await response.json() - fixDates(lst, 'CurrentShoppingList') - setCurrentShoppingListReferences(lst) - - return lst - }, - async purchaseShoppingList(completed_requests) { - const response = await fetch(BASE_URL + '/shopping/', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ items: completed_requests }), - }) - - const purchasedShoppingList = await response.json() - fixDates(purchasedShoppingList, 'PurchasedShoppingList') - setPurchasedShoppingListReferences(purchasedShoppingList) - - return purchasedShoppingList.list - }, - 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 requests = await response.json() - fixDates(requests, 'ShoppingListItem') - - return requests - }, - async unrequestMeal(meal_id) { - const response = await fetch( - BASE_URL + `/shopping/current/meals/${encodeURIComponent(meal_id)}`, - { - method: 'DELETE', - credentials: 'include', - } - ) - - if (!response.ok) { - throw new Error('Failed to unrequest meal') - } - }, - async getPersonsInHome() { - const response = await fetch(BASE_URL + '/persons') - return await response.json() - }, -}