-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathServerStatus.tsx
71 lines (67 loc) · 2.46 KB
/
ServerStatus.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Redux from 'redux';
import * as semver from 'semver';
import {handleFetchErrors} from 'shared/requests';
import {VERSION} from 'shared/schema/Constants';
import {AUTH_SETTINGS, URLS} from '../Constants';
import {getDevicePlatform} from '../Globals';
import {logEvent} from '../Logging';
import {ServerStatusState} from '../reducers/StateTypes';
import {FetchServerStatusResponse, ServerStatusSetAction} from './ActionTypes';
const fetchRetry = require('fetch-retry');
export function fetchServerStatus(log: any = logEvent, fetch: any = window.fetch) {
return (dispatch: Redux.Dispatch<any>): any => {
return fetchRetry(fetch)(AUTH_SETTINGS.URL_BASE + '/announcements', {
retries: 2,
retryDelay: 1000,
})
.then(handleFetchErrors)
.then((response: Response) => response.json())
.then((data: FetchServerStatusResponse) => {
dispatch(handleServerStatus(data));
}).catch((error: Error) => {
dispatch(setServerStatus({
announcement: {
open: true,
message: 'Please try again in a few minutes. If the issue persists, you can contact support at contact@fabricate.io',
},
isLatestAppVersion: false,
serverOffline: true,
}));
log('error', 'status_fetch_err', {label: error});
});
};
}
function handleServerStatus(data: FetchServerStatusResponse) {
return (dispatch: Redux.Dispatch<any>): any => {
const newVersion = data.versions[getDevicePlatform()];
const oldVersion = VERSION;
const isLatestAppVersion = semver.valid(newVersion) && semver.valid(oldVersion) && semver.lte(newVersion, oldVersion) || false;
if (!isLatestAppVersion) {
console.warn('Version mismatch with server:', oldVersion, 'local vs server', newVersion);
}
if (data.message !== null && data.message !== '') {
dispatch(setServerStatus({
announcement: {
open: true,
message: data.message,
link: data.link,
},
isLatestAppVersion,
}));
} else if (!isLatestAppVersion) {
dispatch(setServerStatus({
announcement: {
open: true,
message: 'New version available, click here to upgrade',
link: URLS[getDevicePlatform()],
},
isLatestAppVersion,
}));
} else {
dispatch(setServerStatus({isLatestAppVersion}));
}
};
}
export function setServerStatus(delta: Partial<ServerStatusState>): ServerStatusSetAction {
return {type: 'SERVER_STATUS_SET', delta};
}