Files
old-kivi/web/src/index.js

85 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-11-06 14:38:47 +01:00
import React from 'react';
import {render} from 'react-dom';
import Main from './components/Main';
import Welcome from './components/Welcome';
2017-04-11 10:43:05 +02:00
const getInitialState = url => {
2017-11-06 14:38:47 +01:00
const params = window.location.search.substr (1).split ('&');
2017-04-11 10:43:05 +02:00
const initialState = {
rooms: {},
2017-11-06 14:38:47 +01:00
category: {},
};
2017-04-11 10:43:05 +02:00
for (const param of params) {
2017-11-06 14:38:47 +01:00
const [key, value] = param.split ('=');
2017-04-11 10:43:05 +02:00
if (key === 'rooms' && value !== '') {
2017-11-06 14:38:47 +01:00
initialState.rooms = {};
value.split (',').forEach (k => {
initialState.rooms[parseInt (k)] = true;
});
2017-04-11 10:43:05 +02:00
}
if (key === 'category' && value !== '') {
2017-11-06 14:38:47 +01:00
initialState.category = {};
value.split (',').forEach (k => {
initialState.category[parseInt (k)] = true;
});
2017-04-11 10:43:05 +02:00
}
if (key === 'sort') {
2017-11-06 14:38:47 +01:00
initialState.sort = value;
2017-04-11 10:43:05 +02:00
}
if (key === 'bounds') {
2017-11-06 14:38:47 +01:00
initialState.bounds = value;
2017-04-11 10:43:05 +02:00
}
if (key === 'listingId') {
2017-11-06 14:38:47 +01:00
initialState.listingId = value;
2017-04-11 10:43:05 +02:00
}
2017-11-06 14:38:47 +01:00
if (key === 'adType') {
initialState.adType = value;
2017-04-21 01:55:13 +02:00
}
2017-04-11 10:43:05 +02:00
if (key === 'zoom') {
2017-11-06 14:38:47 +01:00
initialState.zoom = parseInt (value);
2017-04-11 10:43:05 +02:00
}
2017-11-06 14:38:47 +01:00
if (['minSize', 'maxSize', 'minPrice', 'maxPrice'].includes (key)) {
initialState[key] = parseFloat (value);
2017-04-11 10:43:05 +02:00
}
}
2017-11-06 14:38:47 +01:00
return initialState;
};
2017-04-11 10:43:05 +02:00
2017-11-06 14:38:47 +01:00
const root = document.getElementById ('root');
const initialState = getInitialState (window.location);
2017-04-21 01:55:13 +02:00
const renderMain = (additionalState = {}) => {
2017-11-06 14:38:47 +01:00
const main = <Main initialState={{...initialState, ...additionalState}} />;
render (main, root);
};
2017-04-11 10:43:05 +02:00
2017-11-06 14:38:47 +01:00
//renderMain ();
2017-06-08 12:22:48 +02:00
// disable temp
2017-11-06 14:38:47 +01:00
if (
Object.keys (initialState).length === 2 &&
window.localStorage.getItem ('lastLoad') == null
) {
const onSearch = ({adType}) => {
console.log("onSearch()");
//window.location = `/?adType=${adType}`;
renderMain({adType})
};
const welcome = <Welcome onSearch={onSearch} />;
render (welcome, root);
2017-04-21 01:55:13 +02:00
} else {
2017-11-06 14:38:47 +01:00
renderMain ();
2017-04-21 01:55:13 +02:00
}