import React, { Component } from 'react'; import { View, Text, Animated, Easing, Image, TouchableWithoutFeedback } from 'react-native'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import Touch from '../../utils/touch'; import styles from './styles'; import RocketChat from '../../lib/rocketchat'; import { setPreference } from '../../actions/sortPreferences'; import log from '../../utils/log'; import I18n from '../../i18n'; const ANIMATION_DURATION = 200; @connect(state => ({ closeSortDropdown: state.rooms.closeSortDropdown }), dispatch => ({ setSortPreference: preference => dispatch(setPreference(preference)) })) export default class Sort extends Component { static propTypes = { closeSortDropdown: PropTypes.bool, close: PropTypes.func, sortBy: PropTypes.string, groupByType: PropTypes.bool, showFavorites: PropTypes.bool, showUnread: PropTypes.bool, setSortPreference: PropTypes.func } constructor(props) { super(props); this.animatedValue = new Animated.Value(0); } componentDidMount() { Animated.timing( this.animatedValue, { toValue: 1, duration: ANIMATION_DURATION, easing: Easing.ease, useNativeDriver: true }, ).start(); } componentDidUpdate(prevProps) { if (prevProps.closeSortDropdown !== this.props.closeSortDropdown) { this.close(); } } setSortPreference = async(param) => { try { this.props.setSortPreference(param); RocketChat.saveSortPreference(param); } catch (e) { log('RoomsListView.setSortPreference', e); } } sortByName = () => { this.setSortPreference({ sortBy: 'alphabetical' }); } sortByActivity = () => { this.setSortPreference({ sortBy: 'activity' }); } toggleGroupByType = () => { this.setSortPreference({ groupByType: !this.props.groupByType }); } toggleGroupByFavorites = () => { this.setSortPreference({ showFavorites: !this.props.showFavorites }); } toggleUnread = () => { this.setSortPreference({ showUnread: !this.props.showUnread }); } close = () => { Animated.timing( this.animatedValue, { toValue: 0, duration: ANIMATION_DURATION, easing: Easing.ease, useNativeDriver: true }, ).start(() => this.props.close()); } render() { const translateY = this.animatedValue.interpolate({ inputRange: [0, 1], outputRange: [-245, 41] }); const backdropOpacity = this.animatedValue.interpolate({ inputRange: [0, 1], outputRange: [0, 0.3] }); const { sortBy, groupByType, showFavorites, showUnread } = this.props; return ( [ , {I18n.t('Alphabetical')} {sortBy === 'alphabetical' ? : null} {I18n.t('Activity')} {sortBy === 'activity' ? : null} {I18n.t('Group_by_type')} {groupByType ? : null} {I18n.t('Group_by_favorites')} {showFavorites ? : null} {I18n.t('Unread_on_top')} {showUnread ? : null} , {I18n.t('Sorting_by', { key: I18n.t(this.props.sortBy === 'alphabetical' ? 'name' : 'activity') })} ] ); } }