npm-pkg-hook 1.0.0 → 1.0.1
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 +14 -0
- package/.eslintrc.json +108 -0
- package/README.md +1 -0
- package/jsconfig.json +28 -0
- package/next.config.js +129 -0
- package/package.json +26 -4
- package/src/cookies/index.ts +3 -0
- package/src/hooks/index.js +15 -0
- package/src/hooks/useAcumulateDate/index.js +16 -0
- package/src/hooks/useAnimationText/index.jsx +30 -0
- package/src/hooks/useCategoryStore/index.js +7 -0
- package/src/hooks/useCategoryStore/queries.js +16 -0
- package/src/hooks/useDrag/index.js +57 -0
- package/src/hooks/useEvent/index.js +33 -0
- package/src/hooks/useFetchJson/index.js +25 -0
- package/src/hooks/useFetchMoreInteractions/index.jsx +35 -0
- package/src/hooks/useFormTools/index.js +71 -0
- package/src/hooks/useFullScreenMode/index.js +66 -0
- package/src/hooks/useGetCategorieStore/index.js +21 -0
- package/src/hooks/useGetCategorieStore/queries.js +78 -0
- package/src/hooks/useHover/index.js +29 -0
- package/src/hooks/useInnerHtml/index.js +38 -0
- package/src/hooks/useIntersection/index.js +31 -0
- package/src/hooks/useKeypress/index.js +28 -0
- package/src/hooks/useLocalSorage/index.js +36 -0
- package/src/hooks/useLocationNavigate/index.js +54 -0
- package/src/hooks/useRestaurant/index.js +19 -0
- package/src/hooks/useRestaurant/queries.js +70 -0
- package/src/hooks/useSetState/index.js +24 -0
- package/src/hooks/useTimeAgo/useTimeAgo.js +39 -0
- package/src/hooks/useUpdateCart/index.js +123 -0
- package/src/hooks/useUser/index.js +3 -0
- package/src/hooks/useWindowSize/index.js +38 -0
- package/src/index.jsx +1 -6
- package/src/utils/index.js +27 -0
package/.babelrc
ADDED
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es2021": true
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"eslint:recommended",
|
|
8
|
+
"plugin:react/recommended",
|
|
9
|
+
"plugin:@typescript-eslint/recommended"
|
|
10
|
+
],
|
|
11
|
+
"parser": "@typescript-eslint/parser",
|
|
12
|
+
"parserOptions": {
|
|
13
|
+
"ecmaFeatures": {
|
|
14
|
+
"jsx": true
|
|
15
|
+
},
|
|
16
|
+
"ecmaVersion": 12,
|
|
17
|
+
"sourceType": "module"
|
|
18
|
+
},
|
|
19
|
+
"plugins": [
|
|
20
|
+
"react",
|
|
21
|
+
"react-hooks",
|
|
22
|
+
"@typescript-eslint"
|
|
23
|
+
],
|
|
24
|
+
"settings": {
|
|
25
|
+
"react": {
|
|
26
|
+
"version": "detect"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"rules": {
|
|
30
|
+
// react hooks validations
|
|
31
|
+
"react-hooks/rules-of-hooks": "error",
|
|
32
|
+
// suppress errors for missing "import React" in files
|
|
33
|
+
"react/react-in-jsx-scope": "off",
|
|
34
|
+
// allow jsx syntax in js files (for next.js project)
|
|
35
|
+
"react/jsx-filename-extension": [
|
|
36
|
+
1,
|
|
37
|
+
{
|
|
38
|
+
"extensions": [
|
|
39
|
+
".js",
|
|
40
|
+
".jsx",
|
|
41
|
+
".ts",
|
|
42
|
+
".tsx"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"quotes": [
|
|
47
|
+
2,
|
|
48
|
+
"single",
|
|
49
|
+
{
|
|
50
|
+
"avoidEscape": true,
|
|
51
|
+
"allowTemplateLiterals": true
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
"consistent-return": 2,
|
|
55
|
+
"indent": [
|
|
56
|
+
2,
|
|
57
|
+
2,
|
|
58
|
+
{
|
|
59
|
+
"SwitchCase": 1
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
"no-else-return": 1,
|
|
63
|
+
"semi": [
|
|
64
|
+
1,
|
|
65
|
+
"never"
|
|
66
|
+
],
|
|
67
|
+
"space-unary-ops": 2,
|
|
68
|
+
"comma-dangle": [
|
|
69
|
+
"error",
|
|
70
|
+
{
|
|
71
|
+
"arrays": "never",
|
|
72
|
+
"objects": "never",
|
|
73
|
+
"imports": "never",
|
|
74
|
+
"exports": "never",
|
|
75
|
+
"functions": "never"
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
"one-var": [
|
|
79
|
+
2,
|
|
80
|
+
"never"
|
|
81
|
+
],
|
|
82
|
+
"no-var": "error",
|
|
83
|
+
"arrow-body-style": [
|
|
84
|
+
"error",
|
|
85
|
+
"always"
|
|
86
|
+
],
|
|
87
|
+
"no-console": "error",
|
|
88
|
+
"no-multi-spaces": [
|
|
89
|
+
"error",
|
|
90
|
+
{
|
|
91
|
+
"exceptions": {
|
|
92
|
+
"BinaryExpression": true
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
"no-multiple-empty-lines": [
|
|
97
|
+
"error",
|
|
98
|
+
{
|
|
99
|
+
"max": 1,
|
|
100
|
+
"maxBOF": 1
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
"jsx-quotes": [
|
|
104
|
+
"error",
|
|
105
|
+
"prefer-single"
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# pkg-hook
|
package/jsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"lib": [
|
|
5
|
+
"dom",
|
|
6
|
+
"dom.iterable"
|
|
7
|
+
],
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"strict": false,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"module": "commonjs",
|
|
15
|
+
"moduleResolution": "node",
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"isolatedModules": true,
|
|
18
|
+
"jsx": "preserve",
|
|
19
|
+
"incremental": true
|
|
20
|
+
},
|
|
21
|
+
"include": [
|
|
22
|
+
"**/*.ts",
|
|
23
|
+
"**/*.tsx"
|
|
24
|
+
],
|
|
25
|
+
"exclude": [
|
|
26
|
+
"node_modules"
|
|
27
|
+
]
|
|
28
|
+
}
|
package/next.config.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/* eslint-disable consistent-return */
|
|
2
|
+
/** @type {import('next').NextConfig} */
|
|
3
|
+
const {
|
|
4
|
+
PHASE_DEVELOPMENT_SERVER,
|
|
5
|
+
PHASE_PRODUCTION_BUILD
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
7
|
+
} = require('next/constants')
|
|
8
|
+
const nextConfig = {
|
|
9
|
+
reactStrictMode: true
|
|
10
|
+
}
|
|
11
|
+
module.exports = (phase) => {
|
|
12
|
+
|
|
13
|
+
// npm run dev or next dev
|
|
14
|
+
const isDev = phase === PHASE_DEVELOPMENT_SERVER
|
|
15
|
+
// npm run build or next build
|
|
16
|
+
const isProd = phase === PHASE_PRODUCTION_BUILD && process.env.STAGING !== '1'
|
|
17
|
+
// npm run build or next build
|
|
18
|
+
const isStaging = phase === PHASE_PRODUCTION_BUILD && process.env.STAGING === '1'
|
|
19
|
+
const env = {
|
|
20
|
+
NAMEDB: (() => {
|
|
21
|
+
if (isDev) return 'app'
|
|
22
|
+
if (isProd) return 'railway'
|
|
23
|
+
})(),
|
|
24
|
+
USERDB: (() => {
|
|
25
|
+
if (isDev) return 'root'
|
|
26
|
+
if (isProd) return 'root'
|
|
27
|
+
})(),
|
|
28
|
+
PASSDB: (() => {
|
|
29
|
+
if (isDev) return ''
|
|
30
|
+
if (isProd) return '8jpgm70bhVjWIxEFbsrt'
|
|
31
|
+
})(),
|
|
32
|
+
HOSTDB: (() => {
|
|
33
|
+
if (isDev) return 'localhost'
|
|
34
|
+
if (isProd) return 'containers-us-west-65.railway.app'
|
|
35
|
+
})(),
|
|
36
|
+
DIALECTDB: 'mysql',
|
|
37
|
+
SESSION_NAME: 'vp.client',
|
|
38
|
+
SESSION_KEY: '12ba105efUaGjihGrh0LfJHTGIBGu6jXa',
|
|
39
|
+
URL_BASE: (() => {
|
|
40
|
+
if (isDev) return 'http://localhost:3001/app/'
|
|
41
|
+
if (isProd) return 'http://localhost:3001/app/'
|
|
42
|
+
// if (isStaging) return 'Title Stg'
|
|
43
|
+
})(),
|
|
44
|
+
MAIN_URL_BASE: (() => {
|
|
45
|
+
if (isDev) return 'http://localhost:3000/'
|
|
46
|
+
if (isProd) return 'http://localhost:3000/'
|
|
47
|
+
if (isStaging) return 'Title Stg'
|
|
48
|
+
})(),
|
|
49
|
+
// URL_BASE_WS
|
|
50
|
+
URL_ADMIN_SERVER: (() => {
|
|
51
|
+
if (isDev) return 'http://localhost:4000/'
|
|
52
|
+
// if (isDev) return 'https://server-image-food.herokuapp.com/'
|
|
53
|
+
if (isProd) return 'http://localhost:4000/'
|
|
54
|
+
})(),
|
|
55
|
+
// BANCOLOMBIA
|
|
56
|
+
BANCOLOMBIA_CLIENT_KEY: '55929729-85fe-4ffe-928d-0bd317817be4',
|
|
57
|
+
BANCOLOMBIA_SECRET_KEY: 'E1aM2bV2lD7vS8cH1qJ8oN0nD7eN0eP0rM8gU0cG2lL6uY5sO7',
|
|
58
|
+
JWT_EXPIRY: 333300,
|
|
59
|
+
REFRESH_TOKEN_EXPIRY: '604800',
|
|
60
|
+
AUTHO_USER_KEY: '12ba105efUaGjihGrh0LfJHTGIBGu6jXV',
|
|
61
|
+
ACCESS_SID_TWILIO: 'AC7c9ccbdb50400c504faf629e35aea8e4',
|
|
62
|
+
REACT_APP_API_KEY_GOOGLE_MAPS: 'AIzaSyAy0SY1G3OFqesWSTQRHJvzyJzNgURPoN8',
|
|
63
|
+
ACCESS_TOKEN_AUTH_TWILIO: '22e090d4d776ace7bb596ca77cba6b18'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const resolveUniqueReactForHooks = {
|
|
67
|
+
webpack: (config, options) => {
|
|
68
|
+
if (options.isServer) {
|
|
69
|
+
config.externals = ['react', ...config.externals]
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line no-undef
|
|
72
|
+
config.resolve.alias.react = reactPath
|
|
73
|
+
return config
|
|
74
|
+
},
|
|
75
|
+
images: {
|
|
76
|
+
domains: ['http2.mlstatic.com', 'localhost', 'server-image-food.herokuapp.com']
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const headers = () => {
|
|
80
|
+
return [
|
|
81
|
+
{
|
|
82
|
+
source: '/app',
|
|
83
|
+
headers: [
|
|
84
|
+
{
|
|
85
|
+
key: 'x-custom-header-1',
|
|
86
|
+
value: 'my custom header 1'
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
const redirects = () => {
|
|
93
|
+
return [
|
|
94
|
+
{
|
|
95
|
+
source: '/home',
|
|
96
|
+
destination: '/',
|
|
97
|
+
permanent: true
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
const basePath = '/app'
|
|
102
|
+
// puedes sobre escribir la ruta
|
|
103
|
+
const rewrites = () => {
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
source: '/ab',
|
|
107
|
+
destination: '/about'
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
const assetPrefix = isProd ? 'https://cdn.mydomain.com' : ''
|
|
112
|
+
const images = {
|
|
113
|
+
domains: ['http2.mlstatic.com', 'localhost', 'server-image-food.herokuapp.com', '*']
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
env,
|
|
117
|
+
swcMinify: false,
|
|
118
|
+
images,
|
|
119
|
+
headers,
|
|
120
|
+
optimizeFonts: false,
|
|
121
|
+
nextConfig,
|
|
122
|
+
// basePath,
|
|
123
|
+
rewrites,
|
|
124
|
+
redirects,
|
|
125
|
+
assetPrefix,
|
|
126
|
+
resolveUniqueReactForHooks
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// module.exports = nextConfig
|
package/package.json
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "npm-pkg-hook",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "description-pkg-hook",
|
|
5
5
|
"main": "/src/index.jsx",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"rm": "rm -rf node_modules package-lock.json && npm i"
|
|
8
9
|
},
|
|
9
10
|
"author": "",
|
|
10
|
-
"license": "ISC"
|
|
11
|
-
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@apollo/client": "3.6.9",
|
|
14
|
+
"@typescript-eslint/parser": "^5.37.0",
|
|
15
|
+
"js-cookie": "3.0.1",
|
|
16
|
+
"react": "18.1.0",
|
|
17
|
+
"react-dom": "18.1.0",
|
|
18
|
+
"react-query": "^3.39.2"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@babel/preset-env": "^7.19.1",
|
|
22
|
+
"@babel/preset-react": "^7.18.6",
|
|
23
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
24
|
+
"@types/react": "^18.0.20",
|
|
25
|
+
"eslint": "^8.23.1",
|
|
26
|
+
"eslint-config-standard": "^17.0.0",
|
|
27
|
+
"eslint-plugin-import": "^2.26.0",
|
|
28
|
+
"eslint-plugin-n": "^15.2.5",
|
|
29
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
30
|
+
"eslint-plugin-react": "^7.31.8",
|
|
31
|
+
"eslint-plugin-react-hooks": "^4.6.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './useLocationNavigate'
|
|
2
|
+
export * from './useUser'
|
|
3
|
+
export * from './useTimeAgo/useTimeAgo'
|
|
4
|
+
export * from './useEvent'
|
|
5
|
+
export * from './useFetchJson'
|
|
6
|
+
export * from './useRestaurant'
|
|
7
|
+
export * from './useFormTools/index'
|
|
8
|
+
export * from './useDrag'
|
|
9
|
+
export * from './useHover'
|
|
10
|
+
export * from './useKeypress'
|
|
11
|
+
export * from './useLocalSorage'
|
|
12
|
+
export * from './useUpdateCart'
|
|
13
|
+
export * from './useSetState'
|
|
14
|
+
export * from './useGetCategorieStore'
|
|
15
|
+
export * from './useCategoryStore'
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
const data = [
|
|
3
|
+
{ x: '2021-10-17T14:38:45.540Z', y: 2 }
|
|
4
|
+
]
|
|
5
|
+
export const get_date_parts = (iso_string) => {
|
|
6
|
+
const [year, month, day, hr, min, sec] = iso_string.split(/\D/g)
|
|
7
|
+
return { year, month, day, hr, min, sec }
|
|
8
|
+
}
|
|
9
|
+
function group_by_year(arr) {
|
|
10
|
+
let total = arr.reduce((a, b) => {
|
|
11
|
+
const { day } = get_date_parts(b.x)
|
|
12
|
+
return a + parseInt(day)
|
|
13
|
+
}, 0)
|
|
14
|
+
return total
|
|
15
|
+
}
|
|
16
|
+
const result = group_by_year(data)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React, { useEffect, useState, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
const fullText =
|
|
4
|
+
' this is full text it\'ll be animated again! Writing a really huge senetence here so that I can see the animation happen. I know it\'s fast but that\'s how it goes.'
|
|
5
|
+
|
|
6
|
+
const useAnimatedText = textMessage => {
|
|
7
|
+
const fullTextRef = useRef(textMessage)
|
|
8
|
+
const [text, setText] = useState('')
|
|
9
|
+
const [index, setIndex] = useState(0)
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (index < fullText.length) {
|
|
12
|
+
window.requestAnimationFrame(() => {
|
|
13
|
+
// eslint-disable-next-line
|
|
14
|
+
setText(text => text + fullTextRef.current[index]);
|
|
15
|
+
setIndex(() => {return index + 1})
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
}, [index])
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
fullText.current = textMessage
|
|
21
|
+
}, [textMessage])
|
|
22
|
+
|
|
23
|
+
return text
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default function TextHook() {
|
|
27
|
+
const text = useAnimatedText(fullText)
|
|
28
|
+
|
|
29
|
+
return <span>{text} </span>
|
|
30
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
export function useDrag2(ref) {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
const target = ref.current
|
|
6
|
+
if (!target) return
|
|
7
|
+
const previousOffset = { x: 0, y: 0 }
|
|
8
|
+
let originMouseX
|
|
9
|
+
let originMouseY
|
|
10
|
+
function onMousemove(e) {
|
|
11
|
+
const { pageX, pageY } = e
|
|
12
|
+
const x = pageX - originMouseX + previousOffset.x
|
|
13
|
+
const y = pageY - originMouseY + previousOffset.y
|
|
14
|
+
target.style.transform = `translate(${ x }px, ${ y }px)`
|
|
15
|
+
}
|
|
16
|
+
function onMouseup(e) {
|
|
17
|
+
previousOffset.x += e.pageX - originMouseX
|
|
18
|
+
previousOffset.y += e.pageY - originMouseY
|
|
19
|
+
window.removeEventListener('mousemove', onMousemove)
|
|
20
|
+
window.removeEventListener('mouseup', onMouseup)
|
|
21
|
+
}
|
|
22
|
+
function onMousedown(e) {
|
|
23
|
+
originMouseX = e.pageX
|
|
24
|
+
originMouseY = e.pageY
|
|
25
|
+
window.addEventListener('mousemove', onMousemove)
|
|
26
|
+
window.addEventListener('mouseup', onMouseup)
|
|
27
|
+
}
|
|
28
|
+
target.addEventListener('mousedown', onMousedown)
|
|
29
|
+
return () => {
|
|
30
|
+
target?.removeEventListener('mousedown', onMousedown)
|
|
31
|
+
window?.removeEventListener('mouseup', onMouseup)
|
|
32
|
+
window?.removeEventListener('mousemove', onMousemove)
|
|
33
|
+
}
|
|
34
|
+
}, [ref])
|
|
35
|
+
}
|
|
36
|
+
import { useState } from 'react'
|
|
37
|
+
|
|
38
|
+
export const useDrag = (x, y) => {
|
|
39
|
+
const [move, setMove] = useState(false)
|
|
40
|
+
const [moveTo, setMoveTo] = useState([x, y])
|
|
41
|
+
const moveIcon = e => {
|
|
42
|
+
const xcoord = moveTo[0] + e.movementX
|
|
43
|
+
const ycoord = moveTo[1] + e.movementY
|
|
44
|
+
setMoveTo([xcoord, ycoord])
|
|
45
|
+
}
|
|
46
|
+
const handleMove = e => {
|
|
47
|
+
move && moveIcon(e)
|
|
48
|
+
}
|
|
49
|
+
const handelDown = () => {
|
|
50
|
+
setMove(true)
|
|
51
|
+
}
|
|
52
|
+
const handelUp = () => {
|
|
53
|
+
setMove(false)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { move, moveTo, handelDown, handelUp, handleMove }
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
export const on = ({ eventType, callBack }) => {
|
|
3
|
+
document.addEventListener(eventType, callBack);
|
|
4
|
+
};
|
|
5
|
+
export const off = ({ eventType, callBack }) => {
|
|
6
|
+
document.removeEventListener(eventType, callBack);
|
|
7
|
+
};
|
|
8
|
+
export const once = ({ eventType, callBack }) => {
|
|
9
|
+
const handleEventOnce = (event) => {
|
|
10
|
+
callBack(event);
|
|
11
|
+
off({ eventType, callBack: handleEventOnce });
|
|
12
|
+
};
|
|
13
|
+
on({ eventType, callBack: handleEventOnce });
|
|
14
|
+
};
|
|
15
|
+
export const trigger = ({ eventType, data }) => {
|
|
16
|
+
const event = new CustomEvent(eventType, { detail: data });
|
|
17
|
+
document.dispatchEvent(event);
|
|
18
|
+
};
|
|
19
|
+
//This function is used to subscribe components an any event
|
|
20
|
+
export const useEvents = ({ eventType, callBack }) => {
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
on({ eventType, callBack: callBack });
|
|
23
|
+
return () => {
|
|
24
|
+
off({ eventType, callBack: callBack });
|
|
25
|
+
};
|
|
26
|
+
}, [eventType, callBack]);
|
|
27
|
+
};
|
|
28
|
+
// This function create and dispatch event
|
|
29
|
+
export const useTrigerEvent = () => {
|
|
30
|
+
return {
|
|
31
|
+
trigger
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* It fetches a json response, and if the response is not ok, it throws an error with the response and
|
|
3
|
+
* the data
|
|
4
|
+
* @param args - The arguments to pass to fetch.
|
|
5
|
+
*/
|
|
6
|
+
export const fetchJson = async (...args) => {
|
|
7
|
+
try {
|
|
8
|
+
const response = await fetch(...args)
|
|
9
|
+
// if the server replies, there's always some data in json
|
|
10
|
+
// if there's a network error, it will throw at the previous line
|
|
11
|
+
const data = await response.json()
|
|
12
|
+
if (response.ok) {
|
|
13
|
+
return data
|
|
14
|
+
}
|
|
15
|
+
const error = new Error(response.statusText)
|
|
16
|
+
error.response = response
|
|
17
|
+
error.data = data
|
|
18
|
+
throw error
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (!error.data) {
|
|
21
|
+
error.data = { message: error.message }
|
|
22
|
+
}
|
|
23
|
+
throw error
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef
|
|
5
|
+
} from 'react'
|
|
6
|
+
|
|
7
|
+
export const useFetchMoreInteractions = ({ render, fetchMore = true, callback = () => {} }) => {
|
|
8
|
+
const loadingRef = useRef()
|
|
9
|
+
|
|
10
|
+
const useOnScreen = ref => {
|
|
11
|
+
const [isIntersecting, setIsIntersecting] = useState(false)
|
|
12
|
+
|
|
13
|
+
const observer = new IntersectionObserver(
|
|
14
|
+
([entry]) => setIsIntersecting(entry.isIntersecting)
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (ref.current) observer.observe(ref.current)
|
|
19
|
+
// Remove the observer as soon as the component is unmounted
|
|
20
|
+
return () => { observer.disconnect() }
|
|
21
|
+
}, [ref, observer])
|
|
22
|
+
|
|
23
|
+
return isIntersecting
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isVisible = useOnScreen(loadingRef)
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (isVisible && fetchMore) callback()
|
|
30
|
+
}, [isVisible, callback, fetchMore])
|
|
31
|
+
|
|
32
|
+
return <div ref={loadingRef}>
|
|
33
|
+
{isVisible && fetchMore ? render || <div style={{ background: 'red' }} >Loading...{isVisible && 'lol'}</div> : <></>}
|
|
34
|
+
</div>
|
|
35
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useState
|
|
5
|
+
} from 'react'
|
|
6
|
+
import { validationSubmitHooks } from '../../utils'
|
|
7
|
+
/**
|
|
8
|
+
* @version 0.0.1
|
|
9
|
+
* @description Hook con herramientas de validación y eventos de cambio
|
|
10
|
+
* @return {Array} devuelve la función onChange a ejecutar y el estado de error de cada input
|
|
11
|
+
*/
|
|
12
|
+
export const useFormTools = () => {
|
|
13
|
+
const [dataForm, setDataForm] = useState({})
|
|
14
|
+
const [errorForm, setErrorForm] = useState({})
|
|
15
|
+
const [errorSubmit, setErrorSubmit] = useState(false)
|
|
16
|
+
const [calledSubmit, setCalledSubmit] = useState(false)
|
|
17
|
+
|
|
18
|
+
// Handle Change
|
|
19
|
+
const handleChange = useCallback((e, error) => {
|
|
20
|
+
setDataForm({ ...dataForm, [e.target.name]: e.target.value })
|
|
21
|
+
setErrorForm({ ...errorForm, [e.target.name]: error })
|
|
22
|
+
}, [setDataForm, dataForm, errorForm, setErrorForm])
|
|
23
|
+
|
|
24
|
+
// Forzar datos desde una ventana externa
|
|
25
|
+
const handleForcedData = useCallback(data => {
|
|
26
|
+
setDataForm(data)
|
|
27
|
+
}, [setDataForm])
|
|
28
|
+
|
|
29
|
+
// Forzar datos de error desde una ventana externa
|
|
30
|
+
const setForcedError = useCallback(errors => {
|
|
31
|
+
setErrorForm(errors)
|
|
32
|
+
}, [setErrorForm])
|
|
33
|
+
|
|
34
|
+
// Handle submit, al enviar formulario
|
|
35
|
+
const handleSubmit = useCallback(({ event, action, msgSuccess, msgError, actionAfterSuccess }) => {
|
|
36
|
+
!!event && event.preventDefault()
|
|
37
|
+
setCalledSubmit(true)
|
|
38
|
+
let errSub = false
|
|
39
|
+
|
|
40
|
+
// Valida los errores locales
|
|
41
|
+
for (const x in errorForm) {
|
|
42
|
+
if (errorForm[x]) errSub = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// if (errSub) return setErrorSubmit(errSub)
|
|
46
|
+
|
|
47
|
+
// Valida los errores desde el evento
|
|
48
|
+
const errores = validationSubmitHooks(event.target.elements)
|
|
49
|
+
setErrorForm(errores)
|
|
50
|
+
for (const x in errores) {
|
|
51
|
+
if (errores[x]) errSub = true
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Ejecuta la petición si es válido
|
|
55
|
+
if (!errSub && action) {
|
|
56
|
+
action().then(res => {
|
|
57
|
+
if (res) {
|
|
58
|
+
// setAlertBox({ message: msgSuccess || 'Operación exitosa', color: PColor })
|
|
59
|
+
!!actionAfterSuccess && actionAfterSuccess()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}).catch(e => {return console.log({ message: msgError || e?.message || 'Ha ocurrido un error', color: WColor })})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setErrorSubmit(errSub)
|
|
66
|
+
}, [errorForm, setErrorForm])
|
|
67
|
+
|
|
68
|
+
useEffect(() => {return setCalledSubmit(false)}, [calledSubmit])
|
|
69
|
+
|
|
70
|
+
return [handleChange, handleSubmit, handleForcedData, { dataForm, errorForm, errorSubmit, calledSubmit, setForcedError }]
|
|
71
|
+
}
|