enrecipes/app/store.ts

818 lines
22 KiB
TypeScript
Raw Normal View History

2021-04-14 13:38:22 +00:00
import Vue from 'nativescript-vue'
2021-03-21 17:02:04 +00:00
import Vuex from 'vuex'
Vue.use(Vuex)
2021-03-23 06:16:25 +00:00
import { CouchBase } from '@triniwiz/nativescript-couchbase'
2021-04-14 13:38:22 +00:00
import { getFileAccess, File, ApplicationSettings } from '@nativescript/core'
2021-03-23 06:16:25 +00:00
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')
2020-12-29 10:35:19 +00:00
const defaultCuisines = [
2021-03-21 17:02:04 +00:00
'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',
]
2020-12-29 10:35:19 +00:00
const defaultCategories = [
2021-03-21 17:02:04 +00:00
'Appetizers',
'Barbecue',
'Beverages',
'Breads',
2021-04-15 16:44:21 +00:00
'breakfast',
2021-03-21 17:02:04 +00:00
'Desserts',
2021-04-15 16:44:21 +00:00
'dinner',
2021-03-21 17:02:04 +00:00
'Healthy',
2021-04-15 16:44:21 +00:00
'lunch',
2021-03-21 17:02:04 +00:00
'Main dishes',
'Meat',
'Noodles',
'Pasta',
'Poultry',
'Rice',
'Salads',
'Sauces',
'Seafood',
'Side dishes',
2021-04-15 16:44:21 +00:00
'snacks',
2021-03-21 17:02:04 +00:00
'Soups',
'Undefined',
'Vegan',
'Vegetarian',
]
2020-12-29 10:35:19 +00:00
const defaultYieldUnits = [
2021-03-21 17:02:04 +00:00
'Serving',
'Piece',
'Teaspoon',
'Tablespoon',
'Fluid Ounce',
'Ounce',
'Pound',
'Gram',
'Kilogram',
'Cup',
'Gallon',
'Millilitre',
'Litre',
'Roll',
'Patty',
'Loaf',
]
2020-12-29 10:35:19 +00:00
const defaultUnits = [
2021-03-21 17:02:04 +00:00
'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-12-29 10:35:19 +00:00
const listItems = {
cuisines: {
db: userCuisinesDB,
2021-03-21 17:02:04 +00:00
key: 'userCuisines',
stateName: 'cuisines',
2020-12-29 10:35:19 +00:00
sort: true,
2021-03-21 17:02:04 +00:00
defaultItems: defaultCuisines,
2020-12-29 10:35:19 +00:00
},
categories: {
db: userCategoriesDB,
2021-03-21 17:02:04 +00:00
key: 'userCategories',
stateName: 'categories',
2020-12-29 10:35:19 +00:00
sort: true,
2021-03-21 17:02:04 +00:00
defaultItems: defaultCategories,
2020-12-29 10:35:19 +00:00
},
yieldUnits: {
db: userYieldUnitsDB,
2021-03-21 17:02:04 +00:00
key: 'userYieldUnits',
stateName: 'yieldUnits',
2020-12-29 10:35:19 +00:00
sort: false,
2021-03-21 17:02:04 +00:00
defaultItems: defaultYieldUnits,
2020-12-29 10:35:19 +00:00
},
units: {
db: userUnitsDB,
2021-03-21 17:02:04 +00:00
key: 'userUnits',
stateName: 'units',
2020-12-29 10:35:19 +00:00
sort: false,
2021-03-21 17:02:04 +00:00
defaultItems: defaultUnits,
},
}
2021-04-01 10:55:35 +00:00
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: {
2021-04-12 18:09:48 +00:00
back: '\ue900',
bag: '\ue901',
bagged: '\ue902',
cal: '\ue903',
category: '\ue904',
check: '\ue905',
cog: '\ue906',
cuisine: '\ue907',
db: '\ue908',
del: '\ue909',
diff: '\ue90a',
don: '\ue90b',
done: '\ue90c',
edit: '\ue90d',
exp: '\ue90e',
fav: '\ue90f',
faved: '\ue910',
filter: '\ue911',
folder: '\ue912',
gh: '\ue913',
help: '\ue914',
home: '\ue915',
img: '\ue916',
imp: '\ue917',
info: '\ue918',
interface: '\ue919',
lang: '\ue91a',
layout: '\ue91b',
left: '\ue91c',
menu: '\ue91d',
opts: '\ue91e',
plus: '\ue91f',
price: '\ue920',
priv: '\ue921',
reset: '\ue922',
right: '\ue923',
save: '\ue924',
sear: '\ue925',
share: '\ue926',
shuf: '\ue927',
sort: '\ue928',
star: '\ue929',
starred: '\ue92a',
tag: '\ue92b',
tg: '\ue92c',
theme: '\ue92d',
time: '\ue92e',
tod: '\ue92f',
trans: '\ue930',
tried: '\ue931',
try: '\ue932',
uncheck: '\ue933',
undo: '\ue934',
week: '\ue935',
x: '\ue936',
2021-03-21 17:02:04 +00:00
},
currentComponent: 'EnRecipes',
sortType: 'Oldest first',
2020-12-29 10:35:19 +00:00
language: [
2021-04-17 16:24:47 +00:00
// {
// locale: 'da',
// title: 'Dansk',
// },
2021-03-21 17:02:04 +00:00
{
locale: 'de',
title: 'Deutsch',
},
{
locale: 'en-GB',
title: 'English (UK)',
},
{
locale: 'es',
title: 'Español',
},
{
locale: 'fr',
title: 'Français',
},
{
locale: 'fr-BE',
title: 'Français (BE)',
},
{
locale: 'fr-CA',
title: 'Français (CA)',
},
{
locale: 'fr-CH',
title: 'Français (CH)',
},
2021-04-17 16:24:47 +00:00
{
locale: 'hi',
title: 'हिंदी',
},
{
locale: 'ml',
title: 'മലയാളം',
},
{
locale: 'id',
title: 'Indonesia',
},
2021-03-21 17:02:04 +00:00
{
locale: 'it',
title: 'Italiano',
},
{
locale: 'nb-NO',
title: 'Norsk bokmål',
},
{
locale: 'nl',
title: 'Nederlands',
},
2021-04-17 16:24:47 +00:00
// {
// locale: 'pt',
// title: 'Português',
// },
2021-03-21 17:02:04 +00:00
{
locale: 'ru',
title: 'Русский',
},
{
locale: 'ta',
title: 'தமிழ்',
},
2021-04-17 16:24:47 +00:00
{
locale: 'te',
title: 'తెలుగు',
},
2020-12-29 10:35:19 +00:00
],
shakeEnabled: true,
importSummary: {
found: 0,
imported: 0,
2021-03-21 17:02:04 +00:00
updated: 0,
},
layout: 'detailed',
2021-04-01 10:55:35 +00:00
selectedCuisine: null,
selectedCategory: null,
selectedTag: null,
appTheme: 'Light',
2021-04-12 18:09:48 +00:00
mondayFirst: false,
2020-09-15 11:10:16 +00:00
},
mutations: {
2021-04-12 18:09:48 +00:00
clearImportSummary(state) {
for (const key in state.importSummary) state.importSummary[key] = 0
},
setFirstDay(state, bool) {
state.mondayFirst = bool
ApplicationSettings.setBoolean('mondayFirst', bool)
},
2021-04-01 10:55:35 +00:00
setTheme(state, theme) {
state.appTheme = theme
2021-04-12 18:09:48 +00:00
ApplicationSettings.setString('appTheme', theme)
2021-04-01 10:55:35 +00:00
},
clearFilter(state) {
state.selectedCuisine = state.selectedCategory = state.selectedTag = null
},
2021-03-21 17:02:04 +00:00
setLayout(state, type) {
state.layout = type
},
2020-12-29 10:35:19 +00:00
setSortType(state, sortType) {
state.sortType = sortType
2020-10-26 20:49:54 +00:00
},
2021-04-01 10:55:35 +00:00
initRecipes(state) {
2021-03-21 17:02:04 +00:00
EnRecipesDB.query({ select: [] }).forEach((r) => {
2020-12-29 10:35:19 +00:00
if (r.timeRequired) {
2021-03-21 17:02:04 +00:00
r.prepTime = '00:00'
r.cookTime = r.timeRequired
delete r.timeRequired
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +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
// if (!r.hasOwnProperty("inBag"))
2021-01-23 17:20:15 +00:00
// r.inBag = false
2021-03-21 17:02:04 +00:00
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) {
2021-03-21 17:02:04 +00:00
let localRecipesIDs, partition
2020-12-29 10:35:19 +00:00
let imported = 0
let updated = 0
function getUpdatedData(data) {
2021-03-21 17:02:04 +00:00
return data.map((recipe) => {
let r = Object.assign({}, recipe)
if (r.timeRequired) {
2021-03-21 17:02:04 +00:00
r.prepTime = '00:00'
r.cookTime = r.timeRequired
delete r.timeRequired
}
2021-03-21 17:02:04 +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
// if (!r.hasOwnProperty("inBag"))
2021-01-23 17:20:15 +00:00
// r.inBag = false
2021-03-21 17:02:04 +00:00
return r
})
}
2020-11-06 09:07:41 +00:00
function createDocuments(data) {
2021-03-21 17:02:04 +00:00
data = getUpdatedData(data)
state.recipes = [...state.recipes, ...data]
data.forEach((recipe) => {
2020-12-29 10:35:19 +00:00
imported++
2021-03-21 17:02:04 +00:00
EnRecipesDB.createDocument(recipe, recipe.id)
})
2020-11-06 09:07:41 +00:00
}
function updateDocuments(data) {
2021-03-21 17:02:04 +00:00
data = getUpdatedData(data)
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) {
2020-12-29 10:35:19 +00:00
updated++
2021-03-21 17:02:04 +00:00
Object.assign(state.recipes[recipeIndex], recipe)
EnRecipesDB.updateDocument(recipe.id, recipe)
2020-11-23 09:49:58 +00:00
}
2021-03-21 17:02:04 +00:00
})
2020-11-02 11:36:53 +00:00
}
2020-12-29 10:35:19 +00:00
if (state.recipes.length) {
2021-03-21 17:02:04 +00:00
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
},
2021-03-21 17:02:04 +00:00
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)) {
2021-03-21 17:02:04 +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
}
2021-03-21 17:02:04 +00:00
})
2020-12-29 10:35:19 +00:00
},
2021-01-23 17:20:15 +00:00
deleteRecipes(state, ids) {
2021-03-21 17:02:04 +00:00
ids.forEach((id) => {
let index = state.recipes.findIndex((e) => e.id === id)
getFileAccess().deleteFile(state.recipes[index].imageSrc)
state.recipes.splice(index, 1)
EnRecipesDB.deleteDocument(id)
2021-01-23 17:20:15 +00:00
state.recipes.forEach((e, i) => {
if (e.combinations.includes(id)) {
2021-03-21 17:02:04 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i])
2021-01-23 17:20:15 +00:00
}
2021-03-21 17:02:04 +00:00
})
})
2021-01-23 17:20:15 +00:00
},
2021-04-01 10:55:35 +00:00
initListItems(state) {
2020-12-29 10:35:19 +00:00
function initialize(listName) {
2021-03-21 17:02:04 +00:00
let userItems
let db = listItems[listName].db
let key = listItems[listName].key
let stateName = listItems[listName].stateName
let defaultItems = listItems[listName].defaultItems
2020-12-29 10:35:19 +00:00
if (!state[stateName].length) {
2021-03-21 17:02:04 +00:00
let isStored = db.query({ select: [] }).length
2020-12-29 10:35:19 +00:00
if (isStored) {
2021-03-21 17:02:04 +00:00
userItems = db.getDocument(key)[key]
if (userItems.some((e) => defaultItems.includes(e)))
state[stateName] = userItems
else state[stateName] = [...defaultItems, ...userItems]
} else {
2020-12-29 10:35:19 +00:00
if (listItems[listName].sort) {
2021-03-21 17:02:04 +00:00
state[stateName].sort()
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
state[stateName] = defaultItems
db.createDocument(
{
[key]: [],
},
key
)
2020-12-29 10:35:19 +00:00
}
}
2021-03-21 17:02:04 +00:00
}
;['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 }) {
let db = listItems[listName].db
let key = listItems[listName].key
let stateName = listItems[listName].stateName
let lowercase = state[stateName].map((e) => e.toLowerCase())
2020-12-29 10:35:19 +00:00
if (lowercase.indexOf(item.toLowerCase()) == -1) {
2021-03-21 17:02:04 +00:00
state[stateName].push(item)
db.updateDocument(key, { [key]: state[stateName] })
2020-12-29 10:35:19 +00:00
if (listItems[listName].sort)
2021-03-21 17:02:04 +00:00
state[stateName].sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
)
2021-01-23 17:40:30 +00:00
}
},
2021-03-21 17:02:04 +00:00
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()
},
2020-12-29 10:35:19 +00:00
resetListItems(state, listName) {
2021-03-21 17:02:04 +00:00
let stateName = listItems[listName].stateName
let defaultItems = listItems[listName].defaultItems
state[listName] = [...defaultItems]
2020-12-29 10:35:19 +00:00
if (listItems[listName].sort) {
2021-03-21 17:02:04 +00:00
state[stateName].sort()
2020-12-29 10:35:19 +00:00
}
2020-10-26 20:49:54 +00:00
},
2021-04-01 10:55:35 +00:00
initMealPlans(state) {
2021-03-21 17:02:04 +00:00
let isMealPlansDBStored = mealPlansDB.query({ select: [] }).length
2020-12-29 10:35:19 +00:00
if (isMealPlansDBStored) {
2021-03-21 17:02:04 +00:00
let plans = mealPlansDB.getDocument('mealPlans').mealPlans
if (plans.length && plans[0].hasOwnProperty('eventColor')) {
plans.forEach((p) => {
2021-01-23 17:20:15 +00:00
let d = new Date(p.startDate)
2021-03-21 17:02:04 +00:00
p.date = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
0
).getTime()
2021-01-23 17:20:15 +00:00
switch (new Date(p.date).getHours()) {
case 0:
2021-03-21 17:02:04 +00:00
p.type = 'breakfast'
break
2021-01-23 17:20:15 +00:00
case 5:
2021-03-21 17:02:04 +00:00
p.type = 'lunch'
break
2021-01-23 17:20:15 +00:00
case 10:
2021-03-21 17:02:04 +00:00
p.type = 'dinner'
break
2021-01-23 17:20:15 +00:00
case 15:
2021-03-21 17:02:04 +00:00
p.type = 'snacks'
break
2021-01-23 17:20:15 +00:00
}
delete p.startDate
delete p.endDate
delete p.eventColor
state.mealPlans.push(p)
})
2021-03-21 17:02:04 +00:00
mealPlansDB.updateDocument('mealPlans', {
mealPlans: state.mealPlans,
})
} else state.mealPlans = [...plans]
2020-12-29 10:35:19 +00:00
} else {
2021-03-21 17:02:04 +00:00
mealPlansDB.createDocument(
{
mealPlans: [],
},
'mealPlans'
)
2020-12-29 10:35:19 +00:00
}
2020-11-23 09:49:58 +00:00
},
2020-12-29 10:35:19 +00:00
importMealPlans(state, mealPlans) {
2021-03-21 17:02:04 +00:00
let newMealPlans = mealPlans.filter((e) => {
if (e.hasOwnProperty('eventColor')) {
return !state.mealPlans.some((f) => {
2021-01-23 17:20:15 +00:00
let d = new Date(e.startDate)
2021-03-21 17:02:04 +00:00
let date = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
0
).getTime()
2021-01-23 17:20:15 +00:00
let type
switch (d.getHours()) {
case 0:
2021-03-21 17:02:04 +00:00
type = 'breakfast'
break
2021-01-23 17:20:15 +00:00
case 5:
2021-03-21 17:02:04 +00:00
type = 'lunch'
break
2021-01-23 17:20:15 +00:00
case 10:
2021-03-21 17:02:04 +00:00
type = 'dinner'
break
2021-01-23 17:20:15 +00:00
case 15:
2021-03-21 17:02:04 +00:00
type = 'snacks'
break
2021-01-23 17:20:15 +00:00
}
return f.title === e.title && f.date === date && f.type === type
})
} else {
2021-03-21 17:02:04 +00:00
return !state.mealPlans.some(
(f) => f.title === e.title && f.date === e.date && f.type === e.type
)
2021-01-23 17:20:15 +00:00
}
})
let updatedMealPlans = []
2021-03-21 17:02:04 +00:00
if (newMealPlans[0].hasOwnProperty('eventColor')) {
newMealPlans.forEach((p) => {
2021-01-23 17:20:15 +00:00
let d = new Date(p.startDate)
2021-03-21 17:02:04 +00:00
p.date = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
0
).getTime()
2021-01-23 17:20:15 +00:00
switch (d.getHours()) {
case 0:
2021-03-21 17:02:04 +00:00
p.type = 'breakfast'
break
2021-01-23 17:20:15 +00:00
case 5:
2021-03-21 17:02:04 +00:00
p.type = 'lunch'
break
2021-01-23 17:20:15 +00:00
case 10:
2021-03-21 17:02:04 +00:00
p.type = 'dinner'
break
2021-01-23 17:20:15 +00:00
case 15:
2021-03-21 17:02:04 +00:00
p.type = 'snacks'
break
2021-01-23 17:20:15 +00:00
}
delete p.startDate
delete p.endDate
delete p.eventColor
updatedMealPlans.push(p)
2021-03-21 17:02:04 +00:00
})
2021-01-23 17:20:15 +00:00
}
2021-03-21 17:02:04 +00:00
state.mealPlans = [...state.mealPlans, ...updatedMealPlans]
mealPlansDB.updateDocument('mealPlans', { mealPlans: state.mealPlans })
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
addMealPlan(state, { title, date, type, index }) {
2021-01-13 05:02:48 +00:00
let mealPlan = {
2021-01-23 17:20:15 +00:00
title,
date,
2021-03-21 17:02:04 +00:00
type,
2021-01-13 05:02:48 +00:00
}
2021-03-21 17:02:04 +00:00
if (index != null) state.mealPlans.splice(index, 0, mealPlan)
else state.mealPlans.push(mealPlan)
mealPlansDB.updateDocument('mealPlans', {
mealPlans: [...state.mealPlans],
})
},
deleteMealPlan(state, { title, date, type, index }) {
state.mealPlans.splice(index, 1)
state.mealPlans = [...state.mealPlans]
mealPlansDB.updateDocument('mealPlans', {
mealPlans: [...state.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) {
2021-03-21 17:02:04 +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
},
2021-04-01 10:55:35 +00:00
setComponent(state, comp) {
2021-03-21 17:02:04 +00:00
state.currentComponent = comp
2020-11-23 09:49:58 +00:00
},
2021-03-21 17:02:04 +00:00
unSyncCombinations(state, { id, combinations }) {
2020-11-15 10:51:10 +00:00
state.recipes.forEach((e, i) => {
if (combinations.includes(e.id)) {
2021-03-21 17:02:04 +00:00
state.recipes[i].combinations.splice(e.combinations.indexOf(id), 1)
EnRecipesDB.updateDocument(state.recipes[i].id, state.recipes[i])
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
})
2020-12-29 10:35:19 +00:00
},
setShake(state, shake) {
2021-03-21 17:02:04 +00:00
state.shakeEnabled = shake
2021-04-12 18:09:48 +00:00
ApplicationSettings.setBoolean('shakeEnabled', shake)
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
setRating(state, { id, recipe, rating }) {
let index = state.recipes.indexOf(
state.recipes.filter((e) => e.id === id)[0]
)
2020-12-29 10:35:19 +00:00
state.recipes[index].rating = rating
2021-03-21 17:02:04 +00:00
EnRecipesDB.updateDocument(id, recipe)
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
toggleCart(state, { id, recipe }) {
let index = state.recipes.indexOf(
state.recipes.filter((e) => e.id === id)[0]
)
2021-01-23 17:20:15 +00:00
state.recipes[index].inBag = !state.recipes[index].inBag
2021-03-21 17:02:04 +00:00
EnRecipesDB.updateDocument(id, recipe)
2021-01-13 05:02:48 +00:00
},
2020-12-29 10:35:19 +00:00
unlinkBrokenImages(state) {
state.recipes.forEach((r, i) => {
if (r.imageSrc && !File.exists(r.imageSrc)) {
r.imageSrc = null
2021-03-21 17:02:04 +00:00
Object.assign(state.recipes[i], r)
EnRecipesDB.updateDocument(r.id, r)
2020-11-15 10:51:10 +00:00
}
})
2021-03-21 17:02:04 +00:00
},
2021-04-01 10:55:35 +00:00
setCuisine(state, value) {
state.selectedCuisine = value
},
setCategory(state, value) {
state.selectedCategory = value
},
setTag(state, value) {
state.selectedTag = value
},
2020-09-15 11:10:16 +00:00
},
actions: {
2021-04-12 18:09:48 +00:00
clearImportSummary({ commit }) {
commit('clearImportSummary')
},
setFirstDay({ commit }, bool) {
commit('setFirstDay', bool)
},
2021-04-01 10:55:35 +00:00
setTheme({ commit }, theme) {
commit('setTheme', theme)
},
clearFilter({ commit }) {
commit('clearFilter')
},
2021-03-21 17:02:04 +00:00
setLayout({ commit }, type) {
commit('setLayout', type)
},
2021-04-12 18:09:48 +00:00
setSortType({ commit }, sortType) {
2021-03-21 17:02:04 +00:00
commit('setSortType', sortType)
},
2021-04-01 10:55:35 +00:00
initRecipes({ commit }) {
commit('initRecipes')
2021-03-21 17:02:04 +00:00
},
importRecipesAction({ commit }, recipes) {
commit('importRecipes', recipes)
},
addRecipeAction({ commit }, recipe) {
commit('addRecipe', recipe)
},
overwriteRecipeAction({ commit }, updatedRecipe) {
commit('overwriteRecipe', updatedRecipe)
},
deleteRecipeAction({ commit }, recipe) {
commit('deleteRecipe', recipe)
},
deleteRecipesAction({ commit }, ids) {
commit('deleteRecipes', ids)
},
2021-04-01 10:55:35 +00:00
initListItems({ commit }) {
commit('initListItems')
2021-03-21 17:02:04 +00:00
},
importListItemsAction({ commit }, data) {
commit('importListItems', data)
},
addListItemAction({ commit }, item) {
commit('addListItem', item)
},
removeListItemAction({ commit }, item) {
commit('removeListItem', item)
},
resetListItemsAction({ commit }, listName) {
commit('resetListItems', listName)
},
2021-04-01 10:55:35 +00:00
initMealPlans({ commit }) {
commit('initMealPlans')
2021-03-21 17:02:04 +00:00
},
importMealPlansAction({ commit }, mealPlans) {
commit('importMealPlans', mealPlans)
},
addMealPlanAction({ commit }, mealPlan) {
commit('addMealPlan', mealPlan)
},
deleteMealPlanAction({ commit }, mealPlan) {
commit('deleteMealPlan', mealPlan)
},
toggleStateAction({ commit }, toggledRecipe) {
commit('toggleState', toggledRecipe)
},
setRecipeAsTriedAction({ commit }, recipe) {
commit('setRecipeAsTried', recipe)
},
setLastTriedDateAction({ commit }, index) {
commit('setLastTriedDate', index)
},
2021-04-01 10:55:35 +00:00
setComponent({ commit }, comp) {
commit('setComponent', comp)
2021-03-21 17:02:04 +00:00
},
unSyncCombinationsAction({ commit }, combinations) {
commit('unSyncCombinations', combinations)
},
2021-04-12 18:09:48 +00:00
setShake({ commit }, shake) {
2021-03-21 17:02:04 +00:00
commit('setShake', shake)
},
setRatingAction({ commit }, rating) {
commit('setRating', rating)
},
toggleCartAction({ commit }, recipe) {
commit('toggleCart', recipe)
},
unlinkBrokenImages({ commit }) {
commit('unlinkBrokenImages')
},
2021-04-01 10:55:35 +00:00
setCuisine({ commit }, value) {
commit('setCuisine', value)
},
setCategory({ commit }, value) {
commit('setCategory', value)
},
setTag({ commit }, value) {
commit('setTag', value)
},
2021-03-21 17:02:04 +00:00
},
})