pizza.service.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {pizzaList} from '../data/pizzaList.js';
  2. import {renderCards} from '../view/pizza.render.js'
  3. export default class PizzaService {
  4. constructor() {
  5. }
  6. // search by name
  7. get initialList () {
  8. return [...pizzaList];
  9. }
  10. filterByName() {
  11. const searchInputValue = document.querySelector('#search').value;
  12. const searchResult = [...pizzaList].filter(pizza => {
  13. const searchedName = pizza.name.toLocaleLowerCase().includes(searchInputValue.toLocaleLowerCase());;
  14. const searchedIngredients = pizza.composition.join(', ').toLocaleLowerCase().includes(searchInputValue.toLocaleLowerCase());
  15. return searchedName || searchedIngredients;
  16. })
  17. renderCards(searchResult);
  18. }
  19. sortPizzas(resultArr, dir = "default"){
  20. resultArr = [...pizzaList].sort((a, b) => {
  21. if (dir === 'default') return this.initialList;
  22. if (dir === 'less') return b.price - a.price;
  23. if (dir === 'more') return a.price - b.price;
  24. })
  25. return resultArr;
  26. }
  27. filterByPrice(e) {
  28. e.preventDefault();
  29. let small = [...pizzaList].reduce((acc, current) => acc.price < current.price ? acc : current);
  30. let big = [...pizzaList].reduce((acc, current) => acc.price > current.price ? acc : current);
  31. smallPrice.placeholder = small.price;
  32. bigPrice.placeholder = big.price;
  33. const smallPriceAmount = smallPrice.value ? smallPrice.value : small.price;
  34. const bigPriceAmount = bigPrice.value ? bigPrice.value : big.price;
  35. const neededPizzas = [...pizzaList].filter(goods => goods.price >= smallPriceAmount && goods.price <= bigPriceAmount)
  36. renderCards(neededPizzas);
  37. }
  38. filterByCaloricity(e) {
  39. e.preventDefault();
  40. let small = [...pizzaList].reduce((acc, current) => acc.caloricity < current.caloricity ? acc : current);
  41. let big = [...pizzaList].reduce((acc, current) => acc.caloricity > current.caloricity ? acc : current);
  42. smallCaloricity.placeholder = small.caloricity;
  43. bigCaloricity.placeholder = big.caloricity;
  44. const smallCaloricityAmount = smallCaloricity.value ? smallCaloricity.value : small.caloricity;
  45. const bigCaloricityAmount = bigCaloricity.value ? bigCaloricity.value : big.caloricity;
  46. const searchedCaloricity = [...pizzaList].filter(goods => goods.caloricity >= smallCaloricityAmount && goods.caloricity <= bigCaloricityAmount)
  47. renderCards(searchedCaloricity);
  48. }
  49. }