aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/FeaturesStore.ts (renamed from src/stores/FeaturesStore.js)15
-rw-r--r--src/stores/GlobalErrorStore.ts (renamed from src/stores/GlobalErrorStore.js)67
-rw-r--r--src/stores/lib/TypedStore.ts6
3 files changed, 61 insertions, 27 deletions
diff --git a/src/stores/FeaturesStore.js b/src/stores/FeaturesStore.ts
index e2c0c08ef..b63c252df 100644
--- a/src/stores/FeaturesStore.js
+++ b/src/stores/FeaturesStore.ts
@@ -1,8 +1,6 @@
1import { computed, observable, runInAction } from 'mobx'; 1import { computed, observable, runInAction } from 'mobx';
2 2
3import Store from './lib/Store';
4import CachedRequest from './lib/CachedRequest'; 3import CachedRequest from './lib/CachedRequest';
5
6import serviceProxy from '../features/serviceProxy'; 4import serviceProxy from '../features/serviceProxy';
7import basicAuth from '../features/basicAuth'; 5import basicAuth from '../features/basicAuth';
8import workspaces from '../features/workspaces'; 6import workspaces from '../features/workspaces';
@@ -11,8 +9,9 @@ import publishDebugInfo from '../features/publishDebugInfo';
11import communityRecipes from '../features/communityRecipes'; 9import communityRecipes from '../features/communityRecipes';
12import todos from '../features/todos'; 10import todos from '../features/todos';
13import appearance from '../features/appearance'; 11import appearance from '../features/appearance';
12import TypedStore from './lib/TypedStore';
14 13
15export default class FeaturesStore extends Store { 14export default class FeaturesStore extends TypedStore {
16 @observable defaultFeaturesRequest = new CachedRequest( 15 @observable defaultFeaturesRequest = new CachedRequest(
17 this.api.features, 16 this.api.features,
18 'default', 17 'default',
@@ -25,7 +24,7 @@ export default class FeaturesStore extends Store {
25 24
26 @observable features = {}; 25 @observable features = {};
27 26
28 async setup() { 27 async setup(): Promise<void> {
29 this.registerReactions([ 28 this.registerReactions([
30 this._updateFeatures, 29 this._updateFeatures,
31 this._monitorLoginStatus.bind(this), 30 this._monitorLoginStatus.bind(this),
@@ -35,11 +34,11 @@ export default class FeaturesStore extends Store {
35 setTimeout(this._setupFeatures.bind(this), 1); 34 setTimeout(this._setupFeatures.bind(this), 1);
36 } 35 }
37 36
38 @computed get anonymousFeatures() { 37 @computed get anonymousFeatures(): any {
39 return this.defaultFeaturesRequest.execute().result || {}; 38 return this.defaultFeaturesRequest.execute().result || {};
40 } 39 }
41 40
42 _updateFeatures = () => { 41 _updateFeatures = (): void => {
43 const features = {}; 42 const features = {};
44 if (this.stores.user.isLoggedIn) { 43 if (this.stores.user.isLoggedIn) {
45 let requestResult = {}; 44 let requestResult = {};
@@ -55,7 +54,7 @@ export default class FeaturesStore extends Store {
55 }); 54 });
56 }; 55 };
57 56
58 _monitorLoginStatus() { 57 _monitorLoginStatus(): void {
59 if (this.stores.user.isLoggedIn) { 58 if (this.stores.user.isLoggedIn) {
60 this.featuresRequest.invalidate({ immediately: true }); 59 this.featuresRequest.invalidate({ immediately: true });
61 } else { 60 } else {
@@ -64,7 +63,7 @@ export default class FeaturesStore extends Store {
64 } 63 }
65 } 64 }
66 65
67 _setupFeatures() { 66 _setupFeatures(): void {
68 serviceProxy(this.stores); 67 serviceProxy(this.stores);
69 basicAuth(); 68 basicAuth();
70 workspaces(this.stores, this.actions); 69 workspaces(this.stores, this.actions);
diff --git a/src/stores/GlobalErrorStore.js b/src/stores/GlobalErrorStore.ts
index 356a6f298..cb364574b 100644
--- a/src/stores/GlobalErrorStore.js
+++ b/src/stores/GlobalErrorStore.ts
@@ -1,35 +1,61 @@
1import { observable, action } from 'mobx'; 1import { observable, action } from 'mobx';
2import Store from './lib/Store'; 2import { Actions } from 'src/actions/lib/actions';
3import { ApiInterface } from 'src/api';
4import { Stores } from 'src/stores.types';
3import Request from './lib/Request'; 5import Request from './lib/Request';
6import TypedStore from './lib/TypedStore';
4 7
5export default class GlobalErrorStore extends Store { 8interface Message {
6 @observable error = null; 9 type: 'error' | 'log' | 'info';
10 error?: {
11 message?: string;
12 status?: number;
13 };
14 request?: {
15 result: any;
16 wasExecuted: any;
17 method: any;
18 };
19 response?: any;
20 server?: any;
21 info?: any;
22 url?: string;
23 line?: number;
24}
25
26export default class GlobalErrorStore extends TypedStore {
27 @observable error: any | null = null;
7 28
8 @observable messages = []; 29 @observable messages: Message[] = [];
9 30
10 @observable response = {}; 31 @observable response: object = {};
11 32
12 constructor(...args) { 33 // TODO: Get rid of the @ts-ignores in this function.
13 super(...args); 34 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
35 super(stores, api, actions);
14 36
15 window.addEventListener('error', (...errorArgs) => { 37 window.addEventListener('error', (...errorArgs: any[]): void => {
38 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
16 this._handleConsoleError.call(this, ['error', ...errorArgs]); 39 this._handleConsoleError.call(this, ['error', ...errorArgs]);
17 }); 40 });
18 41
19 const origConsoleError = console.error; 42 const origConsoleError = console.error;
20 window.console.error = (...errorArgs) => { 43 window.console.error = (...errorArgs: any[]) => {
44 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
21 this._handleConsoleError.call(this, ['error', ...errorArgs]); 45 this._handleConsoleError.call(this, ['error', ...errorArgs]);
22 origConsoleError.apply(this, errorArgs); 46 origConsoleError.apply(this, errorArgs);
23 }; 47 };
24 48
25 const origConsoleLog = console.log; 49 const origConsoleLog = console.log;
26 window.console.log = (...logArgs) => { 50 window.console.log = (...logArgs: any[]) => {
51 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
27 this._handleConsoleError.call(this, ['log', ...logArgs]); 52 this._handleConsoleError.call(this, ['log', ...logArgs]);
28 origConsoleLog.apply(this, logArgs); 53 origConsoleLog.apply(this, logArgs);
29 }; 54 };
30 55
31 const origConsoleInfo = console.info; 56 const origConsoleInfo = console.info;
32 window.console.info = (...infoArgs) => { 57 window.console.info = (...infoArgs: any[]) => {
58 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
33 this._handleConsoleError.call(this, ['info', ...infoArgs]); 59 this._handleConsoleError.call(this, ['info', ...infoArgs]);
34 origConsoleInfo.apply(this, infoArgs); 60 origConsoleInfo.apply(this, infoArgs);
35 }; 61 };
@@ -37,7 +63,11 @@ export default class GlobalErrorStore extends Store {
37 Request.registerHook(this._handleRequests); 63 Request.registerHook(this._handleRequests);
38 } 64 }
39 65
40 _handleConsoleError(type, error, url, line) { 66 async setup(): Promise<void> {
67 // Not implemented
68 }
69
70 _handleConsoleError(type: any, error: any, url: string, line: number) {
41 if (typeof type === 'object' && type.length > 0) { 71 if (typeof type === 'object' && type.length > 0) {
42 this.messages.push({ 72 this.messages.push({
43 type: type[0], 73 type: type[0],
@@ -53,7 +83,13 @@ export default class GlobalErrorStore extends Store {
53 } 83 }
54 } 84 }
55 85
56 _handleRequests = action(async request => { 86 @action _handleRequests = async (request: {
87 isError: any;
88 error: { json: () => object | PromiseLike<object> };
89 result: any;
90 wasExecuted: any;
91 _method: any;
92 }): Promise<void> => {
57 if (request.isError) { 93 if (request.isError) {
58 this.error = request.error; 94 this.error = request.error;
59 95
@@ -63,9 +99,8 @@ export default class GlobalErrorStore extends Store {
63 } catch { 99 } catch {
64 this.response = {}; 100 this.response = {};
65 } 101 }
66 if (this.error.status === 401) { 102 if (this.error?.status === 401) {
67 window['ferdium'].stores.app.authRequestFailed = true; 103 window['ferdium'].stores.app.authRequestFailed = true;
68 // this.actions.user.logout({ serverLogout: true });
69 } 104 }
70 } 105 }
71 106
@@ -83,5 +118,5 @@ export default class GlobalErrorStore extends Store {
83 } else { 118 } else {
84 window['ferdium'].stores.app.authRequestFailed = false; 119 window['ferdium'].stores.app.authRequestFailed = false;
85 } 120 }
86 }); 121 };
87} 122}
diff --git a/src/stores/lib/TypedStore.ts b/src/stores/lib/TypedStore.ts
index 5d8bf3bbd..7f9d2d60f 100644
--- a/src/stores/lib/TypedStore.ts
+++ b/src/stores/lib/TypedStore.ts
@@ -18,9 +18,9 @@ export default abstract class TypedStore {
18 } 18 }
19 19
20 constructor( 20 constructor(
21 public stores: Stores, 21 public readonly stores: Stores,
22 public api: ApiInterface, 22 public readonly api: ApiInterface,
23 public actions: Actions, 23 public readonly actions: Actions,
24 ) {} 24 ) {}
25 25
26 registerReactions(reactions: { (r: IReactionPublic): void }[]): void { 26 registerReactions(reactions: { (r: IReactionPublic): void }[]): void {