npm-pkg-hook 1.0.0 → 1.0.2

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 (47) hide show
  1. package/.babelrc +14 -0
  2. package/.eslintrc.json +108 -0
  3. package/.github/pull_request_template.md +18 -0
  4. package/.github/workflows/pepeline.yaml +30 -0
  5. package/README.md +1 -0
  6. package/jsconfig.json +28 -0
  7. package/next.config.js +129 -0
  8. package/package.json +33 -3
  9. package/src/cookies/index.ts +3 -0
  10. package/src/hooks/index.js +19 -0
  11. package/src/hooks/useAcumulateDate/index.js +16 -0
  12. package/src/hooks/useAnimationText/index.jsx +30 -0
  13. package/src/hooks/useCategoryStore/index.js +7 -0
  14. package/src/hooks/useCategoryStore/queries.js +16 -0
  15. package/src/hooks/useCheckbox/index.js +114 -0
  16. package/src/hooks/useClients/index.js +13 -0
  17. package/src/hooks/useClients/queries.js +118 -0
  18. package/src/hooks/useDrag/index.js +57 -0
  19. package/src/hooks/useEvent/index.js +33 -0
  20. package/src/hooks/useFetchJson/index.js +25 -0
  21. package/src/hooks/useFetchMoreInteractions/index.jsx +35 -0
  22. package/src/hooks/useFormTools/index.js +71 -0
  23. package/src/hooks/useFullScreenMode/index.js +66 -0
  24. package/src/hooks/useGetCategorieStore/index.js +21 -0
  25. package/src/hooks/useGetCategorieStore/queries.js +78 -0
  26. package/src/hooks/useGetProductsFood/index.js +46 -0
  27. package/src/hooks/useGetProductsFood/queriesStore.js +766 -0
  28. package/src/hooks/useHover/index.js +29 -0
  29. package/src/hooks/useInnerHtml/index.js +38 -0
  30. package/src/hooks/useIntersection/index.js +31 -0
  31. package/src/hooks/useKeypress/index.js +28 -0
  32. package/src/hooks/useLocalSorage/index.js +36 -0
  33. package/src/hooks/useLocationNavigate/index.js +54 -0
  34. package/src/hooks/useMobile/index.js +38 -0
  35. package/src/hooks/useRestaurant/index.js +19 -0
  36. package/src/hooks/useRestaurant/queries.js +70 -0
  37. package/src/hooks/useSales/index.js +489 -0
  38. package/src/hooks/useSales/queries.js +230 -0
  39. package/src/hooks/useSetState/index.js +24 -0
  40. package/src/hooks/useStore/index.js +18 -0
  41. package/src/hooks/useStore/queries.js +136 -0
  42. package/src/hooks/useTimeAgo/useTimeAgo.js +39 -0
  43. package/src/hooks/useUpdateCart/index.js +124 -0
  44. package/src/hooks/useUser/index.js +3 -0
  45. package/src/hooks/useWindowSize/index.js +38 -0
  46. package/src/index.jsx +1 -6
  47. package/src/utils/index.js +54 -0
@@ -0,0 +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
+ }
@@ -0,0 +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 }]
18
+ }
@@ -0,0 +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
+ }
136
+ `
@@ -0,0 +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
+ }
@@ -0,0 +1,124 @@
1
+ import debounce from 'lodash.debounce'
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
+ }
@@ -0,0 +1,3 @@
1
+ export const useUser = () => {
2
+ return []
3
+ }
@@ -0,0 +1,38 @@
1
+ import { useState, useEffect } from 'react';
2
+
3
+ export default function useWindowSize() {
4
+ // Initialize state with undefined width/height so server and client renders match
5
+ // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
6
+ const [windowSize, setWindowSize] = useState({
7
+ width: undefined,
8
+ height: undefined
9
+ });
10
+
11
+ useEffect(() => {
12
+ // Handler to call on window resize
13
+ function handleResize() {
14
+ // Set window width/height to state
15
+ setWindowSize({
16
+ width: window.innerWidth,
17
+ height: window.innerHeight
18
+ });
19
+ }
20
+
21
+ // Add event listener
22
+ window.addEventListener('resize', handleResize);
23
+
24
+ // Call handler right away so state gets updated with initial window size
25
+ handleResize();
26
+
27
+ // Remove event listener on cleanup
28
+ return () => window.removeEventListener('resize', handleResize);
29
+ }, []); // Empty array ensures that effect is only run on mount
30
+
31
+ return windowSize;
32
+ }
33
+ // Como se usa
34
+
35
+ // 1 -const size = useWindowSize();
36
+ // 2 <div>
37
+ // {size.width}px / {size.height}px
38
+ // </div>
package/src/index.jsx CHANGED
@@ -1,6 +1 @@
1
-
2
- const Hello = () => 'Hola Papu'
3
-
4
- module.exports = {
5
- Hello
6
- }
1
+ export * from './hooks/index'
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @description It takes an array of elements and returns an object with a submit hook for each element.
3
+ * @version 0.1.1
4
+ * @param {array} elements elementos del formulario
5
+ * @return {array} devuelve un array de booleanos con el nombre identificador para cada estado en react.
6
+ */
7
+ export const validationSubmitHooks = elements => {
8
+ let errorForm = {}
9
+ for (const element of elements) {
10
+ if (element.name) {
11
+ if (element.type === 'text' || element.type === 'password' || element.type === 'email' || element.type === 'number' || element.type === 'hidden') {
12
+ if (element.dataset.required === 'true') {
13
+ if (!element.value) errorForm = { ...errorForm, [element.name]: !element.value }
14
+ else errorForm = { ...errorForm, [element.name]: !element.value }
15
+ } else {
16
+ errorForm = { ...errorForm, [element.name]: false }
17
+ }
18
+ }
19
+ }
20
+ }
21
+ return errorForm
22
+ }
23
+
24
+ export const getCurrentDomain = () => {
25
+ return typeof window !== 'undefined' && window.location.hostname.split('.').slice(-2).join('.')
26
+ }
27
+
28
+ export function RandomCode(length) {
29
+ let result = ''
30
+ let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
31
+ let charactersLength = characters.length
32
+ for (let i = 0; i < length; i++) {
33
+ result += characters.charAt(Math.floor(Math.random() *
34
+ charactersLength))
35
+ }
36
+ return result
37
+ }
38
+ /**
39
+ * actualizar cache de apollo
40
+ * @param {{ cache: object, query: object, nameFun: string, dataNew: object, type: number, id: string }} params Parámetros para actualizar el cachet de apollo
41
+ * @returns {null} no hay retorno
42
+ */
43
+ export const updateCacheMod = async ({ cache, query, nameFun, dataNew, type, id }) => {
44
+ return cache.modify({
45
+ fields: {
46
+ [nameFun](dataOld = []) {
47
+ if (type === 1) return cache.writeQuery({ query, data: [...(dataOld || []), { ...(dataNew || {}) }] })
48
+ if (type === 2) return cache.writeQuery({ query, data: { ...(dataOld || {}), ...(dataNew || {}) } })
49
+ if (type === 3) return cache.writeQuery({ query, data: dataOld.filter(x => { return x === id }) })
50
+ }
51
+ }
52
+ })
53
+ }
54
+ export const initializer = (initialValue = initialState) => { return JSON.parse(localStorage.getItem(process.env.LOCAL_SALES_STORE)) || initialValue }