Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1.  
  2. import _ from 'underscore';
  3. import DishStepService from './DishStepService';
  4. import DishService from './DishService';
  5. import IngredientService from './IngredientService';
  6. import menuMealShape from '../components/common/shapes/menuMealShape';
  7. import summaryShape from '../components/common/shapes/dishSummaryShape';
  8. import menuApi from '../api/menuApi';
  9.  
  10. class MenuService {
  11.  
  12. constructor(dishes, ingredients) {
  13. this.menu = [];
  14. this.dishes = dishes;
  15. this.ingredients = ingredients;
  16. }
  17.  
  18. mealEnums = {
  19. breakfast: 20,
  20. secondBreakfast: 21,
  21. dinner: 22,
  22. subNight: 23,
  23. supper: 24,
  24. addition: 25,
  25. }
  26.  
  27. emptyMenuMeal = {
  28. breakfast: JSON.parse(JSON.stringify(menuMealShape)),
  29. secondBreakfast: JSON.parse(JSON.stringify(menuMealShape)),
  30. dinner: JSON.parse(JSON.stringify(menuMealShape)),
  31. subNight: JSON.parse(JSON.stringify(menuMealShape)),
  32. supper: JSON.parse(JSON.stringify(menuMealShape)),
  33. summary: JSON.parse(JSON.stringify(summaryShape))
  34. }
  35.  
  36. getNewMenu = () =>
  37. menuApi.menu.getMenu(this.dietId, this.date).then(res => {
  38. this.menu = res;
  39. })
  40.  
  41. getMenuMealComponents = (menuMealComponents) => {
  42. const meals = [];
  43. for (let i = 0; i < menuMealComponents.length; i++) {
  44. let component = {};
  45.  
  46. if (menuMealComponents[i].dishId != null && menuMealComponents[i].dishId !== 0) {
  47. component = DishService.getDishById(menuMealComponents[i].dishId, this.dishes)
  48. }
  49. else {
  50. component = IngredientService.getIngredientById(menuMealComponents[i].ingredientId, this.ingredients)
  51. component.price = component.prices[0].price;
  52. }
  53.  
  54. component.steps = [];
  55. component.multiplier = menuMealComponents[i].multiplier;
  56. component.calories = (component.multiplier * (component.weight * component.nutrition.calories / 100)).toFixed(0);
  57. component.basicWeight = component.weight;
  58. component.weight = (component.basicWeight * component.multiplier).toFixed(2);
  59. component.weightAfterThermalChange = component.thermalChangeFactor * component.weight / 100;
  60. component.iconSteps = _.has(component, 'dishId') ? 'caret down' : null;
  61. meals.push(component);
  62. }
  63. return meals;
  64. }
  65.  
  66. getMenuMealToShape = (destiny, menu) => {
  67. const menuMeal = menu.menuMeals.filter(m => m.mealId === this.mealEnums[destiny])[0];
  68. const thermo = DishService.castDishThermoToBoolean(menuMeal.thermo);
  69. const meals = this.getMenuMealComponents(menuMeal.menuMealComponents);
  70.  
  71. return {
  72. temperatureWarm: thermo.temperatureWarm,
  73. temperatureCold: thermo.temperatureCold,
  74. name: menuMeal.name,
  75. meals,
  76. summary: DishStepService.getUpdatedSummary(meals, false)
  77. }
  78. }
  79.  
  80. getByCaloriesId = (caloriesId) =>
  81. this.menu.filter(m => m.caloriesId === caloriesId)[0]
  82.  
  83. getMenuMeals = () => {
  84. const menu = this.getByCaloriesId(this.caloriesId);
  85. const values = {
  86. breakfast: this.getMenuMealToShape('breakfast', menu),
  87. secondBreakfast: this.getMenuMealToShape('secondBreakfast', menu),
  88. dinner: this.getMenuMealToShape('dinner', menu),
  89. subNight: this.getMenuMealToShape('subNight', menu),
  90. supper: this.getMenuMealToShape('supper', menu),
  91. summary: {}
  92. }
  93. values.summary = MenuService.getUpdatedAllMealsSummary(values)
  94. return values;
  95. }
  96.  
  97. isChangingCalories = () => {
  98. if (this.menu.length > 0) {
  99.  
  100. if (this.menu[0].date === this.date &&
  101. this.menu[0].dietId === this.dietId &&
  102. this.menu[0].caloriesId !== this.caloriesId) {
  103.  
  104. return true;
  105. }
  106. return false;
  107. }
  108. return false;
  109. }
  110.  
  111. getMenu = (dietId, date, caloriesId) => {
  112. this.dietId = dietId;
  113. this.date = date;
  114. this.caloriesId = caloriesId === 0 ? 1 : caloriesId;
  115.  
  116. if (dietId === 0 || date === '') {
  117. return new Promise((resolve) => {
  118. resolve([]);
  119. })
  120. }
  121. if (this.isChangingCalories()) {
  122. console.log('zmiana kalorycznosci')
  123. return new Promise((resolve) => {
  124. resolve(this.getMenuMeals());
  125. })
  126. }
  127.  
  128. return this.getNewMenu().then(() => {
  129. if (this.menu.length === 0)
  130. return _.clone(this.emptyMenuMeal);
  131.  
  132. return this.getMenuMeals()
  133. })
  134. }
  135.  
  136. static getUpdatedAllMealsSummary = (values) => {
  137. const meals = values.breakfast.meals.concat(
  138. values.secondBreakfast.meals,
  139. values.dinner.meals,
  140. values.subNight.meals,
  141. values.supper.meals);
  142.  
  143. return DishStepService.getUpdatedSummary(meals, false);
  144. }
  145. }
  146.  
  147. export default MenuService;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement