-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed filter functionality along with addition of more functionality
- Loading branch information
Showing
9 changed files
with
243 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,113 @@ | ||
import { FILTER_BY_PRICE ,FILTER_BY_CATEGORY} from "../types"; | ||
export const filterByPrice = (price) => { | ||
import { FILTERED_PRODUCTS } from "../types"; | ||
// export const filterByPrice = (price,noOfProducts) => { | ||
// return (dispatch) => { | ||
// console.log(noOfProducts) | ||
// let newArray = [] | ||
// fetch("http://localhost:4000/filterByPrice", { | ||
// method: "post", | ||
// headers: { | ||
// authorization: "Bearer " + localStorage.getItem("jwt"), | ||
// "Content-Type": "application/json", | ||
// }, | ||
// body: JSON.stringify({ | ||
// price: price, | ||
// }), | ||
// }) | ||
// .then((res) => res.json()) | ||
// .then((filteredProducts) => { | ||
// for(let i = 0 ; i < noOfProducts && i < filteredProducts.length; i++){ | ||
// // filteredProducts[i] = | ||
// newArray.push(filteredProducts[i]) | ||
// } | ||
// dispatch({ | ||
// type: FILTER_BY_PRICE, | ||
// payload: newArray, | ||
// }); | ||
// }); | ||
// }; | ||
// }; | ||
|
||
// export const filterByCategory = (category,noOfProducts) => { | ||
// return (dispatch)=>{ | ||
// fetch("http://localhost:4000/filterByCategory",{ | ||
// method:"post", | ||
// headers:{ | ||
// authorization:"Bearer "+localStorage.getItem("jwt"), | ||
// "Content-Type":"application/json" | ||
// }, | ||
// body:JSON.stringify({ | ||
// category:category | ||
// }) | ||
// }) | ||
// .then(res => res.json()) | ||
// .then(filteredProducts =>{ | ||
// dispatch({ | ||
// type:FILTER_BY_CATEGORY, | ||
// payload:filteredProducts | ||
// }) | ||
// } ) | ||
// } | ||
// }; | ||
|
||
export const applyFilter = (price, category, noOfProducts) => { | ||
return (dispatch) => { | ||
fetch("http://localhost:4000/filterByPrice", { | ||
fetch("http://localhost:4000/filter", { | ||
method: "post", | ||
headers: { | ||
authorization: "Bearer " + localStorage.getItem("jwt"), | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
price: price, | ||
category: category, | ||
noOfProducts: noOfProducts, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((filteredProducts) => { | ||
dispatch({ | ||
type: FILTER_BY_PRICE, | ||
payload: filteredProducts, | ||
.then((products) => { | ||
console.log(products) | ||
// console.log(products); | ||
let newArrayOfProducts = []; | ||
let putInArray = (product) => { | ||
newArrayOfProducts.push(product); | ||
}; | ||
new Promise((resolve, rej) => { | ||
products.map((product, ind) => { | ||
if (product.rating.length !== 0) { | ||
let sum = 0; | ||
new Promise((res, rej) => { | ||
let len = product.rating.length | ||
product.rating.map((eachRating, index) => { | ||
sum = sum + eachRating.value; | ||
if(index === len-1){ | ||
res() | ||
console.log("resolved") | ||
} | ||
}); | ||
sum = sum / len; | ||
product.rating = { | ||
value: sum, | ||
}; | ||
}).then(() => { | ||
if(ind === products.length-1) | ||
resolve() | ||
putInArray(product) | ||
}); | ||
} else if (ind === products.length - 1) { | ||
product.rating = { | ||
value: 0, | ||
}; | ||
putInArray(product); | ||
resolve(); | ||
} else { | ||
product.rating = { | ||
value: 0, | ||
}; | ||
putInArray(product); | ||
} | ||
}); | ||
}).then(() => { | ||
dispatch({ type: FILTERED_PRODUCTS, products: newArrayOfProducts }); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
export const filterByCategory = (category) => { | ||
return (dispatch)=>{ | ||
fetch("http://localhost:4000/filterByCategory",{ | ||
method:"post", | ||
headers:{ | ||
authorization:"Bearer "+localStorage.getItem("jwt"), | ||
"Content-Type":"application/json" | ||
}, | ||
body:JSON.stringify({ | ||
category:category | ||
}) | ||
}) | ||
.then(res => res.json()) | ||
.then(filteredProducts =>{ | ||
dispatch({ | ||
type:FILTER_BY_CATEGORY, | ||
payload:filteredProducts | ||
}) | ||
} ) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,8 @@ | ||
import { FETCH_ALL_PRODUCTS, FILTER_BY_PRICE, FILTER_BY_CATEGORY } from "../types"; | ||
import { FILTERED_PRODUCTS } from "../types"; | ||
export const dbdataReducer = (state = null, action) => { | ||
if (action.type === FETCH_ALL_PRODUCTS) { | ||
return action.dbdata; | ||
if (action.type === FILTERED_PRODUCTS) { | ||
return action.products; | ||
} | ||
|
||
else if (action.type === FILTER_BY_PRICE) { | ||
return action.payload | ||
} | ||
|
||
else if(action.type === FILTER_BY_CATEGORY){ | ||
return action.payload | ||
} | ||
|
||
else return state; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.