Unverified Commit 3683b893 authored by Diego Mello's avatar Diego Mello Committed by GitHub
Browse files

[IMPROVEMENT] Use reselect (#1696)

parent b87472f1
...@@ -17,7 +17,7 @@ export default class EmojiKeyboard extends React.PureComponent { ...@@ -17,7 +17,7 @@ export default class EmojiKeyboard extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
const state = store.getState(); const state = store.getState();
this.baseUrl = state.settings.Site_Url || state.server ? state.server.server : ''; this.baseUrl = state.server.server;
} }
onEmojiSelected = (emoji) => { onEmojiSelected = (emoji) => {
......
...@@ -92,7 +92,7 @@ ReplyPreview.propTypes = { ...@@ -92,7 +92,7 @@ ReplyPreview.propTypes = {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
useMarkdown: state.markdown.useMarkdown, useMarkdown: state.markdown.useMarkdown,
Message_TimeFormat: state.settings.Message_TimeFormat, Message_TimeFormat: state.settings.Message_TimeFormat,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '' baseUrl: state.server.server
}); });
export default connect(mapStateToProps)(ReplyPreview); export default connect(mapStateToProps)(ReplyPreview);
...@@ -44,6 +44,7 @@ import { ...@@ -44,6 +44,7 @@ import {
} from './constants'; } from './constants';
import CommandsPreview from './CommandsPreview'; import CommandsPreview from './CommandsPreview';
import { Review } from '../../utils/review'; import { Review } from '../../utils/review';
import { getUserSelector } from '../../selectors/login';
const imagePickerConfig = { const imagePickerConfig = {
cropping: true, cropping: true,
...@@ -881,13 +882,9 @@ class MessageBox extends Component { ...@@ -881,13 +882,9 @@ class MessageBox extends Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
threadsEnabled: state.settings.Threads_enabled, threadsEnabled: state.settings.Threads_enabled,
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id,
username: state.login.user && state.login.user.username,
token: state.login.user && state.login.user.token
},
FileUpload_MediaTypeWhiteList: state.settings.FileUpload_MediaTypeWhiteList, FileUpload_MediaTypeWhiteList: state.settings.FileUpload_MediaTypeWhiteList,
FileUpload_MaxFileSize: state.settings.FileUpload_MaxFileSize FileUpload_MaxFileSize: state.settings.FileUpload_MaxFileSize
}); });
......
...@@ -28,13 +28,9 @@ export async function cancelUpload(item) { ...@@ -28,13 +28,9 @@ export async function cancelUpload(item) {
export function sendFileMessage(rid, fileInfo, tmid, server, user) { export function sendFileMessage(rid, fileInfo, tmid, server, user) {
return new Promise(async(resolve, reject) => { return new Promise(async(resolve, reject) => {
try { try {
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
const serverInfo = await serversCollection.find(server);
const { id: Site_Url } = serverInfo;
const { id, token } = user; const { id, token } = user;
const uploadUrl = `${ Site_Url }/api/v1/rooms.upload/${ rid }`; const uploadUrl = `${ server }/api/v1/rooms.upload/${ rid }`;
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const formData = new FormData(); const formData = new FormData();
......
...@@ -16,6 +16,7 @@ import { removeNotification as removeNotificationAction } from '../../actions/no ...@@ -16,6 +16,7 @@ import { removeNotification as removeNotificationAction } from '../../actions/no
import sharedStyles from '../../views/Styles'; import sharedStyles from '../../views/Styles';
import { ROW_HEIGHT } from '../../presentation/RoomItem'; import { ROW_HEIGHT } from '../../presentation/RoomItem';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { getUserSelector } from '../../selectors/login';
const AVATAR_SIZE = 48; const AVATAR_SIZE = 48;
const ANIMATION_DURATION = 300; const ANIMATION_DURATION = 300;
...@@ -72,8 +73,7 @@ class NotificationBadge extends React.Component { ...@@ -72,8 +73,7 @@ class NotificationBadge extends React.Component {
static propTypes = { static propTypes = {
navigation: PropTypes.object, navigation: PropTypes.object,
baseUrl: PropTypes.string, baseUrl: PropTypes.string,
token: PropTypes.string, user: PropTypes.object,
userId: PropTypes.string,
notification: PropTypes.object, notification: PropTypes.object,
window: PropTypes.object, window: PropTypes.object,
removeNotification: PropTypes.func, removeNotification: PropTypes.func,
...@@ -173,7 +173,7 @@ class NotificationBadge extends React.Component { ...@@ -173,7 +173,7 @@ class NotificationBadge extends React.Component {
render() { render() {
const { const {
baseUrl, token, userId, notification, window, theme baseUrl, user: { id: userId, token }, notification, window, theme
} = this.props; } = this.props;
const { message, payload } = notification; const { message, payload } = notification;
const { type } = payload; const { type } = payload;
...@@ -227,9 +227,8 @@ class NotificationBadge extends React.Component { ...@@ -227,9 +227,8 @@ class NotificationBadge extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
userId: state.login.user && state.login.user.id, user: getUserSelector(state),
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
token: state.login.user && state.login.user.token,
notification: state.notification notification: state.notification
}); });
......
import { createSelector } from 'reselect';
const getUser = state => state.login.user || {};
export const getUserSelector = createSelector(
[getUser],
user => user
);
...@@ -11,6 +11,7 @@ import styles from '../Styles'; ...@@ -11,6 +11,7 @@ import styles from '../Styles';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { getUserSelector } from '../../selectors/login';
class AdminPanelView extends React.Component { class AdminPanelView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => ({ static navigationOptions = ({ navigation, screenProps }) => ({
...@@ -21,12 +22,12 @@ class AdminPanelView extends React.Component { ...@@ -21,12 +22,12 @@ class AdminPanelView extends React.Component {
static propTypes = { static propTypes = {
baseUrl: PropTypes.string, baseUrl: PropTypes.string,
authToken: PropTypes.string, token: PropTypes.string,
theme: PropTypes.string theme: PropTypes.string
} }
render() { render() {
const { baseUrl, authToken, theme } = this.props; const { baseUrl, token, theme } = this.props;
if (!baseUrl) { if (!baseUrl) {
return null; return null;
} }
...@@ -35,7 +36,7 @@ class AdminPanelView extends React.Component { ...@@ -35,7 +36,7 @@ class AdminPanelView extends React.Component {
<StatusBar theme={theme} /> <StatusBar theme={theme} />
<WebView <WebView
source={{ uri: `${ baseUrl }/admin/info?layout=embedded` }} source={{ uri: `${ baseUrl }/admin/info?layout=embedded` }}
injectedJavaScript={`Meteor.loginWithToken('${ authToken }', function() { })`} injectedJavaScript={`Meteor.loginWithToken('${ token }', function() { })`}
/> />
</SafeAreaView> </SafeAreaView>
); );
...@@ -43,8 +44,8 @@ class AdminPanelView extends React.Component { ...@@ -43,8 +44,8 @@ class AdminPanelView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
authToken: state.login.user && state.login.user.token token: getUserSelector(state).token
}); });
export default connect(mapStateToProps)(withTheme(AdminPanelView)); export default connect(mapStateToProps)(withTheme(AdminPanelView));
...@@ -19,6 +19,7 @@ import { formatAttachmentUrl } from '../lib/utils'; ...@@ -19,6 +19,7 @@ import { formatAttachmentUrl } from '../lib/utils';
import RCActivityIndicator from '../containers/ActivityIndicator'; import RCActivityIndicator from '../containers/ActivityIndicator';
import { SaveButton, CloseModalButton } from '../containers/HeaderButton'; import { SaveButton, CloseModalButton } from '../containers/HeaderButton';
import { isAndroid } from '../utils/deviceInfo'; import { isAndroid } from '../utils/deviceInfo';
import { getUserSelector } from '../selectors/login';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
...@@ -142,11 +143,8 @@ class AttachmentView extends React.Component { ...@@ -142,11 +143,8 @@ class AttachmentView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
user: { user: getUserSelector(state)
id: state.login.user && state.login.user.id,
token: state.login.user && state.login.user.token
}
}); });
export default connect(mapStateToProps)(withTheme(AttachmentView)); export default connect(mapStateToProps)(withTheme(AttachmentView));
...@@ -23,6 +23,7 @@ import { SWITCH_TRACK_COLOR, themes } from '../constants/colors'; ...@@ -23,6 +23,7 @@ import { SWITCH_TRACK_COLOR, themes } from '../constants/colors';
import { withTheme } from '../theme'; import { withTheme } from '../theme';
import { themedHeader } from '../utils/navigation'; import { themedHeader } from '../utils/navigation';
import { Review } from '../utils/review'; import { Review } from '../utils/review';
import { getUserSelector } from '../selectors/login';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
...@@ -365,16 +366,13 @@ class CreateChannelView extends React.Component { ...@@ -365,16 +366,13 @@ class CreateChannelView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
error: state.createChannel.error, error: state.createChannel.error,
failure: state.createChannel.failure, failure: state.createChannel.failure,
isFetching: state.createChannel.isFetching, isFetching: state.createChannel.isFetching,
result: state.createChannel.result, result: state.createChannel.result,
users: state.selectedUsers.users, users: state.selectedUsers.users,
user: { user: getUserSelector(state)
id: state.login.user && state.login.user.id,
token: state.login.user && state.login.user.token
}
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
......
...@@ -23,6 +23,7 @@ import { withTheme } from '../../theme'; ...@@ -23,6 +23,7 @@ import { withTheme } from '../../theme';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import styles from './styles'; import styles from './styles';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { getUserSelector } from '../../selectors/login';
class DirectoryView extends React.Component { class DirectoryView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => { static navigationOptions = ({ navigation, screenProps }) => {
...@@ -253,11 +254,8 @@ class DirectoryView extends React.Component { ...@@ -253,11 +254,8 @@ class DirectoryView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id,
token: state.login.user && state.login.user.token
},
isFederationEnabled: state.settings.FEDERATION_Enabled isFederationEnabled: state.settings.FEDERATION_Enabled
}); });
......
...@@ -18,6 +18,7 @@ import Separator from '../../containers/Separator'; ...@@ -18,6 +18,7 @@ import Separator from '../../containers/Separator';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { getUserSelector } from '../../selectors/login';
const LANGUAGES = [ const LANGUAGES = [
{ {
...@@ -185,7 +186,7 @@ class LanguageView extends React.Component { ...@@ -185,7 +186,7 @@ class LanguageView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
userLanguage: state.login.user && state.login.user.language userLanguage: getUserSelector(state).language
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
......
...@@ -17,6 +17,7 @@ import { themes } from '../../constants/colors'; ...@@ -17,6 +17,7 @@ import { themes } from '../../constants/colors';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { withSplit } from '../../split'; import { withSplit } from '../../split';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { getUserSelector } from '../../selectors/login';
const ACTION_INDEX = 0; const ACTION_INDEX = 0;
const CANCEL_INDEX = 1; const CANCEL_INDEX = 1;
...@@ -306,12 +307,8 @@ class MessagesView extends React.Component { ...@@ -306,12 +307,8 @@ class MessagesView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id,
username: state.login.user && state.login.user.username,
token: state.login.user && state.login.user.token
},
customEmojis: state.customEmojis customEmojis: state.customEmojis
}); });
......
...@@ -23,6 +23,7 @@ import StatusBar from '../containers/StatusBar'; ...@@ -23,6 +23,7 @@ import StatusBar from '../containers/StatusBar';
import { themes } from '../constants/colors'; import { themes } from '../constants/colors';
import { withTheme } from '../theme'; import { withTheme } from '../theme';
import { themedHeader } from '../utils/navigation'; import { themedHeader } from '../utils/navigation';
import { getUserSelector } from '../selectors/login';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
safeAreaView: { safeAreaView: {
...@@ -227,11 +228,8 @@ class NewMessageView extends React.Component { ...@@ -227,11 +228,8 @@ class NewMessageView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
user: { user: getUserSelector(state)
id: state.login.user && state.login.user.id,
token: state.login.user && state.login.user.token
}
}); });
export default connect(mapStateToProps)(withTheme(NewMessageView)); export default connect(mapStateToProps)(withTheme(NewMessageView));
...@@ -31,6 +31,7 @@ import StatusBar from '../../containers/StatusBar'; ...@@ -31,6 +31,7 @@ import StatusBar from '../../containers/StatusBar';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { getUserSelector } from '../../selectors/login';
class ProfileView extends React.Component { class ProfileView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => ({ static navigationOptions = ({ navigation, screenProps }) => ({
...@@ -497,16 +498,9 @@ class ProfileView extends React.Component { ...@@ -497,16 +498,9 @@ class ProfileView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id,
name: state.login.user && state.login.user.name,
username: state.login.user && state.login.user.username,
customFields: state.login.user && state.login.user.customFields,
emails: state.login.user && state.login.user.emails,
token: state.login.user && state.login.user.token
},
Accounts_CustomFields: state.settings.Accounts_CustomFields, Accounts_CustomFields: state.settings.Accounts_CustomFields,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '' baseUrl: state.server.server
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
......
...@@ -15,6 +15,7 @@ import StatusBar from '../../containers/StatusBar'; ...@@ -15,6 +15,7 @@ import StatusBar from '../../containers/StatusBar';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { getUserSelector } from '../../selectors/login';
class ReadReceiptView extends React.Component { class ReadReceiptView extends React.Component {
static navigationOptions = ({ screenProps }) => ({ static navigationOptions = ({ screenProps }) => ({
...@@ -26,8 +27,7 @@ class ReadReceiptView extends React.Component { ...@@ -26,8 +27,7 @@ class ReadReceiptView extends React.Component {
navigation: PropTypes.object, navigation: PropTypes.object,
Message_TimeFormat: PropTypes.string, Message_TimeFormat: PropTypes.string,
baseUrl: PropTypes.string, baseUrl: PropTypes.string,
userId: PropTypes.string, user: PropTypes.object,
token: PropTypes.string,
theme: PropTypes.string theme: PropTypes.string
} }
...@@ -92,7 +92,7 @@ class ReadReceiptView extends React.Component { ...@@ -92,7 +92,7 @@ class ReadReceiptView extends React.Component {
renderItem = ({ item }) => { renderItem = ({ item }) => {
const { const {
Message_TimeFormat, userId, baseUrl, token, theme Message_TimeFormat, user: { id: userId, token }, baseUrl, theme
} = this.props; } = this.props;
const time = moment(item.ts).format(Message_TimeFormat); const time = moment(item.ts).format(Message_TimeFormat);
return ( return (
...@@ -167,9 +167,8 @@ class ReadReceiptView extends React.Component { ...@@ -167,9 +167,8 @@ class ReadReceiptView extends React.Component {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
Message_TimeFormat: state.settings.Message_TimeFormat, Message_TimeFormat: state.settings.Message_TimeFormat,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
userId: state.login.user && state.login.user.id, user: getUserSelector(state)
token: state.login.user && state.login.user.token
}); });
export default connect(mapStateToProps)(withTheme(ReadReceiptView)); export default connect(mapStateToProps)(withTheme(ReadReceiptView));
...@@ -24,6 +24,7 @@ import { themes } from '../../constants/colors'; ...@@ -24,6 +24,7 @@ import { themes } from '../../constants/colors';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { CloseModalButton } from '../../containers/HeaderButton'; import { CloseModalButton } from '../../containers/HeaderButton';
import { getUserSelector } from '../../selectors/login';
class RoomActionsView extends React.Component { class RoomActionsView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => { static navigationOptions = ({ navigation, screenProps }) => {
...@@ -523,11 +524,8 @@ class RoomActionsView extends React.Component { ...@@ -523,11 +524,8 @@ class RoomActionsView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id, baseUrl: state.server.server,
token: state.login.user && state.login.user.token
},
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
jitsiEnabled: state.settings.Jitsi_Enabled || false jitsiEnabled: state.settings.Jitsi_Enabled || false
}); });
......
...@@ -19,6 +19,7 @@ import log from '../../utils/log'; ...@@ -19,6 +19,7 @@ import log from '../../utils/log';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { getUserSelector } from '../../selectors/login';
const PERMISSION_EDIT_ROOM = 'edit-room'; const PERMISSION_EDIT_ROOM = 'edit-room';
...@@ -309,11 +310,8 @@ class RoomInfoView extends React.Component { ...@@ -309,11 +310,8 @@ class RoomInfoView extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '', baseUrl: state.server.server,
user: { user: getUserSelector(state),
id: state.login.user && state.login.user.id,
token: state.login.user && state.login.user.token
},
Message_TimeFormat: state.settings.Message_TimeFormat Message_TimeFormat: state.settings.Message_TimeFormat
}); });
......
...@@ -24,6 +24,7 @@ import ActivityIndicator from '../../containers/ActivityIndicator'; ...@@ -24,6 +24,7 @@ import ActivityIndicator from '../../containers/ActivityIndicator';
import { withTheme } from '../../theme'; import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation'; import { themedHeader } from '../../utils/navigation';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
import { getUserSelector } from '../../selectors/login';
const PAGE_SIZE = 25; const PAGE_SIZE = 25;
...@@ -287,11 +288,8 @@ class RoomMembersView extends React.Component { ...@@ -287,11 +288,8 @@ class RoomMembersView extends React.Component {
} }