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.
- package/.babelrc +13 -13
- package/.env +1 -0
- package/.eslintrc.js +132 -0
- package/.eslintrc.json +107 -107
- package/.github/pull_request_template.md +17 -17
- package/.github/workflows/pepeline.yaml +29 -29
- package/.vscode/extensions.json +6 -0
- package/.vscode/settings.json +12 -0
- package/README.md +1 -1
- package/jsconfig.json +27 -27
- package/next.config.js +128 -128
- package/package.json +8 -6
- package/script.txt +7 -0
- package/src/cookies/index.ts +3 -3
- package/src/hooks/index.js +46 -19
- package/src/hooks/updateExtProductFoodsOptional/index.js +38 -0
- package/src/hooks/useAcumulateDate/index.js +16 -16
- package/src/hooks/useAnimationText/index.jsx +31 -30
- package/src/hooks/useBanner/index.js +19 -0
- package/src/hooks/useCatWithProduct/index.js +42 -0
- package/src/hooks/useCatWithProduct/queries.js +172 -0
- package/src/hooks/useCategoryInStore/index.js +94 -0
- package/src/hooks/{useGetCategorieStore → useCategoryInStore}/queries.js +77 -77
- package/src/hooks/useCategoryStore/index.js +7 -7
- package/src/hooks/useCategoryStore/queries.js +16 -16
- package/src/hooks/useChartData/index.js +168 -0
- package/src/hooks/useCheckbox/index.js +114 -114
- package/src/hooks/useClients/index.js +13 -13
- package/src/hooks/useClients/queries.js +117 -117
- package/src/hooks/useConnection/index.js +23 -0
- package/src/hooks/useCreateProduct/helpers/useEditImageProduct/index.js +165 -0
- package/src/hooks/useCreateProduct/index.js +268 -0
- package/src/hooks/useDessert/index.js +141 -0
- package/src/hooks/useDrag/index.js +62 -57
- package/src/hooks/useEvent/index.js +33 -33
- package/src/hooks/useFetchJson/index.js +24 -24
- package/src/hooks/useFetchMoreInteractions/index.jsx +37 -34
- package/src/hooks/useFormTools/index.js +83 -70
- package/src/hooks/useFormatDate/index.js +34 -0
- package/src/hooks/useFullScreenMode/index.js +65 -65
- package/src/hooks/useHover/index.js +28 -28
- package/src/hooks/useImageOptimization/index.js +28 -0
- package/src/hooks/useImageWeight/index.js +52 -0
- package/src/hooks/useImagesStore/index.js +171 -0
- package/src/hooks/useImagesStore/queries.js +216 -0
- package/src/hooks/useInnerHtml/index.js +37 -37
- package/src/hooks/useIntersection/index.js +84 -31
- package/src/hooks/useKeypress/index.js +27 -27
- package/src/hooks/useLazyScript/index.js +72 -0
- package/src/hooks/useLocalSorage/index.js +35 -35
- package/src/hooks/useLocationNavigate/index.js +54 -54
- package/src/hooks/useMobile/index.js +37 -37
- package/src/hooks/useMutateHeight/index.js +37 -0
- package/src/hooks/useProductsFood/index.js +190 -0
- package/src/hooks/{useGetProductsFood → useProductsFood}/queriesStore.js +780 -765
- package/src/hooks/useProductsFood/usetagsProducts.js +57 -0
- package/src/hooks/useReport/index.js +35 -0
- package/src/hooks/useReport/queries.js +122 -0
- package/src/hooks/useRestaurant/index.js +19 -19
- package/src/hooks/useRestaurant/queries.js +79 -69
- package/src/hooks/useSales/index.js +589 -489
- package/src/hooks/useSales/queries.js +290 -229
- package/src/hooks/useSales/useGetSale.js +12 -0
- package/src/hooks/useSales/useTotalSales.js +17 -0
- package/src/hooks/useSaveAvailableProduct/helpers/index.js +30 -0
- package/src/hooks/useSaveAvailableProduct/index.js +26 -0
- package/src/hooks/useSaveAvailableProduct/queries.js +10 -0
- package/src/hooks/useSchedule/index.jsx +23 -0
- package/src/hooks/useSetState/index.js +24 -24
- package/src/hooks/useStore/index.js +17 -17
- package/src/hooks/useStore/queries.js +135 -135
- package/src/hooks/useStoreCalendar/index.js +7 -0
- package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
- package/src/hooks/useUpdateCart/index.js +124 -124
- package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +37 -0
- package/src/hooks/useUser/index.js +7 -3
- package/src/hooks/useUser/queries.js +69 -0
- package/src/hooks/useWindowSize/index.js +37 -37
- package/src/index.jsx +2 -1
- package/src/mock/dessert/index.js +16 -0
- package/src/mock/index.js +2 -0
- package/src/utils/index.js +106 -54
- package/src/hooks/useGetCategorieStore/index.js +0 -21
- package/src/hooks/useGetProductsFood/index.js +0 -46
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
|
|
3
|
-
export const useSetState = initialState => {
|
|
4
|
-
const [state, setState] = useState(initialState)
|
|
5
|
-
const increase = () => {return setState(state + 1)}
|
|
6
|
-
const decrease = () => {return setState(state - 1)}
|
|
7
|
-
const reset = () => {return setState(0)}
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
if (state === -1) return reset()
|
|
10
|
-
return {}
|
|
11
|
-
}, [state])
|
|
12
|
-
// Cambio de estado
|
|
13
|
-
const changeState = () => {
|
|
14
|
-
setState(!state)
|
|
15
|
-
}
|
|
16
|
-
return {
|
|
17
|
-
state,
|
|
18
|
-
increase,
|
|
19
|
-
decrease,
|
|
20
|
-
reset,
|
|
21
|
-
changeState,
|
|
22
|
-
setState
|
|
23
|
-
}
|
|
24
|
-
}
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export const useSetState = initialState => {
|
|
4
|
+
const [state, setState] = useState(initialState)
|
|
5
|
+
const increase = () => {return setState(state + 1)}
|
|
6
|
+
const decrease = () => {return setState(state - 1)}
|
|
7
|
+
const reset = () => {return setState(0)}
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (state === -1) return reset()
|
|
10
|
+
return {}
|
|
11
|
+
}, [state])
|
|
12
|
+
// Cambio de estado
|
|
13
|
+
const changeState = () => {
|
|
14
|
+
setState(!state)
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
state,
|
|
18
|
+
increase,
|
|
19
|
+
decrease,
|
|
20
|
+
reset,
|
|
21
|
+
changeState,
|
|
22
|
+
setState
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { useQuery } from '@apollo/client'
|
|
2
|
-
import { useState } from 'react'
|
|
3
|
-
import { GET_ONE_STORE } from './queries'
|
|
4
|
-
|
|
5
|
-
export const useStore = () => {
|
|
6
|
-
const [store, setStore] = useState({})
|
|
7
|
-
const { loading, error } = useQuery(GET_ONE_STORE, {
|
|
8
|
-
fetchPolicy: 'cache-and-network',
|
|
9
|
-
onCompleted: (res) => {
|
|
10
|
-
const { getStore } = res || {}
|
|
11
|
-
setStore(getStore)
|
|
12
|
-
},
|
|
13
|
-
onError: (err) => {
|
|
14
|
-
console.log(err)
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
return [store, { loading, error }]
|
|
1
|
+
import { useQuery } from '@apollo/client'
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { GET_ONE_STORE } from './queries'
|
|
4
|
+
|
|
5
|
+
export const useStore = () => {
|
|
6
|
+
const [store, setStore] = useState({})
|
|
7
|
+
const { loading, error } = useQuery(GET_ONE_STORE, {
|
|
8
|
+
fetchPolicy: 'cache-and-network',
|
|
9
|
+
onCompleted: (res) => {
|
|
10
|
+
const { getStore } = res || {}
|
|
11
|
+
setStore(getStore)
|
|
12
|
+
},
|
|
13
|
+
onError: (err) => {
|
|
14
|
+
console.log(err)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
return [store, { loading, error }]
|
|
18
18
|
}
|
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
import { gql } from '@apollo/client'
|
|
2
|
-
|
|
3
|
-
export const CREATE_ONE_STORE = gql`
|
|
4
|
-
mutation newRegisterStore($input: IStore){
|
|
5
|
-
newRegisterStore(input: $input){
|
|
6
|
-
success
|
|
7
|
-
message
|
|
8
|
-
idStore
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
`
|
|
12
|
-
export const GET_ONE_STORE = gql`
|
|
13
|
-
query getStore($id: ID){
|
|
14
|
-
getStore(id: $id ){
|
|
15
|
-
cId
|
|
16
|
-
id
|
|
17
|
-
dId
|
|
18
|
-
idStore
|
|
19
|
-
ctId
|
|
20
|
-
neighborhoodStore
|
|
21
|
-
Viaprincipal
|
|
22
|
-
catStore
|
|
23
|
-
storeOwner
|
|
24
|
-
storeName
|
|
25
|
-
ImageName
|
|
26
|
-
emailStore
|
|
27
|
-
storePhone
|
|
28
|
-
socialRaz
|
|
29
|
-
Image
|
|
30
|
-
banner
|
|
31
|
-
|
|
32
|
-
documentIdentifier
|
|
33
|
-
uPhoNum
|
|
34
|
-
storeName
|
|
35
|
-
ULocation
|
|
36
|
-
upLat
|
|
37
|
-
upLon
|
|
38
|
-
uState
|
|
39
|
-
siteWeb
|
|
40
|
-
|
|
41
|
-
description
|
|
42
|
-
createdAt
|
|
43
|
-
secVia
|
|
44
|
-
NitStore
|
|
45
|
-
typeRegiments
|
|
46
|
-
typeContribute
|
|
47
|
-
addressStore
|
|
48
|
-
pais {
|
|
49
|
-
cId
|
|
50
|
-
cName
|
|
51
|
-
cCalCod
|
|
52
|
-
cState
|
|
53
|
-
cDatCre
|
|
54
|
-
cDatMod
|
|
55
|
-
}
|
|
56
|
-
city {
|
|
57
|
-
ctId
|
|
58
|
-
dId
|
|
59
|
-
cName
|
|
60
|
-
cState
|
|
61
|
-
cDatCre
|
|
62
|
-
cDatMod
|
|
63
|
-
}
|
|
64
|
-
department {
|
|
65
|
-
dId
|
|
66
|
-
cId
|
|
67
|
-
dName
|
|
68
|
-
dState
|
|
69
|
-
dDatCre
|
|
70
|
-
dDatMod
|
|
71
|
-
}
|
|
72
|
-
getStoreSchedules {
|
|
73
|
-
idStore
|
|
74
|
-
schId
|
|
75
|
-
id
|
|
76
|
-
schDay
|
|
77
|
-
schHoSta
|
|
78
|
-
schHoEnd
|
|
79
|
-
schState
|
|
80
|
-
}
|
|
81
|
-
cateStore {
|
|
82
|
-
catStore
|
|
83
|
-
cName
|
|
84
|
-
cState
|
|
85
|
-
cDatCre
|
|
86
|
-
cDatMod
|
|
87
|
-
csDescription
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
`
|
|
93
|
-
export const GET_ONE_STORE_BY_ID = gql`
|
|
94
|
-
query getOneStore($idStore: ID){
|
|
95
|
-
getOneStore(idStore: $idStore ){
|
|
96
|
-
cId
|
|
97
|
-
id
|
|
98
|
-
dId
|
|
99
|
-
idStore
|
|
100
|
-
ctId
|
|
101
|
-
neighborhoodStore
|
|
102
|
-
Viaprincipal
|
|
103
|
-
catStore
|
|
104
|
-
storeOwner
|
|
105
|
-
storeName
|
|
106
|
-
emailStore
|
|
107
|
-
storePhone
|
|
108
|
-
socialRaz
|
|
109
|
-
Image
|
|
110
|
-
banner
|
|
111
|
-
documentIdentifier
|
|
112
|
-
uPhoNum
|
|
113
|
-
ULocation
|
|
114
|
-
upLat
|
|
115
|
-
upLon
|
|
116
|
-
uState
|
|
117
|
-
siteWeb
|
|
118
|
-
description
|
|
119
|
-
secVia
|
|
120
|
-
NitStore
|
|
121
|
-
typeRegiments
|
|
122
|
-
typeContribute
|
|
123
|
-
addressStore
|
|
124
|
-
createAt
|
|
125
|
-
cateStore {
|
|
126
|
-
catStore
|
|
127
|
-
cName
|
|
128
|
-
cState
|
|
129
|
-
cDatCre
|
|
130
|
-
cDatMod
|
|
131
|
-
csDescription
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
1
|
+
import { gql } from '@apollo/client'
|
|
2
|
+
|
|
3
|
+
export const CREATE_ONE_STORE = gql`
|
|
4
|
+
mutation newRegisterStore($input: IStore){
|
|
5
|
+
newRegisterStore(input: $input){
|
|
6
|
+
success
|
|
7
|
+
message
|
|
8
|
+
idStore
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
`
|
|
12
|
+
export const GET_ONE_STORE = gql`
|
|
13
|
+
query getStore($id: ID){
|
|
14
|
+
getStore(id: $id ){
|
|
15
|
+
cId
|
|
16
|
+
id
|
|
17
|
+
dId
|
|
18
|
+
idStore
|
|
19
|
+
ctId
|
|
20
|
+
neighborhoodStore
|
|
21
|
+
Viaprincipal
|
|
22
|
+
catStore
|
|
23
|
+
storeOwner
|
|
24
|
+
storeName
|
|
25
|
+
ImageName
|
|
26
|
+
emailStore
|
|
27
|
+
storePhone
|
|
28
|
+
socialRaz
|
|
29
|
+
Image
|
|
30
|
+
banner
|
|
31
|
+
|
|
32
|
+
documentIdentifier
|
|
33
|
+
uPhoNum
|
|
34
|
+
storeName
|
|
35
|
+
ULocation
|
|
36
|
+
upLat
|
|
37
|
+
upLon
|
|
38
|
+
uState
|
|
39
|
+
siteWeb
|
|
40
|
+
|
|
41
|
+
description
|
|
42
|
+
createdAt
|
|
43
|
+
secVia
|
|
44
|
+
NitStore
|
|
45
|
+
typeRegiments
|
|
46
|
+
typeContribute
|
|
47
|
+
addressStore
|
|
48
|
+
pais {
|
|
49
|
+
cId
|
|
50
|
+
cName
|
|
51
|
+
cCalCod
|
|
52
|
+
cState
|
|
53
|
+
cDatCre
|
|
54
|
+
cDatMod
|
|
55
|
+
}
|
|
56
|
+
city {
|
|
57
|
+
ctId
|
|
58
|
+
dId
|
|
59
|
+
cName
|
|
60
|
+
cState
|
|
61
|
+
cDatCre
|
|
62
|
+
cDatMod
|
|
63
|
+
}
|
|
64
|
+
department {
|
|
65
|
+
dId
|
|
66
|
+
cId
|
|
67
|
+
dName
|
|
68
|
+
dState
|
|
69
|
+
dDatCre
|
|
70
|
+
dDatMod
|
|
71
|
+
}
|
|
72
|
+
getStoreSchedules {
|
|
73
|
+
idStore
|
|
74
|
+
schId
|
|
75
|
+
id
|
|
76
|
+
schDay
|
|
77
|
+
schHoSta
|
|
78
|
+
schHoEnd
|
|
79
|
+
schState
|
|
80
|
+
}
|
|
81
|
+
cateStore {
|
|
82
|
+
catStore
|
|
83
|
+
cName
|
|
84
|
+
cState
|
|
85
|
+
cDatCre
|
|
86
|
+
cDatMod
|
|
87
|
+
csDescription
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
`
|
|
93
|
+
export const GET_ONE_STORE_BY_ID = gql`
|
|
94
|
+
query getOneStore($idStore: ID){
|
|
95
|
+
getOneStore(idStore: $idStore ){
|
|
96
|
+
cId
|
|
97
|
+
id
|
|
98
|
+
dId
|
|
99
|
+
idStore
|
|
100
|
+
ctId
|
|
101
|
+
neighborhoodStore
|
|
102
|
+
Viaprincipal
|
|
103
|
+
catStore
|
|
104
|
+
storeOwner
|
|
105
|
+
storeName
|
|
106
|
+
emailStore
|
|
107
|
+
storePhone
|
|
108
|
+
socialRaz
|
|
109
|
+
Image
|
|
110
|
+
banner
|
|
111
|
+
documentIdentifier
|
|
112
|
+
uPhoNum
|
|
113
|
+
ULocation
|
|
114
|
+
upLat
|
|
115
|
+
upLon
|
|
116
|
+
uState
|
|
117
|
+
siteWeb
|
|
118
|
+
description
|
|
119
|
+
secVia
|
|
120
|
+
NitStore
|
|
121
|
+
typeRegiments
|
|
122
|
+
typeContribute
|
|
123
|
+
addressStore
|
|
124
|
+
createAt
|
|
125
|
+
cateStore {
|
|
126
|
+
catStore
|
|
127
|
+
cName
|
|
128
|
+
cState
|
|
129
|
+
cDatCre
|
|
130
|
+
cDatMod
|
|
131
|
+
csDescription
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
136
|
`
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
|
|
3
|
-
const DATE_UNITS = [
|
|
4
|
-
['day', 86400],
|
|
5
|
-
['hour', 3600],
|
|
6
|
-
['minute', 60],
|
|
7
|
-
['second', 1]
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
const getDateDiffs = (timestamp) => {
|
|
11
|
-
const now = Date.now()
|
|
12
|
-
const elapsed = (timestamp - now) / 1000
|
|
13
|
-
|
|
14
|
-
for (const [unit, secondsInUnit] of DATE_UNITS) {
|
|
15
|
-
if (Math.abs(elapsed) > secondsInUnit || unit === 'second') {
|
|
16
|
-
const value = Math.round(elapsed / secondsInUnit)
|
|
17
|
-
return { value, unit }
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const useTimeAgo = (timestamp) => {
|
|
23
|
-
const [timeAgo, setTimeAgo] = useState(() => { return getDateDiffs(timestamp) })
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
const interval = setInterval(() => {
|
|
27
|
-
const newTimeAgo = getDateDiffs(timestamp)
|
|
28
|
-
setTimeAgo(newTimeAgo)
|
|
29
|
-
}, 5000)
|
|
30
|
-
|
|
31
|
-
return () => { return clearInterval(interval) }
|
|
32
|
-
}, [timestamp])
|
|
33
|
-
|
|
34
|
-
const rtf = new Intl.RelativeTimeFormat('es', { style: 'short' })
|
|
35
|
-
|
|
36
|
-
const { value, unit } = timeAgo
|
|
37
|
-
|
|
38
|
-
return rtf && rtf?.format(value, unit)
|
|
39
|
-
}
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
const DATE_UNITS = [
|
|
4
|
+
['day', 86400],
|
|
5
|
+
['hour', 3600],
|
|
6
|
+
['minute', 60],
|
|
7
|
+
['second', 1]
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
const getDateDiffs = (timestamp) => {
|
|
11
|
+
const now = Date.now()
|
|
12
|
+
const elapsed = (timestamp - now) / 1000
|
|
13
|
+
|
|
14
|
+
for (const [unit, secondsInUnit] of DATE_UNITS) {
|
|
15
|
+
if (Math.abs(elapsed) > secondsInUnit || unit === 'second') {
|
|
16
|
+
const value = Math.round(elapsed / secondsInUnit)
|
|
17
|
+
return { value, unit }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const useTimeAgo = (timestamp) => {
|
|
23
|
+
const [timeAgo, setTimeAgo] = useState(() => { return getDateDiffs(timestamp) })
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const interval = setInterval(() => {
|
|
27
|
+
const newTimeAgo = getDateDiffs(timestamp)
|
|
28
|
+
setTimeAgo(newTimeAgo)
|
|
29
|
+
}, 5000)
|
|
30
|
+
|
|
31
|
+
return () => { return clearInterval(interval) }
|
|
32
|
+
}, [timestamp])
|
|
33
|
+
|
|
34
|
+
const rtf = new Intl.RelativeTimeFormat('es', { style: 'short' })
|
|
35
|
+
|
|
36
|
+
const { value, unit } = timeAgo
|
|
37
|
+
|
|
38
|
+
return rtf && rtf?.format(value, unit)
|
|
39
|
+
}
|
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
import debounce from 'lodash
|
|
2
|
-
import { useState, useEffect } from 'react'
|
|
3
|
-
import { getCurrentDomain } from '../../utils'
|
|
4
|
-
import { trigger } from '../useEvent'
|
|
5
|
-
import { Cookies } from '../../cookies'
|
|
6
|
-
|
|
7
|
-
// EXAMPLE
|
|
8
|
-
// https://codesandbox.io/s/nextjs-cart-system-tfg1e?file=/pages/_app.js
|
|
9
|
-
|
|
10
|
-
// Method to execute the event to add all items of the app.cart cookie
|
|
11
|
-
const updateCart = debounce((items = []) => {
|
|
12
|
-
trigger({ eventType: 'app.cart', data: { loading: true, items } })
|
|
13
|
-
}, 3000)
|
|
14
|
-
|
|
15
|
-
const EMPTY_CART = {
|
|
16
|
-
items: [],
|
|
17
|
-
total: 0
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const useUpdateCart = () => {
|
|
21
|
-
const domain = getCurrentDomain()
|
|
22
|
-
const keyToSaveData = 'app.cart'
|
|
23
|
-
const saveDataState = JSON.parse(Cookies.get(keyToSaveData) || '[]')
|
|
24
|
-
const [cart, setCart] = useState(EMPTY_CART)
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
// restore cart from cookie, this could also be tracked in a db
|
|
28
|
-
const cart = Cookies.get(keyToSaveData)
|
|
29
|
-
|
|
30
|
-
// if items in cart, set items and total to state
|
|
31
|
-
if (typeof cart === 'string' && cart !== 'undefined') {
|
|
32
|
-
const cartData = JSON.parse(cart)
|
|
33
|
-
const total = cartData.reduce(
|
|
34
|
-
(total, item) => {return total + item.price * item.quantity},
|
|
35
|
-
0
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
setCart({ items: cartData, total })
|
|
39
|
-
}
|
|
40
|
-
}, [])
|
|
41
|
-
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
Cookies.set(keyToSaveData, JSON.stringify(cart.items), { domain, path: '/' })
|
|
44
|
-
}, [cart, domain])
|
|
45
|
-
|
|
46
|
-
const handleAdd = (item) => {
|
|
47
|
-
// check for item already in cart
|
|
48
|
-
// if not in cart, add item else if item is found increment quantity
|
|
49
|
-
const itemExists = cart.items.find((i) => {return i.pId === item.pId})
|
|
50
|
-
|
|
51
|
-
if (!itemExists) {
|
|
52
|
-
setCart((prevCart) => {return {
|
|
53
|
-
items: [...prevCart.items, { ...item, quantity: 1 }],
|
|
54
|
-
total: prevCart.total + item.price
|
|
55
|
-
}})
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
setCart((prevCart) => {return {
|
|
61
|
-
items: prevCart.items.map((i) => {
|
|
62
|
-
if (i.pId === item.pId) {
|
|
63
|
-
return { ...i, quantity: i.quantity + 1 }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return i
|
|
67
|
-
}),
|
|
68
|
-
total: prevCart.total + item.price
|
|
69
|
-
}})
|
|
70
|
-
updateCart(cart)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const deleteProductCart = (item) => {
|
|
74
|
-
setCart((prevCart) => {
|
|
75
|
-
const items = prevCart.items
|
|
76
|
-
const index = items.findIndex((i) => {return i.pId === item.pId})
|
|
77
|
-
|
|
78
|
-
items.splice(index, 1)
|
|
79
|
-
|
|
80
|
-
const total = items.reduce((t, i) => {return t + i.quantity * i.price}, 0)
|
|
81
|
-
|
|
82
|
-
return { items, total }
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const decreaseItemFromCart = (item) => {
|
|
87
|
-
// check for item already in cart
|
|
88
|
-
// if quantity is more then in cart, subtract item else remove item
|
|
89
|
-
const itemInCart = cart.items.find((i) => {return i.pId === item.pId})
|
|
90
|
-
|
|
91
|
-
if (!itemInCart) {
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (itemInCart.quantity === 1) {
|
|
96
|
-
deleteProductCart(item)
|
|
97
|
-
|
|
98
|
-
return
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
setCart((prevCart) => {return {
|
|
102
|
-
items: prevCart.items.map((i) => {
|
|
103
|
-
if (i.pId === item.pId) {
|
|
104
|
-
return { ...i, quantity: item.quantity - 1 }
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return i
|
|
108
|
-
}),
|
|
109
|
-
total: prevCart.total - item.price
|
|
110
|
-
}})
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const clearCart = () => {
|
|
114
|
-
setCart(EMPTY_CART)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
saveDataState,
|
|
119
|
-
clearCart,
|
|
120
|
-
deleteProductCart,
|
|
121
|
-
decreaseItemFromCart,
|
|
122
|
-
handleAdd
|
|
123
|
-
}
|
|
124
|
-
}
|
|
1
|
+
import debounce from 'lodash'
|
|
2
|
+
import { useState, useEffect } from 'react'
|
|
3
|
+
import { getCurrentDomain } from '../../utils'
|
|
4
|
+
import { trigger } from '../useEvent'
|
|
5
|
+
import { Cookies } from '../../cookies'
|
|
6
|
+
|
|
7
|
+
// EXAMPLE
|
|
8
|
+
// https://codesandbox.io/s/nextjs-cart-system-tfg1e?file=/pages/_app.js
|
|
9
|
+
|
|
10
|
+
// Method to execute the event to add all items of the app.cart cookie
|
|
11
|
+
const updateCart = debounce.debounce((items = []) => {
|
|
12
|
+
trigger({ eventType: 'app.cart', data: { loading: true, items } })
|
|
13
|
+
}, 3000)
|
|
14
|
+
|
|
15
|
+
const EMPTY_CART = {
|
|
16
|
+
items: [],
|
|
17
|
+
total: 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const useUpdateCart = () => {
|
|
21
|
+
const domain = getCurrentDomain()
|
|
22
|
+
const keyToSaveData = 'app.cart'
|
|
23
|
+
const saveDataState = JSON.parse(Cookies.get(keyToSaveData) || '[]')
|
|
24
|
+
const [cart, setCart] = useState(EMPTY_CART)
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
// restore cart from cookie, this could also be tracked in a db
|
|
28
|
+
const cart = Cookies.get(keyToSaveData)
|
|
29
|
+
|
|
30
|
+
// if items in cart, set items and total to state
|
|
31
|
+
if (typeof cart === 'string' && cart !== 'undefined') {
|
|
32
|
+
const cartData = JSON.parse(cart)
|
|
33
|
+
const total = cartData.reduce(
|
|
34
|
+
(total, item) => {return total + item.price * item.quantity},
|
|
35
|
+
0
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
setCart({ items: cartData, total })
|
|
39
|
+
}
|
|
40
|
+
}, [])
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
Cookies.set(keyToSaveData, JSON.stringify(cart.items), { domain, path: '/' })
|
|
44
|
+
}, [cart, domain])
|
|
45
|
+
|
|
46
|
+
const handleAdd = (item) => {
|
|
47
|
+
// check for item already in cart
|
|
48
|
+
// if not in cart, add item else if item is found increment quantity
|
|
49
|
+
const itemExists = cart.items.find((i) => {return i.pId === item.pId})
|
|
50
|
+
|
|
51
|
+
if (!itemExists) {
|
|
52
|
+
setCart((prevCart) => {return {
|
|
53
|
+
items: [...prevCart.items, { ...item, quantity: 1 }],
|
|
54
|
+
total: prevCart.total + item.price
|
|
55
|
+
}})
|
|
56
|
+
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setCart((prevCart) => {return {
|
|
61
|
+
items: prevCart.items.map((i) => {
|
|
62
|
+
if (i.pId === item.pId) {
|
|
63
|
+
return { ...i, quantity: i.quantity + 1 }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return i
|
|
67
|
+
}),
|
|
68
|
+
total: prevCart.total + item.price
|
|
69
|
+
}})
|
|
70
|
+
updateCart(cart)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const deleteProductCart = (item) => {
|
|
74
|
+
setCart((prevCart) => {
|
|
75
|
+
const items = prevCart.items
|
|
76
|
+
const index = items.findIndex((i) => {return i.pId === item.pId})
|
|
77
|
+
|
|
78
|
+
items.splice(index, 1)
|
|
79
|
+
|
|
80
|
+
const total = items.reduce((t, i) => {return t + i.quantity * i.price}, 0)
|
|
81
|
+
|
|
82
|
+
return { items, total }
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const decreaseItemFromCart = (item) => {
|
|
87
|
+
// check for item already in cart
|
|
88
|
+
// if quantity is more then in cart, subtract item else remove item
|
|
89
|
+
const itemInCart = cart.items.find((i) => {return i.pId === item.pId})
|
|
90
|
+
|
|
91
|
+
if (!itemInCart) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (itemInCart.quantity === 1) {
|
|
96
|
+
deleteProductCart(item)
|
|
97
|
+
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setCart((prevCart) => {return {
|
|
102
|
+
items: prevCart.items.map((i) => {
|
|
103
|
+
if (i.pId === item.pId) {
|
|
104
|
+
return { ...i, quantity: item.quantity - 1 }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return i
|
|
108
|
+
}),
|
|
109
|
+
total: prevCart.total - item.price
|
|
110
|
+
}})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const clearCart = () => {
|
|
114
|
+
setCart(EMPTY_CART)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
saveDataState,
|
|
119
|
+
clearCart,
|
|
120
|
+
deleteProductCart,
|
|
121
|
+
decreaseItemFromCart,
|
|
122
|
+
handleAdd
|
|
123
|
+
}
|
|
124
|
+
}
|