aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/RecipesStore.js
diff options
context:
space:
mode:
authorLibravatar Stefan Malzner <stefan@adlk.io>2017-10-13 12:29:40 +0200
committerLibravatar Stefan Malzner <stefan@adlk.io>2017-10-13 12:29:40 +0200
commit58cda9cc7fb79ca9df6746de7f9662bc08dc156a (patch)
tree1211600c2a5d3b5f81c435c6896618111a611720 /src/stores/RecipesStore.js
downloadferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.gz
ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.zst
ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.zip
initial commit
Diffstat (limited to 'src/stores/RecipesStore.js')
-rw-r--r--src/stores/RecipesStore.js96
1 files changed, 96 insertions, 0 deletions
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 @@
1import { action, computed, observable } from 'mobx';
2
3import Store from './lib/Store';
4import CachedRequest from './lib/CachedRequest';
5import Request from './lib/Request';
6import { matchRoute } from '../helpers/routing-helpers';
7
8export default class RecipesStore extends Store {
9 @observable allRecipesRequest = new CachedRequest(this.api.recipes, 'all');
10 @observable installRecipeRequest = new Request(this.api.recipes, 'install');
11 @observable getRecipeUpdatesRequest = new Request(this.api.recipes, 'update');
12
13 constructor(...args) {
14 super(...args);
15
16 // Register action handlers
17 this.actions.recipe.install.listen(this._install.bind(this));
18 this.actions.recipe.update.listen(this._update.bind(this));
19 }
20
21 setup() {
22 return this.all;
23 }
24
25 @computed get all() {
26 return this.allRecipesRequest.execute().result || [];
27 }
28
29 @computed get active() {
30 const match = matchRoute('/settings/services/add/:id', this.stores.router.location.pathname);
31 if (match) {
32 const activeRecipe = this.one(match.id);
33 if (activeRecipe) {
34 return activeRecipe;
35 }
36
37 console.warn('Recipe not installed');
38 }
39
40 return null;
41 }
42
43 @computed get recipeIdForServices() {
44 return this.stores.services.all.map(s => s.recipe.id);
45 }
46
47 one(id) {
48 return this.all.find(recipe => recipe.id === id);
49 }
50
51 isInstalled(id) {
52 return !!this.one(id);
53 }
54
55 // Actions
56 @action async _install({ recipeId }) {
57 // console.log(this.installRecipeRequest._promise);
58 const recipe = await this.installRecipeRequest.execute(recipeId)._promise;
59 await this.allRecipesRequest.invalidate({ immediately: true })._promise;
60 // console.log(this.installRecipeRequest._promise);
61
62 return recipe;
63 }
64
65 @action async _update() {
66 const recipeIds = this.recipeIdForServices;
67 const recipes = {};
68 recipeIds.forEach((r) => {
69 const recipe = this.one(r);
70 recipes[r] = recipe.version;
71 });
72
73 if (Object.keys(recipes).length === 0) return;
74
75 const updates = await this.getRecipeUpdatesRequest.execute(recipes)._promise;
76 const length = updates.length - 1;
77 const syncUpdate = async (i) => {
78 const update = updates[i];
79
80 this.actions.recipe.install({ recipeId: update });
81 await this.installRecipeRequest._promise;
82
83 this.installRecipeRequest.reset();
84
85 if (i === length) {
86 this.stores.ui.showServicesUpdatedInfoBar = true;
87 } else if (i < length) {
88 syncUpdate(i + 1);
89 }
90 };
91
92 if (length >= 0) {
93 syncUpdate(0);
94 }
95 }
96}