Skip to content

Commit d5ff0cd

Browse files
authored
Merge pull request #2590 from StoDevX/fix-dictionary-updates
Fix dictionary updates
2 parents b267172 + 08f1a45 commit d5ff0cd

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

source/views/dictionary/list.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const styles = StyleSheet.create({
4848
type Props = TopLevelViewPropsType
4949

5050
type State = {
51-
results: Array<WordType>,
51+
query: string,
5252
allTerms: Array<WordType>,
5353
refreshing: boolean,
5454
}
@@ -60,7 +60,7 @@ export class DictionaryView extends React.PureComponent<Props, State> {
6060
}
6161

6262
state = {
63-
results: defaultData.data,
63+
query: '',
6464
allTerms: defaultData.data,
6565
refreshing: false,
6666
}
@@ -126,17 +126,7 @@ export class DictionaryView extends React.PureComponent<Props, State> {
126126
)
127127

128128
performSearch = (text: ?string) => {
129-
if (!text) {
130-
this.setState(state => ({results: state.allTerms}))
131-
return
132-
}
133-
134-
const query = text.toLowerCase()
135-
this.setState(state => ({
136-
results: state.allTerms.filter(term =>
137-
termToArray(term).some(word => word.startsWith(query)),
138-
),
139-
}))
129+
this.setState(() => ({query: text ? text.toLowerCase() : ''}))
140130
}
141131

142132
render() {
@@ -147,14 +137,22 @@ export class DictionaryView extends React.PureComponent<Props, State> {
147137
/>
148138
)
149139

140+
let results = this.state.allTerms
141+
if (this.state.query) {
142+
const {query, allTerms} = this.state
143+
results = allTerms.filter(term =>
144+
termToArray(term).some(word => word.startsWith(query)),
145+
)
146+
}
147+
150148
return (
151149
<SearchableAlphabetListView
152150
cell={this.renderRow}
153151
cellHeight={
154152
ROW_HEIGHT +
155153
(Platform.OS === 'ios' ? 11 / 12 * StyleSheet.hairlineWidth : 0)
156154
}
157-
data={groupBy(this.state.results, item => item.word[0])}
155+
data={groupBy(results, item => item.word[0])}
158156
onSearch={this.performSearch}
159157
refreshControl={refreshControl}
160158
renderSeparator={this.renderSeparator}

source/views/student-orgs/list.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import {
1717
} from '../components/list'
1818
import {trackOrgOpen} from '../../analytics'
1919
import {reportNetworkProblem} from '../../lib/report-network-problem'
20-
import size from 'lodash/size'
2120
import sortBy from 'lodash/sortBy'
2221
import groupBy from 'lodash/groupBy'
2322
import uniq from 'lodash/uniq'
2423
import words from 'lodash/words'
2524
import deburr from 'lodash/deburr'
26-
import filter from 'lodash/filter'
2725
import startCase from 'lodash/startCase'
2826
import * as c from '../components/colors'
2927
import type {StudentOrgType} from './types'
@@ -70,7 +68,7 @@ type Props = TopLevelViewPropsType
7068

7169
type State = {
7270
orgs: Array<StudentOrgType>,
73-
results: {[key: string]: StudentOrgType[]},
71+
query: string,
7472
refreshing: boolean,
7573
error: boolean,
7674
loading: boolean,
@@ -86,7 +84,7 @@ export class StudentOrgsView extends React.PureComponent<Props, State> {
8684

8785
state = {
8886
orgs: [],
89-
results: {},
87+
query: '',
9088
refreshing: false,
9189
loading: true,
9290
error: false,
@@ -119,8 +117,7 @@ export class StudentOrgsView extends React.PureComponent<Props, State> {
119117
})
120118

121119
const sorted = sortBy(withSortableNames, '$sortableName')
122-
const grouped = groupBy(sorted, '$groupableName')
123-
this.setState(() => ({orgs: sorted, results: grouped}))
120+
this.setState(() => ({orgs: sorted}))
124121
}
125122

126123
refresh = async () => {
@@ -178,28 +175,15 @@ export class StudentOrgsView extends React.PureComponent<Props, State> {
178175
}
179176

180177
performSearch = (text: ?string) => {
181-
if (!text) {
182-
this.setState(state => ({
183-
results: groupBy(state.orgs, '$groupableName'),
184-
}))
185-
return
186-
}
187-
188-
const query = text.toLowerCase()
189-
this.setState(state => {
190-
const filteredResults = filter(state.orgs, org =>
191-
orgToArray(org).some(word => word.startsWith(query)),
192-
)
193-
return {results: groupBy(filteredResults, '$groupableName')}
194-
})
178+
this.setState(() => ({query: text ? text.toLowerCase() : ''}))
195179
}
196180

197181
render() {
198182
if (this.state.loading) {
199183
return <LoadingView />
200184
}
201185

202-
if (!size(this.state.orgs)) {
186+
if (!this.state.orgs.length) {
203187
return <NoticeView text="No organizations found." />
204188
}
205189

@@ -210,14 +194,23 @@ export class StudentOrgsView extends React.PureComponent<Props, State> {
210194
/>
211195
)
212196

197+
let results = this.state.orgs
198+
if (this.state.query) {
199+
let {query, orgs} = this.state
200+
results = orgs.filter(org =>
201+
orgToArray(org).some(word => word.startsWith(query)),
202+
)
203+
}
204+
let groupedResults = groupBy(results, '$groupableName')
205+
213206
return (
214207
<SearchableAlphabetListView
215208
cell={this.renderRow}
216209
cellHeight={
217210
ROW_HEIGHT +
218211
(Platform.OS === 'ios' ? 11 / 12 * StyleSheet.hairlineWidth : 0)
219212
}
220-
data={this.state.results}
213+
data={groupedResults}
221214
onSearch={this.performSearch}
222215
refreshControl={refreshControl}
223216
renderSeparator={this.renderSeparator}

0 commit comments

Comments
 (0)