@@ -16,6 +16,7 @@ import {
16
16
Loader ,
17
17
Dimmer
18
18
} from 'semantic-ui-react'
19
+ import { restaurantSort , restaurantScorer } from '../helperFuncs'
19
20
20
21
class RestaurantList extends Component {
21
22
constructor ( ) {
@@ -41,8 +42,9 @@ class RestaurantList extends Component {
41
42
foursquareWeight,
42
43
restaurants
43
44
} = this . props
45
+
44
46
restaurants . forEach ( restaurant => {
45
- restaurant . score = this . restaurantScore (
47
+ restaurant . score = restaurantScorer (
46
48
restaurant . reviews ,
47
49
yelpWeight ,
48
50
zomatoWeight ,
@@ -72,57 +74,6 @@ class RestaurantList extends Component {
72
74
this . setState ( { loading : false } )
73
75
}
74
76
75
- restaurantScore = (
76
- reviews ,
77
- yelpWeight ,
78
- zomatoWeight ,
79
- googleWeight ,
80
- foursquareWeight
81
- ) => {
82
- let totalWeight = yelpWeight
83
- let reviewsString = ''
84
- reviews . forEach ( review => {
85
- reviewsString += review . source
86
- } )
87
- const reviewsLength = reviews . length
88
-
89
- if ( reviewsString . indexOf ( 'Zomato' ) !== - 1 ) totalWeight += zomatoWeight
90
- if ( reviewsString . indexOf ( 'Google' ) !== - 1 ) totalWeight += googleWeight
91
- if ( reviewsString . indexOf ( 'Foursquare' ) !== - 1 )
92
- totalWeight += foursquareWeight
93
-
94
- function weighter ( sourceWeight , review ) {
95
- const weight = sourceWeight / totalWeight * reviewsLength
96
- let denominator
97
- if ( review . source === 'Foursquare' ) {
98
- denominator = 10
99
- } else {
100
- denominator = 5
101
- }
102
- const rating = review . rating / denominator
103
- return weight * rating
104
- }
105
-
106
- return Math . round (
107
- reviews
108
- . map ( review => {
109
- if ( review . source === 'Yelp' ) {
110
- return weighter ( yelpWeight , review )
111
- } else if ( review . source === 'Zomato' ) {
112
- return weighter ( zomatoWeight , review )
113
- } else if ( review . source === 'Google' ) {
114
- return weighter ( googleWeight , review )
115
- } else if ( review . source === 'Foursquare' ) {
116
- return weighter ( foursquareWeight , review )
117
- }
118
- } )
119
- . reduce ( ( accum , currentVal ) => accum + currentVal , 0 ) /
120
- reviews . length *
121
- 100 ,
122
- 0
123
- )
124
- }
125
-
126
77
async handleChange ( event ) {
127
78
await this . props . updateSearchBar ( event . target . value )
128
79
@@ -187,7 +138,7 @@ class RestaurantList extends Component {
187
138
} = this . props
188
139
189
140
this . props . restaurants . forEach ( restaurant => {
190
- restaurant . score = this . restaurantScore (
141
+ restaurant . score = restaurantScorer (
191
142
restaurant . reviews ,
192
143
yelpWeight ,
193
144
zomatoWeight ,
@@ -230,23 +181,11 @@ class RestaurantList extends Component {
230
181
/>
231
182
< Divider hidden />
232
183
< Card . Group >
233
- { restaurants
234
- . sort ( ( restaurant1 , restaurant2 ) => {
235
- if ( restaurant2 . reviews . length === restaurant1 . reviews . length ) {
236
- if ( restaurant2 . score === restaurant1 . score ) {
237
- return restaurant2 . id - restaurant1 . id
238
- } else {
239
- return restaurant2 . score - restaurant1 . score
240
- }
241
- } else {
242
- return restaurant2 . reviews . length - restaurant1 . reviews . length
243
- }
244
- } )
245
- . map ( restaurant => {
246
- return (
247
- < RestaurantCard restaurant = { restaurant } key = { restaurant . id } />
248
- )
249
- } ) }
184
+ { restaurantSort ( restaurants ) . map ( restaurant => {
185
+ return (
186
+ < RestaurantCard restaurant = { restaurant } key = { restaurant . id } />
187
+ )
188
+ } ) }
250
189
</ Card . Group >
251
190
</ React . Fragment >
252
191
)
0 commit comments