From 3733f776fc17e6318d00fa5a96087af195d70f1d Mon Sep 17 00:00:00 2001 From: IlarionHalushka Date: Fri, 10 May 2019 20:09:07 +0300 Subject: [PATCH] issue #799 merger message views (#876) On Room Actions, we have Files, Mentions, Starred and Pinned. They have similar APIs and logic. All of those could be merged into one generic view (MessagesView). Maybe even Search could be in this merge. Note: They're similar, but have own rules (unstar, unpin, etc). This change may reduce 1MB to our release bundle size, since we're going to remove a lot of boilerplate. --- app/index.js | 12 +- .../methods/helpers/getFileUrlFromMessage.js | 15 ++ app/views/MentionedMessagesView/index.js | 139 ---------- app/views/MessagesView/index.js | 255 ++++++++++++++++++ .../styles.js | 0 app/views/PinnedMessagesView/index.js | 178 ------------ app/views/PinnedMessagesView/styles.js | 22 -- app/views/RoomActionsView/index.js | 16 +- app/views/RoomFilesView/index.js | 151 ----------- app/views/RoomFilesView/styles.js | 22 -- app/views/StarredMessagesView/index.js | 184 ------------- app/views/StarredMessagesView/styles.js | 22 -- 12 files changed, 281 insertions(+), 735 deletions(-) create mode 100644 app/lib/methods/helpers/getFileUrlFromMessage.js delete mode 100644 app/views/MentionedMessagesView/index.js create mode 100644 app/views/MessagesView/index.js rename app/views/{MentionedMessagesView => MessagesView}/styles.js (100%) delete mode 100644 app/views/PinnedMessagesView/index.js delete mode 100644 app/views/PinnedMessagesView/styles.js delete mode 100644 app/views/RoomFilesView/index.js delete mode 100644 app/views/RoomFilesView/styles.js delete mode 100644 app/views/StarredMessagesView/index.js delete mode 100644 app/views/StarredMessagesView/styles.js diff --git a/app/index.js b/app/index.js index 9be8979b5..56f88def1 100644 --- a/app/index.js +++ b/app/index.js @@ -24,12 +24,9 @@ import RoomActionsView from './views/RoomActionsView'; import RoomInfoView from './views/RoomInfoView'; import RoomInfoEditView from './views/RoomInfoEditView'; import RoomMembersView from './views/RoomMembersView'; -import RoomFilesView from './views/RoomFilesView'; -import MentionedMessagesView from './views/MentionedMessagesView'; -import StarredMessagesView from './views/StarredMessagesView'; import SearchMessagesView from './views/SearchMessagesView'; -import PinnedMessagesView from './views/PinnedMessagesView'; import ThreadMessagesView from './views/ThreadMessagesView'; +import MessagesView from './views/MessagesView'; import SelectedUsersView from './views/SelectedUsersView'; import CreateChannelView from './views/CreateChannelView'; import LegalView from './views/LegalView'; @@ -108,13 +105,10 @@ const ChatsStack = createStackNavigator({ RoomInfoView, RoomInfoEditView, RoomMembersView, - RoomFilesView, - MentionedMessagesView, - StarredMessagesView, SearchMessagesView, - PinnedMessagesView, SelectedUsersView, - ThreadMessagesView + ThreadMessagesView, + MessagesView }, { defaultNavigationOptions: defaultHeader }); diff --git a/app/lib/methods/helpers/getFileUrlFromMessage.js b/app/lib/methods/helpers/getFileUrlFromMessage.js new file mode 100644 index 000000000..60995aee1 --- /dev/null +++ b/app/lib/methods/helpers/getFileUrlFromMessage.js @@ -0,0 +1,15 @@ +export default function(message) { + if (/image/.test(message.type)) { + return { image_url: message.url }; + } + if (/audio/.test(message.type)) { + return { audio_url: message.url }; + } + if (/video/.test(message.type)) { + return { video_url: message.url }; + } + return { + title_link: message.url, + type: 'file' + }; +} diff --git a/app/views/MentionedMessagesView/index.js b/app/views/MentionedMessagesView/index.js deleted file mode 100644 index 987e9da9d..000000000 --- a/app/views/MentionedMessagesView/index.js +++ /dev/null @@ -1,139 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import { FlatList, View, Text } from 'react-native'; -import { connect } from 'react-redux'; -import { SafeAreaView } from 'react-navigation'; -import equal from 'deep-equal'; - -import LoggedView from '../View'; -import styles from './styles'; -import Message from '../../containers/message/Message'; -import RCActivityIndicator from '../../containers/ActivityIndicator'; -import I18n from '../../i18n'; -import RocketChat from '../../lib/rocketchat'; -import StatusBar from '../../containers/StatusBar'; - -@connect(state => ({ - baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', - customEmojis: state.customEmojis, - user: { - id: state.login.user && state.login.user.id, - username: state.login.user && state.login.user.username, - token: state.login.user && state.login.user.token - } -})) -/** @extends React.Component */ -export default class MentionedMessagesView extends LoggedView { - static navigationOptions = { - title: I18n.t('Mentions') - } - - static propTypes = { - user: PropTypes.object, - baseUrl: PropTypes.string, - customEmojis: PropTypes.object, - navigation: PropTypes.object - } - - constructor(props) { - super('MentionedMessagesView', props); - this.state = { - loading: false, - messages: [] - }; - this.rid = props.navigation.getParam('rid'); - this.t = props.navigation.getParam('t'); - } - - componentDidMount() { - this.load(); - } - - shouldComponentUpdate(nextProps, nextState) { - const { loading, messages } = this.state; - if (nextState.loading !== loading) { - return true; - } - if (!equal(nextState.messages, messages)) { - return true; - } - return false; - } - - load = async() => { - const { - messages, total, loading - } = this.state; - const { user } = this.props; - if (messages.length === total || loading) { - return; - } - - this.setState({ loading: true }); - - try { - const result = await RocketChat.getMessages( - this.rid, - this.t, - { 'mentions._id': { $in: [user.id] } }, - messages.length - ); - if (result.success) { - this.setState(prevState => ({ - messages: [...prevState.messages, ...result.messages], - total: result.total, - loading: false - })); - } - } catch (error) { - this.setState({ loading: false }); - console.log('MentionedMessagesView -> load -> catch -> error', error); - } - } - - renderEmpty = () => ( - - {I18n.t('No_mentioned_messages')} - - ) - - renderItem = ({ item }) => { - const { user, customEmojis, baseUrl } = this.props; - return ( - - ); - } - - render() { - const { messages, loading } = this.state; - - if (!loading && messages.length === 0) { - return this.renderEmpty(); - } - - return ( - - - item._id} - onEndReached={this.load} - ListFooterComponent={loading ? : null} - /> - - ); - } -} diff --git a/app/views/MessagesView/index.js b/app/views/MessagesView/index.js new file mode 100644 index 000000000..3e7b4d236 --- /dev/null +++ b/app/views/MessagesView/index.js @@ -0,0 +1,255 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { FlatList, View, Text } from 'react-native'; +import { connect } from 'react-redux'; +import { SafeAreaView } from 'react-navigation'; +import equal from 'deep-equal'; +import ActionSheet from 'react-native-action-sheet'; + +import LoggedView from '../View'; +import styles from './styles'; +import Message from '../../containers/message/Message'; +import RCActivityIndicator from '../../containers/ActivityIndicator'; +import I18n from '../../i18n'; +import RocketChat from '../../lib/rocketchat'; +import StatusBar from '../../containers/StatusBar'; +import getFileUrlFromMessage from '../../lib/methods/helpers/getFileUrlFromMessage'; + +const ACTION_INDEX = 0; +const CANCEL_INDEX = 1; + +@connect(state => ({ + baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', + customEmojis: state.customEmojis, + user: { + id: state.login.user && state.login.user.id, + username: state.login.user && state.login.user.username, + token: state.login.user && state.login.user.token + } +})) +/** @extends React.Component */ +export default class MessagesView extends LoggedView { + static navigationOptions = ({ navigation }) => ({ + title: navigation.state.params.name + }); + + static propTypes = { + user: PropTypes.object, + baseUrl: PropTypes.string, + customEmojis: PropTypes.object, + navigation: PropTypes.object + } + + constructor(props) { + super('MessagesView', props); + this.state = { + loading: false, + messages: [] + }; + this.rid = props.navigation.getParam('rid'); + this.t = props.navigation.getParam('t'); + this.content = this.defineMessagesViewContent(props.navigation.getParam('name')); + } + + componentDidMount() { + this.load(); + } + + shouldComponentUpdate(nextProps, nextState) { + const { loading, messages } = this.state; + if (nextState.loading !== loading) { + return true; + } + if (!equal(nextState.messages, messages)) { + return true; + } + return false; + } + + defineMessagesViewContent = (name) => { + const { messages } = this.state; + const { user, baseUrl, customEmojis } = this.props; + + const renderItemCommonProps = item => ({ + customEmojis, + baseUrl, + user, + author: item.u || item.user, + ts: item.ts || item.uploadedAt, + timeFormat: 'MMM Do YYYY, h:mm:ss a', + edited: !!item.editedAt, + header: true, + attachments: item.attachments || [] + }); + + return ({ + // Files Messages Screen + Files: { + name: I18n.t('Files'), + fetchFunc: async() => { + const result = await RocketChat.getFiles(this.rid, this.t, messages.length); + return { ...result, messages: result.files }; + }, + noDataMsg: I18n.t('No_files'), + testID: 'room-files-view', + renderItem: (item) => { + const url = getFileUrlFromMessage(item); + + return ( + + ); + } + }, + // Mentions Messages Screen + Mentions: { + name: I18n.t('Mentions'), + fetchFunc: () => RocketChat.getMessages( + this.rid, + this.t, + { 'mentions._id': { $in: [user.id] } }, + messages.length + ), + noDataMsg: I18n.t('No_mentioned_messages'), + testID: 'mentioned-messages-view', + renderItem: item => ( + + ) + }, + // Starred Messages Screen + Starred: { + name: I18n.t('Starred'), + fetchFunc: () => RocketChat.getMessages( + this.rid, + this.t, + { 'starred._id': { $in: [user.id] } }, + messages.length + ), + noDataMsg: I18n.t('No_starred_messages'), + testID: 'starred-messages-view', + renderItem: item => ( + this.onLongPress(item)} + /> + ), + actionTitle: I18n.t('Unstar'), + handleActionPress: message => RocketChat.toggleStarMessage(message) + }, + // Pinned Messages Screen + Pinned: { + name: I18n.t('Pinned'), + fetchFunc: () => RocketChat.getMessages(this.rid, this.t, { pinned: true }, messages.length), + noDataMsg: I18n.t('No_pinned_messages'), + testID: 'pinned-messages-view', + renderItem: item => ( + this.onLongPress(item)} + /> + ), + actionTitle: I18n.t('Unpin'), + handleActionPress: message => RocketChat.togglePinMessage(message) + } + }[name]); + } + + load = async() => { + const { + messages, total, loading + } = this.state; + if (messages.length === total || loading) { + return; + } + + this.setState({ loading: true }); + + try { + const result = await this.content.fetchFunc(); + if (result.success) { + this.setState({ + messages: [...messages, ...result.messages], + total: result.total, + loading: false + }); + } + } catch (error) { + this.setState({ loading: false }); + console.warn('MessagesView -> catch -> error', error); + } + } + + onLongPress = (message) => { + this.setState({ message }); + this.showActionSheet(); + } + + showActionSheet = () => { + ActionSheet.showActionSheetWithOptions({ + options: [this.content.actionTitle, I18n.t('Cancel')], + cancelButtonIndex: CANCEL_INDEX, + title: I18n.t('Actions') + }, (actionIndex) => { + this.handleActionPress(actionIndex); + }); + } + + handleActionPress = async(actionIndex) => { + if (actionIndex === ACTION_INDEX) { + const { message } = this.state; + + try { + const result = await this.content.handleActionPress(message); + if (result.success) { + this.setState(prevState => ({ + messages: prevState.messages.filter(item => item._id !== message._id), + total: prevState.total - 1 + })); + } + } catch (error) { + console.warn('MessagesView -> handleActionPress -> catch -> error', error); + } + } + } + + renderEmpty = () => ( + + {this.content.noDataMsg} + + ) + + renderItem = ({ item }) => this.content.renderItem(item) + + render() { + const { messages, loading } = this.state; + + if (!loading && messages.length === 0) { + return this.renderEmpty(); + } + + return ( + + + item._id} + onEndReached={this.load} + ListFooterComponent={loading ? : null} + /> + + ); + } +} diff --git a/app/views/MentionedMessagesView/styles.js b/app/views/MessagesView/styles.js similarity index 100% rename from app/views/MentionedMessagesView/styles.js rename to app/views/MessagesView/styles.js diff --git a/app/views/PinnedMessagesView/index.js b/app/views/PinnedMessagesView/index.js deleted file mode 100644 index 65cb15517..000000000 --- a/app/views/PinnedMessagesView/index.js +++ /dev/null @@ -1,178 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import { FlatList, View, Text } from 'react-native'; -import { connect } from 'react-redux'; -import ActionSheet from 'react-native-action-sheet'; -import { SafeAreaView } from 'react-navigation'; -import equal from 'deep-equal'; - -import LoggedView from '../View'; -import styles from './styles'; -import Message from '../../containers/message/Message'; -import RCActivityIndicator from '../../containers/ActivityIndicator'; -import I18n from '../../i18n'; -import RocketChat from '../../lib/rocketchat'; -import StatusBar from '../../containers/StatusBar'; - -const PIN_INDEX = 0; -const CANCEL_INDEX = 1; -const options = [I18n.t('Unpin'), I18n.t('Cancel')]; - -@connect(state => ({ - baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', - customEmojis: state.customEmojis, - user: { - id: state.login.user && state.login.user.id, - username: state.login.user && state.login.user.username, - token: state.login.user && state.login.user.token - } -})) -/** @extends React.Component */ -export default class PinnedMessagesView extends LoggedView { - static navigationOptions = { - title: I18n.t('Pinned') - } - - static propTypes = { - user: PropTypes.object, - baseUrl: PropTypes.string, - customEmojis: PropTypes.object, - navigation: PropTypes.object - } - - constructor(props) { - super('PinnedMessagesView', props); - this.state = { - loading: false, - messages: [] - }; - this.rid = props.navigation.getParam('rid'); - this.t = props.navigation.getParam('t'); - } - - componentDidMount() { - this.load(); - } - - shouldComponentUpdate(nextProps, nextState) { - const { loading, messages } = this.state; - if (nextState.loading !== loading) { - return true; - } - if (!equal(nextState.messages, messages)) { - return true; - } - return false; - } - - onLongPress = (message) => { - this.setState({ message }); - this.showActionSheet(); - } - - showActionSheet = () => { - ActionSheet.showActionSheetWithOptions({ - options, - cancelButtonIndex: CANCEL_INDEX, - title: I18n.t('Actions') - }, (actionIndex) => { - this.handleActionPress(actionIndex); - }); - } - - handleActionPress = (actionIndex) => { - switch (actionIndex) { - case PIN_INDEX: - this.unPin(); - break; - default: - break; - } - } - - unPin = async() => { - const { message } = this.state; - try { - const result = await RocketChat.togglePinMessage(message); - if (result.success) { - this.setState(prevState => ({ - messages: prevState.messages.filter(item => item._id !== message._id) - })); - } - } catch (error) { - console.log('PinnedMessagesView -> unPin -> catch -> error', error); - } - } - - load = async() => { - const { - messages, total, loading - } = this.state; - if (messages.length === total || loading) { - return; - } - - this.setState({ loading: true }); - - try { - const result = await RocketChat.getMessages(this.rid, this.t, { pinned: true }, messages.length); - if (result.success) { - this.setState(prevState => ({ - messages: [...prevState.messages, ...result.messages], - total: result.total, - loading: false - })); - } - } catch (error) { - this.setState({ loading: false }); - console.log('PinnedMessagesView -> catch -> error', error); - } - } - - renderEmpty = () => ( - - {I18n.t('No_pinned_messages')} - - ) - - renderItem = ({ item }) => { - const { user, customEmojis, baseUrl } = this.props; - return ( - this.onLongPress(item)} - /> - ); - } - - render() { - const { messages, loading } = this.state; - - if (!loading && messages.length === 0) { - return this.renderEmpty(); - } - - return ( - - - item._id} - onEndReached={this.load} - ListFooterComponent={loading ? : null} - /> - - ); - } -} diff --git a/app/views/PinnedMessagesView/styles.js b/app/views/PinnedMessagesView/styles.js deleted file mode 100644 index 9bcaa5580..000000000 --- a/app/views/PinnedMessagesView/styles.js +++ /dev/null @@ -1,22 +0,0 @@ -import { StyleSheet } from 'react-native'; - -import sharedStyles from '../Styles'; -import { COLOR_WHITE } from '../../constants/colors'; - -export default StyleSheet.create({ - list: { - flex: 1, - backgroundColor: COLOR_WHITE - }, - listEmptyContainer: { - flex: 1, - alignItems: 'center', - justifyContent: 'center', - backgroundColor: COLOR_WHITE - }, - noDataFound: { - fontSize: 14, - ...sharedStyles.textRegular, - ...sharedStyles.textColorNormal - } -}); diff --git a/app/views/RoomActionsView/index.js b/app/views/RoomActionsView/index.js index 31a4f3852..7e7c5e339 100644 --- a/app/views/RoomActionsView/index.js +++ b/app/views/RoomActionsView/index.js @@ -215,22 +215,22 @@ export default class RoomActionsView extends LoggedView { { icon: 'file-generic', name: I18n.t('Files'), - route: 'RoomFilesView', - params: { rid, t }, + route: 'MessagesView', + params: { rid, t, name: 'Files' }, testID: 'room-actions-files' }, { icon: 'at', name: I18n.t('Mentions'), - route: 'MentionedMessagesView', - params: { rid, t }, + route: 'MessagesView', + params: { rid, t, name: 'Mentions' }, testID: 'room-actions-mentioned' }, { icon: 'star', name: I18n.t('Starred'), - route: 'StarredMessagesView', - params: { rid, t }, + route: 'MessagesView', + params: { rid, t, name: 'Starred' }, testID: 'room-actions-starred' }, { @@ -249,8 +249,8 @@ export default class RoomActionsView extends LoggedView { { icon: 'pin', name: I18n.t('Pinned'), - route: 'PinnedMessagesView', - params: { rid, t }, + route: 'MessagesView', + params: { rid, t, name: 'Pinned' }, testID: 'room-actions-pinned' } ], diff --git a/app/views/RoomFilesView/index.js b/app/views/RoomFilesView/index.js deleted file mode 100644 index aa41a302d..000000000 --- a/app/views/RoomFilesView/index.js +++ /dev/null @@ -1,151 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import { FlatList, View, Text } from 'react-native'; -import { connect } from 'react-redux'; -import { SafeAreaView } from 'react-navigation'; -import equal from 'deep-equal'; - -import LoggedView from '../View'; -import styles from './styles'; -import Message from '../../containers/message/Message'; -import RCActivityIndicator from '../../containers/ActivityIndicator'; -import I18n from '../../i18n'; -import RocketChat from '../../lib/rocketchat'; -import StatusBar from '../../containers/StatusBar'; - -@connect(state => ({ - baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', - customEmojis: state.customEmojis, - user: { - id: state.login.user && state.login.user.id, - username: state.login.user && state.login.user.username, - token: state.login.user && state.login.user.token - } -})) -/** @extends React.Component */ -export default class RoomFilesView extends LoggedView { - static navigationOptions = { - title: I18n.t('Files') - } - - static propTypes = { - user: PropTypes.object, - baseUrl: PropTypes.string, - customEmojis: PropTypes.object, - navigation: PropTypes.object - } - - constructor(props) { - super('RoomFilesView', props); - this.state = { - loading: false, - messages: [] - }; - this.rid = props.navigation.getParam('rid'); - this.t = props.navigation.getParam('t'); - } - - componentDidMount() { - this.load(); - } - - shouldComponentUpdate(nextProps, nextState) { - const { loading, messages } = this.state; - if (nextState.loading !== loading) { - return true; - } - if (!equal(nextState.messages, messages)) { - return true; - } - return false; - } - - load = async() => { - const { - messages, total, loading - } = this.state; - if (messages.length === total || loading) { - return; - } - - this.setState({ loading: true }); - - try { - const result = await RocketChat.getFiles(this.rid, this.t, messages.length); - if (result.success) { - this.setState(prevState => ({ - messages: [...prevState.messages, ...result.files], - total: result.total, - loading: false - })); - } - } catch (error) { - this.setState({ loading: false }); - console.log('RoomFilesView -> catch -> error', error); - } - } - - renderEmpty = () => ( - - {I18n.t('No_files')} - - ) - - renderItem = ({ item }) => { - const { user, baseUrl, customEmojis } = this.props; - - let url = {}; - if (/image/.test(item.type)) { - url = { image_url: item.url }; - } else if (/audio/.test(item.type)) { - url = { audio_url: item.url }; - } else if (/video/.test(item.type)) { - url = { video_url: item.url }; - } else { - url = { - title_link: item.url, - type: 'file' - }; - } - - return ( - - ); - } - - render() { - const { messages, loading } = this.state; - - if (!loading && messages.length === 0) { - return this.renderEmpty(); - } - - return ( - - - item._id} - onEndReached={this.load} - ListFooterComponent={loading ? : null} - /> - - ); - } -} diff --git a/app/views/RoomFilesView/styles.js b/app/views/RoomFilesView/styles.js deleted file mode 100644 index 9bcaa5580..000000000 --- a/app/views/RoomFilesView/styles.js +++ /dev/null @@ -1,22 +0,0 @@ -import { StyleSheet } from 'react-native'; - -import sharedStyles from '../Styles'; -import { COLOR_WHITE } from '../../constants/colors'; - -export default StyleSheet.create({ - list: { - flex: 1, - backgroundColor: COLOR_WHITE - }, - listEmptyContainer: { - flex: 1, - alignItems: 'center', - justifyContent: 'center', - backgroundColor: COLOR_WHITE - }, - noDataFound: { - fontSize: 14, - ...sharedStyles.textRegular, - ...sharedStyles.textColorNormal - } -}); diff --git a/app/views/StarredMessagesView/index.js b/app/views/StarredMessagesView/index.js deleted file mode 100644 index a399c3e9c..000000000 --- a/app/views/StarredMessagesView/index.js +++ /dev/null @@ -1,184 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import { FlatList, View, Text } from 'react-native'; -import { connect } from 'react-redux'; -import ActionSheet from 'react-native-action-sheet'; -import { SafeAreaView } from 'react-navigation'; -import equal from 'deep-equal'; - -import LoggedView from '../View'; -import styles from './styles'; -import Message from '../../containers/message/Message'; -import RCActivityIndicator from '../../containers/ActivityIndicator'; -import I18n from '../../i18n'; -import RocketChat from '../../lib/rocketchat'; -import StatusBar from '../../containers/StatusBar'; - -const STAR_INDEX = 0; -const CANCEL_INDEX = 1; -const options = [I18n.t('Unstar'), I18n.t('Cancel')]; - -@connect(state => ({ - baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', - customEmojis: state.customEmojis, - user: { - id: state.login.user && state.login.user.id, - username: state.login.user && state.login.user.username, - token: state.login.user && state.login.user.token - } -})) -/** @extends React.Component */ -export default class StarredMessagesView extends LoggedView { - static navigationOptions = { - title: I18n.t('Starred') - } - - static propTypes = { - user: PropTypes.object, - baseUrl: PropTypes.string, - customEmojis: PropTypes.object, - navigation: PropTypes.object - } - - constructor(props) { - super('StarredMessagesView', props); - this.state = { - loading: false, - messages: [] - }; - this.rid = props.navigation.getParam('rid'); - this.t = props.navigation.getParam('t'); - } - - componentDidMount() { - this.load(); - } - - shouldComponentUpdate(nextProps, nextState) { - const { loading, messages } = this.state; - if (nextState.loading !== loading) { - return true; - } - if (!equal(nextState.messages, messages)) { - return true; - } - return false; - } - - onLongPress = (message) => { - this.setState({ message }); - this.showActionSheet(); - } - - showActionSheet = () => { - ActionSheet.showActionSheetWithOptions({ - options, - cancelButtonIndex: CANCEL_INDEX, - title: I18n.t('Actions') - }, (actionIndex) => { - this.handleActionPress(actionIndex); - }); - } - - handleActionPress = (actionIndex) => { - switch (actionIndex) { - case STAR_INDEX: - this.unStar(); - break; - default: - break; - } - } - - unStar = async() => { - const { message } = this.state; - try { - const result = await RocketChat.toggleStarMessage(message); - if (result.success) { - this.setState(prevState => ({ - messages: prevState.messages.filter(item => item._id !== message._id) - })); - } - } catch (error) { - console.log('StarredMessagesView -> unStar -> catch -> error', error); - } - } - - load = async() => { - const { - messages, total, loading - } = this.state; - const { user } = this.props; - if (messages.length === total || loading) { - return; - } - - this.setState({ loading: true }); - - try { - const result = await RocketChat.getMessages( - this.rid, - this.t, - { 'starred._id': { $in: [user.id] } }, - messages.length - ); - if (result.success) { - this.setState(prevState => ({ - messages: [...prevState.messages, ...result.messages], - total: result.total, - loading: false - })); - } - } catch (error) { - this.setState({ loading: false }); - console.log('StarredMessagesView -> load -> catch -> error', error); - } - } - - renderEmpty = () => ( - - {I18n.t('No_starred_messages')} - - ) - - renderItem = ({ item }) => { - const { user, customEmojis, baseUrl } = this.props; - return ( - this.onLongPress(item)} - /> - ); - } - - render() { - const { messages, loading } = this.state; - - if (!loading && messages.length === 0) { - return this.renderEmpty(); - } - - return ( - - - item._id} - onEndReached={this.load} - ListFooterComponent={loading ? : null} - /> - - ); - } -} diff --git a/app/views/StarredMessagesView/styles.js b/app/views/StarredMessagesView/styles.js deleted file mode 100644 index 9bcaa5580..000000000 --- a/app/views/StarredMessagesView/styles.js +++ /dev/null @@ -1,22 +0,0 @@ -import { StyleSheet } from 'react-native'; - -import sharedStyles from '../Styles'; -import { COLOR_WHITE } from '../../constants/colors'; - -export default StyleSheet.create({ - list: { - flex: 1, - backgroundColor: COLOR_WHITE - }, - listEmptyContainer: { - flex: 1, - alignItems: 'center', - justifyContent: 'center', - backgroundColor: COLOR_WHITE - }, - noDataFound: { - fontSize: 14, - ...sharedStyles.textRegular, - ...sharedStyles.textColorNormal - } -});