<commit_before>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2020
 */

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Text, View } from 'react-native';

import { SyncIcon } from './SyncIcon';

import { syncStrings } from '../localization';
import { formatDate } from '../utilities';
import globalStyles from '../globalStyles';

const mapStateToProps = state => {
  const { sync } = state;
  return sync;
};

export const SyncStateComponent = ({ lastSyncTime, isSyncing, errorMessage, showText }) => {
  const syncMessage = isSyncing ? syncStrings.sync_in_progress : syncStrings.sync_enabled;
  const formattedDate = formatDate(lastSyncTime, 'dots');
  const errorText = `${syncStrings.sync_error}. ${syncStrings.last_sync} ${formattedDate}`;
  const hasError = !!errorMessage?.length;

  return (
    <View style={globalStyles.navBarRightContainer}>
      {showText && (
        <Text style={globalStyles.navBarText}>{hasError ? errorText : syncMessage}</Text>
      )}
      <SyncIcon isActive={!hasError} />
    </View>
  );
};

SyncStateComponent.defaultProps = {
  showText: true,
};

SyncStateComponent.propTypes = {
  lastSyncTime: PropTypes.number.isRequired,
  isSyncing: PropTypes.bool.isRequired,
  errorMessage: PropTypes.string.isRequired,
  showText: PropTypes.bool,
};

export const SyncState = connect(mapStateToProps)(SyncStateComponent);<commit_msg>Add further SyncState simplifications<commit_after>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2020
 */

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Text, View, TouchableOpacity } from 'react-native';

import { SyncIcon } from './SyncIcon';

import { syncStrings } from '../localization';
import { formatDate } from '../utilities';
import globalStyles from '../globalStyles';
import { openSyncModal } from '../actions/SyncActions';

const mapStateToProps = state => {
  const { sync } = state;
  return sync;
};

const mapDispatchToProps = dispatch => {
  const onOpenSyncModal = () => dispatch(openSyncModal());
  return { onOpenSyncModal };
};

export const SyncStateComponent = ({
  lastSyncTime,
  isSyncing,
  errorMessage,
  showText,
  onOpenSyncModal,
}) => {
  const syncMessage = isSyncing ? syncStrings.sync_in_progress : syncStrings.sync_enabled;
  const formattedDate = formatDate(lastSyncTime, 'dots');
  const errorText = `${syncStrings.sync_error}. ${syncStrings.last_sync} ${formattedDate}`;
  const hasError = !!errorMessage?.length;

  const Container = showText ? TouchableOpacity : View;

  return (
    <Container onPress={onOpenSyncModal} style={globalStyles.navBarRightContainer}>
      {showText && (
        <Text style={globalStyles.navBarText}>{hasError ? errorText : syncMessage}</Text>
      )}
      <SyncIcon isActive={!hasError} />
    </Container>
  );
};

SyncStateComponent.defaultProps = {
  showText: true,
  onOpenSyncModal: null,
};

SyncStateComponent.propTypes = {
  lastSyncTime: PropTypes.number.isRequired,
  isSyncing: PropTypes.bool.isRequired,
  errorMessage: PropTypes.string.isRequired,
  showText: PropTypes.bool,
  onOpenSyncModal: PropTypes.func,
};

export const SyncState = connect(mapStateToProps, mapDispatchToProps)(SyncStateComponent);
{'fraction_non_alphanumeric': 0.12911010558069383, 'fraction_numerical': 0.0024132730015082957, 'mean_word_length': 4.7172413793103445, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2726591', 'n_tokens_mistral': 994, 'n_tokens_neox': 999, 'n_words': 227}