enrecipes/app/store.js

463 lines
14 KiB
JavaScript
Raw Normal View History

2020-10-14 19:32:32 +00:00
import Vue from "vue"
import Vuex from "vuex"
import {
Couchbase
} from "nativescript-couchbase-plugin"
import {
getFileAccess
} from "@nativescript/core"
2020-11-06 09:07:41 +00:00
const EnRecipesDB = new Couchbase("EnRecipes")
const userCategoriesDB = new Couchbase("userCategories")
const userYieldUnitsDB = new Couchbase("userYieldUnits")
2020-11-23 09:49:58 +00:00
const mealPlansDB = new Couchbase("mealPlans")
2020-10-14 19:32:32 +00:00
Vue.use(Vuex)
const defaultCategories = ["Appetizers", "Barbecue", "Beverages", "Breads", "Breakfast", "Desserts", "Dinner", "Drinks", "Healthy", "Lunch", "Main dishes", "Meat", "Noodles", "Pasta", "Poultry", "Rice", "Salads", "Sauces", "Seafood", "Side dishes", "Snacks", "Soups", "Undefined", "Vegan", "Vegetarian", ]
const defaultYieldUnits = ["Serving", "Piece", "Teaspoon", "Tablespoon", "Fluid Ounce", "Ounce", "Pound", "Gram", "Kilogram", "Cup", "Gallon", "Millilitre", "Litre", "Roll", "Patty", "Loaf", ]
2020-09-15 11:10:16 +00:00
export default new Vuex.Store({
state: {
2020-11-29 12:54:21 +00:00
recipes: [],
2020-10-26 20:49:54 +00:00
categories: [],
2020-11-02 11:36:53 +00:00
userCategories: [],
units: ["unit", "tsp", "dsp", "tbsp", "fl oz", "cup", "pt", "qt", "gal", "ml", "l", "oz", "lb", "mg", "g", "kg", "cm", "in", "leaf", "clove", "piece", "pinch", "drop", "dozen", "stick", "small", "medium", "large", ],
2020-11-02 11:36:53 +00:00
yieldUnits: [],
userYieldUnits: [],
2020-11-23 09:49:58 +00:00
mealPlans: [],
2020-10-14 19:32:32 +00:00
icon: {
2020-11-02 11:36:53 +00:00
home: "\ued3b",
heart: "\ued36",
heartOutline: "\uea6c",
label: "\ued51",
2020-11-10 18:28:48 +00:00
labelOutline: "\uea8b",
2020-11-02 11:36:53 +00:00
cog: "\ueca6",
info: "\ued49",
menu: "\ueac1",
search: "\ueb54",
sort: "\ueac2",
plus: "\ueb21",
plusCircle: "\ueb22",
close: "\uebe9",
image: "\uea7f",
food: "\ueb3e",
back: "\uea95",
save: "\uedeb",
camera: "\uec61",
share: "\uedf3",
edit: "\uedba",
theme: "\uecaa",
link: "\ueaa0",
file: "\ued02",
2020-11-10 18:28:48 +00:00
detail: "\ue9f9",
2020-11-02 11:36:53 +00:00
user: "\uee33",
trash: "\uee26",
donate: "\ueb4f",
trylater: "\uec31",
trylaterOutline: "\ue94a",
note: "\ueb04",
copy: "\ue9e6",
2020-11-28 19:21:57 +00:00
check: "\ue9a4",
2020-11-02 11:36:53 +00:00
telegram: "\ueec7",
time: "\ueba2",
item: "\ue99d",
step: "\ue948",
source: "\ueaa0",
2020-11-06 09:07:41 +00:00
export: "\ued07",
import: "\ued0c",
2020-11-15 10:51:10 +00:00
outline: "\ueb07",
2020-11-23 09:49:58 +00:00
calendar: "\uec55",
today: "\ue97c",
globe: "\uea5a",
2020-10-14 19:32:32 +00:00
},
2020-10-21 17:54:45 +00:00
currentComponent: "EnRecipes",
language: [{
locale: 'en',
title: 'English'
}, {
locale: 'de',
title: 'Deutsch'
}, {
locale: 'ta',
title: 'தமிழ்'
}, ]
2020-09-15 11:10:16 +00:00
},
mutations: {
2020-10-26 20:49:54 +00:00
initializeRecipes(state) {
EnRecipesDB.query({
select: []
}).forEach((recipe) => {
2020-11-10 18:28:48 +00:00
state.recipes.push(recipe)
2020-10-26 20:49:54 +00:00
})
},
initializeCategories(state) {
let isCategoriesStored = userCategoriesDB.query({
select: []
}).length
2020-10-26 20:49:54 +00:00
if (isCategoriesStored) {
state.userCategories = userCategoriesDB.getDocument("userCategories").userCategories
2020-11-02 11:36:53 +00:00
let categoriesWithRecipes = state.recipes.map((e) => e.category)
state.userCategories = state.userCategories.filter((e) => categoriesWithRecipes.includes(e))
2020-10-26 20:49:54 +00:00
} else {
userCategoriesDB.createDocument({
userCategories: []
}, "userCategories")
2020-11-02 11:36:53 +00:00
}
state.categories = [...defaultCategories, ...state.userCategories]
state.categories.sort()
},
initializeYieldUnits(state) {
let isYieldUnitsStored = userYieldUnitsDB.query({
select: []
}).length
2020-11-02 11:36:53 +00:00
if (isYieldUnitsStored) {
state.userYieldUnits = userYieldUnitsDB.getDocument("userYieldUnits").userYieldUnits
2020-11-02 11:36:53 +00:00
let yieldUnitsWithRecipes = state.recipes.map((e) => e.yield.unit)
state.userYieldUnits = state.userYieldUnits.filter((e) => yieldUnitsWithRecipes.includes(e))
2020-11-02 11:36:53 +00:00
} else {
userYieldUnitsDB.createDocument({
userYieldUnits: []
}, "userYieldUnits")
2020-10-26 20:49:54 +00:00
}
2020-11-02 11:36:53 +00:00
state.yieldUnits = [...defaultYieldUnits, ...state.userYieldUnits]
2020-10-26 20:49:54 +00:00
},
2020-11-23 09:49:58 +00:00
initializeMealPlans(state) {
let isMealPlansDBStored = mealPlansDB.query({
select: []
}).length
2020-11-23 09:49:58 +00:00
if (isMealPlansDBStored) {
state.mealPlans = mealPlansDB.getDocument("mealPlans").mealPlans
} else {
mealPlansDB.createDocument({
mealPlans: []
}, "mealPlans")
2020-11-23 09:49:58 +00:00
}
2020-11-06 09:07:41 +00:00
},
importRecipes(state, recipes) {
let localRecipesIDs, partition
if (state.recipes.length) {
localRecipesIDs = state.recipes.map((e) => e.id)
partition = recipes.reduce(
(result, recipe, i) => {
localRecipesIDs.indexOf(recipe.id) < 0 ? result[0].push(recipe) // create candidates
: result[1].push(recipe) // update candidates
2020-11-06 09:07:41 +00:00
return result
},
[
[],
[]
])
2020-11-06 09:07:41 +00:00
if (partition[0].length) createDocuments(partition[0])
if (partition[1].length) updateDocuments(partition[1])
} else {
createDocuments(recipes)
}
function getUpdatedData(data) {
return data.map((recipe) => {
let r = Object.assign({}, recipe)
if (r.timeRequired) {
r.prepTime = "00:00"
r.cookTime = r.timeRequired
delete r.timeRequired
}
return r
})
}
2020-11-06 09:07:41 +00:00
function createDocuments(data) {
data = getUpdatedData(data)
2020-11-06 09:07:41 +00:00
state.recipes = [...state.recipes, ...data]
data.forEach((recipe) => {
EnRecipesDB.createDocument(recipe, recipe.id)
})
}
2020-11-06 09:07:41 +00:00
function updateDocuments(data) {
data = getUpdatedData(data)
2020-11-06 09:07:41 +00:00
data.forEach((recipe) => {
let recipeIndex = state.recipes.map((e, i) => {
let d1 = new Date(e.lastModified).getTime()
let d2 = new Date(recipe.lastModified).getTime()
return e.id === recipe.id && d1 < d2 ? i : -1
}).filter((e) => e >= 0)[0]
2020-11-23 09:49:58 +00:00
if (recipeIndex >= 0) {
Object.assign(state.recipes[recipeIndex], recipe)
EnRecipesDB.updateDocument(recipe.id, recipe)
}
2020-11-06 09:07:41 +00:00
})
}
2020-10-26 20:49:54 +00:00
},
2020-11-23 09:49:58 +00:00
importCategories(state, categories) {
state.userCategories = new Set([...state.userCategories, ...categories])
userCategoriesDB.updateDocument("userCategories", {
userCategories: [...state.userCategories],
})
state.categories = [...defaultCategories, ...state.userCategories]
state.categories.sort()
},
importYieldUnits(state, yieldUnits) {
state.userYieldUnits = new Set([...state.userYieldUnits, ...yieldUnits])
userYieldUnitsDB.updateDocument("userYieldUnits", {
userYieldUnits: [...state.userYieldUnits],
})
state.yieldUnits = [...defaultYieldUnits, ...state.userYieldUnits]
},
importMealPlans(state, mealPlans) {
let newMealPlans = mealPlans.filter(
(e) => !state.mealPlans.some(
(f) => f.title === e.title && f.startDate === e.startDate))
2020-11-23 09:49:58 +00:00
state.mealPlans = [...state.mealPlans, ...newMealPlans]
mealPlansDB.updateDocument("mealPlans", {
mealPlans: [...state.mealPlans],
})
},
addRecipe(state, {
id,
recipe
}) {
2020-11-23 09:49:58 +00:00
state.recipes.push(recipe)
EnRecipesDB.createDocument(recipe, id)
},
2020-10-26 20:49:54 +00:00
addCategory(state, category) {
2020-11-02 11:36:53 +00:00
let lowercase = state.categories.map((e) => e.toLowerCase())
if (lowercase.indexOf(category.toLowerCase()) == -1) {
state.userCategories.push(category)
userCategoriesDB.updateDocument("userCategories", {
2020-11-02 11:36:53 +00:00
userCategories: [...state.userCategories],
})
state.categories = [...defaultCategories, ...state.userCategories]
2020-10-26 20:49:54 +00:00
state.categories.sort()
2020-11-02 11:36:53 +00:00
}
},
2020-11-06 09:07:41 +00:00
addYieldUnit(state, yieldUnit) {
2020-11-02 11:36:53 +00:00
let lowercase = state.yieldUnits.map((e) => e.toLowerCase())
2020-11-06 09:07:41 +00:00
if (lowercase.indexOf(yieldUnit.toLowerCase()) == -1) {
state.userYieldUnits.push(yieldUnit)
userYieldUnitsDB.updateDocument("userYieldUnits", {
2020-11-02 11:36:53 +00:00
userYieldUnits: [...state.userYieldUnits],
2020-10-26 20:49:54 +00:00
})
2020-11-02 11:36:53 +00:00
state.yieldUnits = [...defaultYieldUnits, ...state.userYieldUnits]
2020-10-26 20:49:54 +00:00
}
},
addMealPlan(state, {
event,
eventColor
}) {
2020-11-23 09:49:58 +00:00
state.mealPlans.push({
title: event.title,
startDate: event.startDate,
endDate: event.endDate,
eventColor,
})
mealPlansDB.updateDocument("mealPlans", {
mealPlans: [...state.mealPlans],
2020-11-06 09:07:41 +00:00
})
2020-10-26 20:49:54 +00:00
},
deleteRecipe(state, {
index,
id
}) {
getFileAccess().deleteFile(state.recipes[index].imageSrc)
2020-10-26 20:49:54 +00:00
state.recipes.splice(index, 1)
2020-11-06 09:07:41 +00:00
EnRecipesDB.deleteDocument(id)
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (e.combinations.includes(id)) {
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i])
}
})
2020-10-26 20:49:54 +00:00
},
deleteMealPlan(state, {
title,
startDate
}) {
2020-11-23 09:49:58 +00:00
let mealPlan = state.mealPlans.filter((e) => {
console.log(e.startDate, startDate)
let sd = new Date(e.startDate).getTime()
return e.title === title && sd === startDate.getTime()
})[0]
let index = state.mealPlans.indexOf(mealPlan)
state.mealPlans.splice(index, 1)
state.mealPlans = [...state.mealPlans]
let mealPlans = mealPlansDB.getDocument("mealPlans").mealPlans
mealPlans.splice(index, 1)
mealPlansDB.updateDocument("mealPlans", {
mealPlans: [...mealPlans],
})
},
overwriteRecipe(state, {
id,
recipe
}) {
let index = state.recipes.indexOf(state.recipes.filter((e) => e.id === id)[0])
2020-11-23 09:49:58 +00:00
Object.assign(state.recipes[index], recipe)
EnRecipesDB.updateDocument(id, recipe)
},
toggleState(state, {
id,
recipe,
key,
setDate
}) {
let index = state.recipes.indexOf(state.recipes.filter((e) => e.id === id)[0])
2020-10-26 20:49:54 +00:00
state.recipes[index][key] = !state.recipes[index][key]
2020-11-02 11:36:53 +00:00
if (setDate) state.recipes[index].lastTried = new Date()
2020-11-06 09:07:41 +00:00
EnRecipesDB.updateDocument(id, recipe)
2020-10-26 20:49:54 +00:00
},
setRecipeAsTried(state, {
id,
recipe
}) {
let index = state.recipes.indexOf(state.recipes.filter((e) => e.id === id)[0])
state.recipes[index].tried = true
state.recipes[index].lastTried = new Date()
EnRecipesDB.updateDocument(id, recipe)
},
2020-11-02 11:36:53 +00:00
setLastTriedDate(state, index) {
state.recipes[index].lastTried = new Date()
2020-11-06 09:07:41 +00:00
EnRecipesDB.updateDocument(state.recipes[index].id, state.recipes[index])
2020-11-02 11:36:53 +00:00
},
renameCategory(state, {
current,
updated
}) {
2020-11-02 11:36:53 +00:00
let lowercase = state.categories.map((e) => e.toLowerCase())
if (lowercase.indexOf(updated.toLowerCase()) == -1) {
state.userCategories.push(updated)
userCategoriesDB.updateDocument("userCategories", {
2020-11-02 11:36:53 +00:00
userCategories: [...state.userCategories],
2020-10-26 20:49:54 +00:00
})
2020-11-02 11:36:53 +00:00
state.categories = [...defaultCategories, ...state.userCategories]
state.categories.sort()
2020-10-26 20:49:54 +00:00
}
state.recipes.forEach((e, i) => {
if (e.category == current) {
state.recipes[i].category = updated
2020-11-06 09:07:41 +00:00
EnRecipesDB.inBatch(() => {
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i])
2020-10-26 20:49:54 +00:00
})
}
})
},
2020-11-23 09:49:58 +00:00
setCurrentComponent(state, comp) {
state.currentComponent = comp
},
unSyncCombinations(state, {
id,
combinations
}) {
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (combinations.includes(e.id)) {
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i])
}
})
},
2020-09-15 11:10:16 +00:00
},
actions: {
initializeRecipes({
commit
}) {
2020-10-26 20:49:54 +00:00
commit("initializeRecipes")
},
initializeCategories({
commit
}) {
2020-10-26 20:49:54 +00:00
commit("initializeCategories")
},
initializeYieldUnits({
commit
}) {
2020-11-02 11:36:53 +00:00
commit("initializeYieldUnits")
},
initializeMealPlans({
commit
}) {
2020-11-23 09:49:58 +00:00
commit("initializeMealPlans")
2020-10-26 20:49:54 +00:00
},
importRecipesAction({
commit
}, recipes) {
2020-11-06 09:07:41 +00:00
commit("importRecipes", recipes)
},
importCategoriesAction({
commit
}, categories) {
2020-11-06 09:07:41 +00:00
commit("importCategories", categories)
},
importYieldUnitsAction({
commit
}, yieldUnits) {
2020-11-23 09:49:58 +00:00
commit("importYieldUnits", yieldUnits)
},
importMealPlansAction({
commit
}, mealPlans) {
2020-11-23 09:49:58 +00:00
commit("importMealPlans", mealPlans)
},
addRecipeAction({
commit
}, recipe) {
2020-11-23 09:49:58 +00:00
commit("addRecipe", recipe)
},
addYieldUnitAction({
commit
}, yieldUnit) {
2020-11-02 11:36:53 +00:00
commit("addYieldUnit", yieldUnit)
},
addCategoryAction({
commit
}, category) {
2020-11-23 09:49:58 +00:00
commit("addCategory", category)
2020-11-06 09:07:41 +00:00
},
addMealPlanAction({
commit
}, mealPlan) {
2020-11-23 09:49:58 +00:00
commit("addMealPlan", mealPlan)
},
deleteMealPlanAction({
commit
}, mealPlan) {
2020-11-23 09:49:58 +00:00
commit("deleteMealPlan", mealPlan)
2020-10-26 20:49:54 +00:00
},
deleteRecipeAction({
commit
}, recipe) {
2020-10-26 20:49:54 +00:00
commit("deleteRecipe", recipe)
},
overwriteRecipeAction({
commit
}, updatedRecipe) {
2020-11-23 09:49:58 +00:00
commit("overwriteRecipe", updatedRecipe)
},
toggleStateAction({
commit
}, toggledRecipe) {
2020-10-26 20:49:54 +00:00
commit("toggleState", toggledRecipe)
},
setRecipeAsTriedAction({
commit
}, recipe) {
commit("setRecipeAsTried", recipe)
},
setLastTriedDateAction({
commit
}, index) {
2020-11-02 11:36:53 +00:00
commit("setLastTriedDate", index)
},
renameCategoryAction({
commit
}, category) {
2020-10-26 20:49:54 +00:00
commit("renameCategory", category)
},
setCurrentComponentAction({
commit
}, comp) {
2020-11-23 09:49:58 +00:00
commit("setCurrentComponent", comp)
},
unSyncCombinationsAction({
commit
}, combinations) {
2020-11-15 10:51:10 +00:00
commit("unSyncCombinations", combinations)
},
2020-10-14 19:32:32 +00:00
},
})