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'
107
118const styles = StyleSheet . create ( {
129 container : {
1310 flex : 1 ,
1411 } ,
1512} )
1613
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 = {
14+ type Props = {
2715 navigation : {
28- state : NavigationState ,
16+ state : {
17+ params : { |
18+ + title : string ,
19+ + initialFilters : Array < FilterType > ,
20+ + onDismiss : ( filters : Array < FilterType > ) => mixed ,
21+ | } ,
22+ } ,
2923 } ,
3024}
3125
32- type ReduxStateProps = {
33- filters : FilterType [ ] ,
26+ type State = {
27+ filters : Array < FilterType > ,
3428}
3529
36- type Props = ReactProps & ReduxStateProps
30+ export class FilterView extends React . Component < Props , State > {
31+ static navigationOptions ( { navigation} : Props ) {
32+ let { title} = navigation . state . params
33+ return { title}
34+ }
3735
38- class FilterViewComponent extends React . PureComponent < Props > {
39- static navigationOptions = ( { navigation} : Props ) => {
40- return {
41- title : navigation . state . params . title ,
42- }
36+ state = {
37+ filters : [ ] ,
38+ }
39+
40+ static getDerivedStateFromProps ( props : Props ) {
41+ let { initialFilters : filters } = props . navigation . state . params
42+ return { filters }
4343 }
4444
4545 componentWillUnmount ( ) {
46- if ( this . props . navigation . state . params . onLeave ) {
47- this . props . navigation . state . params . onLeave ( this . props . filters )
46+ let { onDismiss } = this . props . navigation . state . params
47+ if ( onDismiss ) {
48+ onDismiss ( this . state . filters )
4849 }
4950 }
5051
5152 onFilterChanged = ( filter : FilterType ) => {
52- const { onChange} = this . props . navigation . state . params
5353 // 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 )
54+ this . setState ( state => {
55+ let edited = state . filters . map ( f => ( f . key !== filter . key ? f : filter ) )
56+ return { filters : edited }
57+ } )
5858 }
5959
6060 render ( ) {
61- const contents = this . props . filters . map ( filter => (
61+ const contents = this . state . filters . map ( filter => (
6262 < FilterSection
6363 key = { filter . key }
6464 filter = { filter }
@@ -73,11 +73,3 @@ class FilterViewComponent extends React.PureComponent<Props> {
7373 )
7474 }
7575}
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