Commit 556a73e9 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza
Browse files

Ensure that all serverStorage data loaded before app startup

parent cd279a7b
......@@ -15,24 +15,25 @@ const PORT = process.env.PORT || 3000;
configureBackendApi({ apiUrl: `http://localhost:${PORT}/proxy` });
const refreshAppToken = () => {
LoginService.loginApp(config.clientId).then((token) => {
return LoginService.loginApp(config.clientId, config.clientSecret, config.apiUrl).then(async (token) => {
console.log('Received token: \n', token);
configureBackendApi({ accessToken: token.access_token });
const refreshIn = (token.expires_in - 30) * 1000;
console.log(`Scheduling app token refresh in ${refreshIn}s`);
config.access_token = token;
setTimeout(refreshAppToken, refreshIn);
refreshServerStorage();
await refreshServerStorage();
}).catch((axiosError) => {
console.log(`Error refreshing app token: ${axiosError.response.statusText}`, axiosError.response);
});
};
refreshAppToken();
// start server on PORT
Loadable.preloadAll().then(() => {
server.listen(PORT, () => {
console.log('HTTP server listening on: ' + PORT);
});
});
refreshAppToken()
.then(() => {
// start server on PORT
Loadable.preloadAll().then(() => {
server.listen(PORT, () => {
console.log('HTTP server listening on: ' + PORT);
});
});
});
// constants
import 'babel-polyfill';
import { RECEIVE_CROPS } from 'crop/constants';
import { GET_SERVER_INFO } from 'constants/serverInfo';
// model
......@@ -7,6 +8,9 @@ import ApiCall from 'model/ApiCall';
import { CropService } from 'service/CropService';
import ApiInfoService from 'service/genesys/ApiInfoService';
// server
import config from '../config';
const REFRESH_TIME = 15 * 60 * 1000;
export const serverStorage = {
......@@ -20,19 +24,31 @@ export const serverStorage = {
},
};
const directXhrConfig = {
baseURL: config.apiUrl,
};
const refreshCrops = () => {
CropService.listCrops()
.then((crops) => serverStorage.crops.data = {apiCall: ApiCall.success(crops)});
const start = Date.now();
return CropService.listCrops(directXhrConfig)
.then((crops) => {
console.log(`Crops fetched in ${Date.now() - start}ms`);
serverStorage.crops.data = {apiCall: ApiCall.success(crops)};
});
};
const refreshServerInfo = () => {
ApiInfoService.apiInfo()
.then((serverInfo) => serverStorage.serverInfo.data = {info: serverInfo});
const start = Date.now();
return ApiInfoService.apiInfo(directXhrConfig)
.then((serverInfo) => {
console.log(`Server info fetched in ${ Date.now() - start }ms`);
serverStorage.serverInfo.data = {info: serverInfo};
});
};
export const refreshServerStorage = () => {
refreshCrops();
refreshServerInfo();
export const refreshServerStorage = async () => {
await refreshCrops();
await refreshServerInfo();
console.log('Server storage refresh done');
setTimeout(refreshServerStorage, REFRESH_TIME);
};
......@@ -39,7 +39,7 @@ export const createApiCaller = (method, payloadType: string, config: {direct?: b
xhrConfig.baseURL = getState().applicationConfig.apiUrl;
}
if (config.direct) {
if (config.withoutTimeout) {
xhrConfig.timeout = 0;
}
......@@ -95,7 +95,7 @@ export const createPureApiCaller = (method, config: {direct?: boolean, withoutTi
xhrConfig.baseURL = getState().applicationConfig.apiUrl;
}
if (config.direct) {
if (config.withoutTimeout) {
xhrConfig.timeout = 0;
}
......
......@@ -14,9 +14,9 @@ export const VERIFY_GOOGLE_TOKEN_URL = `/api/google/verify-token`;
export class LoginService {
public static loginApp(clientId: string, clientSecret?: string) {
public static loginApp(clientId: string, clientSecret?: string, baseURL?: string) {
return axiosBackend.post(LOGIN_URL, null, {
// baseURL: '/proxy',
baseURL: baseURL || '/proxy',
params: {
grant_type: 'client_credentials',
client_id: clientId,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment