Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

145 linhas
3.8 KiB

  1. import { useMemo } from 'react'
  2. import { createStore, applyMiddleware } from 'redux'
  3. import { composeWithDevTools } from 'redux-devtools-extension'
  4. import { productsData } from './products'
  5. let store
  6. const initialState = {
  7. products: productsData,
  8. cart: [],
  9. total: 0,
  10. }
  11. const reducer = (state = initialState, action) => {
  12. switch (action.type) {
  13. case 'ADD_TO_CART':
  14. let addedItem = state.products.find(item => item.id === action.id)
  15. let existed_item = state.cart.find(item => action.id === item.id)
  16. if(existed_item){
  17. addedItem.quantity += 1
  18. return {
  19. ...state,
  20. total: state.total + addedItem.price
  21. }
  22. } else {
  23. addedItem.quantity = 1
  24. let newTotal = state.total + addedItem.price
  25. return {
  26. ...state,
  27. cart: [...state.cart, addedItem],
  28. total: newTotal
  29. }
  30. }
  31. case 'ADD_QUANTITY':
  32. let existingItem = state.cart.find(item => item.id === action.id)
  33. existingItem.quantity += 1
  34. let newTotal = state.total + existingItem.price
  35. return {
  36. ...state,
  37. total: newTotal
  38. }
  39. case 'SUB_QUANTITY':
  40. let exItem = state.products.find(item=> item.id === action.id)
  41. if(exItem.quantity === 1){
  42. let new_items = state.cart.filter(item=>item.id !== action.id)
  43. let newTotal = state.total - exItem.price
  44. return {
  45. ...state,
  46. cart: new_items,
  47. total: newTotal
  48. }
  49. } else {
  50. exItem.quantity -= 1
  51. let newTotal = state.total - exItem.price
  52. return {
  53. ...state,
  54. total: newTotal
  55. }
  56. }
  57. case 'ADD_QUANTITY_WITH_NUMBER':
  58. let addedItemD = state.products.find(item => item.id === action.id)
  59. //check if the action id exists in the addedItems
  60. let existed_itemd = state.cart.find(item=> action.id === item.id)
  61. if(existed_itemd)
  62. {
  63. addeaddedItemDdItem.quantity += action.qty
  64. return {
  65. ...state,
  66. total: state.total + addedItemD.price * action.qty
  67. }
  68. } else {
  69. addedItemD.quantity = action.qty;
  70. //calculating the total
  71. let newTotal = state.total + addedItemD.price * action.qty
  72. return {
  73. ...state,
  74. cart: [...state.cart, addedItemD],
  75. total : newTotal
  76. }
  77. }
  78. case 'REMOVE_ITEM':
  79. let itemToRemove = state.cart.find(item=> action.id === item.id)
  80. let new_items = state.cart.filter(item=> action.id !== item.id)
  81. //calculating the total
  82. let newTotalRemove = state.total - (itemToRemove.price * itemToRemove.quantity );
  83. return {
  84. ...state,
  85. cart: new_items,
  86. total: newTotalRemove
  87. }
  88. case 'RESET':
  89. return {
  90. ...state,
  91. cart: [],
  92. }
  93. default:
  94. return state
  95. }
  96. }
  97. function initStore(preloadedState = initialState) {
  98. return createStore(
  99. reducer,
  100. preloadedState,
  101. composeWithDevTools(applyMiddleware())
  102. )
  103. }
  104. export const initializeStore = (preloadedState) => {
  105. let _store = store ?? initStore(preloadedState)
  106. // After navigating to a page with an initial Redux state, merge that state
  107. // with the current state in the store, and create a new store
  108. if (preloadedState && store) {
  109. _store = initStore({
  110. ...store.getState(),
  111. ...preloadedState,
  112. })
  113. // Reset the current store
  114. store = undefined
  115. }
  116. // For SSG and SSR always create a new store
  117. if (typeof window === 'undefined') return _store
  118. // Create the store once in the client
  119. if (!store) store = _store
  120. return _store
  121. }
  122. export function useStore(initialState) {
  123. const store = useMemo(() => initializeStore(initialState), [initialState])
  124. return store
  125. }