From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/stores/RecipesStore.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/stores/RecipesStore.js (limited to 'src/stores/RecipesStore.js') diff --git a/src/stores/RecipesStore.js b/src/stores/RecipesStore.js new file mode 100644 index 000000000..cdc274685 --- /dev/null +++ b/src/stores/RecipesStore.js @@ -0,0 +1,96 @@ +import { action, computed, observable } from 'mobx'; + +import Store from './lib/Store'; +import CachedRequest from './lib/CachedRequest'; +import Request from './lib/Request'; +import { matchRoute } from '../helpers/routing-helpers'; + +export default class RecipesStore extends Store { + @observable allRecipesRequest = new CachedRequest(this.api.recipes, 'all'); + @observable installRecipeRequest = new Request(this.api.recipes, 'install'); + @observable getRecipeUpdatesRequest = new Request(this.api.recipes, 'update'); + + constructor(...args) { + super(...args); + + // Register action handlers + this.actions.recipe.install.listen(this._install.bind(this)); + this.actions.recipe.update.listen(this._update.bind(this)); + } + + setup() { + return this.all; + } + + @computed get all() { + return this.allRecipesRequest.execute().result || []; + } + + @computed get active() { + const match = matchRoute('/settings/services/add/:id', this.stores.router.location.pathname); + if (match) { + const activeRecipe = this.one(match.id); + if (activeRecipe) { + return activeRecipe; + } + + console.warn('Recipe not installed'); + } + + return null; + } + + @computed get recipeIdForServices() { + return this.stores.services.all.map(s => s.recipe.id); + } + + one(id) { + return this.all.find(recipe => recipe.id === id); + } + + isInstalled(id) { + return !!this.one(id); + } + + // Actions + @action async _install({ recipeId }) { + // console.log(this.installRecipeRequest._promise); + const recipe = await this.installRecipeRequest.execute(recipeId)._promise; + await this.allRecipesRequest.invalidate({ immediately: true })._promise; + // console.log(this.installRecipeRequest._promise); + + return recipe; + } + + @action async _update() { + const recipeIds = this.recipeIdForServices; + const recipes = {}; + recipeIds.forEach((r) => { + const recipe = this.one(r); + recipes[r] = recipe.version; + }); + + if (Object.keys(recipes).length === 0) return; + + const updates = await this.getRecipeUpdatesRequest.execute(recipes)._promise; + const length = updates.length - 1; + const syncUpdate = async (i) => { + const update = updates[i]; + + this.actions.recipe.install({ recipeId: update }); + await this.installRecipeRequest._promise; + + this.installRecipeRequest.reset(); + + if (i === length) { + this.stores.ui.showServicesUpdatedInfoBar = true; + } else if (i < length) { + syncUpdate(i + 1); + } + }; + + if (length >= 0) { + syncUpdate(0); + } + } +} -- cgit v1.2.3-54-g00ecf