index.js 3.39 KB
import React, { Component } from 'react'
import { View, FlatList } from 'react-native'
import { Button, ListItem, SearchBar } from 'react-native-elements'
import { StackActions } from 'react-navigation'
import { connect } from 'react-redux'
import {
  verDetalleSolicitud,
  cambiarPaginaSolicitud,
  cambiarBusquedaSolicitud
} from '../../redux/actions/solicitudes'
import Pagination from '../../components/Pagination'
import DefaultIndicator from '../../components/DefaultIndicator'
import css from './style'

// AIP Movil
const navegarSolicitud = (self, solicitud) => {
  self.props.verDetalleSolicitud(solicitud)
  const pushAction = StackActions.push({
    routeName: 'DetalleSolicitud',
    params: solicitud
  })

  self.props.navigation.dispatch(pushAction)
}

const renderItem = function (self) {
  return ({ item }) => (
    <ListItem
      key={item.id}
      title={item.titulo}
      subtitle={`# ${item.id}`}
      badge={item.formato ? { value: item.formato.nombre } : null}
      titleStyle={{ fontWeight: 'bold' }}
      subtitleStyle={{ fontWeight: 'normal' }}
      onPress={() => navegarSolicitud(self, item)}
    />
  )
}

class BuscarSolicitudes extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: 'Buscar Solicitudes',
    headerStyle: css.headerStyle,
    headerTitleStyle: css.headerTitleStyle,
    headerLeft:
      <Button
        icon={{ name: 'menu', size: 27, color: 'white' }}
        buttonStyle={{ backgroundColor: 'transparent' }}
        onPress={() => navigation.toggleDrawer()}
      />
  });

  componentWillMount() {
    this.props.cambiarPaginaSolicitud({ page: 1, by_text: null, by_usuario: null }, 0)
  }
  render() {
    const self = this
    const { busqueda } = this.props
    const spinner = <DefaultIndicator />
    const pagination = (show) => {
      if (show) {
        return (
        <Pagination
          next={() => self.props.cambiarPaginaSolicitud({ page: busqueda.page + 1 }, 0)}
          previous={() => self.props.cambiarPaginaSolicitud({ page: busqueda.page - 1 }, 0)}
          page={busqueda.page}
          totalPages={this.props.pages.totalPages}
          totalCount={this.props.pages.totalCount}
        />)
      }
    }
    const section = (
      <FlatList
        data={this.props.solicitudes}
        renderItem={renderItem(this)}
        keyExtractor={(item, index) => index.toString()}
        ListFooterComponent={() => pagination(!self.props.loading)}
      />)
    return (
      <View style={[css.pane, css.container]}>
        <SearchBar
          lightTheme
          onChangeText={(text) => self.props.cambiarBusquedaSolicitud({ by_titulo: text }, 300)}
          icon={{ type: 'font-awesome', name: 'search' }}
          placeholder='Buscar Solicitudes'
        />
        {this.props.loading ? spinner : section}
      </View>
    )
  }
}


const mapStateToProps = (state) => ({
    token: state.autenticacion.token,
    loading: state.solicitudes.loading,
    busqueda: state.solicitudes.busqueda,
    pages: state.solicitudes.pages,
    solicitudes: state.solicitudes.data,
})
const mapDispatchToProps = (dispatch) => ({
  verDetalleSolicitud: (solicitud) => dispatch(verDetalleSolicitud(solicitud)),
  cambiarBusquedaSolicitud: (busqueda) => dispatch(cambiarBusquedaSolicitud(busqueda)),
  cambiarPaginaSolicitud: (busqueda) => dispatch(cambiarPaginaSolicitud(busqueda))
})

export default connect(mapStateToProps, mapDispatchToProps)(BuscarSolicitudes)