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/login'; import log from '../../utils/log'; import I18n from '../../i18n'; const ANIMATION_DURATION = 200; @connect(state => ({ closeSortDropdown: state.rooms.closeSortDropdown }), dispatch => ({ setPreference: preference => dispatch(setPreference(preference)) })) export default class Sort extends Component { static propTypes = { closeSortDropdown: PropTypes.bool, close: PropTypes.func, sidebarSortby: PropTypes.string, sidebarGroupByType: PropTypes.bool, sidebarShowFavorites: PropTypes.bool, sidebarShowUnread: PropTypes.bool, setPreference: 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(); } } saveUserPreference = async(param) => { try { this.props.setPreference(param); await RocketChat.saveUserPreferences(param); } catch (e) { log('RoomsListView.saveUserPreference', e); } } sortByName = () => { this.saveUserPreference({ sidebarSortby: 'alphabetical' }); } sortByActivity = () => { this.saveUserPreference({ sidebarSortby: 'activity' }); } toggleGroupByType = () => { this.saveUserPreference({ sidebarGroupByType: !this.props.sidebarGroupByType }); } toggleGroupByFavorites = () => { this.saveUserPreference({ sidebarShowFavorites: !this.props.sidebarShowFavorites }); } toggleUnread = () => { this.saveUserPreference({ sidebarShowUnread: !this.props.sidebarShowUnread }); } 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 { sidebarSortby, sidebarGroupByType, sidebarShowFavorites, sidebarShowUnread } = this.props; return ( [ , {I18n.t('Alphabetical')} {sidebarSortby === 'alphabetical' ? : null} {I18n.t('Activity')} {sidebarSortby === 'activity' ? : null} {I18n.t('Group_by_type')} {sidebarGroupByType ? : null} {I18n.t('Group_by_favorites')} {sidebarShowFavorites ? : null} {I18n.t('Unread_on_top')} {sidebarShowUnread ? : null} , {I18n.t('Sorting_by', { key: I18n.t(this.props.sidebarSortby === 'alphabetical' ? 'name' : 'activity') })} ] ); } }