Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import _ from 'underscore';
- import DishStepService from './DishStepService';
- import DishService from './DishService';
- import IngredientService from './IngredientService';
- import menuMealShape from '../components/common/shapes/menuMealShape';
- import summaryShape from '../components/common/shapes/dishSummaryShape';
- import menuApi from '../api/menuApi';
- class MenuService {
- constructor(dishes, ingredients) {
- this.menu = [];
- this.dishes = dishes;
- this.ingredients = ingredients;
- }
- mealEnums = {
- breakfast: 20,
- secondBreakfast: 21,
- dinner: 22,
- subNight: 23,
- supper: 24,
- addition: 25,
- }
- emptyMenuMeal = {
- breakfast: JSON.parse(JSON.stringify(menuMealShape)),
- secondBreakfast: JSON.parse(JSON.stringify(menuMealShape)),
- dinner: JSON.parse(JSON.stringify(menuMealShape)),
- subNight: JSON.parse(JSON.stringify(menuMealShape)),
- supper: JSON.parse(JSON.stringify(menuMealShape)),
- summary: JSON.parse(JSON.stringify(summaryShape))
- }
- getNewMenu = () =>
- menuApi.menu.getMenu(this.dietId, this.date).then(res => {
- this.menu = res;
- })
- getMenuMealComponents = (menuMealComponents) => {
- const meals = [];
- for (let i = 0; i < menuMealComponents.length; i++) {
- let component = {};
- if (menuMealComponents[i].dishId != null && menuMealComponents[i].dishId !== 0) {
- component = DishService.getDishById(menuMealComponents[i].dishId, this.dishes)
- }
- else {
- component = IngredientService.getIngredientById(menuMealComponents[i].ingredientId, this.ingredients)
- component.price = component.prices[0].price;
- }
- component.steps = [];
- component.multiplier = menuMealComponents[i].multiplier;
- component.calories = (component.multiplier * (component.weight * component.nutrition.calories / 100)).toFixed(0);
- component.basicWeight = component.weight;
- component.weight = (component.basicWeight * component.multiplier).toFixed(2);
- component.weightAfterThermalChange = component.thermalChangeFactor * component.weight / 100;
- component.iconSteps = _.has(component, 'dishId') ? 'caret down' : null;
- meals.push(component);
- }
- return meals;
- }
- getMenuMealToShape = (destiny, menu) => {
- const menuMeal = menu.menuMeals.filter(m => m.mealId === this.mealEnums[destiny])[0];
- const thermo = DishService.castDishThermoToBoolean(menuMeal.thermo);
- const meals = this.getMenuMealComponents(menuMeal.menuMealComponents);
- return {
- temperatureWarm: thermo.temperatureWarm,
- temperatureCold: thermo.temperatureCold,
- name: menuMeal.name,
- meals,
- summary: DishStepService.getUpdatedSummary(meals, false)
- }
- }
- getByCaloriesId = (caloriesId) =>
- this.menu.filter(m => m.caloriesId === caloriesId)[0]
- getMenuMeals = () => {
- const menu = this.getByCaloriesId(this.caloriesId);
- const values = {
- breakfast: this.getMenuMealToShape('breakfast', menu),
- secondBreakfast: this.getMenuMealToShape('secondBreakfast', menu),
- dinner: this.getMenuMealToShape('dinner', menu),
- subNight: this.getMenuMealToShape('subNight', menu),
- supper: this.getMenuMealToShape('supper', menu),
- summary: {}
- }
- values.summary = MenuService.getUpdatedAllMealsSummary(values)
- return values;
- }
- isChangingCalories = () => {
- if (this.menu.length > 0) {
- if (this.menu[0].date === this.date &&
- this.menu[0].dietId === this.dietId &&
- this.menu[0].caloriesId !== this.caloriesId) {
- return true;
- }
- return false;
- }
- return false;
- }
- getMenu = (dietId, date, caloriesId) => {
- this.dietId = dietId;
- this.date = date;
- this.caloriesId = caloriesId === 0 ? 1 : caloriesId;
- if (dietId === 0 || date === '') {
- return new Promise((resolve) => {
- resolve([]);
- })
- }
- if (this.isChangingCalories()) {
- console.log('zmiana kalorycznosci')
- return new Promise((resolve) => {
- resolve(this.getMenuMeals());
- })
- }
- return this.getNewMenu().then(() => {
- if (this.menu.length === 0)
- return _.clone(this.emptyMenuMeal);
- return this.getMenuMeals()
- })
- }
- static getUpdatedAllMealsSummary = (values) => {
- const meals = values.breakfast.meals.concat(
- values.secondBreakfast.meals,
- values.dinner.meals,
- values.subNight.meals,
- values.supper.meals);
- return DishStepService.getUpdatedSummary(meals, false);
- }
- }
- export default MenuService;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement