Advertisement
code_paster

Untitled

Jul 1st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import {
  2. NEW_BASE,
  3. NEW_SAUSE,
  4. NEW_TOPPING,
  5. REMOVE_TOPPING,
  6. DRONE_DELIVERY,
  7. NODRONE_DELIVERY
  8. } from '../actions/pizza'
  9. import { pizzabases, sauses, toppings } from '../shared/data'
  10.  
  11. const initialState = {
  12. base: {
  13. id: '',
  14. price: 0
  15. },
  16. sauses: {
  17. id: '',
  18. price: 0
  19. },
  20. toppings: [],
  21. delivery: false,
  22. totalPrice: 0
  23. }
  24.  
  25. const totalPrice = (state) => {
  26. var totalPrice = 0;
  27. totalPrice = state.base.price + state.sauses.price;
  28. if (state.toppings.length > 0) {
  29. state.toppings.map((topping) => {
  30. totalPrice = totalPrice + topping.price;
  31. });
  32. }
  33.  
  34. if (state.delivery === true) totalPrice = totalPrice + totalPrice * 0.1;
  35. return +(Math.round(totalPrice + "e+2") + "e-2")
  36. }
  37.  
  38. export default function (state = initialState, action) {
  39. switch (action.type) {
  40. case NEW_BASE:
  41. state.base = {
  42. id: pizzabases[action.payload.id - 1].id,
  43. price: pizzabases[action.payload.id - 1].price
  44. }
  45. state.totalPrice = totalPrice(state);
  46. return { ...state };
  47. case NEW_SAUSE:
  48. state.sauses = {
  49. id: sauses[action.payload.id - 1].id,
  50. price: sauses[action.payload.id - 1].price
  51. }
  52. state.totalPrice = totalPrice(state);
  53. return { ...state };
  54. case NEW_TOPPING:
  55. state.toppings = [...state.toppings, {
  56. id: toppings[action.payload.id - 1].id,
  57. price: toppings[action.payload.id - 1].price
  58. }]
  59. state.totalPrice = totalPrice(state);
  60. return { ...state };
  61. case REMOVE_TOPPING:
  62. var amount = state.toppings.length;
  63. if (amount === 1) {
  64. state.toppings = [];
  65. } else {
  66. var toppingsArray = state.toppings;
  67. toppingsArray.map((topping, index) => {
  68. if(topping.id === action.payload.id - 1) state.toppings.splice(index, 1);
  69. });
  70. }
  71. state.totalPrice = totalPrice(state);
  72. return { ...state }
  73. case DRONE_DELIVERY:
  74. state.delivery = action.payload.select;
  75. state.totalPrice = totalPrice(state);
  76. return { ...state }
  77. case NODRONE_DELIVERY:
  78. state.delivery = action.payload.select;
  79. state.totalPrice = totalPrice(state);
  80. return { ...state }
  81. default:
  82. return state
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement