Commit 2c2f45d8 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza
Browse files

Code cleanup in serverStorage

parent c6bc432a
......@@ -38,7 +38,7 @@ import ApiError from 'model/ApiError';
import * as path from 'path';
import {readFileSync} from 'fs';
import { serverStorage } from './serverStorage';
import { receiveServerStorage } from 'actions/serverStorage';
import { initFromServerStorage } from 'actions/serverStorage';
const reactLoadableStats = JSON.parse(readFileSync(path.join('./', 'react-loadable.json'), {encoding: 'utf8'}));
// console.log(`react-loadable stats: `, reactLoadableStats);
......@@ -51,7 +51,7 @@ const prerenderer = (html, errHtml) => (req, res) => {
const store = createStore(rootReducer, {} as any, applyMiddleware(thunk, routerMiddleware(createMemoryHistory)));
// Only send public data to client
store.dispatch(configure({ frontendPath: config.frontendPath, apiUrl: config.apiUrl, googleClientId: config.googleClientId, clientId: config.clientId }));
receiveServerStorage(serverStorage, store.dispatch);
initFromServerStorage(serverStorage, store.dispatch);
console.log(`Processing request`, req._parsedOriginalUrl);
const pathname = req._parsedOriginalUrl.pathname;
......
// constants
import { RECEIVE_CROPS } from 'crop/constants';
import { GET_SERVER_INFO } from 'constants/serverInfo';
// model
import ApiCall from 'model/ApiCall';
// service
import { CropService } from 'service/CropService';
import ApiInfoService from 'service/genesys/ApiInfoService';
const REFRESH_TIME = 15 * 60 * 1000;
export const serverStorage = {
crops: [],
serverInfo: {},
crops: {
actionType: RECEIVE_CROPS,
data: null,
},
serverInfo: {
actionType: GET_SERVER_INFO,
data: null,
},
};
const refreshCrops = () => {
CropService.listCrops()
.then((crops) => {
serverStorage.crops = crops;
setTimeout(refreshCrops, REFRESH_TIME);
});
.then((crops) => serverStorage.crops.data = {apiCall: ApiCall.success(crops)});
};
const refreshServerInfo = () => {
ApiInfoService.apiInfo()
.then((serverInfo) => {
serverStorage.serverInfo = serverInfo;
setTimeout(refreshServerInfo, REFRESH_TIME);
});
.then((serverInfo) => serverStorage.serverInfo.data = {info: serverInfo});
};
export const refreshServerStorage = () => {
refreshCrops();
refreshServerInfo();
setTimeout(refreshServerStorage, REFRESH_TIME);
};
import { RECEIVE_CROPS } from '../crop/constants';
import ApiCall from '../model/ApiCall';
import { GET_SERVER_INFO } from '../constants/serverInfo';
// TODO rename
export const receiveServerStorage = (serverStorage, dispatch) => {
console.log('ServerStorage: ', serverStorage);
dispatch({type: RECEIVE_CROPS, payload: {apiCall: ApiCall.success(serverStorage.crops)}});
dispatch({type: GET_SERVER_INFO, payload: {info: serverStorage.serverInfo}});
export const initFromServerStorage = (serverStorage, dispatch) => {
// console.log('ServerStorage: ', serverStorage);
Object.keys(serverStorage).forEach((key) => dispatch({type: serverStorage[key].actionType, payload: serverStorage[key].data }));
};
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