Reputation: 159
I have a problem with the filtration of goods.
Is it possible to solve this situation somehow more beautifully?
Here, with the addition of one more parameter, the volume of code is increased by factorial, so I see such a terrible solution =)
let query = firebase.firestore().collection('products').orderBy('price')
// TODO: How I can rewrite it?
if (payload.category && payload.size && payload.color) {
query = query
.where('category', '==', payload.category)
.where('color', '==', payload.color)
.where('size', '==', payload.size)
} else if (payload.size && payload.color) {
query = query
.where('color', '==', payload.color)
.where('size', '==', payload.size)
} else if (payload.category && payload.color) {
query = query
.where('category', '==', payload.category)
.where('color', '==', payload.color)
} else if (payload.size && payload.category) {
query = query
.where('category', '==', payload.category)
.where('size', '==', payload.size)
} else if (payload.size) {
query = query
.where('size', '==', payload.size)
} else if (payload.category) {
query = query
.where('category', '==', payload.category)
} else if (payload.color) {
query = query
.where('color', '==', payload.color)
}
query.get() ...
Upvotes: 1
Views: 546
Reputation: 5271
Get rid of the giant if/else structure and just add the query
for each that is needed.
let query = firebase.firestore().collection('products').orderBy('price')
if (payload.size) {
query = query.where('size', '==', payload.size)
}
if (payload.category) {
query = query.where('category', '==', payload.category)
}
if (payload.color) {
query = query.where('color', '==', payload.color)
}
Upvotes: 3