npm-pkg-hook 1.1.5 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/.eslintrc.js +19 -113
  2. package/.eslintrc.json +127 -108
  3. package/.github/pull_request_template.md +17 -17
  4. package/.github/workflows/pepeline.yaml +29 -29
  5. package/next.config.js +2 -5
  6. package/package.json +27 -24
  7. package/script.txt +6 -6
  8. package/src/config/client/errors.js +14 -14
  9. package/src/config/client/index.js +1 -1
  10. package/src/hooks/getCategoriesWithProduct/helpers/index.js +7 -0
  11. package/src/hooks/getCategoriesWithProduct/index.js +62 -0
  12. package/src/hooks/getSession/index.js +18 -18
  13. package/src/hooks/index.js +13 -9
  14. package/src/hooks/useAcumulateDate/index.js +1 -1
  15. package/src/hooks/useAnimationFrame/index.js +45 -45
  16. package/src/hooks/useAsideCart/helpers/index.js +22 -0
  17. package/src/hooks/useAsideCart/index.js +156 -0
  18. package/src/hooks/useAsideCart/queries.js +10 -0
  19. package/src/hooks/useBanner/index.js +11 -11
  20. package/src/hooks/useCart/index.js +2 -0
  21. package/src/hooks/useCart/queries.js +164 -0
  22. package/src/hooks/useCart/useCart/helpers/index.js +75 -0
  23. package/src/hooks/useCart/useCart/index.js +411 -0
  24. package/src/hooks/useCart/useGetCart/index.js +31 -0
  25. package/src/hooks/useCatWithProduct/index.js +15 -15
  26. package/src/hooks/useCatWithProduct/queries.js +1 -1
  27. package/src/hooks/useCatWithProductClient/index.js +48 -0
  28. package/src/hooks/useCatWithProductClient/queries.js +55 -0
  29. package/src/hooks/useCategoriesProduct/index.js +12 -12
  30. package/src/hooks/useCategoriesProduct/queries.js +16 -16
  31. package/src/hooks/useCategoryInStore/index.js +39 -36
  32. package/src/hooks/useCategoryInStore/queries.js +78 -78
  33. package/src/hooks/useCategoryStore/index.js +2 -2
  34. package/src/hooks/useChartData/index.js +159 -156
  35. package/src/hooks/useChartData/useChartData/index.js +199 -197
  36. package/src/hooks/useChartData/useChartDataAllOrders/index.js +94 -94
  37. package/src/hooks/useCheckbox/index.js +115 -115
  38. package/src/hooks/useClients/index.js +12 -14
  39. package/src/hooks/useClients/queries.js +1 -1
  40. package/src/hooks/useConnection/index.js +20 -21
  41. package/src/hooks/useCreateProduct/helpers/useEditImageProduct/index.js +6 -2
  42. package/src/hooks/useCreateProduct/index.js +22 -19
  43. package/src/hooks/useDeleteSubProductOptional/index.js +31 -30
  44. package/src/hooks/useDeleteSubProductOptional/queries.js +10 -10
  45. package/src/hooks/useDessert/helpers/index.js +51 -51
  46. package/src/hooks/useDessert/index.js +56 -65
  47. package/src/hooks/useDevices/index.js +35 -36
  48. package/src/hooks/useDevices/queries.js +19 -19
  49. package/src/hooks/useDropzone/index.js +94 -79
  50. package/src/hooks/useDynamicAuth/index.js +13 -13
  51. package/src/hooks/useDynamicAuth/queries.js +24 -24
  52. package/src/hooks/useEmployee/queries.js +1 -1
  53. package/src/hooks/useEvent/index.js +33 -33
  54. package/src/hooks/useFavoriteStores/index.js +19 -0
  55. package/src/hooks/useFavoriteStores/queries.js +47 -0
  56. package/src/hooks/useFetchJson/index.js +25 -25
  57. package/src/hooks/useFingerprintjs/index.js +172 -176
  58. package/src/hooks/useFormTools/index.js +12 -11
  59. package/src/hooks/useFormatDate/index.js +20 -21
  60. package/src/hooks/useFormatNumberPhone/index.js +9 -9
  61. package/src/hooks/useFullScreenMode/index.js +65 -65
  62. package/src/hooks/useGenerateNumberArray/index.js +17 -17
  63. package/src/hooks/useGetExtProductFoodsSubOptionalAll/index.js +14 -0
  64. package/src/hooks/useGetFoodRecomended/index.js +33 -0
  65. package/src/hooks/useGetFoodRecomended/queries.js +47 -0
  66. package/src/hooks/useGetMinPrice/index.js +8 -0
  67. package/src/hooks/useGetMinPrice/queries.js +7 -0
  68. package/src/hooks/useGetOneStoreRating/index.js +40 -0
  69. package/src/hooks/useGetOneStoreRating/queries.js +18 -0
  70. package/src/hooks/useGoogleLogin/index.js +160 -161
  71. package/src/hooks/useHover/index.js +29 -29
  72. package/src/hooks/useImageOptimization/index.js +15 -15
  73. package/src/hooks/useImageWeight/index.js +18 -19
  74. package/src/hooks/useImagesStore/index.js +139 -140
  75. package/src/hooks/useImagesStore/queries.js +19 -18
  76. package/src/hooks/useInnerHtml/index.js +39 -38
  77. package/src/hooks/useIntersection/index.js +4 -4
  78. package/src/hooks/useKeypress/index.js +28 -28
  79. package/src/hooks/useLazyScript/index.js +35 -36
  80. package/src/hooks/useLocalSorage/index.js +2 -2
  81. package/src/hooks/useLocationNavigate/index.js +54 -54
  82. package/src/hooks/useLogout/index.js +24 -24
  83. package/src/hooks/useManageQueryParams/index.js +36 -37
  84. package/src/hooks/useMobile/index.js +39 -38
  85. package/src/hooks/useMutateHeight/index.js +15 -16
  86. package/src/hooks/useOrderClient/index.js +5 -0
  87. package/src/hooks/useOrders/index.js +10 -16
  88. package/src/hooks/useOrders/queries.js +1 -1
  89. package/src/hooks/useProductsFood/index.js +32 -32
  90. package/src/hooks/useProductsFood/queriesStore.js +49 -48
  91. package/src/hooks/useProductsFood/useEditProduct.js +2 -4
  92. package/src/hooks/useProductsFood/usetagsProducts.js +83 -83
  93. package/src/hooks/useProviders/index.js +3 -3
  94. package/src/hooks/useProviders/queries.js +31 -31
  95. package/src/hooks/useProviders/useProvidersCreateStore/index.js +12 -13
  96. package/src/hooks/useProviders/useProvidersDataStore/index.js +24 -24
  97. package/src/hooks/useProvidersStore/index.js +24 -24
  98. package/src/hooks/useProvidersStore/queries.js +31 -31
  99. package/src/hooks/useRatingArrayData/index.js +54 -0
  100. package/src/hooks/{useRatingData → useRatingArrayData}/queries.js +19 -18
  101. package/src/hooks/useReactToPrint/index.js +1120 -1098
  102. package/src/hooks/useRemoveExtraProductFoodsOptional/index.js +23 -23
  103. package/src/hooks/useRemoveExtraProductFoodsOptional/queries.js +48 -48
  104. package/src/hooks/useReport/index.js +9 -9
  105. package/src/hooks/useReport/queries.js +1 -1
  106. package/src/hooks/useRestaurant/index.js +19 -19
  107. package/src/hooks/useRestaurant/queries.js +80 -80
  108. package/src/hooks/useSales/index.js +476 -451
  109. package/src/hooks/useSales/queries.js +14 -15
  110. package/src/hooks/useSales/useGetAllSales/index.js +25 -25
  111. package/src/hooks/useSales/useTotalSales.js +18 -18
  112. package/src/hooks/useSaveAvailableProduct/helpers/index.js +29 -29
  113. package/src/hooks/useSaveAvailableProduct/index.js +16 -16
  114. package/src/hooks/useSaveAvailableProduct/queries.js +1 -1
  115. package/src/hooks/useSchedule/index.js +33 -36
  116. package/src/hooks/useSchedule/index.jsx +22 -22
  117. package/src/hooks/useSchedule/queries.js +34 -35
  118. package/src/hooks/useScheduleData/index.js +123 -171
  119. package/src/hooks/useScroll/index.js +56 -57
  120. package/src/hooks/useScrollRotate/index.js +16 -14
  121. package/src/hooks/useSetSession/index.js +16 -16
  122. package/src/hooks/useSetState/index.js +3 -3
  123. package/src/hooks/useStatusOpenStore/helpers/index.js +101 -102
  124. package/src/hooks/useStatusOpenStore/index.js +172 -173
  125. package/src/hooks/useStatusOrdersClient/helpers/index.js +14 -0
  126. package/src/hooks/useStatusOrdersClient/index.js +17 -0
  127. package/src/hooks/useStatusOrdersClient/queries.js +80 -0
  128. package/src/hooks/useStore/index.js +40 -25
  129. package/src/hooks/useStore/queries.js +162 -136
  130. package/src/hooks/useStoreCalendar/index.js +5 -7
  131. package/src/hooks/useStoreContacts/index.js +16 -16
  132. package/src/hooks/useStoreContacts/queries.js +1 -1
  133. package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
  134. package/src/hooks/useUpdateCart/index.js +34 -28
  135. package/src/hooks/useUpdateExistingOrders/index.js +35 -35
  136. package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +38 -37
  137. package/src/hooks/useUser/index.js +1 -1
  138. package/src/hooks/useUser/queries.js +1 -1
  139. package/src/hooks/useWindowSize/index.js +38 -38
  140. package/src/mock/dessert/index.js +16 -16
  141. package/src/mock/index.js +1 -2
  142. package/src/utils/index.js +42 -45
  143. package/src/hooks/useRatingData/index.js +0 -53
  144. package/src/hooks/useSalesClient/index.js +0 -1
@@ -0,0 +1,75 @@
1
+ import { filterKeyObject } from '../../../../utils'
2
+
3
+ const filters = ['__typename']
4
+
5
+ export const filterDataOptional = (dataOptional) => {
6
+ if (!Array.isArray(dataOptional)) {
7
+ throw new Error('Input data is not an array')
8
+ }
9
+
10
+ try {
11
+ const filteredDataOptional = dataOptional.map(item => {
12
+ const checkedSubOptions = item?.ExtProductFoodsSubOptionalAll?.filter(subItem => subItem.check === true)
13
+ const ExtProductFoodsSubOptionalAll = checkedSubOptions?.map(subItemsOptional => {
14
+ return {
15
+ ...filterKeyObject(subItemsOptional, filters)
16
+ }
17
+ })
18
+ return {
19
+ ...filterKeyObject(item, filters),
20
+ ExtProductFoodsSubOptionalAll
21
+ }
22
+ }).filter(item => item?.ExtProductFoodsSubOptionalAll.length > 0)
23
+ return filteredDataOptional
24
+ } catch (error) {
25
+ console.error('An error occurred while filtering data:', error.message)
26
+ return []
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Valida los requerimientos de elementos opcionales.
32
+ * @param {Array} filteredDataOptional - El array de datos a validar.
33
+ * @returns {boolean} Devuelve true si se cumplen los requerimientos, de lo contrario, false.
34
+ */
35
+ export function validateRequirements (filteredDataOptional) {
36
+ if (Array.isArray(filteredDataOptional)) {
37
+ for (const item of filteredDataOptional) {
38
+ if (item.required === 1) {
39
+ const checkedSubOptions = item?.ExtProductFoodsSubOptionalAll.filter(subItem => subItem.check === true)
40
+ if (checkedSubOptions.length !== item.numbersOptionalOnly) {
41
+ return true
42
+ }
43
+ }
44
+ }
45
+ }
46
+ return false
47
+ }
48
+
49
+ export function validateExtraProducts (dataExtra) {
50
+ const requiredItems = dataExtra.filter(item => item.exState === 1)
51
+
52
+ if (requiredItems.length > 0) {
53
+ const hasSelectedRequiredProduct = requiredItems.some(item => item.quantity > 0)
54
+ return hasSelectedRequiredProduct
55
+ }
56
+
57
+ return true
58
+ }
59
+
60
+ export const filterExtra = (dataExtra) => {
61
+ if (!Array.isArray(dataExtra)) {
62
+ throw new Error('Input data is not an array')
63
+ }
64
+ try {
65
+ const dataExtraFiltered = dataExtra.filter(extra => extra.quantity !== 0)
66
+ .map(extra => {
67
+ return {
68
+ ...filterKeyObject(extra, filters)
69
+ }
70
+ })
71
+ return dataExtraFiltered
72
+ } catch (error) {
73
+ return []
74
+ }
75
+ }
@@ -0,0 +1,411 @@
1
+ import { useState, useCallback, useEffect } from 'react'
2
+ import { useMutation } from '@apollo/client'
3
+ import { CREATE_SHOPPING_CARD, GET_ALL_SHOPPING_CARD } from '../queries'
4
+ import { RandomCode, updateCacheMod, numberFormat } from '../../../utils'
5
+ import { useExtProductFoodsAll, useGetOneProductsFood } from '../../useProductsFood'
6
+ import { useGetExtProductFoodsSubOptionalAll } from '../../useGetExtProductFoodsSubOptionalAll'
7
+ import { useGetCart } from '../useGetCart'
8
+ import { useRouter } from 'next/router'
9
+ import {
10
+ filterDataOptional,
11
+ filterExtra,
12
+ validateExtraProducts,
13
+ validateRequirements
14
+ } from './helpers'
15
+ import { useManageQueryParams } from '../../useManageQueryParams'
16
+
17
+ /**
18
+ * Custom hook for managing cart functionality.
19
+ *
20
+ * @param {Object} options - Options object.
21
+ * @param {Function} options.setAlertBox - Function to set an alert message.
22
+ * @returns {Object} - Object containing cart state and functions.
23
+ */
24
+ /**
25
+ * The `useCart` function is a custom hook in JavaScript that handles the management of a shopping
26
+ * cart, including adding products, managing quantities, and handling optional extras.
27
+ * @param [] - - `openModalProduct`: A boolean indicating whether the modal for the product is open or
28
+ * not. Default value is `false`.
29
+ * @returns The `useCart` function returns an object with the following properties and methods:
30
+ */
31
+ export const useCart = ({
32
+ openModalProduct = false,
33
+ handleMenu = () => { },
34
+ setOpenModalProduct = () => { },
35
+ setAlertBox = () => { }
36
+ } = {}) => {
37
+ // sub products
38
+ const { handleCleanQuery } = useManageQueryParams()
39
+
40
+ const [dataOptional, setDataOptional] = useState([])
41
+ const [dataExtra, setDataExtra] = useState([])
42
+ const [quantity, setQuantity] = useState(1)
43
+ const [comments, setComments] = useState('')
44
+ const location = useRouter()
45
+ const queryParamProduct = location.query.plato
46
+ const [registerShoppingCard] = useMutation(CREATE_SHOPPING_CARD, {
47
+ onError: (error) => {
48
+ console.error('Error registering shopping card:', error)
49
+ }
50
+ })
51
+ // CUSTOM HOOKS
52
+ const [dataShoppingCard, { loading }] = useGetCart()
53
+ const [handleExtProductFoodsAll] = useExtProductFoodsAll()
54
+
55
+ const [ExtProductFoodsSubOptionalAll] = useGetExtProductFoodsSubOptionalAll()
56
+ const [dataOneProduct, setDataOneProduct] = useState({})
57
+ const [handleGetOneProduct,
58
+ {
59
+ loading: loadingProduct
60
+ }
61
+ ] = useGetOneProductsFood({ fetchOnlyProduct: true })
62
+
63
+ const getOneProduct = async food => {
64
+ const { pId, intoCart } = food || {}
65
+ const isEditing = intoCart
66
+ if (!pId) return {}
67
+ setOpenModalProduct(true)
68
+ const product = await handleGetOneProduct({ variables: { pId } })
69
+ const productFoodsOne = product.data.productFoodsOne || {}
70
+ setDataOneProduct(productFoodsOne)
71
+
72
+ const matchingItemInShoppingCart = dataShoppingCard?.find((item) => {
73
+ return item?.productFood && item?.productFood?.pId === pId
74
+ })
75
+ const matchingItemInShoppingCartOptional = matchingItemInShoppingCart?.salesExtProductFoodOptional || []
76
+ if (matchingItemInShoppingCart && queryParamProduct) {
77
+ setDataOneProduct({
78
+ ...productFoodsOne,
79
+ intoCart: true
80
+ })
81
+ const comments = matchingItemInShoppingCart?.comments
82
+ if (comments) {
83
+ setComments(comments || '')
84
+ }
85
+ const quantityProduct = matchingItemInShoppingCart?.cantProducts
86
+ if (quantityProduct) {
87
+ setQuantity(quantityProduct || 1)
88
+ }
89
+ }
90
+ const optionalAll = await ExtProductFoodsSubOptionalAll({
91
+ variables: { pId }
92
+ })
93
+ const optionalFetch = optionalAll?.data?.ExtProductFoodsOptionalAll
94
+ const shoppingCartOptionalAll = matchingItemInShoppingCart?.ExtProductFoodsAll || []
95
+ if (Array.isArray(optionalFetch)) {
96
+ // Filtra y procesa los objetos de optionalFetch
97
+ const filteredDataOptional = optionalFetch
98
+ .map((obj) => {
99
+ const filteredSubOptions = (obj.ExtProductFoodsSubOptionalAll || []).filter(
100
+ (subObj) => subObj.check !== false
101
+ )
102
+
103
+ if (filteredSubOptions.length === 0) {
104
+ return null
105
+ }
106
+
107
+ return {
108
+ ...obj,
109
+ ExtProductFoodsSubOptionalAll: filteredSubOptions
110
+ }
111
+ })
112
+ .filter((obj) => obj !== null)
113
+
114
+ if (matchingItemInShoppingCartOptional?.length === 0) {
115
+ setDataOptional(filteredDataOptional || [])
116
+ }
117
+
118
+ if (matchingItemInShoppingCartOptional?.length) {
119
+ // Actualiza los objetos de filteredDataOptional con información de matchingItemInShoppingCartOptional
120
+ const updateOption = filteredDataOptional?.map((obj) => {
121
+ const matchingArray = matchingItemInShoppingCartOptional?.find(
122
+ (o) => o && o.opExPid === obj.opExPid
123
+ )
124
+
125
+ if (!matchingArray) {
126
+ return obj
127
+ }
128
+
129
+ // Actualiza las propiedades específicas
130
+ const updatedExtProductFoodsSubOptionalAll = (obj.ExtProductFoodsSubOptionalAll || []).map((subObj) => {
131
+ const newItem = matchingArray.saleExtProductFoodsSubOptionalAll?.find((newItem) => newItem && newItem.opSubExPid === subObj.opSubExPid)
132
+
133
+ if (newItem) {
134
+ return {
135
+ ...subObj,
136
+ check: true
137
+ }
138
+ }
139
+
140
+ return subObj
141
+ })
142
+
143
+ return {
144
+ ...obj,
145
+ ExtProductFoodsSubOptionalAll: updatedExtProductFoodsSubOptionalAll
146
+ }
147
+ }).filter((obj) => obj)
148
+
149
+ setDataOptional(updateOption || [])
150
+ }
151
+ }
152
+
153
+ const resultExtra = await handleExtProductFoodsAll(pId)
154
+ const originalArray = resultExtra?.data?.ExtProductFoodsAll
155
+
156
+ if (Array.isArray(originalArray) && originalArray?.length) {
157
+ const fetchedDataExtra = originalArray?.map(extra => {
158
+ const updatedExtra = shoppingCartOptionalAll?.find(updatedItem => updatedItem.exPid === extra.exPid)
159
+ if (updatedExtra) {
160
+ return {
161
+ ...extra,
162
+ quantity: updatedExtra?.quantity || 0,
163
+ newExtraPrice: updatedExtra?.newExtraPrice || 0
164
+ }
165
+ }
166
+ return {
167
+ ...extra,
168
+ quantity: 0
169
+ }
170
+ })
171
+
172
+ setDataExtra(fetchedDataExtra || [])
173
+ }
174
+ }
175
+
176
+ /**
177
+ * The function `handleIncrementExtra` updates the quantity and price of a specific extra product in
178
+ * an array based on the provided index and product ID.
179
+ */
180
+ function handleIncrementExtra ({ extra, index }) {
181
+ // Desestructura las propiedades necesarias de dataOneProduct
182
+ const { pId } = dataOneProduct || {}
183
+
184
+ // Desestructura exPid de extra o establece un valor predeterminado si no existe
185
+ const { exPid = null } = extra
186
+
187
+ if (exPid && pId) {
188
+ const newExtra = dataExtra.map((producto) => {
189
+ if (exPid === producto.exPid) {
190
+ // Desestructura la cantidad y el precio extra del producto o establece valores predeterminados
191
+ const { quantity = 0, extraPrice = 0 } = producto
192
+
193
+ // Calcula la nueva cantidad y el nuevo precio extra
194
+ const newQuantity = quantity + 1
195
+ const newExtraPrice = extraPrice * newQuantity
196
+
197
+ return {
198
+ ...producto,
199
+ quantity: newQuantity,
200
+ newExtraPrice
201
+ }
202
+ }
203
+ return producto
204
+ })
205
+
206
+ // Actualiza el estado de dataExtra con el nuevo array
207
+ setDataExtra(newExtra)
208
+ }
209
+ }
210
+
211
+ function handleDecrementExtra ({ extra, index }) {
212
+ // Desestructura las propiedades necesarias de dataOneProduct
213
+ const { pId } = dataOneProduct || {}
214
+
215
+ // Desestructura exPid de extra o establece un valor predeterminado si no existe
216
+ const { exPid = null } = extra
217
+
218
+ // Encuentra el índice del objeto extra en dataExtra
219
+ const extraIndex = dataExtra.findIndex((item) => item.exPid === exPid)
220
+
221
+ if (pId && exPid && extraIndex !== -1) {
222
+ const newExtra = dataExtra.map((producto, i) => {
223
+ if (exPid === producto.exPid) {
224
+ // Desestructura la cantidad y el precio extra del producto o establece valores predeterminados
225
+ const { quantity = 0, extraPrice = 0 } = producto
226
+
227
+ // Calcula la nueva cantidad, evitando que sea negativa
228
+ const newQuantity = Math.max(quantity - 1, 0)
229
+
230
+ // Calcula el nuevo precio extra
231
+ const newExtraPrice = newQuantity === 0 ? extraPrice : extraPrice * newQuantity
232
+
233
+ return {
234
+ ...producto,
235
+ quantity: newQuantity,
236
+ newExtraPrice
237
+ }
238
+ }
239
+ return producto
240
+ })
241
+
242
+ // Actualiza el estado de dataExtra con el nuevo array
243
+ setDataExtra(newExtra)
244
+ }
245
+ }
246
+
247
+ const handleIncrease = () => {
248
+ return setQuantity((prev) => { return prev + 1 })
249
+ }
250
+
251
+ const handleDecrease = () => {
252
+ return setQuantity((prev) => { return prev - 1 })
253
+ }
254
+
255
+ const handleCountProducts = useCallback((ProPrice, quantity) => {
256
+ if (!ProPrice || !quantity) {
257
+ return 0 // Manejo de valores nulos o no numéricos
258
+ }
259
+
260
+ const price = parseFloat(ProPrice)
261
+ const numericQuantity = parseInt(quantity, 10)
262
+
263
+ if (isNaN(price) || isNaN(numericQuantity)) {
264
+ return 0 // Manejo de valores no numéricos
265
+ }
266
+
267
+ if (numericQuantity <= 0 || price <= 0) {
268
+ return price // Manejo de cantidades o precios no positivos
269
+ }
270
+
271
+ const totalPrice = Math.abs(numericQuantity * price)
272
+ return numberFormat(totalPrice) // Si es necesario, aplicar formateo aquí
273
+ }, [dataOneProduct])
274
+
275
+ const handleAddOptional = ({ exOptional = null, codeCategory = null }) => {
276
+ if (!exOptional || !codeCategory) return
277
+ const item = dataOptional.find((item) => item.code === codeCategory)
278
+ if (!item) return
279
+ const idx = item.ExtProductFoodsSubOptionalAll.findIndex(
280
+ (el) => el.opSubExPid === exOptional
281
+ )
282
+ if (item && idx !== -1) {
283
+ const updatedItem = {
284
+ ...item,
285
+ ExtProductFoodsSubOptionalAll: [
286
+ ...item.ExtProductFoodsSubOptionalAll.slice(0, idx),
287
+ {
288
+ ...item.ExtProductFoodsSubOptionalAll[idx],
289
+ check: !item.ExtProductFoodsSubOptionalAll[idx].check
290
+ },
291
+ ...item.ExtProductFoodsSubOptionalAll.slice(idx + 1)
292
+ ]
293
+ }
294
+ const newData = dataOptional.map((el) =>
295
+ el.code === codeCategory ? updatedItem : el
296
+ )
297
+ setDataOptional((prevData) => [...newData])
298
+ }
299
+ }
300
+
301
+ const isValidDataExtra = (!dataExtra.length && !dataOneProduct?.pId) ? false : !validateExtraProducts(dataExtra)
302
+
303
+ const isValid = (!dataOptional.length && !dataOneProduct?.pId) ? false : validateRequirements(dataOptional)
304
+
305
+ const disabled = isValid || isValidDataExtra
306
+
307
+ /**
308
+ * Handles the addition of products to the cart.
309
+ *
310
+ * @param {Object} food - The selected food item.
311
+ */
312
+
313
+ const handleAddProducts = async (food) => {
314
+ if (disabled || !food) return
315
+ const idStore = food?.getStore?.idStore
316
+ if (!idStore) {
317
+ return
318
+ }
319
+ const isExistItemInShoppingCart = dataShoppingCard?.find((item) => {
320
+ return item?.productFood && item?.productFood?.pId === food.pId
321
+ }) ?? null
322
+ if (!isExistItemInShoppingCart) handleMenu(1)
323
+ const filteredDataOptional = filterDataOptional(dataOptional)
324
+
325
+ const dataExtraFiltered = filterExtra(dataExtra)
326
+
327
+ const refCodePid = RandomCode(20)
328
+
329
+ console.log(isExistItemInShoppingCart)
330
+ const idShoppingCart = isExistItemInShoppingCart?.ShoppingCard
331
+ try {
332
+ const idStore = food?.getStore?.idStore
333
+ const response = await registerShoppingCard({
334
+ variables: {
335
+ input: {
336
+ ShoppingCard: idShoppingCart ?? null,
337
+ cState: 1,
338
+ pId: food.pId,
339
+ idStore,
340
+ refCodePid: idShoppingCart ? null : refCodePid,
341
+ comments,
342
+ cName: '',
343
+ cantProducts: quantity,
344
+ csDescription: '',
345
+ dataExtra: dataExtraFiltered || [],
346
+ dataOptional: filteredDataOptional || []
347
+ },
348
+ idSubArray: {
349
+ setID: []
350
+ }
351
+ },
352
+ update: (cache, { data: { getAllShoppingCard } }) => {
353
+ return updateCacheMod({
354
+ cache,
355
+ query: GET_ALL_SHOPPING_CARD,
356
+ nameFun: 'getAllShoppingCard',
357
+ dataNew: getAllShoppingCard
358
+ })
359
+ }
360
+ })
361
+
362
+ if (response?.data) {
363
+ // Perform actions after adding products to cart
364
+ }
365
+ } catch (error) {
366
+ setAlertBox({ message: 'Ocurrió un error al añadir el producto al carrito' })
367
+ }
368
+ }
369
+
370
+ const handleShowModalProduct = () => {
371
+ if (openModalProduct) {
372
+ setDataExtra([])
373
+ setDataOneProduct({})
374
+ setQuantity(1)
375
+ setComments('')
376
+ setDataOptional([])
377
+ }
378
+ if (queryParamProduct && openModalProduct) {
379
+ handleCleanQuery('plato')
380
+ }
381
+ return setOpenModalProduct(!openModalProduct)
382
+ }
383
+
384
+ useEffect(() => {
385
+ if (queryParamProduct) {
386
+ const product = { pId: queryParamProduct }
387
+ getOneProduct(product)
388
+ }
389
+ }, [queryParamProduct])
390
+
391
+ return {
392
+ quantity,
393
+ disabled,
394
+ comments,
395
+ loading: loadingProduct || loading,
396
+ dataOneProduct,
397
+ dataExtra,
398
+ dataOptional,
399
+ setQuantity,
400
+ handleShowModalProduct,
401
+ handleDecrease,
402
+ handleIncrease,
403
+ getOneProduct,
404
+ handleIncrementExtra,
405
+ handleDecrementExtra,
406
+ handleAddOptional,
407
+ setComments,
408
+ handleCountProducts,
409
+ handleAddProducts
410
+ }
411
+ }
@@ -0,0 +1,31 @@
1
+ import { GET_ALL_SHOPPING_CARD } from '../queries'
2
+ import { useQuery } from '@apollo/client'
3
+
4
+ export const useGetCart = ({
5
+ setAlertBox = () => { },
6
+ setCountItemProduct = () => { }
7
+ } = {}) => {
8
+ const {
9
+ data,
10
+ loading,
11
+ error,
12
+ called
13
+ } = useQuery(GET_ALL_SHOPPING_CARD, {
14
+ onCompleted: data => {
15
+ if (Array.isArray(data?.getAllShoppingCard) && Number(data?.getAllShoppingCard?.length)) {
16
+ setCountItemProduct(data?.getAllShoppingCard?.length)
17
+ }
18
+ },
19
+ fetchPolicy: 'cache-and-network',
20
+ onError: () => {
21
+ setAlertBox({ message: 'No pudimos cargar el carrito...' })
22
+ }
23
+ })
24
+ const cart = data?.getAllShoppingCard || []
25
+ return [cart, {
26
+ data,
27
+ called,
28
+ loading,
29
+ error
30
+ }]
31
+ }
@@ -2,32 +2,32 @@ import { useQuery } from '@apollo/client'
2
2
  import { GET_ALL_CATEGORIES_WITH_PRODUCT } from './queries'
3
3
 
4
4
  export const useCatWithProduct = ({
5
- max = 1,
6
- min = 0,
7
- search = null,
8
- productName = null,
9
- searchFilter= {}
5
+ max = 1,
6
+ min = 0,
7
+ search = null,
8
+ productName = null,
9
+ searchFilter = {}
10
10
  }) => {
11
- const {
12
- gender,
13
- desc,
14
- speciality
15
- } = searchFilter || {}
11
+ const {
12
+ gender,
13
+ desc,
14
+ speciality
15
+ } = searchFilter || {}
16
16
  const {
17
17
  data,
18
18
  loading,
19
19
  error,
20
20
  fetchMore
21
- } = useQuery(GET_ALL_CATEGORIES_WITH_PRODUCT, {
21
+ } = useQuery(GET_ALL_CATEGORIES_WITH_PRODUCT, {
22
22
  fetchPolicy: 'network-only',
23
23
  variables:
24
24
  {
25
25
  search,
26
26
  productName,
27
- gender: gender,
27
+ gender,
28
28
  min,
29
29
  max: 400,
30
- desc: desc,
30
+ desc,
31
31
  categories: speciality
32
32
  }
33
33
  })
@@ -39,6 +39,6 @@ export const useCatWithProduct = ({
39
39
  error,
40
40
  fetchMore,
41
41
  totalCount
42
- }
42
+ }
43
43
  ]
44
- }
44
+ }
@@ -170,4 +170,4 @@ query getCatProductsWithProduct($search: String, $productName: String, $min: Int
170
170
  }
171
171
  }
172
172
  }
173
- `
173
+ `
@@ -0,0 +1,48 @@
1
+ import { useQuery } from '@apollo/client'
2
+ import { GET_ALL_CATEGORIES_WITH_PRODUCT_CLIENTS } from './queries'
3
+
4
+ /**
5
+ * Custom hook to fetch categories with product data.
6
+ *
7
+ * @param {string} idStore - The ID of the store.
8
+ * @returns {object} - The hook result containing data and fetch function.
9
+ */
10
+ export const useCatWithProductClient = (idStore) => {
11
+ const { data, loading, error, fetchMore } = useQuery(GET_ALL_CATEGORIES_WITH_PRODUCT_CLIENTS, {
12
+ fetchPolicy: 'cache-and-network',
13
+ variables: {
14
+ max: 100,
15
+ idStore,
16
+ search: '',
17
+ gender: [],
18
+ desc: [],
19
+ categories: []
20
+ }
21
+ })
22
+
23
+ const fetchCategories = () => {
24
+ fetchMore({
25
+ variables: {
26
+ max: 100,
27
+ idStore,
28
+ search: '',
29
+ gender: [],
30
+ desc: [],
31
+ categories: []
32
+ },
33
+ updateQuery: (prev, { fetchMoreResult }) => {
34
+ if (!fetchMoreResult) return prev
35
+ return {
36
+ ...prev,
37
+ getCatProductsWithProductClient: fetchMoreResult.getCatProductsWithProductClient
38
+ }
39
+ }
40
+ })
41
+ }
42
+
43
+ return [data?.getCatProductsWithProductClient || [], {
44
+ loading,
45
+ error,
46
+ fetchCategories
47
+ }]
48
+ }
@@ -0,0 +1,55 @@
1
+ import { gql } from '@apollo/client'
2
+
3
+ export const GET_ALL_CATEGORIES_WITH_PRODUCT_CLIENTS = gql`
4
+ query getCatProductsWithProductClient($search: String, $min: Int, $max: Int, $gender: [String], $desc: [String], $categories: [ID], $carProId: ID $idStore: ID ) {
5
+ getCatProductsWithProductClient(search: $search, min: $min, max: $max, gender: $gender, desc: $desc, categories: $categories, carProId: $carProId idStore: $idStore) {
6
+ carProId
7
+ pState
8
+ pState
9
+ ProImage
10
+ idStore
11
+ pName
12
+ ProDescription
13
+ ProImage
14
+ pState
15
+ pDatCre
16
+ pDatMod
17
+ productFoodsAll {
18
+ pId
19
+ sizeId
20
+ colorId
21
+ carProId
22
+ cId
23
+ dId
24
+ ValueDelivery
25
+ ctId
26
+ idStore
27
+ caId
28
+ fId
29
+ pName
30
+ ProPrice
31
+ ProDescuento
32
+ ProUniDisponibles
33
+ ProDescription
34
+ ProProtegido
35
+ ProAssurance
36
+ ProImage
37
+ ProStar
38
+ ProWidth
39
+ ProHeight
40
+ ProLength
41
+ ProWeight
42
+ ProQuantity
43
+ ProOutstanding
44
+ ProDelivery
45
+ ProVoltaje
46
+ pState
47
+ sTateLogistic
48
+ pDatCre
49
+ pDatMod
50
+
51
+ }
52
+
53
+ }
54
+ }
55
+ `