/
EventListHeader.js
151 lines (141 loc) · 4.28 KB
/
EventListHeader.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { If, Else } from 'react-if';
import PlacesAutocomplete, { geocodeByAddress } from 'react-places-autocomplete';
import {
LocationIcon,
BackIcon,
CollapseIcon,
ExpandIcon,
} from '../../components/common/Icons';
import { selectors } from '../../reducers/events';
import {
selectors as appSelectors,
} from '../../reducers/app';
class EventListHeader extends Component {
static propTypes = {
onMinimizeClick: PropTypes.func.isRequired,
eventTitle: PropTypes.string,
updateSearchResultViewport: PropTypes.func.isRequired,
history: PropTypes.shape({
push: PropTypes.func.isRequired,
}).isRequired,
listState: PropTypes.bool,
router: PropTypes.shape({
pathname: PropTypes.string,
search: PropTypes.string,
hash: PropTypes.string,
key: PropTypes.string,
action: PropTypes.string,
}).isRequired,
};
static defaultProps = {
eventId: '',
eventTitle: '',
listState: false,
};
state = {
searchString: '',
}
handleChange = (searchString) => {
this.setState({ searchString });
}
handleSelect = (address) => {
geocodeByAddress(address)
.then(results => this.props.updateSearchResultViewport(results[0].geometry.viewport))
.catch(error => console.error('Error', error));
}
render() {
const {
onMinimizeClick,
eventTitle,
history,
listState,
router,
} = this.props;
const ifListDisplayed =
!!router.pathname.split('/')[2] || !!window.location.pathname.split('/')[2];
/*
define if pathname contains event ID and then decide if render
search bar or event title (in last case we take it from store)
*/
return (
<div className="EventsList-header">
<If condition={ifListDisplayed}>
<button
className="EventsList-header-back"
onClick={() => history.goBack()}
>
<BackIcon />
</button>
<Else>
<LocationIcon />
</Else>
</If>
<If condition={ifListDisplayed}>
<span className="EventsList-header-title">
{eventTitle}
</span>
<Else>
<PlacesAutocomplete
value={this.state.searchString}
onChange={this.handleChange}
onSelect={this.handleSelect}
>
{
({ getInputProps, suggestions, getSuggestionItemProps }) => (
<div className="EventsList-header-searchbar-container">
<input
{
...getInputProps({
className: 'EventsList-header-searchbar',
type: 'text',
placeholder: 'Search location',
})
}
/>
<div className="EventsList-header-suggestions-block">
{
suggestions.map(sugg => {
return (
<div
{...getSuggestionItemProps(sugg)}
key={sugg.id}
className="EventsList-header-suggestion"
>
<span className="EventsList-header-suggestion-txt">
{sugg.description}
</span>
</div>
)
})
}
</div>
</div>
)
}
</PlacesAutocomplete>
</Else>
</If>
<div
role="button"
className="EventsList-header-minimize"
onClick={onMinimizeClick}
>
{
listState ?
<CollapseIcon /> :
<ExpandIcon />
}
</div>
</div>
);
}
}
const mapStateToProps = (state) => ({
eventTitle: selectors.getEventTitle(state),
listState: selectors.getShowEventWindow(state),
router: appSelectors.getRouterInfo(state),
});
export default connect(mapStateToProps)(EventListHeader);