npm-pkg-hook 1.3.7 → 1.3.8
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.
- package/.eslintrc.js +38 -38
- package/.eslintrc.json +127 -127
- package/.github/pull_request_template.md +17 -17
- package/.github/workflows/pepeline.yaml +29 -29
- package/package.json +46 -46
- package/script.txt +6 -6
- package/src/config/client/index.js +1 -1
- package/src/hooks/getCategoriesWithProduct/helpers/index.js +7 -7
- package/src/hooks/getCategoriesWithProduct/index.js +62 -62
- package/src/hooks/getSession/index.js +18 -18
- package/src/hooks/useAnimationFrame/index.js +45 -45
- package/src/hooks/useAsideCart/index.js +155 -155
- package/src/hooks/useAsideCart/queries.js +10 -10
- package/src/hooks/useCart/index.js +2 -2
- package/src/hooks/useCart/queries.js +164 -164
- package/src/hooks/useCart/useCart/helpers/index.js +75 -75
- package/src/hooks/useCart/useCart/index.js +410 -410
- package/src/hooks/useCart/useGetCart/index.js +31 -31
- package/src/hooks/useCatWithProductClient/index.js +56 -56
- package/src/hooks/useCatWithProductClient/queries.js +55 -55
- package/src/hooks/useCategoriesProduct/index.js +12 -12
- package/src/hooks/useCategoriesProduct/queries.js +16 -16
- package/src/hooks/useCategoryInStore/queries.js +78 -78
- package/src/hooks/useChartData/useChartData/index.js +199 -199
- package/src/hooks/useChartData/useChartDataAllOrders/index.js +94 -94
- package/src/hooks/useCheckbox/index.js +115 -115
- package/src/hooks/useCreateProduct/helpers/manageCacheDataCatProduct/index.js +52 -0
- package/src/hooks/useCreateProduct/index.js +8 -3
- package/src/hooks/useDeleteSubProductOptional/index.js +30 -30
- package/src/hooks/useDeleteSubProductOptional/queries.js +10 -10
- package/src/hooks/useDessert/helpers/index.js +51 -51
- package/src/hooks/useDevices/index.js +35 -35
- package/src/hooks/useDevices/queries.js +19 -19
- package/src/hooks/useDropzone/index.js +94 -94
- package/src/hooks/useDynamicAuth/index.js +13 -13
- package/src/hooks/useDynamicAuth/queries.js +24 -24
- package/src/hooks/useEditSubProductOptional/queries.js +10 -10
- package/src/hooks/useEvent/index.js +33 -33
- package/src/hooks/useFavoriteStores/index.js +19 -19
- package/src/hooks/useFavoriteStores/queries.js +47 -47
- package/src/hooks/useFetchJson/index.js +25 -25
- package/src/hooks/useFingerprintjs/index.js +172 -172
- package/src/hooks/useFullScreenMode/index.js +65 -65
- package/src/hooks/useGenerateNumberArray/index.js +17 -17
- package/src/hooks/useGetExtProductFoodsSubOptionalAll/index.js +14 -14
- package/src/hooks/useGetFoodRecomended/index.js +32 -33
- package/src/hooks/useGetFoodRecomended/queries.js +47 -47
- package/src/hooks/useGetMinPrice/index.js +12 -12
- package/src/hooks/useGetMinPrice/queries.js +7 -7
- package/src/hooks/useGetOneStoreRating/index.js +40 -40
- package/src/hooks/useGetOneStoreRating/queries.js +18 -18
- package/src/hooks/useGoogleLogin/index.js +160 -160
- package/src/hooks/useGoogleLogin/loadScript.js +15 -15
- package/src/hooks/useGoogleLogin/removeScript.js +7 -7
- package/src/hooks/useHover/index.js +29 -29
- package/src/hooks/useImagesStore/index.js +5 -4
- package/src/hooks/useInnerHtml/index.js +39 -39
- package/src/hooks/useKeypress/index.js +28 -28
- package/src/hooks/useLocationNavigate/index.js +54 -54
- package/src/hooks/useLogout/index.js +29 -21
- package/src/hooks/useManageQueryParams/index.js +37 -36
- package/src/hooks/useMobile/index.js +39 -39
- package/src/hooks/useOrderClient/index.js +5 -5
- package/src/hooks/useProviders/index.js +3 -3
- package/src/hooks/useProviders/queries.js +31 -31
- package/src/hooks/useProviders/useProvidersCreateStore/index.js +12 -12
- package/src/hooks/useProviders/useProvidersDataStore/index.js +24 -24
- package/src/hooks/useProvidersStore/index.js +24 -24
- package/src/hooks/useProvidersStore/queries.js +31 -31
- package/src/hooks/useRatingArrayData/index.js +54 -54
- package/src/hooks/useRatingArrayData/queries.js +19 -19
- package/src/hooks/useRemoveExtraProductFoodsOptional/index.js +23 -23
- package/src/hooks/useRemoveExtraProductFoodsOptional/queries.js +48 -48
- package/src/hooks/useRestaurant/index.js +19 -19
- package/src/hooks/useRestaurant/queries.js +80 -80
- package/src/hooks/useSales/useGetAllSales/index.js +25 -25
- package/src/hooks/useSchedule/index.js +72 -72
- package/src/hooks/useSchedule/index.jsx +22 -22
- package/src/hooks/useSchedule/queries.js +43 -43
- package/src/hooks/useScheduleData/index.js +123 -123
- package/src/hooks/useScroll/index.js +56 -56
- package/src/hooks/useScrollRotate/index.js +16 -16
- package/src/hooks/useStatusOpenStore/helpers/index.js +101 -101
- package/src/hooks/useStatusOpenStore/index.js +172 -172
- package/src/hooks/useStatusOrdersClient/queries.js +80 -80
- package/src/hooks/useStore/queries.js +163 -163
- package/src/hooks/useStoreCalendar/index.js +5 -5
- package/src/hooks/useStoreContacts/queries.js +1 -1
- package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
- package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +38 -38
- package/src/hooks/useWindowSize/index.js +38 -38
- package/src/mock/dessert/index.js +16 -16
- package/src/mock/index.js +1 -1
- package/.vscode/extensions.json +0 -6
- package/.vscode/settings.json +0 -8
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import { jaccardSimilarity } from './helpers'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get categories that contain products with names similar to the given product name.
|
|
5
|
-
* Uses the Jaccard similarity coefficient for string comparison.
|
|
6
|
-
* @param {Array} data - The array of categories with product information.
|
|
7
|
-
* @param {string} productName - The name of the product to search for.
|
|
8
|
-
* @param {number} similarityThreshold - The minimum similarity threshold for products (0.0 to 1.0).
|
|
9
|
-
* @returns {Array} - An array of categories containing similar products.
|
|
10
|
-
*/
|
|
11
|
-
export const getCategoriesWithProduct = (
|
|
12
|
-
data = [],
|
|
13
|
-
productName = '',
|
|
14
|
-
similarityThreshold = 0.0) => {
|
|
15
|
-
// Validate input data
|
|
16
|
-
if (!data || !Array.isArray(data)) {
|
|
17
|
-
return data || []
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (typeof productName !== 'string' || productName.trim() === '') {
|
|
21
|
-
return data
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Convert product name to lowercase for case-insensitive comparison
|
|
25
|
-
const productNameLower = productName.toLowerCase()
|
|
26
|
-
|
|
27
|
-
// Use a Set to store unique product names for better performance
|
|
28
|
-
const uniqueProductNames = new Set()
|
|
29
|
-
|
|
30
|
-
// Store unique product names in the Set
|
|
31
|
-
data.forEach(category => {
|
|
32
|
-
if (category?.productFoodsAll && Array.isArray(category.productFoodsAll)) {
|
|
33
|
-
category.productFoodsAll.forEach(product => {
|
|
34
|
-
const productLower = product.pName.toLowerCase()
|
|
35
|
-
uniqueProductNames.add(productLower)
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
// Filter categories that contain products with names similar to the given product name
|
|
41
|
-
const matchingCategories = data.filter(category => {
|
|
42
|
-
if (category?.productFoodsAll && Array.isArray(category.productFoodsAll)) {
|
|
43
|
-
const products = category.productFoodsAll.some(product => {
|
|
44
|
-
const productLower = product?.pName?.toLowerCase()
|
|
45
|
-
return jaccardSimilarity(productNameLower, productLower) >= similarityThreshold
|
|
46
|
-
})
|
|
47
|
-
return products
|
|
48
|
-
}
|
|
49
|
-
return false
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
// Find similar products in each matching category
|
|
53
|
-
const findProduct = matchingCategories?.map(categories => {
|
|
54
|
-
const similarProducts = categories.productFoodsAll.filter(product => {
|
|
55
|
-
const productLower = product?.pName?.toLowerCase()
|
|
56
|
-
return productLower.includes(productNameLower)
|
|
57
|
-
})
|
|
58
|
-
return similarProducts.length > 0 ? { ...categories, productFoodsAll: similarProducts } : null
|
|
59
|
-
}).filter(Boolean)
|
|
60
|
-
|
|
61
|
-
return findProduct
|
|
62
|
-
}
|
|
1
|
+
import { jaccardSimilarity } from './helpers'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get categories that contain products with names similar to the given product name.
|
|
5
|
+
* Uses the Jaccard similarity coefficient for string comparison.
|
|
6
|
+
* @param {Array} data - The array of categories with product information.
|
|
7
|
+
* @param {string} productName - The name of the product to search for.
|
|
8
|
+
* @param {number} similarityThreshold - The minimum similarity threshold for products (0.0 to 1.0).
|
|
9
|
+
* @returns {Array} - An array of categories containing similar products.
|
|
10
|
+
*/
|
|
11
|
+
export const getCategoriesWithProduct = (
|
|
12
|
+
data = [],
|
|
13
|
+
productName = '',
|
|
14
|
+
similarityThreshold = 0.0) => {
|
|
15
|
+
// Validate input data
|
|
16
|
+
if (!data || !Array.isArray(data)) {
|
|
17
|
+
return data || []
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof productName !== 'string' || productName.trim() === '') {
|
|
21
|
+
return data
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Convert product name to lowercase for case-insensitive comparison
|
|
25
|
+
const productNameLower = productName.toLowerCase()
|
|
26
|
+
|
|
27
|
+
// Use a Set to store unique product names for better performance
|
|
28
|
+
const uniqueProductNames = new Set()
|
|
29
|
+
|
|
30
|
+
// Store unique product names in the Set
|
|
31
|
+
data.forEach(category => {
|
|
32
|
+
if (category?.productFoodsAll && Array.isArray(category.productFoodsAll)) {
|
|
33
|
+
category.productFoodsAll.forEach(product => {
|
|
34
|
+
const productLower = product.pName.toLowerCase()
|
|
35
|
+
uniqueProductNames.add(productLower)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// Filter categories that contain products with names similar to the given product name
|
|
41
|
+
const matchingCategories = data.filter(category => {
|
|
42
|
+
if (category?.productFoodsAll && Array.isArray(category.productFoodsAll)) {
|
|
43
|
+
const products = category.productFoodsAll.some(product => {
|
|
44
|
+
const productLower = product?.pName?.toLowerCase()
|
|
45
|
+
return jaccardSimilarity(productNameLower, productLower) >= similarityThreshold
|
|
46
|
+
})
|
|
47
|
+
return products
|
|
48
|
+
}
|
|
49
|
+
return false
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// Find similar products in each matching category
|
|
53
|
+
const findProduct = matchingCategories?.map(categories => {
|
|
54
|
+
const similarProducts = categories.productFoodsAll.filter(product => {
|
|
55
|
+
const productLower = product?.pName?.toLowerCase()
|
|
56
|
+
return productLower.includes(productNameLower)
|
|
57
|
+
})
|
|
58
|
+
return similarProducts.length > 0 ? { ...categories, productFoodsAll: similarProducts } : null
|
|
59
|
+
}).filter(Boolean)
|
|
60
|
+
|
|
61
|
+
return findProduct
|
|
62
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export const getSession = async () => {
|
|
2
|
-
try {
|
|
3
|
-
const res = await fetch(`${process.env.URL_BASE}/api/auth/getAuth`,
|
|
4
|
-
{ method: 'GET', headers: { 'Content-Type': 'application/json' } })
|
|
5
|
-
const data = await res.json()
|
|
6
|
-
const { ok, ok: { user }, isSession } = data || {}
|
|
7
|
-
const { deviceid, token } = user || {}
|
|
8
|
-
return {
|
|
9
|
-
user,
|
|
10
|
-
ok,
|
|
11
|
-
isSession,
|
|
12
|
-
deviceid,
|
|
13
|
-
token
|
|
14
|
-
}
|
|
15
|
-
} catch (e) {
|
|
16
|
-
return e
|
|
17
|
-
}
|
|
18
|
-
}
|
|
1
|
+
export const getSession = async () => {
|
|
2
|
+
try {
|
|
3
|
+
const res = await fetch(`${process.env.URL_BASE}/api/auth/getAuth`,
|
|
4
|
+
{ method: 'GET', headers: { 'Content-Type': 'application/json' } })
|
|
5
|
+
const data = await res.json()
|
|
6
|
+
const { ok, ok: { user }, isSession } = data || {}
|
|
7
|
+
const { deviceid, token } = user || {}
|
|
8
|
+
return {
|
|
9
|
+
user,
|
|
10
|
+
ok,
|
|
11
|
+
isSession,
|
|
12
|
+
deviceid,
|
|
13
|
+
token
|
|
14
|
+
}
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return e
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react'
|
|
2
|
-
|
|
3
|
-
export const useAnimationFrame = (callback, start, end, duration = 1000) => {
|
|
4
|
-
const functionRef = useRef()
|
|
5
|
-
const delta = Math.abs(start - end)
|
|
6
|
-
const frameCount = Math.ceil(60 * (duration / 1000))
|
|
7
|
-
const iteration = useRef(frameCount)
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
const animate = (rafId) => {
|
|
10
|
-
if (iteration.current <= 0) {
|
|
11
|
-
cancelAnimationFrame(rafId)
|
|
12
|
-
iteration.current = frameCount
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
callback(Math.max(delta / iteration.current, 1))
|
|
16
|
-
iteration.current--
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (delta > 0) functionRef.current = requestAnimationFrame(animate)
|
|
20
|
-
|
|
21
|
-
return () => { return cancelAnimationFrame(functionRef.current) }
|
|
22
|
-
}, [callback, delta, frameCount, iteration])
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// const Counter = ({ numeral = 0 }) => {
|
|
26
|
-
// const [currentValue, setCurrentValue] = useState(0)
|
|
27
|
-
// const fxOperator = currentValue > numeral ? 'subtraction' : 'addition'
|
|
28
|
-
|
|
29
|
-
// useAnimationFrame(
|
|
30
|
-
// (diffValue) => {
|
|
31
|
-
// // Pass on a function to the setter of the state
|
|
32
|
-
// // to make sure we always have the latest state
|
|
33
|
-
// setCurrentValue((prevCount) => {
|
|
34
|
-
// return fxOperator === 'addition'
|
|
35
|
-
// ? prevCount + diffValue
|
|
36
|
-
// : prevCount - diffValue
|
|
37
|
-
// }
|
|
38
|
-
// )
|
|
39
|
-
// },
|
|
40
|
-
// currentValue,
|
|
41
|
-
// numeral,
|
|
42
|
-
// 300
|
|
43
|
-
// )
|
|
44
|
-
// return <>{new Intl.NumberFormat().format(Math.round(currentValue))}</>
|
|
45
|
-
// }
|
|
1
|
+
import { useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
export const useAnimationFrame = (callback, start, end, duration = 1000) => {
|
|
4
|
+
const functionRef = useRef()
|
|
5
|
+
const delta = Math.abs(start - end)
|
|
6
|
+
const frameCount = Math.ceil(60 * (duration / 1000))
|
|
7
|
+
const iteration = useRef(frameCount)
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const animate = (rafId) => {
|
|
10
|
+
if (iteration.current <= 0) {
|
|
11
|
+
cancelAnimationFrame(rafId)
|
|
12
|
+
iteration.current = frameCount
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
callback(Math.max(delta / iteration.current, 1))
|
|
16
|
+
iteration.current--
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (delta > 0) functionRef.current = requestAnimationFrame(animate)
|
|
20
|
+
|
|
21
|
+
return () => { return cancelAnimationFrame(functionRef.current) }
|
|
22
|
+
}, [callback, delta, frameCount, iteration])
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// const Counter = ({ numeral = 0 }) => {
|
|
26
|
+
// const [currentValue, setCurrentValue] = useState(0)
|
|
27
|
+
// const fxOperator = currentValue > numeral ? 'subtraction' : 'addition'
|
|
28
|
+
|
|
29
|
+
// useAnimationFrame(
|
|
30
|
+
// (diffValue) => {
|
|
31
|
+
// // Pass on a function to the setter of the state
|
|
32
|
+
// // to make sure we always have the latest state
|
|
33
|
+
// setCurrentValue((prevCount) => {
|
|
34
|
+
// return fxOperator === 'addition'
|
|
35
|
+
// ? prevCount + diffValue
|
|
36
|
+
// : prevCount - diffValue
|
|
37
|
+
// }
|
|
38
|
+
// )
|
|
39
|
+
// },
|
|
40
|
+
// currentValue,
|
|
41
|
+
// numeral,
|
|
42
|
+
// 300
|
|
43
|
+
// )
|
|
44
|
+
// return <>{new Intl.NumberFormat().format(Math.round(currentValue))}</>
|
|
45
|
+
// }
|
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useState,
|
|
3
|
-
useEffect,
|
|
4
|
-
useMemo
|
|
5
|
-
} from 'react'
|
|
6
|
-
import { useMutation } from '@apollo/client'
|
|
7
|
-
import { DELETE_ONE_ITEM_SHOPPING_PRODUCT } from './queries'
|
|
8
|
-
import { useCart, useGetCart } from '../useCart'
|
|
9
|
-
import { useManageQueryParams } from '../useManageQueryParams'
|
|
10
|
-
import { calculateTotalPrice } from './helpers'
|
|
11
|
-
export * from './helpers'
|
|
12
|
-
/**
|
|
13
|
-
* Custom hook for managing the shopping cart functionality.
|
|
14
|
-
* @param {Object} props - Props to control various UI elements.
|
|
15
|
-
* @param {function} props.setCountItemProduct - Function to set the count of items in the cart.
|
|
16
|
-
* @param {function} props.setAlertBox - Function to set an alert message.
|
|
17
|
-
* @param {function} props.handleMenu - Function to handle cart menu visibility.
|
|
18
|
-
* @returns {Object} An object with various shopping cart-related functions and data.
|
|
19
|
-
*/
|
|
20
|
-
export const useAsideCart = ({
|
|
21
|
-
openModalProduct = false,
|
|
22
|
-
setCountItemProduct = () => { },
|
|
23
|
-
setAlertBox = () => { },
|
|
24
|
-
setOpenModalProduct = () => { },
|
|
25
|
-
handleMenu = () => { }
|
|
26
|
-
} = {}) => {
|
|
27
|
-
const { getOneProduct } = useCart({
|
|
28
|
-
handleMenu,
|
|
29
|
-
openModalProduct,
|
|
30
|
-
setOpenModalProduct
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
const { handleQuery } = useManageQueryParams()
|
|
34
|
-
|
|
35
|
-
const [totalProductPrice, setTotalProductPrice] = useState(0)
|
|
36
|
-
|
|
37
|
-
const [dataShoppingCard, { loading }] = useGetCart({ setCountItemProduct })
|
|
38
|
-
|
|
39
|
-
// Lógica de transformación de los datos
|
|
40
|
-
const result2 = useMemo(() => {
|
|
41
|
-
if (!loading && dataShoppingCard) {
|
|
42
|
-
return dataShoppingCard.reduce((r, a) => {
|
|
43
|
-
const storeName = a.getStore?.storeName
|
|
44
|
-
if (storeName) {
|
|
45
|
-
r[storeName] = r[storeName] || []
|
|
46
|
-
r[storeName].push(a)
|
|
47
|
-
}
|
|
48
|
-
return r
|
|
49
|
-
}, {})
|
|
50
|
-
}
|
|
51
|
-
return {}
|
|
52
|
-
}, [loading, dataShoppingCard])
|
|
53
|
-
|
|
54
|
-
// Obtener dataProduct2 directamente
|
|
55
|
-
const key = useMemo(() => {
|
|
56
|
-
return Object.keys(result2)
|
|
57
|
-
}, [result2])
|
|
58
|
-
|
|
59
|
-
useEffect(() => {
|
|
60
|
-
const totalPrice = calculateTotalPrice(dataShoppingCard)
|
|
61
|
-
setTotalProductPrice(Math.abs(totalPrice))
|
|
62
|
-
}, [dataShoppingCard])
|
|
63
|
-
|
|
64
|
-
console.log(dataShoppingCard)
|
|
65
|
-
const [deleteOneItem] = useMutation(DELETE_ONE_ITEM_SHOPPING_PRODUCT, {
|
|
66
|
-
onCompleted: data => {
|
|
67
|
-
setAlertBox({ message: data?.deleteOneItem?.message })
|
|
68
|
-
|
|
69
|
-
if (dataShoppingCard?.length === 1 && data?.deleteOneItem?.success) {
|
|
70
|
-
setAlertBox({ message: 'Tu carrito está vacío' }) // Ajusta el mensaje en español
|
|
71
|
-
handleMenu(false) // Oculta el menú del carrito
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
const handleEditProduct = async (item) => {
|
|
77
|
-
const pId = item?.pId || null
|
|
78
|
-
if (pId) handleQuery('plato', pId)
|
|
79
|
-
if (pId) {
|
|
80
|
-
const product = { pId, intoCart: true }
|
|
81
|
-
getOneProduct(product)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Handle the deletion of a shopping cart item.
|
|
86
|
-
* @param {Object} item - The item to be deleted from the shopping cart.
|
|
87
|
-
*/
|
|
88
|
-
const handleDeleteItemShopping = async (item) => {
|
|
89
|
-
try {
|
|
90
|
-
const { cState, ShoppingCard } = item
|
|
91
|
-
await deleteOneItem({
|
|
92
|
-
variables: {
|
|
93
|
-
cState,
|
|
94
|
-
ShoppingCard
|
|
95
|
-
},
|
|
96
|
-
update: (cache, { data }) => {
|
|
97
|
-
const success = data?.deleteOneItem?.success
|
|
98
|
-
if (success && ShoppingCard) {
|
|
99
|
-
cache.modify({
|
|
100
|
-
fields: {
|
|
101
|
-
getAllShoppingCard (existingCart, { readField }) {
|
|
102
|
-
if (Array.isArray(existingCart) && existingCart) {
|
|
103
|
-
if (existingCart.length === 1) {
|
|
104
|
-
setCountItemProduct(0)
|
|
105
|
-
}
|
|
106
|
-
const updatedCart = {
|
|
107
|
-
...existingCart,
|
|
108
|
-
...existingCart?.filter(product =>
|
|
109
|
-
readField('ShoppingCard', product) !== ShoppingCard
|
|
110
|
-
)
|
|
111
|
-
}
|
|
112
|
-
if (typeof updatedCart === 'object' && updatedCart !== null) {
|
|
113
|
-
const newLength = Object.keys(updatedCart)
|
|
114
|
-
if (updatedCart && newLength) {
|
|
115
|
-
setCountItemProduct(Object.keys(updatedCart).length)
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return updatedCart
|
|
119
|
-
} else {
|
|
120
|
-
return []
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
} catch (error) {
|
|
129
|
-
setAlertBox({ message: 'Error borranto el item. Por favor intenta nuevamente.', color: 'error' })
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Calculate the total price of a product.
|
|
134
|
-
* @param {number} ProPrice - The price of the product.
|
|
135
|
-
* @param {number} ProDelivery - The delivery cost of the product.
|
|
136
|
-
* @param {number} cant - The quantity of the product.
|
|
137
|
-
* @returns {number} The calculated total price.
|
|
138
|
-
*/
|
|
139
|
-
const sumProduct = (ProPrice, ProDelivery, cant) => {
|
|
140
|
-
const price = parseInt(ProPrice)
|
|
141
|
-
const priceFinal = cant * price
|
|
142
|
-
const delivery = parseInt(ProDelivery || 0)
|
|
143
|
-
return delivery ? priceFinal + delivery : priceFinal
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
key,
|
|
148
|
-
totalProductPrice,
|
|
149
|
-
result2,
|
|
150
|
-
dataShoppingCard,
|
|
151
|
-
handleDeleteItemShopping,
|
|
152
|
-
handleEditProduct,
|
|
153
|
-
sumProduct
|
|
154
|
-
}
|
|
155
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo
|
|
5
|
+
} from 'react'
|
|
6
|
+
import { useMutation } from '@apollo/client'
|
|
7
|
+
import { DELETE_ONE_ITEM_SHOPPING_PRODUCT } from './queries'
|
|
8
|
+
import { useCart, useGetCart } from '../useCart'
|
|
9
|
+
import { useManageQueryParams } from '../useManageQueryParams'
|
|
10
|
+
import { calculateTotalPrice } from './helpers'
|
|
11
|
+
export * from './helpers'
|
|
12
|
+
/**
|
|
13
|
+
* Custom hook for managing the shopping cart functionality.
|
|
14
|
+
* @param {Object} props - Props to control various UI elements.
|
|
15
|
+
* @param {function} props.setCountItemProduct - Function to set the count of items in the cart.
|
|
16
|
+
* @param {function} props.setAlertBox - Function to set an alert message.
|
|
17
|
+
* @param {function} props.handleMenu - Function to handle cart menu visibility.
|
|
18
|
+
* @returns {Object} An object with various shopping cart-related functions and data.
|
|
19
|
+
*/
|
|
20
|
+
export const useAsideCart = ({
|
|
21
|
+
openModalProduct = false,
|
|
22
|
+
setCountItemProduct = () => { },
|
|
23
|
+
setAlertBox = () => { },
|
|
24
|
+
setOpenModalProduct = () => { },
|
|
25
|
+
handleMenu = () => { }
|
|
26
|
+
} = {}) => {
|
|
27
|
+
const { getOneProduct } = useCart({
|
|
28
|
+
handleMenu,
|
|
29
|
+
openModalProduct,
|
|
30
|
+
setOpenModalProduct
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const { handleQuery } = useManageQueryParams()
|
|
34
|
+
|
|
35
|
+
const [totalProductPrice, setTotalProductPrice] = useState(0)
|
|
36
|
+
|
|
37
|
+
const [dataShoppingCard, { loading }] = useGetCart({ setCountItemProduct })
|
|
38
|
+
|
|
39
|
+
// Lógica de transformación de los datos
|
|
40
|
+
const result2 = useMemo(() => {
|
|
41
|
+
if (!loading && dataShoppingCard) {
|
|
42
|
+
return dataShoppingCard.reduce((r, a) => {
|
|
43
|
+
const storeName = a.getStore?.storeName
|
|
44
|
+
if (storeName) {
|
|
45
|
+
r[storeName] = r[storeName] || []
|
|
46
|
+
r[storeName].push(a)
|
|
47
|
+
}
|
|
48
|
+
return r
|
|
49
|
+
}, {})
|
|
50
|
+
}
|
|
51
|
+
return {}
|
|
52
|
+
}, [loading, dataShoppingCard])
|
|
53
|
+
|
|
54
|
+
// Obtener dataProduct2 directamente
|
|
55
|
+
const key = useMemo(() => {
|
|
56
|
+
return Object.keys(result2)
|
|
57
|
+
}, [result2])
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const totalPrice = calculateTotalPrice(dataShoppingCard)
|
|
61
|
+
setTotalProductPrice(Math.abs(totalPrice))
|
|
62
|
+
}, [dataShoppingCard])
|
|
63
|
+
|
|
64
|
+
console.log(dataShoppingCard)
|
|
65
|
+
const [deleteOneItem] = useMutation(DELETE_ONE_ITEM_SHOPPING_PRODUCT, {
|
|
66
|
+
onCompleted: data => {
|
|
67
|
+
setAlertBox({ message: data?.deleteOneItem?.message })
|
|
68
|
+
|
|
69
|
+
if (dataShoppingCard?.length === 1 && data?.deleteOneItem?.success) {
|
|
70
|
+
setAlertBox({ message: 'Tu carrito está vacío' }) // Ajusta el mensaje en español
|
|
71
|
+
handleMenu(false) // Oculta el menú del carrito
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const handleEditProduct = async (item) => {
|
|
77
|
+
const pId = item?.pId || null
|
|
78
|
+
if (pId) handleQuery('plato', pId)
|
|
79
|
+
if (pId) {
|
|
80
|
+
const product = { pId, intoCart: true }
|
|
81
|
+
getOneProduct(product)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Handle the deletion of a shopping cart item.
|
|
86
|
+
* @param {Object} item - The item to be deleted from the shopping cart.
|
|
87
|
+
*/
|
|
88
|
+
const handleDeleteItemShopping = async (item) => {
|
|
89
|
+
try {
|
|
90
|
+
const { cState, ShoppingCard } = item
|
|
91
|
+
await deleteOneItem({
|
|
92
|
+
variables: {
|
|
93
|
+
cState,
|
|
94
|
+
ShoppingCard
|
|
95
|
+
},
|
|
96
|
+
update: (cache, { data }) => {
|
|
97
|
+
const success = data?.deleteOneItem?.success
|
|
98
|
+
if (success && ShoppingCard) {
|
|
99
|
+
cache.modify({
|
|
100
|
+
fields: {
|
|
101
|
+
getAllShoppingCard (existingCart, { readField }) {
|
|
102
|
+
if (Array.isArray(existingCart) && existingCart) {
|
|
103
|
+
if (existingCart.length === 1) {
|
|
104
|
+
setCountItemProduct(0)
|
|
105
|
+
}
|
|
106
|
+
const updatedCart = {
|
|
107
|
+
...existingCart,
|
|
108
|
+
...existingCart?.filter(product =>
|
|
109
|
+
readField('ShoppingCard', product) !== ShoppingCard
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
if (typeof updatedCart === 'object' && updatedCart !== null) {
|
|
113
|
+
const newLength = Object.keys(updatedCart)
|
|
114
|
+
if (updatedCart && newLength) {
|
|
115
|
+
setCountItemProduct(Object.keys(updatedCart).length)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return updatedCart
|
|
119
|
+
} else {
|
|
120
|
+
return []
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
} catch (error) {
|
|
129
|
+
setAlertBox({ message: 'Error borranto el item. Por favor intenta nuevamente.', color: 'error' })
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Calculate the total price of a product.
|
|
134
|
+
* @param {number} ProPrice - The price of the product.
|
|
135
|
+
* @param {number} ProDelivery - The delivery cost of the product.
|
|
136
|
+
* @param {number} cant - The quantity of the product.
|
|
137
|
+
* @returns {number} The calculated total price.
|
|
138
|
+
*/
|
|
139
|
+
const sumProduct = (ProPrice, ProDelivery, cant) => {
|
|
140
|
+
const price = parseInt(ProPrice)
|
|
141
|
+
const priceFinal = cant * price
|
|
142
|
+
const delivery = parseInt(ProDelivery || 0)
|
|
143
|
+
return delivery ? priceFinal + delivery : priceFinal
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
key,
|
|
148
|
+
totalProductPrice,
|
|
149
|
+
result2,
|
|
150
|
+
dataShoppingCard,
|
|
151
|
+
handleDeleteItemShopping,
|
|
152
|
+
handleEditProduct,
|
|
153
|
+
sumProduct
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { gql } from '@apollo/client'
|
|
2
|
-
|
|
3
|
-
export const DELETE_ONE_ITEM_SHOPPING_PRODUCT = gql`
|
|
4
|
-
mutation deleteOneItem($cState: Int, $ShoppingCard: ID) {
|
|
5
|
-
deleteOneItem(cState: $cState, ShoppingCard: $ShoppingCard){
|
|
6
|
-
success
|
|
7
|
-
message
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
`
|
|
1
|
+
import { gql } from '@apollo/client'
|
|
2
|
+
|
|
3
|
+
export const DELETE_ONE_ITEM_SHOPPING_PRODUCT = gql`
|
|
4
|
+
mutation deleteOneItem($cState: Int, $ShoppingCard: ID) {
|
|
5
|
+
deleteOneItem(cState: $cState, ShoppingCard: $ShoppingCard){
|
|
6
|
+
success
|
|
7
|
+
message
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
`
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './useCart'
|
|
2
|
-
export * from './useGetCart'
|
|
1
|
+
export * from './useCart'
|
|
2
|
+
export * from './useGetCart'
|