Skip to content

Commit 3092d62

Browse files
authored
Merge pull request #2722 from StoDevX/filter-no-redux
Remove menu filters from Redux
2 parents b8d9d88 + 9646e6a commit 3092d62

File tree

23 files changed

+569
-625
lines changed

23 files changed

+569
-625
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"htmlparser2": "3.9.2",
9090
"keyword-search": "0.1.1",
9191
"lodash": "4.17.10",
92+
"mem": "3.0.1",
9293
"moment": "2.22.2",
9394
"moment-timezone": "0.5.21",
9495
"p-props": "1.2.0",
@@ -109,7 +110,7 @@
109110
"react-native-restart": "0.0.6",
110111
"react-native-safari-view": "2.1.0",
111112
"react-native-search-bar": "3.4.2",
112-
"react-native-searchbar": "https://github.com/MosesEsan/react-native-searchbar.git#setValue-function",
113+
"react-native-searchbar-controlled": "1.0.0",
113114
"react-native-tableview-simple": "0.17.5",
114115
"react-native-typography": "1.3.0",
115116
"react-native-vector-icons": "5.0.0",

source/flux/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ import reduxThunk from 'redux-thunk'
77

88
import {app, type State as AppState} from './parts/app'
99
import {homescreen, type State as HomescreenState} from './parts/homescreen'
10-
import {menus, type State as MenusState} from './parts/menus'
1110
import {settings, type State as SettingsState} from './parts/settings'
1211
import {balances, type State as BalancesState} from './parts/balances'
1312
import {buildings, type State as BuildingsState} from './parts/buildings'
1413
import {help, type State as HelpState} from './parts/help'
1514
import {courses, type State as CoursesState} from './parts/courses'
1615

1716
export {init as initRedux} from './init'
18-
export {updateMenuFilters} from './parts/menus'
1917

2018
export type ReduxState = {
2119
app?: AppState,
2220
courses?: CoursesState,
2321
homescreen?: HomescreenState,
24-
menus?: MenusState,
2522
settings?: SettingsState,
2623
balances?: BalancesState,
2724
buildings?: BuildingsState,
@@ -33,7 +30,6 @@ export const makeStore = () => {
3330
app,
3431
courses,
3532
homescreen,
36-
menus,
3733
settings,
3834
balances,
3935
buildings,

source/flux/parts/courses.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,13 @@ import {
1414
formatFilterCombo,
1515
} from '../../views/sis/course-search/lib/format-filter-combo'
1616

17-
const UPDATE_COURSE_FILTERS = 'courses/UPDATE_COURSE_FILTERS'
1817
const LOAD_CACHED_COURSES = 'courses/LOAD_CACHED_COURSES'
1918
const COURSES_LOADED = 'courses/COURSES_LOADED'
2019

2120
type Dispatch<A: Action> = (action: A | Promise<A> | ThunkAction<A>) => any
2221
type GetState = () => ReduxState
2322
type ThunkAction<A: Action> = (dispatch: Dispatch<A>, getState: GetState) => any
2423

25-
type UpdateCourseFiltersAction = {|
26-
type: 'courses/UPDATE_COURSE_FILTERS',
27-
payload: Array<FilterType>,
28-
|}
29-
export function updateCourseFilters(
30-
filters: FilterType[],
31-
): UpdateCourseFiltersAction {
32-
return {type: UPDATE_COURSE_FILTERS, payload: filters}
33-
}
34-
3524
const UPDATE_RECENT_FILTERS = 'courses/UPDATE_RECENT_FILTERS'
3625

3726
type UpdateRecentFiltersAction = {|
@@ -151,7 +140,6 @@ export function updateRecentSearches(
151140
}
152141

153142
type Action =
154-
| UpdateCourseFiltersAction
155143
| LoadCachedCoursesAction
156144
| CoursesLoadedAction
157145
| UpdateRecentSearchesAction
@@ -160,7 +148,6 @@ type Action =
160148
| LoadRecentFiltersAction
161149

162150
export type State = {|
163-
filters: Array<FilterType>,
164151
allCourses: Array<CourseType>,
165152
readyState: 'not-loaded' | 'ready',
166153
validGEs: string[],
@@ -169,7 +156,6 @@ export type State = {|
169156
|}
170157

171158
const initialState = {
172-
filters: [],
173159
allCourses: [],
174160
readyState: 'not-loaded',
175161
validGEs: [],
@@ -179,9 +165,6 @@ const initialState = {
179165

180166
export function courses(state: State = initialState, action: Action) {
181167
switch (action.type) {
182-
case UPDATE_COURSE_FILTERS:
183-
return {...state, filters: action.payload}
184-
185168
case LOAD_RECENT_FILTERS:
186169
return {...state, recentFilters: action.payload}
187170

source/flux/parts/menus.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

source/views/components/filter/filter-view.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,80 @@
22
import * as React from 'react'
33
import {ScrollView, StyleSheet} from 'react-native'
44
import {type FilterType} from './types'
5-
import {type ReduxState} from '../../../flux'
65
import {FilterSection} from './section'
76
import {TableView} from 'react-native-tableview-simple'
8-
import {connect} from 'react-redux'
9-
import get from 'lodash/get'
7+
import {NoticeView} from '../notice'
108

119
const 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)

source/views/components/filter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
export {ConnectedFilterView as FilterView} from './filter-view'
2+
export {FilterView} from './filter-view'
33
export type {FilterType, ListType, ToggleType, PickerType} from './types'
44
export {applyFiltersToItem} from './apply-filters'
55
export {stringifyFilters} from './stringify-filters'

source/views/components/searchable-alphabet-listview.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,10 @@ const styles = StyleSheet.create({
1414
},
1515
})
1616

17-
type Props = any
17+
type Props = any & {query: string}
1818

19-
export class SearchableAlphabetListView extends React.PureComponent<Props> {
20-
searchBar: any = null
21-
22-
_performSearch = (text: string | Object) => {
23-
// Android clear button returns an object
24-
if (typeof text !== 'string') {
25-
return this.props.onSearch(null)
26-
}
27-
28-
return this.props.onSearch(text)
29-
}
19+
export class SearchableAlphabetListView extends React.Component<Props> {
20+
_performSearch = (text: string) => this.props.onSearch(text)
3021

3122
// We need to make the search run slightly behind the UI,
3223
// so I'm slowing it down by 50ms. 0ms also works, but seems
@@ -36,12 +27,7 @@ export class SearchableAlphabetListView extends React.PureComponent<Props> {
3627
render() {
3728
return (
3829
<View style={styles.wrapper}>
39-
<SearchBar
40-
getRef={ref => (this.searchBar = ref)}
41-
onChangeText={this.performSearch}
42-
// if we don't use the arrow function here, searchBar ref is null...
43-
onSearchButtonPress={() => this.searchBar.unFocus()}
44-
/>
30+
<SearchBar onChange={this.performSearch} value={this.props.query} />
4531
<StyledAlphabetListView
4632
headerHeight={
4733
Platform.OS === 'ios'

source/views/components/searchbar/index.android.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import * as React from 'react'
44
import {StyleSheet} from 'react-native'
55
import * as c from '../colors'
6-
import NativeSearchBar from 'react-native-searchbar'
6+
import NativeSearchBar from 'react-native-searchbar-controlled'
77
import Icon from 'react-native-vector-icons/Ionicons'
8+
import {type Props} from './types'
89

910
const iconStyles = StyleSheet.create({
1011
icon: {
@@ -23,48 +24,37 @@ const styles = StyleSheet.create({
2324
},
2425
})
2526

26-
type Props = {
27-
getRef?: any,
28-
style?: any,
29-
placeholder?: string,
30-
onChangeText: string => any,
31-
onCancel: () => any,
32-
onFocus: () => any,
33-
onSearchButtonPress: string => any,
34-
searchActive: boolean,
35-
}
36-
37-
type State = {
38-
input: string,
39-
}
40-
41-
export class SearchBar extends React.PureComponent<Props, State> {
42-
state = {
43-
input: '',
27+
export class SearchBar extends React.Component<Props> {
28+
static defaultProps = {
29+
active: false,
30+
onCancel: () => {},
31+
onChange: () => {},
32+
onFocus: () => {},
33+
onSubmit: () => {},
34+
placeholder: 'Search',
35+
value: '',
4436
}
4537

46-
updateText = input => {
47-
this.setState({input: input})
48-
}
49-
50-
onSearch = () => {
51-
this.props.onSearchButtonPress(this.state.input)
38+
handleRef = (ref: NativeSearchBar) => {
39+
this.props.getRef && this.props.getRef(ref)
5240
}
5341

5442
render() {
55-
const backButton = this.props.searchActive ? backIcon : searchIcon
43+
let backButton = this.props.active ? backIcon : searchIcon
44+
5645
return (
5746
<NativeSearchBar
58-
ref={this.props.getRef}
47+
ref={this.handleRef}
5948
backButton={backButton}
60-
closeButton={this.props.searchActive ? closeIcon : null}
49+
closeButton={this.props.active ? closeIcon : null}
6150
focusOnLayout={false}
62-
handleChangeText={this.updateText}
63-
hideX={!this.props.searchActive}
51+
handleChangeText={this.props.onChange}
52+
hideClose={!this.props.active}
53+
input={this.props.value}
6454
onBack={this.props.onCancel}
6555
onFocus={this.props.onFocus}
66-
onSubmitEditing={this.onSearch}
67-
placeholder={this.props.placeholder || 'Search'}
56+
onSubmitEditing={this.props.onSubmit}
57+
placeholder={this.props.placeholder}
6858
showOnLoad={true}
6959
style={[styles.searchbar, this.props.style]}
7060
/>

0 commit comments

Comments
 (0)