npm-pkg-hook 1.0.2 → 1.0.4

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 (84) hide show
  1. package/.babelrc +13 -13
  2. package/.env +1 -0
  3. package/.eslintrc.js +132 -0
  4. package/.eslintrc.json +107 -107
  5. package/.github/pull_request_template.md +17 -17
  6. package/.github/workflows/pepeline.yaml +29 -29
  7. package/.vscode/extensions.json +6 -0
  8. package/.vscode/settings.json +12 -0
  9. package/README.md +1 -1
  10. package/jsconfig.json +27 -27
  11. package/next.config.js +128 -128
  12. package/package.json +8 -6
  13. package/script.txt +7 -0
  14. package/src/cookies/index.ts +3 -3
  15. package/src/hooks/index.js +46 -19
  16. package/src/hooks/updateExtProductFoodsOptional/index.js +38 -0
  17. package/src/hooks/useAcumulateDate/index.js +16 -16
  18. package/src/hooks/useAnimationText/index.jsx +31 -30
  19. package/src/hooks/useBanner/index.js +19 -0
  20. package/src/hooks/useCatWithProduct/index.js +42 -0
  21. package/src/hooks/useCatWithProduct/queries.js +172 -0
  22. package/src/hooks/useCategoryInStore/index.js +94 -0
  23. package/src/hooks/{useGetCategorieStore → useCategoryInStore}/queries.js +77 -77
  24. package/src/hooks/useCategoryStore/index.js +7 -7
  25. package/src/hooks/useCategoryStore/queries.js +16 -16
  26. package/src/hooks/useChartData/index.js +168 -0
  27. package/src/hooks/useCheckbox/index.js +114 -114
  28. package/src/hooks/useClients/index.js +13 -13
  29. package/src/hooks/useClients/queries.js +117 -117
  30. package/src/hooks/useConnection/index.js +23 -0
  31. package/src/hooks/useCreateProduct/helpers/useEditImageProduct/index.js +165 -0
  32. package/src/hooks/useCreateProduct/index.js +268 -0
  33. package/src/hooks/useDessert/index.js +141 -0
  34. package/src/hooks/useDrag/index.js +62 -57
  35. package/src/hooks/useEvent/index.js +33 -33
  36. package/src/hooks/useFetchJson/index.js +24 -24
  37. package/src/hooks/useFetchMoreInteractions/index.jsx +37 -34
  38. package/src/hooks/useFormTools/index.js +83 -70
  39. package/src/hooks/useFormatDate/index.js +34 -0
  40. package/src/hooks/useFullScreenMode/index.js +65 -65
  41. package/src/hooks/useHover/index.js +28 -28
  42. package/src/hooks/useImageOptimization/index.js +28 -0
  43. package/src/hooks/useImageWeight/index.js +52 -0
  44. package/src/hooks/useImagesStore/index.js +171 -0
  45. package/src/hooks/useImagesStore/queries.js +216 -0
  46. package/src/hooks/useInnerHtml/index.js +37 -37
  47. package/src/hooks/useIntersection/index.js +84 -31
  48. package/src/hooks/useKeypress/index.js +27 -27
  49. package/src/hooks/useLazyScript/index.js +72 -0
  50. package/src/hooks/useLocalSorage/index.js +35 -35
  51. package/src/hooks/useLocationNavigate/index.js +54 -54
  52. package/src/hooks/useMobile/index.js +37 -37
  53. package/src/hooks/useMutateHeight/index.js +37 -0
  54. package/src/hooks/useProductsFood/index.js +190 -0
  55. package/src/hooks/{useGetProductsFood → useProductsFood}/queriesStore.js +780 -765
  56. package/src/hooks/useProductsFood/usetagsProducts.js +57 -0
  57. package/src/hooks/useReport/index.js +35 -0
  58. package/src/hooks/useReport/queries.js +122 -0
  59. package/src/hooks/useRestaurant/index.js +19 -19
  60. package/src/hooks/useRestaurant/queries.js +79 -69
  61. package/src/hooks/useSales/index.js +589 -489
  62. package/src/hooks/useSales/queries.js +290 -229
  63. package/src/hooks/useSales/useGetSale.js +12 -0
  64. package/src/hooks/useSales/useTotalSales.js +17 -0
  65. package/src/hooks/useSaveAvailableProduct/helpers/index.js +30 -0
  66. package/src/hooks/useSaveAvailableProduct/index.js +26 -0
  67. package/src/hooks/useSaveAvailableProduct/queries.js +10 -0
  68. package/src/hooks/useSchedule/index.jsx +23 -0
  69. package/src/hooks/useSetState/index.js +24 -24
  70. package/src/hooks/useStore/index.js +17 -17
  71. package/src/hooks/useStore/queries.js +135 -135
  72. package/src/hooks/useStoreCalendar/index.js +7 -0
  73. package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
  74. package/src/hooks/useUpdateCart/index.js +124 -124
  75. package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +37 -0
  76. package/src/hooks/useUser/index.js +7 -3
  77. package/src/hooks/useUser/queries.js +69 -0
  78. package/src/hooks/useWindowSize/index.js +37 -37
  79. package/src/index.jsx +2 -1
  80. package/src/mock/dessert/index.js +16 -0
  81. package/src/mock/index.js +2 -0
  82. package/src/utils/index.js +106 -54
  83. package/src/hooks/useGetCategorieStore/index.js +0 -21
  84. package/src/hooks/useGetProductsFood/index.js +0 -46
@@ -1,489 +1,589 @@
1
- import {
2
- useState,
3
- useEffect,
4
- useCallback,
5
- useReducer
6
- } from 'react'
7
- import {
8
- getCurrentDomain,
9
- RandomCode,
10
- updateCacheMod
11
- } from '../../utils'
12
- import { Cookies } from '../../cookies'
13
- import { useGetProductsFood } from '../useGetProductsFood'
14
- import { useStore } from '../useStore'
15
- import moment from 'moment'
16
- import {
17
- CREATE_SHOPPING_CARD_TO_USER_STORE,
18
- GET_ALL_SALES,
19
- GET_ALL_SALES_STATISTICS
20
- } from './queries'
21
- import { useLazyQuery, useMutation } from '@apollo/client'
22
- import { GET_ALL_EXTRA_PRODUCT, GET_EXTRAS_PRODUCT_FOOD_OPTIONAL, GET_ONE_PRODUCTS_FOOD } from '../useGetProductsFood/queriesStore'
23
-
24
- const initialState = {
25
- PRODUCT: [],
26
- totalPrice: 0,
27
- sortBy: null,
28
- itemsInCart: 0,
29
- animateType: '',
30
- startAnimateUp: '',
31
- priceRange: 0,
32
- counter: 0,
33
- totalAmount: 0,
34
- payMethodPState: 0
35
- }
36
-
37
- const initializer = (initialValue = initialState) => { return JSON.parse(Cookies.get(process.env.LOCAL_SALES_STORE) || JSON.stringify(initialState)) || initialValue }
38
-
39
- export const useSales = () => {
40
- const domain = getCurrentDomain()
41
- const [modalItem, setModalItem] = useState(false)
42
- const keyToSaveData = process.env.LOCAL_SALES_STORE
43
- const saveDataState = JSON.parse(Cookies.get(keyToSaveData) || '[]')
44
- const [search, setSearch] = useState('')
45
- const [arr] = useState([])
46
- const [totalProductPrice, setTotalProductPrice] = useState(0)
47
- const [showMore, setShowMore] = useState(50)
48
- const [inputValue, setInputValue] = useState('')
49
- const [delivery, setDelivery] = useState(false)
50
- const [print, setPrint] = useState(false)
51
- const [values, setValues] = useState({})
52
- const [dataStore] = useStore()
53
- const { createdAt } = dataStore || {}
54
- const [valuesDates, setValuesDates] = useState(() => { return { fromDate: moment(createdAt || null).format('YYYY-MM-DD'), toDate: moment().format('YYYY-MM-DD') } })
55
- const [registerSalesStore] = useMutation(CREATE_SHOPPING_CARD_TO_USER_STORE)
56
- const [product, setProduct] = useState({
57
- PRODUCT: {},
58
- })
59
- const [productFoodsOne, { data: dataProduct, loading: loadingProduct }] = useLazyQuery(GET_ONE_PRODUCTS_FOOD)
60
- const [ExtProductFoodsOptionalAll, { data: dataOptional }] = useLazyQuery(GET_EXTRAS_PRODUCT_FOOD_OPTIONAL)
61
- const [ExtProductFoodsAll, { data: dataExtra }] = useLazyQuery(GET_ALL_EXTRA_PRODUCT)
62
-
63
- const [productsFood, { loading, fetchMore }] = useGetProductsFood({
64
- search: search?.length >= 4 ? search : '',
65
- gender: [],
66
- desc: [],
67
- categories: arr || [],
68
- toDate: valuesDates?.toDate,
69
- fromDate: valuesDates?.fromDate,
70
- max: showMore,
71
- min: 0
72
- })
73
-
74
- const max = productsFood?.reduce(function (a, b) {
75
- return Math.max(a, b?.ProPrice || 0)
76
- }, 0)
77
- const initialStateSales = {
78
- PRODUCT: [],
79
- totalPrice: 0,
80
- sortBy: null,
81
- itemsInCart: 0,
82
- animateType: '',
83
- startAnimateUp: '',
84
- priceRange: max || 0,
85
- counter: 0,
86
- totalAmount: 0,
87
- payMethodPState: 0
88
- }
89
- // HANDLESS
90
- // FILTER PRODUCT DATA_DB
91
- const handleChangeFilter = (e) => { return setSearch(e.target.value) }
92
- const handleChange = e => { return setValues({ ...values, [e.target.name]: e.target.value }) }
93
- const onChangeInput = (e) => { return setValuesDates({ ...valuesDates, [e.target.name]: e.target.value }) }
94
- const handleChangeFilterProduct = (e) => {
95
- let text = searchedInput(e.target.value)
96
- if (text === undefined || text === '') {
97
- return
98
- }
99
- let filteredData = handleList(text)
100
- setFilteredList(filteredData)
101
- }
102
-
103
- const handleChangeNumber = useCallback((state, action) => {
104
- const event = action.payload
105
- const { value, index, id } = event || {}
106
- const productExist = productsFood?.find((items) => {
107
- return items.pId === id
108
- })
109
- const OneProduct = state?.PRODUCT.find((items) => {
110
- return items.pId === id
111
- })
112
- if (value <= 0) {
113
- dispatch({ type: 'REMOVE_PRODUCT_TO_CART', payload: OneProduct })
114
- }
115
- const finalQuantity = state.PRODUCT['ProQuantity'] = value || 0
116
- const ARR_PRODUCT = state?.PRODUCT?.map((items, i) => {
117
- return i === index
118
- ? {
119
- ...items,
120
- ProQuantity: (finalQuantity),
121
- ProPrice: value ? (value * productExist?.ProPrice) : productExist?.ProPrice
122
- }
123
- : items
124
- })
125
- return {
126
- ...state,
127
- PRODUCT: ARR_PRODUCT,
128
- counter: state.counter + 1
129
- }
130
- }, [productsFood])
131
- const PRODUCT = (state, action) => {
132
- const productExist = state.PRODUCT.find((items) => {
133
- return items.pId === action.id
134
- })
135
- const OurProduct = productsFood.find((items) => {
136
- return items.pId === action.id
137
- })
138
- const isFree = productExist?.free
139
-
140
- switch (action.type) {
141
- case 'ADD_TO_CART':
142
- return addToCartFunc(state, action)
143
- case 'ADD_PRODUCT':
144
- return {
145
- ...state,
146
- // eslint-disable-next-line
147
- PRODUCT: [...state?.PRODUCT, action?.payload],
148
- }
149
- case 'REMOVE_PRODUCT':
150
- return removeFunc(state, action)
151
- case 'REMOVE_PRODUCT_TO_CART':
152
- return {
153
- ...state,
154
- PRODUCT: state?.PRODUCT?.filter((t) => {
155
- return t.pId !== action?.payload.pId
156
- }),
157
- counter: state.counter - action.payload.ProQuantity
158
- }
159
- case 'ON_CHANGE': {
160
- return handleChangeNumber(state, action)
161
- }
162
- case 'REMOVE_ALL_PRODUCTS':
163
- return {
164
- ...state,
165
- PRODUCT: [],
166
- counter: 0
167
- }
168
-
169
- case 'TOGGLE_FREE_PRODUCT':
170
- return toggleFreeProducts(state, action)
171
- case 'INCREMENT':
172
- return {
173
- ...state,
174
- counter: state.counter + 1,
175
- PRODUCT: state?.PRODUCT?.map((items) => {
176
- return items.pId === action.id
177
- ? {
178
- ...items,
179
- ProQuantity: items.ProQuantity + 1,
180
- ProPrice: isFree ? 0 : (productExist.ProQuantity + 1) * OurProduct?.ProPrice
181
- }
182
- : items
183
- })
184
- }
185
- case 'PRICE_RANGE':
186
- return {
187
- ...state,
188
- priceRange: action.payload
189
- }
190
- case 'SORT':
191
- return { ...state, sortBy: action.payload }
192
- case 'DECREMENT':
193
- return {
194
- ...state
195
- // counter: state.counter - 1,
196
- // PRODUCT: state?.PRODUCT?.map((items) => {
197
- // return items.pId === action.id ? {
198
- // ...items,
199
- // ProQuantity: items.ProQuantity - 1,
200
- // // ProPrice: ((productExist.ProQuantity + 1) * OurProduct?.ProPrice),
201
- // } : items
202
- // })
203
- }
204
- case 'PAYMENT_METHOD_TRANSACTION':
205
- return {
206
- ...state,
207
- payMethodPState: 1
208
- }
209
- case 'PAYMENT_METHOD_MONEY':
210
- return {
211
- ...state,
212
- payMethodPState: 0
213
- }
214
- default:
215
- return state
216
- }
217
- }
218
- const [data, dispatch] = useReducer(PRODUCT, initialStateSales, initializer)
219
- useEffect(() => {
220
- Cookies.set(keyToSaveData, JSON.stringify(data), { domain, path: '/' })
221
- }, [data, domain])
222
-
223
-
224
- function addToCartFunc(state, action) {
225
- const productExist = state.PRODUCT.find((items) => {
226
- return items.pId === action.payload.pId
227
- })
228
- const OurProduct = productsFood.find((items) => {
229
- return items.pId === action.payload.pId
230
- })
231
- const isFree = productExist?.free
232
- return {
233
- ...state,
234
- counter: state.counter + 1,
235
- totalAmount: state.totalAmount + action.payload.ProPrice,
236
- startAnimateUp: 'start-animate-up',
237
- PRODUCT: !productExist
238
- ? [
239
- ...state.PRODUCT,
240
- {
241
- pId: action.payload.pId,
242
- pName: action.payload.pName,
243
- ProDescription: action.payload.ProDescription,
244
- ProImage: action.payload.ProImage,
245
- ProPrice: action.payload.ProPrice,
246
- ProQuantity: 1
247
- }
248
- ]
249
- : state.PRODUCT.map((items) => {
250
- return items.pId === action.payload.pId
251
- ? {
252
- ...items,
253
- ProPrice: isFree ? 0 : (productExist.ProQuantity + 1) * OurProduct?.ProPrice,
254
- ProQuantity: productExist.ProQuantity + 1,
255
- free: isFree ? true : false
256
- }
257
- : items
258
- })
259
- }
260
- }
261
- function removeFunc(state, action) {
262
- const productExist = state.PRODUCT.find((items) => {
263
- return items.pId === action.payload.pId
264
- })
265
- const OurProduct = productsFood.find((items) => {
266
- return items.pId === action.payload.pId
267
- })
268
- return {
269
- ...state,
270
- counter: state.counter - 1,
271
- totalAmount: state.totalAmount - action.payload.ProPrice,
272
- PRODUCT: action.payload.ProQuantity > 1
273
- ? state.PRODUCT.map((items) => {
274
- return items.pId === action.payload.pId
275
- ? {
276
- ...items,
277
- pId: action.payload.pId,
278
- ProQuantity: items.ProQuantity - 1,
279
- ProPrice: (productExist.ProPrice - OurProduct?.ProPrice)
280
- }
281
- : items
282
- })
283
- : state.PRODUCT.filter((items) => {
284
- return items.pId !== action.payload.pId
285
- })
286
- }
287
- }
288
- // TOGGLE_FREE_PRODUCT
289
- function toggleFreeProducts(state, action) {
290
- const productExist = productsFood.find((items) => {
291
- return items.pId === action.payload.pId
292
- })
293
- return {
294
- ...state,
295
- PRODUCT: state?.PRODUCT?.map((items) => {
296
- return items.pId === action.payload.pId
297
- ? {
298
- ...items,
299
- free: !items.free,
300
- ProPrice: items.ProPrice
301
- ? 0
302
- : items.ProQuantity * productExist?.ProPrice
303
- }
304
- : items
305
- })
306
- }
307
- }
308
-
309
- const getSortedProduct = (sortData, sortBy) => {
310
- if (sortBy && sortBy === 'PRICE_HIGH_TO_LOW') {
311
- return (
312
- sortData ??
313
- sortData.sort((a, b) => {
314
- return b['ProPrice'] - a['ProPrice']
315
- })
316
- )
317
- }
318
- if (sortBy && sortBy === 'PRICE_LOW_TO_HIGH') {
319
- return (
320
- sortData ??
321
- sortData.sort((a, b) => {
322
- return a['ProPrice'] - b['ProPrice']
323
- })
324
- )
325
- }
326
- return sortData
327
- }
328
- const PriceRangeFunc = (products, price) => {
329
- return (
330
- products?.length > 0 &&
331
- products?.filter((items) => {
332
- return items?.ProPrice >= price
333
- })
334
- )
335
- }
336
- const [_, setFilteredList] = useState([]);
337
- const sortedProduct = getSortedProduct(data.PRODUCT, data.sortBy)
338
- const finalFilter = PriceRangeFunc(sortedProduct, data.priceRange)
339
-
340
- const handleList = (text) => {
341
- let inputText = text.toLowerCase()
342
- let dataList = []
343
- dataList = finalFilter.filter((item) => {
344
- return item.pName.toLowerCase().includes(inputText)
345
- })
346
- return dataList
347
- }
348
- const searchedInput = (words) => {
349
- setInputValue(words)
350
- let n = words.split(' ')
351
- if (n.length !== 0) {
352
- if (n[n.length - 1] === '') {
353
- n.pop()
354
- }
355
- return n[n.length - 1]
356
- }
357
- return ''
358
- }
359
- const newArrayProducts =
360
- data?.PRODUCT?.length > 0 &&
361
- data?.PRODUCT?.map((x) => {
362
- return {
363
- pId: x?.pId,
364
- id: values?.cliId,
365
- cantProducts: x?.ProQuantity,
366
- comments: 'Comentarios'
367
- }
368
- })
369
-
370
- const handleSubmit = () => {
371
- const code = RandomCode(5)
372
- return registerSalesStore({
373
- variables: {
374
- input: newArrayProducts || [],
375
- id: values?.cliId,
376
- pCodeRef: code,
377
- change: values.change,
378
- valueDelivery: parseInt(values.valueDelivery),
379
- payMethodPState: data.payMethodPState,
380
- pickUp: 1,
381
- totalProductsPrice: data?.totalAmount || 0
382
- },
383
- update: (cache, { data: { getAllSalesStoreStatistic } }) => {
384
- return updateCacheMod(
385
- {
386
- cache,
387
- query: GET_ALL_SALES_STATISTICS,
388
- nameFun: 'getAllSalesStoreStatistic',
389
- dataNew: getAllSalesStoreStatistic,
390
- type: 2
391
- },
392
- cache.modify({
393
- fields: {
394
- getAllSalesStore(dataOld = []) {
395
- return cache.writeQuery({
396
- query: GET_ALL_SALES,
397
- data: dataOld
398
- })
399
- }
400
- }
401
- })
402
- )
403
- }
404
- })
405
- .then((responseRegisterR) => {
406
- if (responseRegisterR) {
407
- const { data } = responseRegisterR || {}
408
- const { registerSalesStore } = data || {}
409
- const { Response } = registerSalesStore || {}
410
- if (Response.success === true) {
411
- console.log({ message: `${Response.message}`, color: 'success' })
412
- dispatch({ type: 'REMOVE_ALL_PRODUCTS' })
413
- setValues({})
414
- }
415
- }
416
- })
417
- }
418
- let suma = 0
419
- let total = 0
420
- useEffect(() => {
421
- data.PRODUCT.forEach((a) => {
422
- const { ProPrice } = a || {}
423
- suma += ProPrice
424
- setTotalProductPrice(Math.abs(suma))
425
- })
426
- if (data.PRODUCT.length === 0) {
427
- setTotalProductPrice(0)
428
- }
429
- }, [totalProductPrice, suma, total, data])
430
-
431
-
432
- const handleProduct = (PRODUCT) => {
433
- const { pId } = PRODUCT || {}
434
- try {
435
- productFoodsOne({ variables: { pId } })
436
- ExtProductFoodsOptionalAll({ variables: { pId } })
437
- ExtProductFoodsAll({ variables: { pId } })
438
- setProduct(() => {
439
- return {
440
- PRODUCT,
441
- }})
442
- } catch (error) {
443
- console.log({ message: 'Lo sentimos, ocurrió un error' })
444
- }
445
- }
446
-
447
- return {
448
- loading,
449
- fetchMore,
450
- totalProductPrice,
451
- saveDataState,
452
- product,
453
- data,
454
- inputValue,
455
- newArrayProducts,
456
- delivery,
457
- valuesDates,
458
- print,
459
- finalFilter,
460
- showMore,
461
- max,
462
- values,
463
- initialStateSales,
464
- productsFood,
465
- modalItem,
466
-
467
- dataProduct: dataProduct?.productFoodsOne || {},
468
- dataOptional: dataOptional?.ExtProductFoodsOptionalAll || [],
469
- dataExtra: dataExtra?.ExtProductFoodsAll || [],
470
-
471
- dispatch,
472
- setModalItem,
473
- handleChangeFilter,
474
- handleProduct,
475
- handleChange,
476
- onChangeInput,
477
- setDelivery,
478
- setValues,
479
- setShowMore,
480
- PriceRangeFunc,
481
- handleSubmit,
482
- handleChangeFilterProduct,
483
- setTotalProductPrice,
484
- setInputValue,
485
- getSortedProduct,
486
- setPrint,
487
- PRODUCT
488
- }
489
- }
1
+ import { useLazyQuery, useMutation } from '@apollo/client'
2
+ import {
3
+ useCallback,
4
+ useEffect,
5
+ useReducer,
6
+ useState
7
+ } from 'react'
8
+ import { Cookies } from '../../cookies'
9
+ import {
10
+ getCurrentDomain,
11
+ RandomCode,
12
+ updateCacheMod
13
+ } from '../../utils'
14
+ import { useFormatDate } from '../useFormatDate'
15
+ import { useProductsFood } from '../useProductsFood'
16
+ import {
17
+ GET_ALL_EXTRA_PRODUCT,
18
+ GET_EXTRAS_PRODUCT_FOOD_OPTIONAL,
19
+ GET_ONE_PRODUCTS_FOOD
20
+ } from '../useProductsFood/queriesStore'
21
+ import { useStore } from '../useStore'
22
+ import {
23
+ CREATE_SHOPPING_CARD_TO_USER_STORE,
24
+ GET_ALL_COUNT_SALES,
25
+ GET_ALL_SALES,
26
+ GET_ALL_SALES_STATISTICS
27
+ } from './queries'
28
+
29
+ const initialState = {
30
+ PRODUCT: [],
31
+ totalPrice: 0,
32
+ sortBy: null,
33
+ itemsInCart: 0,
34
+ animateType: '',
35
+ startAnimateUp: '',
36
+ priceRange: 0,
37
+ counter: 0,
38
+ totalAmount: 0,
39
+ payMethodPState: 0
40
+ }
41
+
42
+ const initializer = (initialValue = initialState) => { return JSON.parse(Cookies.get(process.env.LOCAL_SALES_STORE) || JSON.stringify(initialState)) || initialValue }
43
+
44
+ export const useSales = ({
45
+ disabled,
46
+ sendNotification,
47
+ router,
48
+ setAlertBox
49
+ }) => {
50
+ const domain = getCurrentDomain()
51
+ const [modalItem, setModalItem] = useState(false)
52
+ const [openCommentModal, setOpenCommentModal] = useState(false)
53
+ const keyToSaveData = process.env.LOCAL_SALES_STORE
54
+ const saveDataState = JSON.parse(Cookies.get(keyToSaveData) || '[]')
55
+ const [search, setSearch] = useState('')
56
+ const [arr] = useState([])
57
+ const [totalProductPrice, setTotalProductPrice] = useState(0)
58
+ const [showMore, setShowMore] = useState(50)
59
+ const [inputValue, setInputValue] = useState('')
60
+ const [_, setFilteredList] = useState([]);
61
+ const [delivery, setDelivery] = useState(false)
62
+ const [print, setPrint] = useState(false)
63
+ const [values, setValues] = useState({})
64
+ const [dataStore] = useStore()
65
+ const [code, setCode] = useState(null)
66
+ const [openCurrentSale, setOpenCurrentSale] = useState(null)
67
+ const { createdAt } = dataStore || {}
68
+ const {yearMonthDay} = useFormatDate({ date: createdAt })
69
+ const [valuesDates, setValuesDates] = useState(() => { return { fromDate: yearMonthDay, toDate: '' } })
70
+
71
+ const [registerSalesStore, { loading: loadingRegisterSale }] = useMutation(CREATE_SHOPPING_CARD_TO_USER_STORE, {
72
+ onCompleted: (data) => {
73
+ const message = `${data?.registerSalesStore?.Response?.message}`
74
+ const error = data?.registerSalesStore?.Response.success ? 'Éxito' : 'Error'
75
+ sendNotification({ title: error, description: message })
76
+ setAlertBox({ message: message, type: 'success' })
77
+ setOpenCurrentSale(data?.registerSalesStore?.Response.success)
78
+
79
+ },
80
+ onError: (error) => {
81
+ console.log('error', error)
82
+ }
83
+ })
84
+ const [product, setProduct] = useState({
85
+ PRODUCT: {},
86
+ })
87
+ const [productFoodsOne, { data: dataProduct }] = useLazyQuery(GET_ONE_PRODUCTS_FOOD)
88
+ const [ExtProductFoodsOptionalAll, { data: dataOptional }] = useLazyQuery(GET_EXTRAS_PRODUCT_FOOD_OPTIONAL)
89
+ const [ExtProductFoodsAll, { data: dataExtra }] = useLazyQuery(GET_ALL_EXTRA_PRODUCT)
90
+
91
+ const [productsFood, { loading, fetchMore }] = useProductsFood({
92
+ search: search?.length >= 4 ? search : '',
93
+ gender: [],
94
+ desc: [],
95
+ categories: arr || [],
96
+ toDate: valuesDates?.toDate,
97
+ fromDate: valuesDates?.fromDate,
98
+ max: showMore,
99
+ min: 0
100
+ })
101
+ const max = productsFood?.reduce(function (a, b) {
102
+ return Math.max(a, b?.ProPrice || 0)
103
+ }, 0)
104
+ const initialStateSales = {
105
+ PRODUCT: [],
106
+ totalPrice: 0,
107
+ sortBy: null,
108
+ itemsInCart: 0,
109
+ animateType: '',
110
+ startAnimateUp: '',
111
+ priceRange: max || 0,
112
+ counter: 0,
113
+ totalAmount: 0,
114
+ payMethodPState: 0
115
+ }
116
+ // HANDLESS
117
+ // FILTER PRODUCT DATA_DB
118
+ const handlePrint = ({ callback }) => {
119
+ if (disabled) {
120
+ return sendNotification({
121
+ title: 'Error',
122
+ description: 'Esta es la descr',
123
+ backgroundColor: 'red'
124
+ })
125
+ }
126
+ setPrint(!print)
127
+ }
128
+ const handleChangeFilter = (e) => { return setSearch(e.target.value) }
129
+ const handleChange = e => { return setValues({ ...values, [e.target.name]: e.target.value }) }
130
+ const onChangeInput = (e) => { return setValuesDates({ ...valuesDates, [e.target.name]: e.target.value }) }
131
+ const handleChangeFilterProduct = (e) => {
132
+ let text = searchedInput(e.target.value)
133
+ if (text === undefined || text === '') {
134
+ return
135
+ }
136
+ let filteredData = handleList(text)
137
+ setFilteredList(filteredData)
138
+ }
139
+ const [oneProductToComment, setOneProductToComment] = useState({})
140
+ const handleComment = (product) => {
141
+ if (product) {
142
+ setOneProductToComment(product)
143
+ setValues({
144
+ ...values,
145
+ comment: product?.comment || ''
146
+ })
147
+ }
148
+ setOpenCommentModal(!openCommentModal)
149
+ }
150
+ const handleChangeNumber = useCallback((state, action) => {
151
+ const event = action.payload
152
+ const { value, index, id } = event || {}
153
+ const productExist = productsFood?.find((items) => {
154
+ return items.pId === id
155
+ })
156
+ const OneProduct = state?.PRODUCT.find((items) => {
157
+ return items.pId === id
158
+ })
159
+ if (value <= 0) {
160
+ dispatch({ type: 'REMOVE_PRODUCT_TO_CART', payload: OneProduct })
161
+ }
162
+ const finalQuantity = state.PRODUCT['ProQuantity'] = value || 0
163
+ const ARR_PRODUCT = state?.PRODUCT?.map((items, i) => {
164
+ return i === index
165
+ ? {
166
+ ...items,
167
+ ProQuantity: (finalQuantity),
168
+ ProPrice: value ? (value * productExist?.ProPrice) : productExist?.ProPrice
169
+ }
170
+ : items
171
+ })
172
+ return {
173
+ ...state,
174
+ PRODUCT: ARR_PRODUCT,
175
+ counter: state.counter + 1
176
+ }
177
+ }, [productsFood])
178
+ const PRODUCT = (state, action) => {
179
+ const productExist = state.PRODUCT.find((items) => {
180
+ return items.pId === action.id
181
+ })
182
+ const OurProduct = productsFood.find((items) => {
183
+ return items.pId === action.id
184
+ })
185
+ const isFree = productExist?.free
186
+
187
+ switch (action.type) {
188
+ case 'ADD_TO_CART':
189
+ return addToCartFunc(state, action)
190
+ case 'ADD_PRODUCT':
191
+ return {
192
+ ...state,
193
+ // eslint-disable-next-line
194
+ PRODUCT: [...state?.PRODUCT, action?.payload],
195
+ }
196
+ case 'REMOVE_PRODUCT':
197
+ return removeFunc(state, action)
198
+ case 'REMOVE_PRODUCT_TO_CART':
199
+ return {
200
+ ...state,
201
+ PRODUCT: state?.PRODUCT?.filter((t) => {
202
+ return t.pId !== action?.payload.pId
203
+ }),
204
+ counter: state.counter - action.payload.ProQuantity
205
+ }
206
+ case 'ON_CHANGE': {
207
+ return handleChangeNumber(state, action)
208
+ }
209
+ case 'REMOVE_ALL_PRODUCTS':
210
+ return {
211
+ ...state,
212
+ PRODUCT: [],
213
+ counter: 0
214
+ }
215
+
216
+ case 'TOGGLE_FREE_PRODUCT':
217
+ return toggleFreeProducts(state, action)
218
+ case 'INCREMENT':
219
+ return {
220
+ ...state,
221
+ counter: state.counter + 1,
222
+ PRODUCT: state?.PRODUCT?.map((items) => {
223
+ return items.pId === action.id
224
+ ? {
225
+ ...items,
226
+ ProQuantity: items.ProQuantity + 1,
227
+ ProPrice: isFree ? 0 : (productExist.ProQuantity + 1) * OurProduct?.ProPrice
228
+ }
229
+ : items
230
+ })
231
+ }
232
+ case 'PUT_COMMENT': return commentProducts(state, action)
233
+ case 'PRICE_RANGE':
234
+ return {
235
+ ...state,
236
+ priceRange: action.payload
237
+ }
238
+ case 'SORT':
239
+ return { ...state, sortBy: action.payload }
240
+ case 'DECREMENT':
241
+ return {
242
+ ...state
243
+ // counter: state.counter - 1,
244
+ // PRODUCT: state?.PRODUCT?.map((items) => {
245
+ // return items.pId === action.id ? {
246
+ // ...items,
247
+ // ProQuantity: items.ProQuantity - 1,
248
+ // // ProPrice: ((productExist.ProQuantity + 1) * OurProduct?.ProPrice),
249
+ // } : items
250
+ // })
251
+ }
252
+ case 'PAYMENT_METHOD_TRANSACTION':
253
+ return {
254
+ ...state,
255
+ payMethodPState: 1
256
+ }
257
+ case 'PAYMENT_METHOD_MONEY':
258
+ return {
259
+ ...state,
260
+ payMethodPState: 0
261
+ }
262
+ default:
263
+ return state
264
+ }
265
+ }
266
+ const [data, dispatch] = useReducer(PRODUCT, initialStateSales, initializer)
267
+ const handleRemoveValue = useCallback(({name, value, pId}) => {
268
+ setValues({
269
+ ...values,
270
+ [name]: value ?? ''
271
+ })
272
+ return dispatch({
273
+ type: 'PUT_COMMENT',
274
+ payload: pId,
275
+ value: ''
276
+ })
277
+ }, [])
278
+ useEffect(() => {
279
+ Cookies.set(keyToSaveData, JSON.stringify(data), { domain, path: '/' })
280
+ }, [data, domain])
281
+
282
+
283
+ function addToCartFunc(state, action) {
284
+ const productExist = state.PRODUCT.find((items) => {
285
+ return items.pId === action.payload.pId
286
+ })
287
+ const OurProduct = productsFood.find((items) => {
288
+ return items.pId === action.payload.pId
289
+ })
290
+ const isFree = productExist?.free
291
+ return {
292
+ ...state,
293
+ counter: state.counter + 1,
294
+ totalAmount: state.totalAmount + action.payload.ProPrice,
295
+ startAnimateUp: 'start-animate-up',
296
+ PRODUCT: !productExist
297
+ ? [
298
+ ...state.PRODUCT,
299
+ {
300
+ pId: action.payload.pId,
301
+ pName: action.payload.pName,
302
+ ProDescription: action.payload.ProDescription,
303
+ ProImage: action.payload.ProImage,
304
+ ProPrice: action.payload.ProPrice,
305
+ ProQuantity: 1
306
+ }
307
+ ]
308
+ : state.PRODUCT.map((items) => {
309
+ return items.pId === action.payload.pId
310
+ ? {
311
+ ...items,
312
+ ProPrice: isFree ? 0 : (productExist.ProQuantity + 1) * OurProduct?.ProPrice,
313
+ ProQuantity: productExist.ProQuantity + 1,
314
+ free: isFree ? true : false
315
+ }
316
+ : items
317
+ })
318
+ }
319
+ }
320
+ function removeFunc(state, action) {
321
+ const productExist = state.PRODUCT.find((items) => {
322
+ return items.pId === action.payload.pId
323
+ })
324
+ const OurProduct = productsFood.find((items) => {
325
+ return items.pId === action.payload.pId
326
+ })
327
+ return {
328
+ ...state,
329
+ counter: state.counter - 1,
330
+ totalAmount: state.totalAmount - action.payload.ProPrice,
331
+ PRODUCT: action.payload.ProQuantity > 1
332
+ ? state.PRODUCT.map((items) => {
333
+ return items.pId === action.payload.pId
334
+ ? {
335
+ ...items,
336
+ pId: action.payload.pId,
337
+ ProQuantity: items.ProQuantity - 1,
338
+ ProPrice: (productExist.ProPrice - OurProduct?.ProPrice)
339
+ }
340
+ : items
341
+ })
342
+ : state.PRODUCT.filter((items) => {
343
+ return items.pId !== action.payload.pId
344
+ })
345
+ }
346
+ }
347
+ // TOGGLE_FREE_PRODUCT
348
+ function toggleFreeProducts(state, action) {
349
+ const productExist = productsFood.find((items) => {
350
+ return items.pId === action.payload.pId
351
+ })
352
+ return {
353
+ ...state,
354
+ PRODUCT: state?.PRODUCT?.map((items) => {
355
+ return items.pId === action.payload.pId
356
+ ? {
357
+ ...items,
358
+ free: !items.free,
359
+ ProPrice: items.ProPrice
360
+ ? 0
361
+ : items.ProQuantity * productExist?.ProPrice
362
+ }
363
+ : items
364
+ })
365
+ }
366
+ }
367
+
368
+ // COMMENT_FREE_PRODUCT
369
+ function commentProducts(state, action, deleteValue) {
370
+ return {
371
+ ...state,
372
+ PRODUCT: state?.PRODUCT?.map((items) => {
373
+ return items.pId === action.payload
374
+ ? {
375
+ ...items,
376
+ comment: deleteValue ? '' : values.comment,
377
+ }
378
+ : items
379
+ })
380
+ }
381
+ }
382
+
383
+ const getSortedProduct = (sortData, sortBy) => {
384
+ if (sortBy && sortBy === 'PRICE_HIGH_TO_LOW') {
385
+ return (
386
+ sortData ??
387
+ sortData.sort((a, b) => {
388
+ return b['ProPrice'] - a['ProPrice']
389
+ })
390
+ )
391
+ }
392
+ if (sortBy && sortBy === 'PRICE_LOW_TO_HIGH') {
393
+ return (
394
+ sortData ??
395
+ sortData.sort((a, b) => {
396
+ return a['ProPrice'] - b['ProPrice']
397
+ })
398
+ )
399
+ }
400
+ return sortData
401
+ }
402
+ const PriceRangeFunc = (products, price) => {
403
+ return (
404
+ products?.length > 0 &&
405
+ products?.filter((items) => {
406
+ return items?.ProPrice >= price
407
+ })
408
+ )
409
+ }
410
+
411
+ const sortedProduct = getSortedProduct(data.PRODUCT, data.sortBy)
412
+ const finalFilter = PriceRangeFunc(sortedProduct, data.priceRange)
413
+
414
+ const handleList = (text) => {
415
+ let inputText = text.toLowerCase()
416
+ let dataList = []
417
+ dataList = finalFilter.filter((item) => {
418
+ return item.pName.toLowerCase().includes(inputText)
419
+ })
420
+ return dataList
421
+ }
422
+ const searchedInput = (words) => {
423
+ setInputValue(words)
424
+ let n = words.split(' ')
425
+ if (n.length !== 0) {
426
+ if (n[n.length - 1] === '') {
427
+ n.pop()
428
+ }
429
+ return n[n.length - 1]
430
+ }
431
+ return ''
432
+ }
433
+ const newArrayProducts =
434
+ data?.PRODUCT?.length > 0 &&
435
+ data?.PRODUCT?.map((product) => {
436
+ return {
437
+ pId: product?.pId,
438
+ id: values?.cliId,
439
+ cantProducts: parseInt(product?.ProQuantity ? product?.ProQuantity : 0),
440
+ comments: product?.comment ?? ''
441
+ }
442
+ })
443
+ const handleSubmit = () => {
444
+ const code = RandomCode(10)
445
+ setCode(code)
446
+ return registerSalesStore({
447
+ variables: {
448
+ input: newArrayProducts || [],
449
+ id: values?.cliId,
450
+ pCodeRef: code,
451
+ change: values.change,
452
+ valueDelivery: parseInt(values.valueDelivery),
453
+ payMethodPState: data.payMethodPState,
454
+ pickUp: 1,
455
+ totalProductsPrice: totalProductPrice || 0
456
+ },
457
+ update: (cache, { data: {
458
+ getAllSalesStoreStatistic,
459
+ getTodaySales,
460
+ getAllSalesStore
461
+ } }) => {
462
+ updateCacheMod(
463
+ {
464
+ cache,
465
+ query: GET_ALL_SALES,
466
+ nameFun: 'getAllSalesStore',
467
+ dataNew: getAllSalesStore,
468
+ })
469
+ updateCacheMod(
470
+ {
471
+ cache,
472
+ query: GET_ALL_COUNT_SALES,
473
+ nameFun: 'getTodaySales',
474
+ dataNew: getTodaySales,
475
+ })
476
+ return updateCacheMod(
477
+ {
478
+ cache,
479
+ query: GET_ALL_SALES_STATISTICS,
480
+ nameFun: 'getAllSalesStoreStatistic',
481
+ dataNew: getAllSalesStoreStatistic,
482
+ type: 2
483
+ }
484
+ )
485
+ }
486
+ })
487
+ .then((responseRegisterR) => {
488
+ if (responseRegisterR) {
489
+ const { data } = responseRegisterR || {}
490
+ const { registerSalesStore } = data || {}
491
+ const { Response } = registerSalesStore || {}
492
+ console.log(Response)
493
+ if (Response && Response.success === true) {
494
+ // dispatch({ type: 'REMOVE_ALL_PRODUCTS' })
495
+ router.push(
496
+ {
497
+ query: {
498
+ ...router.query,
499
+ saleId: code
500
+ }
501
+ },
502
+ undefined,
503
+ { shallow: true }
504
+ )
505
+ setValues({})
506
+ }
507
+ }
508
+ })
509
+ }
510
+ let suma = 0
511
+ let total = 0
512
+
513
+ useEffect(() => {
514
+ data.PRODUCT.forEach((a) => {
515
+ const { ProPrice } = a || {}
516
+ suma += ProPrice
517
+ setTotalProductPrice(Math.abs(suma))
518
+ })
519
+ if (data.PRODUCT.length === 0) {
520
+ setTotalProductPrice(0)
521
+ }
522
+ }, [totalProductPrice, suma, total, data])
523
+
524
+
525
+ const handleProduct = (PRODUCT) => {
526
+ const { pId } = PRODUCT || {}
527
+ try {
528
+ productFoodsOne({ variables: { pId } })
529
+ ExtProductFoodsOptionalAll({ variables: { pId } })
530
+ ExtProductFoodsAll({ variables: { pId } })
531
+ setProduct(() => {
532
+ return {
533
+ PRODUCT,
534
+ }})
535
+ } catch (error) {
536
+ console.log({ message: 'Lo sentimos, ocurrió un error' })
537
+ }
538
+ }
539
+
540
+ return {
541
+ loading,
542
+ loadingRegisterSale,
543
+ openCurrentSale,
544
+ fetchMore,
545
+ code,
546
+ totalProductPrice,
547
+ saveDataState,
548
+ product,
549
+ data,
550
+ openCommentModal,
551
+ inputValue,
552
+ newArrayProducts,
553
+ delivery,
554
+ valuesDates,
555
+ print,
556
+ finalFilter,
557
+ showMore,
558
+ max,
559
+ values,
560
+ initialStateSales,
561
+ productsFood,
562
+ modalItem,
563
+ oneProductToComment: oneProductToComment ?? null,
564
+ dataProduct: dataProduct?.productFoodsOne || {},
565
+ dataOptional: dataOptional?.ExtProductFoodsOptionalAll || [],
566
+ dataExtra: dataExtra?.ExtProductFoodsAll || [],
567
+
568
+ dispatch,
569
+ handleComment,
570
+ setModalItem,
571
+ handleChangeFilter,
572
+ handleProduct,
573
+ handleChange,
574
+ setOpenCurrentSale,
575
+ onChangeInput,
576
+ handleRemoveValue,
577
+ setDelivery,
578
+ setValues,
579
+ setShowMore,
580
+ PriceRangeFunc,
581
+ handleSubmit,
582
+ handleChangeFilterProduct,
583
+ setTotalProductPrice,
584
+ setInputValue,
585
+ getSortedProduct,
586
+ setPrint: handlePrint,
587
+ PRODUCT
588
+ }
589
+ }