22import * as React from 'react'
33import { ScrollView , StyleSheet } from 'react-native'
44import { type FilterType } from './types'
5- import { type ReduxState } from '../../../flux'
65import { FilterSection } from './section'
76import { TableView } from 'react-native-tableview-simple'
8- import { connect } from 'react-redux'
9- import get from 'lodash/get'
7+ import { NoticeView } from '../notice'
108
119const styles = StyleSheet . create ( {
1210 container : {
1311 flex : 1 ,
1412 } ,
1513} )
1614
17- type NavigationState = {
18- params : {
19- title : string ,
20- pathToFilters : string [ ] ,
21- onChange : ( x : FilterType [ ] ) => any ,
22- onLeave ?: ( filters : FilterType [ ] ) => any ,
23- } ,
24- }
25-
26- type ReactProps = {
15+ type Props = {
2716 navigation : {
28- state : NavigationState ,
17+ state : {
18+ params : { |
19+ + title : string ,
20+ + initialFilters : Array < FilterType > ,
21+ + onDismiss : ( filters : Array < FilterType > ) => mixed ,
22+ | } ,
23+ } ,
24+ addListener : (
25+ ev : string ,
26+ ( payload : mixed ) => mixed ,
27+ ) = > { remove : ( ) => void } ,
2928 } ,
3029}
3130
32- type ReduxStateProps = {
33- filters : FilterType [ ] ,
31+ type State = {
32+ filters : Array < FilterType > ,
3433}
3534
36- type Props = ReactProps & ReduxStateProps
35+ export class FilterView extends React . Component < Props , State > {
36+ static navigationOptions ( { navigation} : Props ) {
37+ let { title} = navigation . state . params
38+ return { title}
39+ }
3740
38- class FilterViewComponent extends React . PureComponent < Props > {
39- static navigationOptions = ( { navigation} : Props ) => {
40- return {
41- title : navigation . state . params . title ,
42- }
41+ state = {
42+ filters : [ ] ,
43+ }
44+
45+ static getDerivedStateFromProps ( props : Props ) {
46+ let { initialFilters : filters = [ ] } = props . navigation . state . params
47+ return { filters }
48+ }
49+
50+ componentDidMount ( ) {
51+ this . _subscription = this . props . navigation . addListener ( 'willBlur' , ( ) => {
52+ let { onDismiss} = this . props . navigation . state . params
53+ if ( onDismiss ) {
54+ onDismiss ( this . state . filters )
55+ }
56+ } )
4357 }
4458
4559 componentWillUnmount ( ) {
46- if ( this . props . navigation . state . params . onLeave ) {
47- this . props . navigation . state . params . onLeave ( this . props . filters )
48- }
60+ this . _subscription && this . _subscription . remove ( )
4961 }
5062
63+ _subscription: ?{ remove: ( ) => void } = null
64+
5165 onFilterChanged = ( filter : FilterType ) => {
52- const { onChange} = this . props . navigation . state . params
5366 // replace the changed filter in the array, maintaining position
54- let result = this . props . filters . map (
55- f => ( f . key !== filter . key ? f : filter ) ,
56- )
57- onChange ( result )
67+ this . setState ( state => {
68+ let edited = state . filters . map ( f => ( f . key !== filter . key ? f : filter ) )
69+ return { filters : edited }
70+ } )
5871 }
5972
6073 render ( ) {
61- const contents = this . props . filters . map ( filter => (
74+ if ( ! this . state . filters . length ) {
75+ return < NoticeView text = "No filters available" />
76+ }
77+
78+ const contents = this . state . filters . map ( filter => (
6279 < FilterSection
6380 key = { filter . key }
6481 filter = { filter }
@@ -73,11 +90,3 @@ class FilterViewComponent extends React.PureComponent<Props> {
7390 )
7491 }
7592}
76-
77- function mapState ( state : ReduxState , actualProps : ReactProps ) : ReduxStateProps {
78- return {
79- filters : get ( state , actualProps . navigation . state . params . pathToFilters , [ ] ) ,
80- }
81- }
82-
83- export const ConnectedFilterView = connect ( mapState ) ( FilterViewComponent )
0 commit comments