2019-01-12 14:09:17 +01:00
|
|
|
import React from "react";
|
2019-01-29 20:30:43 +01:00
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import withStyles from "@material-ui/core/styles/withStyles";
|
2019-01-12 14:09:17 +01:00
|
|
|
import { connect } from "react-redux";
|
2019-01-29 20:30:43 +01:00
|
|
|
import { ITEMS_CHANGED, USER_DATA_CHANGED } from "constants/actionTypes";
|
|
|
|
|
import { areObjectEqual } from "utils/helpers";
|
2019-01-12 14:09:17 +01:00
|
|
|
import { createOlxLink } from "utils/createOlxLink";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
2019-01-29 20:30:43 +01:00
|
|
|
import image from "assets/img/sidebar-1.jpg";
|
|
|
|
|
import logo from "assets/img/reactlogo.png";
|
2019-01-16 22:06:06 +01:00
|
|
|
|
2019-01-29 20:30:43 +01:00
|
|
|
import Sidebar from "components/Sidebar.js";
|
2019-01-12 14:09:17 +01:00
|
|
|
import ItemsContainer from "./items/itemscontainer/ItemsContainer";
|
2019-01-16 22:06:06 +01:00
|
|
|
import NotificationModal from "./NotificationModal";
|
2019-01-12 14:09:17 +01:00
|
|
|
|
2019-01-29 20:30:43 +01:00
|
|
|
import dashboardStyle from "assets/dashboardStyle.js";
|
2019-01-12 14:09:17 +01:00
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
|
return {
|
|
|
|
|
category: state.category,
|
|
|
|
|
options: state.options,
|
|
|
|
|
subcategory: state.subcategory,
|
2019-01-19 16:53:27 +01:00
|
|
|
items: state.items,
|
|
|
|
|
userdata: state.userdata
|
2019-01-12 14:09:17 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2019-01-19 16:53:27 +01:00
|
|
|
onItemsChanged: items => dispatch({ type: ITEMS_CHANGED, items }),
|
|
|
|
|
onUserDataChange: change => dispatch({ type: USER_DATA_CHANGED, ...change })
|
2019-01-12 14:09:17 +01:00
|
|
|
});
|
|
|
|
|
|
2019-01-14 22:41:53 +01:00
|
|
|
let lastUpdateTime = null;
|
|
|
|
|
let interval = null;
|
2019-01-12 14:09:17 +01:00
|
|
|
class App extends React.Component {
|
2019-01-14 22:41:53 +01:00
|
|
|
componentDidMount() {
|
|
|
|
|
interval = setInterval(() => {
|
|
|
|
|
if (lastUpdateTime && Date.now() - lastUpdateTime > 2000) {
|
2019-01-19 16:53:27 +01:00
|
|
|
const {
|
|
|
|
|
category,
|
|
|
|
|
options,
|
|
|
|
|
subcategory,
|
|
|
|
|
onItemsChanged,
|
|
|
|
|
onUserDataChange
|
|
|
|
|
} = this.props;
|
2019-01-14 22:41:53 +01:00
|
|
|
let url = createOlxLink(category, subcategory, options);
|
|
|
|
|
url = encodeURI(url);
|
2019-01-19 16:53:27 +01:00
|
|
|
onUserDataChange({ info: "olx_url", value: url });
|
2019-01-14 22:41:53 +01:00
|
|
|
if (url) {
|
|
|
|
|
axios
|
2019-01-30 14:35:19 +01:00
|
|
|
.get(`/items/${url}`)
|
2019-01-19 16:53:27 +01:00
|
|
|
.then(response => {
|
|
|
|
|
onItemsChanged(response.data.items);
|
|
|
|
|
onUserDataChange({
|
|
|
|
|
info: "last_date",
|
|
|
|
|
value: response.data.last_date
|
|
|
|
|
});
|
|
|
|
|
})
|
2019-01-14 22:41:53 +01:00
|
|
|
.catch(error => console.log(error));
|
|
|
|
|
}
|
|
|
|
|
lastUpdateTime = null;
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
clearInterval(interval);
|
|
|
|
|
}
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
|
const { subcategory, category, options } = this.props;
|
|
|
|
|
if (
|
|
|
|
|
newProps.subcategory !== subcategory ||
|
|
|
|
|
newProps.category !== category ||
|
|
|
|
|
!areObjectEqual(newProps.options, options)
|
|
|
|
|
) {
|
|
|
|
|
lastUpdateTime = Date.now();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 14:09:17 +01:00
|
|
|
|
|
|
|
|
render() {
|
2019-01-29 20:30:43 +01:00
|
|
|
const { items, classes } = this.props;
|
2019-01-12 14:09:17 +01:00
|
|
|
|
|
|
|
|
return (
|
2019-01-29 20:30:43 +01:00
|
|
|
<div className={classes.wrapper}>
|
|
|
|
|
<Sidebar logoText={"Market Alarm"} logo={logo} image={image} />
|
|
|
|
|
<div className={classes.mainPanel}>
|
2019-03-07 12:49:19 -08:00
|
|
|
{items.length && <h3 className={classes.itemsCountTitle}>Pronađeno {items.length} nekretnina. Napravite notifikaciju i primite vise detalja na vas emailu adresu.</h3>}
|
2019-01-29 20:30:43 +01:00
|
|
|
{items.length ? <NotificationModal /> : null}
|
|
|
|
|
</div>
|
2019-01-12 14:09:17 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-29 20:30:43 +01:00
|
|
|
App.propTypes = {
|
|
|
|
|
classes: PropTypes.object.isRequired
|
|
|
|
|
};
|
2019-01-12 14:09:17 +01:00
|
|
|
|
2019-01-29 20:30:43 +01:00
|
|
|
export default withStyles(dashboardStyle)(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)(App)
|
|
|
|
|
);
|