|
| 1 | +import React from 'react'; |
| 2 | +import {connect, ConnectedProps} from 'react-redux'; |
| 3 | +import RefreshListView, {RefreshState} from 'react-native-refresh-list-view'; |
| 4 | +import {RootState} from '@/model/dva/Models'; |
| 5 | +import {ListRenderItemInfo, StyleSheet, View} from 'react-native'; |
| 6 | +import {RouteProp} from '@react-navigation/native'; |
| 7 | +import {RootNavigation, RootStackParamList} from '@/navigator/Router'; |
| 8 | +import {Item} from '@/model/Daily'; |
| 9 | +import ImageTextItem from '@/components/ImageTextItem'; |
| 10 | + |
| 11 | +const CLEAR_TYPE = 'categoryDetail/setState'; |
| 12 | +const REFRESH_TYPE = 'categoryDetail/onRefresh'; |
| 13 | +const LOAD_MORE_TYPE = 'categoryDetail/onLoadMore'; |
| 14 | + |
| 15 | +const mapStateToProps = ({categoryDetail}: RootState) => { |
| 16 | + return { |
| 17 | + dataList: categoryDetail.dataList, |
| 18 | + refreshState: categoryDetail.refreshState, |
| 19 | + nextPageUrl: categoryDetail.nextPageUrl, |
| 20 | + }; |
| 21 | +}; |
| 22 | + |
| 23 | +const connector = connect(mapStateToProps); |
| 24 | + |
| 25 | +type ModelState = ConnectedProps<typeof connector>; |
| 26 | + |
| 27 | +interface IProps extends ModelState { |
| 28 | + navigation: RootNavigation; |
| 29 | + route: RouteProp<RootStackParamList, 'CategoryDetail'>; |
| 30 | +} |
| 31 | + |
| 32 | +class CategoryDetailPage extends React.Component<IProps> { |
| 33 | + componentDidMount() { |
| 34 | + const {navigation, route} = this.props; |
| 35 | + navigation.setOptions({title: route.params.item.name}); |
| 36 | + this.onHeaderRefresh(); |
| 37 | + } |
| 38 | + |
| 39 | + componentWillUnmount() { |
| 40 | + const {dispatch} = this.props; |
| 41 | + dispatch({ |
| 42 | + type: CLEAR_TYPE, |
| 43 | + payload: { |
| 44 | + dataList: [], |
| 45 | + refreshState: RefreshState.Idle, |
| 46 | + nextPageUrl: null, |
| 47 | + }, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + onHeaderRefresh = () => { |
| 52 | + const {dispatch, route} = this.props; |
| 53 | + dispatch({ |
| 54 | + type: REFRESH_TYPE, |
| 55 | + payload: { |
| 56 | + id: route.params.item.id, |
| 57 | + }, |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + onFooterRefresh = () => { |
| 62 | + const {nextPageUrl} = this.props; |
| 63 | + if (nextPageUrl == null) { |
| 64 | + return; |
| 65 | + } |
| 66 | + const {dispatch} = this.props; |
| 67 | + dispatch({ |
| 68 | + type: LOAD_MORE_TYPE, |
| 69 | + payload: { |
| 70 | + nextPageUrl: nextPageUrl, |
| 71 | + }, |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | + renderItem = ({item}: ListRenderItemInfo<Item>) => { |
| 76 | + return <ImageTextItem item={item} />; |
| 77 | + }; |
| 78 | + |
| 79 | + keyExtractor = (item: Item) => { |
| 80 | + return `${item.data.id}`; |
| 81 | + }; |
| 82 | + |
| 83 | + render() { |
| 84 | + const {dataList, refreshState} = this.props; |
| 85 | + return ( |
| 86 | + <View style={styles.container}> |
| 87 | + <RefreshListView |
| 88 | + data={dataList} |
| 89 | + renderItem={this.renderItem} |
| 90 | + keyExtractor={this.keyExtractor} |
| 91 | + refreshState={refreshState} |
| 92 | + onHeaderRefresh={this.onHeaderRefresh} |
| 93 | + onFooterRefresh={this.onFooterRefresh} |
| 94 | + showsVerticalScrollIndicator={false} |
| 95 | + /> |
| 96 | + </View> |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const styles = StyleSheet.create({ |
| 102 | + container: { |
| 103 | + backgroundColor: '#fff', |
| 104 | + }, |
| 105 | +}); |
| 106 | + |
| 107 | +export default connector(CategoryDetailPage); |
0 commit comments