2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-11-05 03:25:03 +00:00
|
|
|
import ReactDOMServer from 'react-dom/server'
|
2021-01-08 05:15:53 +00:00
|
|
|
import { connect } from 'react-redux'
|
2020-11-05 03:25:03 +00:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import PullToRefresh from 'pulltorefreshjs'
|
|
|
|
import moment from 'moment-mini'
|
2020-05-07 05:03:34 +01:00
|
|
|
import { BREAKPOINT_EXTRA_SMALL } from '../constants'
|
|
|
|
import Responsive from '../features/ui//util/responsive_component'
|
2020-11-05 03:25:03 +00:00
|
|
|
import Text from './text'
|
2020-05-07 05:03:34 +01:00
|
|
|
|
2020-11-05 03:25:03 +00:00
|
|
|
class PullToRefresher extends React.PureComponent {
|
|
|
|
|
|
|
|
state = {
|
|
|
|
lastRefreshDate: null,
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-11-05 18:00:38 +00:00
|
|
|
this.handleDestroy()
|
2020-11-09 07:47:29 +00:00
|
|
|
|
|
|
|
// : TODO : optimize
|
|
|
|
return
|
|
|
|
|
2020-11-05 03:25:03 +00:00
|
|
|
if (this.props.isDisabled) return
|
2021-01-08 05:15:53 +00:00
|
|
|
if (this.props.width > BREAKPOINT_EXTRA_SMALL) return
|
2020-11-05 03:25:03 +00:00
|
|
|
|
|
|
|
const textProps = {
|
|
|
|
color: 'secondary',
|
|
|
|
weight: 'medium',
|
|
|
|
size: 'medium',
|
|
|
|
className: [_s.py10].join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
const ptr = PullToRefresh.init({
|
|
|
|
mainElement: 'body',
|
2020-11-05 17:36:07 +00:00
|
|
|
distMax: 130,
|
2020-11-05 03:25:03 +00:00
|
|
|
onRefresh: this.handleOnRefresh,
|
|
|
|
instructionsPullToRefresh: ReactDOMServer.renderToString(
|
|
|
|
<Text {...textProps}>Pull to Refresh</Text>
|
|
|
|
),
|
|
|
|
instructionsReleaseToRefresh: ReactDOMServer.renderToString(
|
|
|
|
<Text {...textProps}>Release to Refresh</Text>
|
|
|
|
),
|
|
|
|
instructionsRefreshing: ReactDOMServer.renderToString(
|
|
|
|
<Text {...textProps}>Refreshing</Text>
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-11-05 18:00:38 +00:00
|
|
|
this.handleDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDestroy() {
|
2020-11-05 03:25:03 +00:00
|
|
|
PullToRefresh.destroyAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnRefresh = () => {
|
|
|
|
const { lastRefreshDate } = this.state
|
|
|
|
|
|
|
|
if (!lastRefreshDate) {
|
|
|
|
this.props.onRefresh()
|
|
|
|
this.setState({ lastRefreshDate: new Date() })
|
|
|
|
} else {
|
|
|
|
const diff = moment().diff(lastRefreshDate, 'seconds')
|
|
|
|
if (diff > 10) {
|
|
|
|
this.props.onRefresh()
|
|
|
|
this.setState({ lastRefreshDate: new Date() })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 05:03:34 +01:00
|
|
|
|
|
|
|
render() {
|
2020-11-05 03:25:03 +00:00
|
|
|
const { isDisabled } = this.props
|
|
|
|
|
|
|
|
if (isDisabled) return null
|
|
|
|
|
|
|
|
return <div/>
|
2020-05-07 05:03:34 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 03:25:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 05:15:53 +00:00
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
width: state.getIn(['settings', 'window_dimensions', 'width']),
|
|
|
|
})
|
|
|
|
|
2020-11-05 03:25:03 +00:00
|
|
|
PullToRefresher.propTypes = {
|
|
|
|
onRefresh: PropTypes.func,
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:15:53 +00:00
|
|
|
export default connect(mapStateToProps)(PullToRefresher)
|