enrecipes/app/store.js

648 lines
17 KiB
JavaScript
Raw Normal View History

2020-12-29 10:35:19 +00:00
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
import {Couchbase} from "nativescript-couchbase-plugin";
import {getFileAccess, File, ApplicationSettings} from "@nativescript/core";
const EnRecipesDB = new Couchbase("EnRecipes");
const userCuisinesDB = new Couchbase("userCuisines");
const userCategoriesDB = new Couchbase("userCategories");
const userYieldUnitsDB = new Couchbase("userYieldUnits");
const userUnitsDB = new Couchbase("userUnits");
const mealPlansDB = new Couchbase("mealPlans");
const defaultCuisines = [
"American",
"Brazilian",
"British",
"Chinese",
"Danish",
"Egyptian",
"Filipino",
"French",
"German",
"Greek",
"Indian",
"Irish",
"Italian",
"Jamaican",
"Japanese",
"Jewish",
"Kenyan",
"Korean",
"Mexican",
"Nigerian",
"Portuguese",
"Russian",
"Scottish",
"Spanish",
"Sri Lankan",
"Swedish",
"Thai",
"Turkish",
"Vietnamese"
];
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"
];
const defaultUnits = [
"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"
];
const listItems = {
cuisines: {
db: userCuisinesDB,
key: "userCuisines",
stateName: "cuisines",
sort: true,
defaultItems: defaultCuisines
},
categories: {
db: userCategoriesDB,
key: "userCategories",
stateName: "categories",
sort: true,
defaultItems: defaultCategories
},
yieldUnits: {
db: userYieldUnitsDB,
key: "userYieldUnits",
stateName: "yieldUnits",
sort: false,
defaultItems: defaultYieldUnits
},
units: {
db: userUnitsDB,
key: "userUnits",
stateName: "units",
sort: false,
defaultItems: defaultUnits
}
};
2020-09-15 11:10:16 +00:00
export default new Vuex.Store({
state: {
2020-11-29 12:54:21 +00:00
recipes: [],
2020-12-29 10:35:19 +00:00
cuisines: [],
2020-10-26 20:49:54 +00:00
categories: [],
2020-11-02 11:36:53 +00:00
yieldUnits: [],
2020-12-29 10:35:19 +00:00
units: [],
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",
2020-12-29 10:35:19 +00:00
heartLine: "\uea6c",
cuisine: "\ueb3e",
category: "\uec7c",
categoryLine: "\ue99c",
tag: "\uee12",
tagLine: "\ueb97",
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",
2020-12-29 10:35:19 +00:00
donate: "\uece2",
2020-11-02 11:36:53 +00:00
trylater: "\uec31",
2020-12-29 10:35:19 +00:00
trylaterLine: "\ue94a",
2020-11-02 11:36:53 +00:00
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-12-29 10:35:19 +00:00
lock: "\ued61",
github: "\uee8b",
gitlab: "\uee8c",
shuffle: "\ueb6b",
reset: "\ueb3d",
emptyCart: "\ue999",
cart: "\uec77",
meter: "\ueb96",
star: "\uee0a",
starLine: "\ueb83",
compass: "\uecb4",
show: "\uedfd"
2020-10-14 19:32:32 +00:00
},
2020-10-21 17:54:45 +00:00
currentComponent: "EnRecipes",
2020-12-29 10:35:19 +00:00
sortType: "Oldest first",
language: [
{
locale: "da",
title: "Dansk"
}, {
locale: "de",
title: "Deutsch"
}, {
locale: "en-GB",
title: "English (United Kingdom)"
2020-12-29 10:35:19 +00:00
}, {
locale: "es",
title: "Español"
}, {
locale: "nl",
title: "Nederlands"
}, {
locale: "pt",
title: "Português"
}, {
locale: "ru",
title: "Русский"
}, {
locale: "ta",
title: "தமிழ்"
}
],
shakeEnabled: true,
importSummary: {
found: 0,
imported: 0,
updated: 0
}
2020-09-15 11:10:16 +00:00
},
mutations: {
2020-12-29 10:35:19 +00:00
setSortType(state, sortType) {
state.sortType = sortType
2020-10-26 20:49:54 +00:00
},
2020-12-29 10:35:19 +00:00
initializeRecipes(state) {
EnRecipesDB.query({select: []}).forEach(r => {
if (r.timeRequired) {
r.prepTime = "00:00";
r.cookTime = r.timeRequired;
delete r.timeRequired;
}
if (!r.hasOwnProperty("cuisine"))
r.cuisine = "Undefined"
if (!r.hasOwnProperty("tags"))
r.tags = []
if (!r.hasOwnProperty("difficulty"))
r.difficulty = "Easy"
if (!r.hasOwnProperty("rating"))
r.rating = 0
if (!r.hasOwnProperty("created"))
r.created = r.lastModified
state.recipes.push(r);
});
state.shakeEnabled = ApplicationSettings.getBoolean("shakeEnabled", true)
state.sortType = ApplicationSettings.getString("sortType", "Oldest first")
2020-11-06 09:07:41 +00:00
},
importRecipes(state, recipes) {
2020-12-29 10:35:19 +00:00
let localRecipesIDs,
partition
let imported = 0
let updated = 0
function getUpdatedData(data) {
2020-12-29 10:35:19 +00:00
return data.map(recipe => {
let r = Object.assign({}, recipe);
if (r.timeRequired) {
2020-12-29 10:35:19 +00:00
r.prepTime = "00:00";
r.cookTime = r.timeRequired;
delete r.timeRequired;
}
2020-12-29 10:35:19 +00:00
if (!r.hasOwnProperty("cuisine"))
r.cuisine = "Undefined"
if (!r.hasOwnProperty("tags"))
r.tags = []
if (!r.hasOwnProperty("difficulty"))
r.difficulty = "Easy"
if (!r.hasOwnProperty("rating"))
r.rating = 0
if (!r.hasOwnProperty("created"))
r.created = r.lastModified
return r;
});
}
2020-11-06 09:07:41 +00:00
function createDocuments(data) {
2020-12-29 10:35:19 +00:00
data = getUpdatedData(data);
state.recipes = [
...state.recipes,
...data
];
data.forEach(recipe => {
imported++
EnRecipesDB.createDocument(recipe, recipe.id);
});
2020-11-06 09:07:41 +00:00
}
function updateDocuments(data) {
2020-12-29 10:35:19 +00:00
data = getUpdatedData(data);
data.forEach(recipe => {
let recipeIndex = state.recipes.map((e, i) => {
2020-12-29 10:35:19 +00:00
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) {
2020-12-29 10:35:19 +00:00
updated++
Object.assign(state.recipes[recipeIndex], recipe);
EnRecipesDB.updateDocument(recipe.id, recipe);
2020-11-23 09:49:58 +00:00
}
2020-12-29 10:35:19 +00:00
});
2020-11-02 11:36:53 +00:00
}
2020-12-29 10:35:19 +00:00
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
return result;
}, [[], []]);
if (partition[0].length)
createDocuments(partition[0]);
if (partition[1].length)
updateDocuments(partition[1]);
}
else {
createDocuments(recipes);
2020-10-26 20:49:54 +00:00
}
2020-12-29 10:35:19 +00:00
state.importSummary.found = recipes.length
state.importSummary.imported = imported
state.importSummary.updated = updated
},
addRecipe(state, {id, recipe}) {
state.recipes.push(recipe);
EnRecipesDB.createDocument(recipe, id);
},
overwriteRecipe(state, {id, recipe}) {
let index = state.recipes.indexOf(state.recipes.filter(e => e.id === id)[0]);
Object.assign(state.recipes[index], recipe);
EnRecipesDB.updateDocument(id, recipe);
},
deleteRecipe(state, {index, id}) {
getFileAccess().deleteFile(state.recipes[index].imageSrc);
state.recipes.splice(index, 1);
EnRecipesDB.deleteDocument(id);
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (e.combinations.includes(id)) {
2020-12-29 10:35:19 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1);
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i]);
2020-11-15 10:51:10 +00:00
}
2020-12-29 10:35:19 +00:00
});
},
initializeListItems(state) {
function initialize(listName) {
let userItems;
let db = listItems[listName].db;
let key = listItems[listName].key;
let stateName = listItems[listName].stateName;
let defaultItems = listItems[listName].defaultItems;
if (!state[stateName].length) {
let isStored = db.query({select: []}).length;
if (isStored) {
userItems = db.getDocument(key)[key];
if (userItems.some(e => defaultItems.includes(e)))
state[stateName] = userItems;
else
state[stateName] = [
...defaultItems,
...userItems
];
}
else {
if (listItems[listName].sort) {
state[stateName].sort();
}
state[stateName] = defaultItems;
db.createDocument({
[key]: []
}, key);
}
}
}["cuisines", "categories", "yieldUnits", "units"].forEach(item => {
initialize(item);
});
},
importListItems(state, {data, listName}) {
let db = listItems[listName].db;
let key = listItems[listName].key;
let stateName = listItems[listName].stateName;
let items = new Set([
...state[stateName],
...data
]);
state[stateName] = [...items];
if (listItems[listName].sort)
state[stateName].sort();
db.updateDocument(key, {[key]: state[stateName]});
},
addListItem(state, {item, listName}) {
console.log(item, listName);
let db = listItems[listName].db;
let key = listItems[listName].key;
let stateName = listItems[listName].stateName;
let lowercase = state[stateName].map(e => e.toLowerCase());
if (lowercase.indexOf(item.toLowerCase()) == -1) {
state[stateName].push(item);
db.updateDocument(key, {[key]: state[stateName]});
if (listItems[listName].sort)
state[stateName].sort();
}
},
removeListItem(state, {item, listName}) {
let db = listItems[listName].db;
let key = listItems[listName].key;
let stateName = listItems[listName].stateName;
let index = state[stateName].indexOf(item);
state[stateName].splice(index, 1);
db.updateDocument(key, {[key]: state[stateName]});
if (listItems[listName].sort)
state[stateName].sort();
}
,
resetListItems(state, listName) {
let stateName = listItems[listName].stateName;
let defaultItems = listItems[listName].defaultItems;
let items = new Set([
...defaultItems,
...state[listName]
]);
state[listName] = [...items];
if (listItems[listName].sort) {
state[stateName].sort();
}
2020-10-26 20:49:54 +00:00
},
2020-12-29 10:35:19 +00:00
initializeMealPlans(state) {
let isMealPlansDBStored = mealPlansDB.query({select: []}).length;
if (isMealPlansDBStored) {
state.mealPlans = mealPlansDB.getDocument("mealPlans").mealPlans;
} else {
mealPlansDB.createDocument({
mealPlans: []
}, "mealPlans");
}
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
importMealPlans(state, mealPlans) {
let newMealPlans = mealPlans.filter(e => !state.mealPlans.some(f => f.title === e.title && f.startDate === e.startDate));
state.mealPlans = [
...state.mealPlans,
...newMealPlans
];
mealPlansDB.updateDocument("mealPlans", {mealPlans: state.mealPlans});
},
addMealPlan(state, {event, eventColor}) {
state.mealPlans.push({title: event.title, startDate: event.startDate, endDate: event.endDate, eventColor});
mealPlansDB.updateDocument("mealPlans", {
mealPlans: [...state.mealPlans]
});
},
deleteMealPlan(state, {title, startDate}) {
let mealPlan = state.mealPlans.filter(e => {
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]
});
},
toggleState(state, {id, recipe, key, setDate}) {
let index = state.recipes.indexOf(state.recipes.filter(e => e.id === id)[0]);
state.recipes[index][key] = !state.recipes[index][key];
if (setDate)
state.recipes[index].lastTried = new Date();
EnRecipesDB.updateDocument(id, recipe);
},
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) {
2020-12-29 10:35:19 +00:00
state.recipes[index].lastTried = new Date();
EnRecipesDB.updateDocument(state.recipes[index].id, state.recipes[index]);
2020-10-26 20:49:54 +00:00
},
2020-11-23 09:49:58 +00:00
setCurrentComponent(state, comp) {
2020-12-29 10:35:19 +00:00
state.currentComponent = comp;
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
unSyncCombinations(state, {id, combinations}) {
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (combinations.includes(e.id)) {
2020-12-29 10:35:19 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1);
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i]);
}
});
},
setShake(state, shake) {
state.shakeEnabled = shake;
},
setRating(state, {id, recipe, rating}) {
let index = state.recipes.indexOf(state.recipes.filter(e => e.id === id)[0]);
state.recipes[index].rating = rating
EnRecipesDB.updateDocument(id, recipe);
},
unlinkBrokenImages(state) {
state.recipes.forEach((r, i) => {
if (r.imageSrc && !File.exists(r.imageSrc)) {
r.imageSrc = null
Object.assign(state.recipes[i], r);
EnRecipesDB.updateDocument(r.id, r);
2020-11-15 10:51:10 +00:00
}
})
2020-12-29 10:35:19 +00:00
}
2020-09-15 11:10:16 +00:00
},
actions: {
2020-12-29 10:35:19 +00:00
setSortTypeAction({
commit
2020-12-29 10:35:19 +00:00
}, sortType) {
commit("setSortType", sortType)
2020-10-26 20:49:54 +00:00
},
2020-12-29 10:35:19 +00:00
initializeRecipes({commit}) {
commit("initializeRecipes");
2020-10-26 20:49:54 +00:00
},
2020-12-29 10:35:19 +00:00
importRecipesAction({
commit
2020-12-29 10:35:19 +00:00
}, recipes) {
commit("importRecipes", recipes);
2020-11-02 11:36:53 +00:00
},
2020-12-29 10:35:19 +00:00
addRecipeAction({
commit
2020-12-29 10:35:19 +00:00
}, recipe) {
commit("addRecipe", recipe);
2020-10-26 20:49:54 +00:00
},
2020-12-29 10:35:19 +00:00
overwriteRecipeAction({
commit
2020-12-29 10:35:19 +00:00
}, updatedRecipe) {
commit("overwriteRecipe", updatedRecipe);
2020-11-06 09:07:41 +00:00
},
2020-12-29 10:35:19 +00:00
deleteRecipeAction({
commit
2020-12-29 10:35:19 +00:00
}, recipe) {
commit("deleteRecipe", recipe);
},
initializeListItems({commit}) {
commit("initializeListItems");
2020-11-06 09:07:41 +00:00
},
2020-12-29 10:35:19 +00:00
importListItemsAction({
commit
2020-12-29 10:35:19 +00:00
}, data) {
commit("importListItems", data);
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
addListItemAction({
commit
2020-12-29 10:35:19 +00:00
}, item) {
commit("addListItem", item);
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
removeListItemAction({
commit
2020-12-29 10:35:19 +00:00
}, item) {
commit("removeListItem", item);
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
resetListItemsAction({
commit
2020-12-29 10:35:19 +00:00
}, listName) {
commit("resetListItems", listName);
},
initializeMealPlans({commit}) {
commit("initializeMealPlans");
2020-11-02 11:36:53 +00:00
},
2020-12-29 10:35:19 +00:00
importMealPlansAction({
commit
2020-12-29 10:35:19 +00:00
}, mealPlans) {
commit("importMealPlans", mealPlans);
2020-11-06 09:07:41 +00:00
},
addMealPlanAction({
commit
}, mealPlan) {
2020-12-29 10:35:19 +00:00
commit("addMealPlan", mealPlan);
2020-11-23 09:49:58 +00:00
},
deleteMealPlanAction({
commit
}, mealPlan) {
2020-12-29 10:35:19 +00:00
commit("deleteMealPlan", mealPlan);
2020-11-23 09:49:58 +00:00
},
toggleStateAction({
commit
}, toggledRecipe) {
2020-12-29 10:35:19 +00:00
commit("toggleState", toggledRecipe);
2020-10-26 20:49:54 +00:00
},
setRecipeAsTriedAction({
commit
}, recipe) {
2020-12-29 10:35:19 +00:00
commit("setRecipeAsTried", recipe);
},
setLastTriedDateAction({
commit
}, index) {
2020-12-29 10:35:19 +00:00
commit("setLastTriedDate", index);
2020-10-26 20:49:54 +00:00
},
setCurrentComponentAction({
commit
}, comp) {
2020-12-29 10:35:19 +00:00
commit("setCurrentComponent", comp);
2020-11-23 09:49:58 +00:00
},
unSyncCombinationsAction({
commit
}, combinations) {
2020-12-29 10:35:19 +00:00
commit("unSyncCombinations", combinations);
2020-11-15 10:51:10 +00:00
},
2020-12-29 10:35:19 +00:00
setShakeAction({
commit
}, shake) {
commit("setShake", shake);
},
setRatingAction({
commit
}, rating) {
commit("setRating", rating);
},
unlinkBrokenImages({commit}) {
commit("unlinkBrokenImages");
}
}
});