Browse Source

Improved search performance

Arnaud Vergnet 4 years ago
parent
commit
c48887a0d8

+ 4
- 2
components/Lists/ClubListItem.js View File

25
     getCategoriesRender(categories: Array<number | null>) {
25
     getCategoriesRender(categories: Array<number | null>) {
26
         let final = [];
26
         let final = [];
27
         for (let i = 0; i < categories.length; i++) {
27
         for (let i = 0; i < categories.length; i++) {
28
-            if (categories[i] !== null)
29
-                final.push(this.props.chipRender(this.props.categoryTranslator(categories[i])));
28
+            if (categories[i] !== null){
29
+                const category = this.props.categoryTranslator(categories[i]);
30
+                final.push(this.props.chipRender(category, this.props.item.id + ':' + category.id));
31
+            }
30
         }
32
         }
31
         return <View style={{flexDirection: 'row'}}>{final}</View>;
33
         return <View style={{flexDirection: 'row'}}>{final}</View>;
32
     }
34
     }

+ 0
- 2
navigation/DrawerNavigator.js View File

262
                 name="ClubDisplayScreen"
262
                 name="ClubDisplayScreen"
263
                 component={ClubDisplayScreen}
263
                 component={ClubDisplayScreen}
264
                 options={({navigation}) => {
264
                 options={({navigation}) => {
265
-                    const openDrawer = getDrawerButton.bind(this, navigation);
266
                     return {
265
                     return {
267
                         title: "",
266
                         title: "",
268
-                        headerLeft: openDrawer,
269
                         ...TransitionPresets.ModalSlideFromBottomIOS,
267
                         ...TransitionPresets.ModalSlideFromBottomIOS,
270
                     };
268
                     };
271
                 }}
269
                 }}

+ 13
- 6
screens/Amicale/ClubDisplayScreen.js View File

52
         return "";
52
         return "";
53
     }
53
     }
54
 
54
 
55
-    getCategoriesRender(categories: Array<number|null>) {
55
+    getCategoriesRender(categories: Array<number | null>) {
56
         let final = [];
56
         let final = [];
57
         for (let i = 0; i < categories.length; i++) {
57
         for (let i = 0; i < categories.length; i++) {
58
-            if (categories[i] !== null)
59
-                final.push(<Chip style={{marginRight: 5}}>{this.getCategoryName(categories[i])}</Chip>);
58
+            if (categories[i] !== null) {
59
+                final.push(
60
+                    <Chip
61
+                        style={{marginRight: 5}}
62
+                        key={i.toString()}>
63
+                        {this.getCategoryName(categories[i])}
64
+                    </Chip>
65
+                );
66
+            }
60
         }
67
         }
61
         return <View style={{flexDirection: 'row', marginTop: 5}}>{final}</View>;
68
         return <View style={{flexDirection: 'row', marginTop: 5}}>{final}</View>;
62
     }
69
     }
63
 
70
 
64
-    getResponsiblesRender(resp: Array<string>) {
71
+    getManagersRender(resp: Array<string>) {
65
         let final = [];
72
         let final = [];
66
         for (let i = 0; i < resp.length; i++) {
73
         for (let i = 0; i < resp.length; i++) {
67
-            final.push(<Paragraph>{resp[i]}</Paragraph>)
74
+            final.push(<Paragraph key={i.toString()}>{resp[i]}</Paragraph>)
68
         }
75
         }
69
         const hasManagers = resp.length > 0;
76
         const hasManagers = resp.length > 0;
70
         return (
77
         return (
120
                               onLinkPress={openWebLink}/>
127
                               onLinkPress={openWebLink}/>
121
                     </Card.Content>
128
                     </Card.Content>
122
                     : <View/>}
129
                     : <View/>}
123
-                {this.getResponsiblesRender(this.displayData.responsibles)}
130
+                {this.getManagersRender(this.displayData.responsibles)}
124
             </ScrollView>
131
             </ScrollView>
125
         );
132
         );
126
     }
133
     }

+ 11
- 27
screens/Amicale/ClubListScreen.js View File

6
 import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
6
 import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
7
 import i18n from "i18n-js";
7
 import i18n from "i18n-js";
8
 import ClubListItem from "../../components/Lists/ClubListItem";
8
 import ClubListItem from "../../components/Lists/ClubListItem";
9
+import {isItemInCategoryFilter, stringMatchQuery} from "../../utils/Search";
9
 
10
 
10
 type Props = {
11
 type Props = {
11
     navigation: Object,
12
     navigation: Object,
27
     colors: Object;
28
     colors: Object;
28
 
29
 
29
     getRenderItem: Function;
30
     getRenderItem: Function;
30
-    originalData: Array<Object>;
31
     categories: Array<Object>;
31
     categories: Array<Object>;
32
 
32
 
33
     constructor(props) {
33
     constructor(props) {
69
      * @param str The new search string
69
      * @param str The new search string
70
      */
70
      */
71
     onSearchStringChange = (str: string) => {
71
     onSearchStringChange = (str: string) => {
72
-        this.updateFilteredData(this.sanitizeString(str), null);
72
+        this.updateFilteredData(str, null);
73
     };
73
     };
74
 
74
 
75
-    /**
76
-     * Sanitizes the given string to improve search performance
77
-     *
78
-     * @param str The string to sanitize
79
-     * @return {string} The sanitized string
80
-     */
81
-    sanitizeString(str: string): string {
82
-        return str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
83
-    }
84
-
85
     keyExtractor = (item: Object) => {
75
     keyExtractor = (item: Object) => {
86
-        return item.name + item.logo;
76
+        return item.id.toString();
87
     };
77
     };
88
 
78
 
89
     getScreen = (data: Object) => {
79
     getScreen = (data: Object) => {
90
         this.categories = data.categories;
80
         this.categories = data.categories;
91
         return (
81
         return (
82
+            //$FlowFixMe
92
             <FlatList
83
             <FlatList
93
                 data={data.clubs}
84
                 data={data.clubs}
94
                 keyExtractor={this.keyExtractor}
85
                 keyExtractor={this.keyExtractor}
112
             if (index === -1)
103
             if (index === -1)
113
                 newCategoriesState.push(categoryId);
104
                 newCategoriesState.push(categoryId);
114
             else
105
             else
115
-                newCategoriesState.splice(index);
106
+                newCategoriesState.splice(index,1);
116
         }
107
         }
117
         if (filterStr !== null || categoryId !== null)
108
         if (filterStr !== null || categoryId !== null)
118
             this.setState({
109
             this.setState({
121
             })
112
             })
122
     }
113
     }
123
 
114
 
124
-    isItemInCategoryFilter(categories: Array<string>) {
125
-        for (const category of categories) {
126
-            if (this.state.currentlySelectedCategories.indexOf(category) !== -1)
127
-                return true;
128
-        }
129
-        return false;
130
-    }
131
-
132
-    getChipRender = (category: Object) => {
115
+    getChipRender = (category: Object, key: string) => {
133
         const onPress = this.onChipSelect.bind(this, category.id);
116
         const onPress = this.onChipSelect.bind(this, category.id);
134
         return <Chip
117
         return <Chip
135
-            selected={this.isItemInCategoryFilter([category.id])}
118
+            selected={isItemInCategoryFilter(this.state.currentlySelectedCategories, [category.id])}
136
             mode={'outlined'}
119
             mode={'outlined'}
137
             onPress={onPress}
120
             onPress={onPress}
138
             style={{marginRight: 5, marginBottom: 5}}
121
             style={{marginRight: 5, marginBottom: 5}}
122
+            key={key}
139
         >
123
         >
140
             {category.name}
124
             {category.name}
141
         </Chip>;
125
         </Chip>;
144
     getListHeader() {
128
     getListHeader() {
145
         let final = [];
129
         let final = [];
146
         for (let i = 0; i < this.categories.length; i++) {
130
         for (let i = 0; i < this.categories.length; i++) {
147
-            final.push(this.getChipRender(this.categories[i]));
131
+            final.push(this.getChipRender(this.categories[i], this.categories[i].id));
148
         }
132
         }
149
         return <View style={{
133
         return <View style={{
150
             justifyContent: 'space-around',
134
             justifyContent: 'space-around',
163
 
147
 
164
     shouldRenderItem(item) {
148
     shouldRenderItem(item) {
165
         let shouldRender = this.state.currentlySelectedCategories.length === 0
149
         let shouldRender = this.state.currentlySelectedCategories.length === 0
166
-            || this.isItemInCategoryFilter(item.category);
150
+            || isItemInCategoryFilter(this.state.currentlySelectedCategories, item.category);
167
         if (shouldRender)
151
         if (shouldRender)
168
-            shouldRender = this.sanitizeString(item.name).includes(this.state.currentSearchString);
152
+            shouldRender = stringMatchQuery(item.name, this.state.currentSearchString);
169
         return shouldRender;
153
         return shouldRender;
170
     }
154
     }
171
 
155
 

+ 24
- 0
utils/Search.js View File

1
+
2
+
3
+
4
+/**
5
+ * Sanitizes the given string to improve search performance
6
+ *
7
+ * @param str The string to sanitize
8
+ * @return {string} The sanitized string
9
+ */
10
+export function sanitizeString(str: string): string {
11
+    return str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
12
+}
13
+
14
+export function stringMatchQuery(str: string, query: string) {
15
+    return sanitizeString(str).includes(sanitizeString(query));
16
+}
17
+
18
+export function isItemInCategoryFilter(filter: Array<string>, categories: Array<string>) {
19
+    for (const category of categories) {
20
+        if (filter.indexOf(category) !== -1)
21
+            return true;
22
+    }
23
+    return false;
24
+}

Loading…
Cancel
Save