verdnatura-chat/app/views/RoomsListView/Header/index.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
toggleServerDropdown, closeServerDropdown, closeSortDropdown, setSearch as setSearchAction
} from '../../../actions/rooms';
import Header from './Header';
2019-12-04 16:39:53 +00:00
import { withTheme } from '../../../theme';
2019-11-25 20:01:17 +00:00
import EventEmitter from '../../../utils/events';
import { KEY_COMMAND, handleCommandOpenServerDropdown } from '../../../commands';
import { isTablet } from '../../../utils/deviceInfo';
class RoomsListHeaderView extends PureComponent {
static propTypes = {
showServerDropdown: PropTypes.bool,
showSortDropdown: PropTypes.bool,
showSearchHeader: PropTypes.bool,
serverName: PropTypes.string,
connecting: PropTypes.bool,
isFetching: PropTypes.bool,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
open: PropTypes.func,
close: PropTypes.func,
closeSort: PropTypes.func,
2019-03-12 16:23:06 +00:00
setSearch: PropTypes.func
}
2019-11-25 20:01:17 +00:00
componentDidMount() {
if (isTablet) {
EventEmitter.addEventListener(KEY_COMMAND, this.handleCommands);
}
}
componentWillUnmount() {
if (isTablet) {
EventEmitter.removeListener(KEY_COMMAND, this.handleCommands);
}
}
2020-02-13 19:24:39 +00:00
// eslint-disable-next-line react/sort-comp
2019-11-25 20:01:17 +00:00
handleCommands = ({ event }) => {
if (handleCommandOpenServerDropdown(event)) {
this.onPress();
}
}
onSearchChangeText = (text) => {
const { setSearch } = this.props;
setSearch(text.trim());
}
onPress = () => {
const {
showServerDropdown, showSortDropdown, close, open, closeSort
} = this.props;
if (showServerDropdown) {
close();
} else if (showSortDropdown) {
closeSort();
setTimeout(() => {
open();
}, 300);
} else {
open();
}
}
render() {
const {
2019-12-04 16:39:53 +00:00
serverName, showServerDropdown, showSearchHeader, connecting, isFetching, theme
} = this.props;
return (
<Header
2019-12-04 16:39:53 +00:00
theme={theme}
serverName={serverName}
showServerDropdown={showServerDropdown}
showSearchHeader={showSearchHeader}
connecting={connecting}
isFetching={isFetching}
onPress={this.onPress}
2020-02-13 19:24:39 +00:00
onSearchChangeText={this.onSearchChangeText}
/>
);
}
}
const mapStateToProps = state => ({
showServerDropdown: state.rooms.showServerDropdown,
showSortDropdown: state.rooms.showSortDropdown,
showSearchHeader: state.rooms.showSearchHeader,
connecting: state.meteor.connecting || state.server.loading,
isFetching: state.rooms.isFetching,
serverName: state.settings.Site_Name
});
const mapDispatchtoProps = dispatch => ({
close: () => dispatch(closeServerDropdown()),
open: () => dispatch(toggleServerDropdown()),
closeSort: () => dispatch(closeSortDropdown()),
setSearch: searchText => dispatch(setSearchAction(searchText))
});
2019-12-04 16:39:53 +00:00
export default connect(mapStateToProps, mapDispatchtoProps)(withTheme(RoomsListHeaderView));