|
| 1 | +// @flow |
| 2 | + |
| 3 | +import * as React from 'react' |
| 4 | +import {FlatList} from 'react-native' |
| 5 | +import {DebugRow} from './row' |
| 6 | +import {connect} from 'react-redux' |
| 7 | +import {NoticeView} from '@frogpond/notice' |
| 8 | +import {ListSeparator} from '@frogpond/lists' |
| 9 | +import toPairs from 'lodash/toPairs' |
| 10 | +import {toLaxTitleCase} from 'titlecase' |
| 11 | +import get from 'lodash/get' |
| 12 | +import type {NavigationState} from 'react-navigation' |
| 13 | +import type {TopLevelViewPropsType} from '../../../types' |
| 14 | + |
| 15 | +type Props = TopLevelViewPropsType & { |
| 16 | + state: any, |
| 17 | +} |
| 18 | + |
| 19 | +export class DebugListView extends React.PureComponent<Props> { |
| 20 | + static navigationOptions = ({navigation}: NavigationState) => { |
| 21 | + let titleParam = navigation.getParam('keyPath', 'Debug') |
| 22 | + let title = |
| 23 | + titleParam === 'Debug' ? titleParam : titleParam[titleParam.length - 1] |
| 24 | + |
| 25 | + return { |
| 26 | + title: toLaxTitleCase(title), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + onPressRow = (row: any) => { |
| 31 | + let keyPath = this.props.navigation.getParam('keyPath', []) |
| 32 | + keyPath = [...keyPath, row.key] |
| 33 | + this.props.navigation.push('DebugView', {keyPath: keyPath}) |
| 34 | + } |
| 35 | + |
| 36 | + keyExtractor = (item: any) => item.key |
| 37 | + |
| 38 | + renderItem = ({item}: {item: any}) => ( |
| 39 | + <DebugRow |
| 40 | + data={item} |
| 41 | + navigation={this.props.navigation} |
| 42 | + onPress={() => this.onPressRow(item)} |
| 43 | + /> |
| 44 | + ) |
| 45 | + |
| 46 | + render() { |
| 47 | + let keyed = toPairs(this.props.state).map(([key, value]) => { |
| 48 | + return {key, value} |
| 49 | + }) |
| 50 | + |
| 51 | + return ( |
| 52 | + <FlatList |
| 53 | + ItemSeparatorComponent={ListSeparator} |
| 54 | + ListEmptyComponent={<NoticeView text="Nothing found in redux." />} |
| 55 | + data={keyed} |
| 56 | + keyExtractor={this.keyExtractor} |
| 57 | + renderItem={this.renderItem} |
| 58 | + /> |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function mapStateToProps(state, ownProps) { |
| 64 | + if (!ownProps.navigation.getParam('keyPath', null)) { |
| 65 | + return {state} |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + state: get(state, ownProps.navigation.getParam('keyPath')), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export const ConnectedDebugListView = connect(mapStateToProps)(DebugListView) |
0 commit comments