import React from 'react'; import {connect} from 'react-redux'; import {appConfig} from 'appConfig'; import {ILocation} from '../../interfaces'; import {superdeskApi} from '../../superdeskApi'; import * as selectors from '../../selectors'; import * as actions from '../../actions'; import {getMapUrl} from '../../utils'; import {formatLocationToAddress} from '../../utils/locations'; import {List} from '../UI'; interface IProps { currentLocation?: ILocation; locations: Array; isLoading?: boolean; editLocation(location: ILocation): void; loadMore(): void; deleteLocation(location: ILocation): void; } interface IState { scrollTop: number; activeItemIndex: number; navigateDown: boolean; } const mapStateToProps = (state) => ({ locations: selectors.locations.locations(state), isLoading: selectors.locations.loadingLocations(state), currentLocation: selectors.locations.getEditLocation(state), }); const mapDispatchToProps = (dispatch) => ({ editLocation: (location) => dispatch(actions.locations.editLocation(location)), loadMore: () => dispatch(actions.locations.getMoreLocations()), deleteLocation: (location) => dispatch(actions.locations.deleteLocationConfirmation(location)), }); export class LocationsListComponent extends React.Component { constructor(props) { super(props); this.state = { scrollTop: 0, activeItemIndex: -1, // Active item in the list navigateDown: true, // Navigation direction }; this.handleScroll = this.handleScroll.bind(this); } handleScroll(event) { if (this.props.isLoading) { return; } const node = event.target; // scroll event gets fired on hover of each item in the list. // this.state.scrollTop is used to check if the scroll position has changed if (node && node.scrollTop + node.offsetHeight + 100 >= node.scrollHeight && this.state.scrollTop < node.scrollTop) { this.props.loadMore(); } } render() { const {gettext} = superdeskApi.localization; return (
{this.props.locations?.length ? null : ( {gettext('No result')} )} {this.props.locations.map((location, index) => ( {location.name} {gettext('Address')}: {location.address.line?.[0]} {gettext('City/Town')}: {location.address.city ?? location.address.area} {gettext('State/Province/Region')}: {location.address.state ?? location.address.locality} {gettext('Post Code')}: {location.address.postal_code} {gettext('Country')}: {location.address.country} ))}
); } } export const LocationsList = connect( mapStateToProps, mapDispatchToProps )(LocationsListComponent);