npm-pkg-hook 1.0.2 → 1.0.5

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 (85) 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 +47 -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 +170 -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/useReactToPrint/index.js +1201 -0
  58. package/src/hooks/useReport/index.js +35 -0
  59. package/src/hooks/useReport/queries.js +122 -0
  60. package/src/hooks/useRestaurant/index.js +19 -19
  61. package/src/hooks/useRestaurant/queries.js +79 -69
  62. package/src/hooks/useSales/index.js +589 -489
  63. package/src/hooks/useSales/queries.js +290 -229
  64. package/src/hooks/useSales/useGetSale.js +12 -0
  65. package/src/hooks/useSales/useTotalSales.js +17 -0
  66. package/src/hooks/useSaveAvailableProduct/helpers/index.js +30 -0
  67. package/src/hooks/useSaveAvailableProduct/index.js +26 -0
  68. package/src/hooks/useSaveAvailableProduct/queries.js +10 -0
  69. package/src/hooks/useSchedule/index.jsx +23 -0
  70. package/src/hooks/useSetState/index.js +24 -24
  71. package/src/hooks/useStore/index.js +17 -17
  72. package/src/hooks/useStore/queries.js +135 -135
  73. package/src/hooks/useStoreCalendar/index.js +7 -0
  74. package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
  75. package/src/hooks/useUpdateCart/index.js +124 -124
  76. package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +37 -0
  77. package/src/hooks/useUser/index.js +7 -3
  78. package/src/hooks/useUser/queries.js +69 -0
  79. package/src/hooks/useWindowSize/index.js +37 -37
  80. package/src/index.jsx +2 -1
  81. package/src/mock/dessert/index.js +16 -0
  82. package/src/mock/index.js +2 -0
  83. package/src/utils/index.js +106 -54
  84. package/src/hooks/useGetCategorieStore/index.js +0 -21
  85. package/src/hooks/useGetProductsFood/index.js +0 -46
@@ -0,0 +1,268 @@
1
+ import { useMutation } from '@apollo/client'
2
+ import { useRef, useState } from 'react'
3
+ import { convertBase64, RandomCode } from '../../utils'
4
+ import useLocalStorage from '../useLocalSorage'
5
+ import {
6
+ GET_ALL_FOOD_PRODUCTS,
7
+ UPDATE_IMAGE_PRODUCT_FOOD,
8
+ UPDATE_PRODUCT_FOOD
9
+ } from '../useProductsFood/queriesStore'
10
+ import { useStore } from '../useStore'
11
+ import { useTagsProducts } from './../useProductsFood/usetagsProducts'
12
+ import { useEditImageProduct } from './helpers/useEditImageProduct'
13
+
14
+ export const useCreateProduct = ({
15
+ setAlertBox = () => { },
16
+ router,
17
+ sendNotification = () => { }
18
+ }) => {
19
+ const [errors, setErrors] = useState({
20
+ names: ''
21
+ })
22
+ const [values, setValues] = useState({})
23
+ const [names, setName] = useLocalStorage('namefood', '')
24
+ const [showMore, setShowMore] = useState(50)
25
+ const [search, setSearch] = useState('')
26
+ const [imageBase64, setImageBase64] = useState(null)
27
+ const [active, setActive] = useState(0)
28
+ const [pId, setPid] = useState(null)
29
+
30
+ const [searchFilter, setSearchFilter] = useState({ gender: [], desc: [], speciality: [] })
31
+ const [filter, setFilter] = useState({ gender: [], desc: [], speciality: [] })
32
+ const initialState = { alt: '/ images/DEFAULTBANNER.png', src: '/images/DEFAULTBANNER.png' }
33
+ const [image, setImage] = useState(null)
34
+ const [{ alt, src }, setPreviewImg] = useState(initialState)
35
+ const fileInputRef = useRef(null)
36
+ const [arrTags, setTags] = useState([])
37
+
38
+ const [dataStore] = useStore()
39
+ const {
40
+ handleRegister: handleRegisterTags,
41
+ data,
42
+ handleAddTag,
43
+ tags
44
+ } = useTagsProducts()
45
+
46
+ // HANDLESS
47
+ const [check, setCheck] = useState({
48
+ availability: true,
49
+ noAvailability: false,
50
+ })
51
+
52
+ const handleCheck = (e) => {
53
+ const { name, checked } = e.target
54
+ return setCheck({ ...check, [name]: checked ? true : false })
55
+ }
56
+
57
+ const handleUpdateBanner = event => {
58
+ const { files } = event.target
59
+ setPreviewImg(
60
+ files.length
61
+ ? {
62
+ src: URL.createObjectURL(files[0]),
63
+ alt: files[0].name
64
+ }
65
+ : initialState
66
+ )
67
+ }
68
+ const handleChange = (e, error) => {
69
+ setValues({ ...values, [e.target.name]: e.target.value })
70
+ setErrors({ ...errors, [e.target.name]: error })
71
+ }
72
+ const handleChangeFilter = e => {
73
+ setSearch(e.target.value)
74
+ }
75
+ const onClickSearch = () => {
76
+ setSearchFilter({ ...filter })
77
+ }
78
+ const onClickClear = () => {
79
+ setSearchFilter({})
80
+ }
81
+ const handleChangeClick = e => {
82
+ const {
83
+ name,
84
+ value,
85
+ checked
86
+ } = e.target
87
+ !checked
88
+ ? setFilter(s => { return { ...s, [name]: s[name].filter(f => { return f !== value }) } })
89
+ : setFilter({ ...filter, [name]: [...filter[name], value] })
90
+ setSearchFilter({ ...filter })
91
+ }
92
+ const handleCheckFreeShipping = e => {
93
+ // setCheck(e.target.checked)
94
+ handleCheck(e)
95
+ setValues({
96
+ ...values,
97
+ ValueDelivery: '',
98
+ })
99
+ }
100
+
101
+ const [updateProductFoods] = useMutation(UPDATE_PRODUCT_FOOD, {
102
+ })
103
+ const [setImageProducts] = useMutation(UPDATE_IMAGE_PRODUCT_FOOD, {
104
+ context: { clientName: 'admin-server' }
105
+ })
106
+
107
+ const onFileInputChange = async event => {
108
+ const { files } = event.target
109
+
110
+ const file = event.target.files[0]
111
+ setImage(file)
112
+ const base64 = await convertBase64(file)
113
+ // eslint-disable-next-line
114
+ // const [size, { unit }] = await getFileSizeByUnit(file, "B");
115
+ setImageBase64(base64)
116
+ setPreviewImg(
117
+ files.length
118
+ ? {
119
+ src: URL.createObjectURL(files[0]),
120
+ alt: files[0].name
121
+ }
122
+ : initialState
123
+ )
124
+ }
125
+ const onTargetClick = () => {
126
+ fileInputRef.current.click()
127
+ }
128
+ const { img } = useEditImageProduct({ sendNotification, initialState })
129
+ const handleRegister = async () => {
130
+ const {
131
+ ProPrice,
132
+ ProDescuento,
133
+ ProDescription,
134
+ ProWeight,
135
+ ProHeight,
136
+ ValueDelivery,
137
+ carProId
138
+ } = values
139
+ if (!carProId && !names) return setErrors({ ...errors, carProId: true })
140
+
141
+ if (!ProPrice?.length > 0) {
142
+ return setErrors({ ...errors, ProPrice: true })
143
+ }
144
+ const ProImage = `${process.env.URL_ADMIN_SERVER}static/platos/${image?.name}`
145
+ const pCode = RandomCode(9)
146
+ const formatPrice = ProPrice ? parseFloat(ProPrice.replace(/\./g, '')) : 0
147
+ try {
148
+ updateProductFoods({
149
+ variables: {
150
+ input: {
151
+ idStore: dataStore?.getStore?.idStore || '',
152
+ ProPrice: check ? 0 : formatPrice,
153
+ ProDescuento: check ? 0 : parseInt(ProDescuento),
154
+ ValueDelivery: check ? 0 : parseFloat(ValueDelivery),
155
+ ProDescription,
156
+ pName: names,
157
+ pCode,
158
+ carProId,
159
+ pState: 1,
160
+ sTateLogistic: 1,
161
+ ProStar: 0,
162
+ ProImage,
163
+ ProHeight: parseFloat(ProHeight),
164
+ ProWeight,
165
+ ProOutstanding: check ? 1 : 0,
166
+ ProDelivery: check ? 1 : 0
167
+ }
168
+ },
169
+ update (cache) {
170
+ cache.modify({
171
+ fields: {
172
+ productFoodsAll (dataOld = []) {
173
+ return cache.writeQuery({ query: GET_ALL_FOOD_PRODUCTS, data: dataOld })
174
+ }
175
+ }
176
+ })
177
+ }
178
+ }).then((res) => {
179
+ const { updateProductFoods } = res?.data || {}
180
+ const { pId } = updateProductFoods || {}
181
+ setPid(pId ?? null)
182
+ router.push(
183
+ {
184
+ query: {
185
+ ...router.query,
186
+ food: pId
187
+ }
188
+ },
189
+ undefined,
190
+ { shallow: true }
191
+ )
192
+ sendNotification({ title: 'Success', description: `El producto ${names} subido con éxito` })
193
+ const objTag = {
194
+ aName: tags.tag,
195
+ pId
196
+ }
197
+ handleRegisterTags(objTag)
198
+ // setValues({})
199
+ }).catch(err => { return sendNotification({ title: `${err}`, description: 'Error inesperado' }) })
200
+ if (image !== null) {
201
+ setImageProducts({
202
+ variables: {
203
+ input: {
204
+ file: image,
205
+ pCode
206
+ }
207
+ }
208
+ }).then((response) => {
209
+ }).catch((error) => {
210
+ sendNotification({ title: `Ocurrió un error en la imagen en el producto ${names}`, description: 'error' })
211
+ })
212
+ }
213
+ } catch (error) {
214
+ setAlertBox({ message: `${error.message}`, duration: 7000 })
215
+ }
216
+ }
217
+
218
+ // Manage tags
219
+ const changeHandler = (name, value) => {
220
+ if (name === 'tags') {
221
+ setTags(value)
222
+ if (value.length > 0 && errors.tags) {
223
+ setErrors(prev => {
224
+ const prevErrors = { ...prev }
225
+ delete prevErrors.tags
226
+ return prevErrors
227
+ })
228
+ }
229
+ }
230
+ }
231
+ return {
232
+ errors,
233
+ src,
234
+ names,
235
+ setName,
236
+ alt,
237
+ showMore,
238
+ success: true,
239
+ fileInputRef,
240
+ values,
241
+ search,
242
+ pId,
243
+ dataTags: data,
244
+ tags,
245
+ tags,
246
+ active,
247
+ idStore: dataStore?.getStore?.idStore || '',
248
+ arrTags,
249
+ setPid,
250
+ handleChange,
251
+ onClickClear,
252
+ handleChangeClick,
253
+ handleChangeFilter,
254
+ handleUpdateBanner,
255
+ onClickSearch,
256
+ changeHandler,
257
+ setErrors,
258
+ handleRegister,
259
+ setActive,
260
+ onFileInputChange,
261
+ handleRegisterTags,
262
+ setShowMore,
263
+ onTargetClick,
264
+ handleAddTag,
265
+ handleCheckFreeShipping,
266
+ check
267
+ }
268
+ }
@@ -0,0 +1,141 @@
1
+ import { useState } from 'react'
2
+ import { MockData } from '../../mock'
3
+ import { RandomCode } from '../../utils'
4
+ import { useUpdateExtProductFoodsOptional } from '../updateExtProductFoodsOptional'
5
+ import { useUpdateExtProductFoodsSubOptional } from '../useUpdateExtProductFoodsSubOptional'
6
+
7
+ export const useDessert = ({ pId }) => {
8
+ // STATES
9
+ const [setCheck, setChecker] = useState({})
10
+ const [valueItems, setValueItems] = useState('')
11
+ const [title, setTitle] = useState('')
12
+ const [data, setData] = useState(MockData)
13
+ const dataListIds = data?.listIds?.filter(x => { return x !== '01list' })
14
+ // HOOKS
15
+ const { handleMutateExtProductFoodsSubOptional } = useUpdateExtProductFoodsSubOptional()
16
+ const { handleUpdateExtProduct } = useUpdateExtProductFoodsOptional()
17
+
18
+ // HANDLESS
19
+ const handleCheck = (e) => {
20
+ const { name, checked } = e.target
21
+ setChecker({ ...setCheck, [name]: checked ? true : false })
22
+ }
23
+ const handleRemoveList = i => {
24
+ const Lines = data?.listIds?.filter((_, index) => { return index !== i })
25
+ setData({
26
+ listIds: [...Lines],
27
+ lists: {
28
+ ...data.lists
29
+ }
30
+ })
31
+ }
32
+ const removeOneItem = ({ listID, id }) => {
33
+ const currentList = data.lists[listID]
34
+ const totalCart = currentList?.cards?.filter((cart) => { return cart.id !== id })
35
+ setData({
36
+ listIds: [...data.listIds],
37
+ lists: {
38
+ ...data.lists,
39
+ [listID]: {
40
+ ...currentList,
41
+ cards: totalCart
42
+ }
43
+ }
44
+ })
45
+ }
46
+ const addCard = async (title, listId) => {
47
+ const id = await RandomCode(9)
48
+ const newCard = {
49
+ id: id,
50
+ title: title,
51
+ numberLimit: 5,
52
+ value: '',
53
+ required: setCheck.exState ? 1 : 0
54
+ }
55
+ const list = data.lists[listId]
56
+ list.cards = [...list.cards, newCard]
57
+ setData({
58
+ ...data,
59
+ lists: {
60
+ ...data.lists,
61
+ [listId]: list
62
+ }
63
+ })
64
+ handleMutateExtProductFoodsSubOptional({
65
+ pId,
66
+ title,
67
+ listId,
68
+ id,
69
+ state: 1
70
+ })
71
+ setTitle('')
72
+ }
73
+ const handleAdd = ({ listId }) => {
74
+ if (valueItems !== '') {
75
+ addCard(valueItems, listId)
76
+ }
77
+ }
78
+
79
+
80
+ const handleAddList = async ({ title, numberLimit }) => {
81
+ if (title !== '') {
82
+ const newListId = await RandomCode(9)
83
+ setData({
84
+ listIds: [...data.listIds, newListId],
85
+ lists: {
86
+ ...data.lists,
87
+ [newListId]: {
88
+ id: newListId,
89
+ title: title,
90
+ required: setCheck.exState ? 1 : 0,
91
+ numberLimit: numberLimit,
92
+ value: '',
93
+ cards: []
94
+ }
95
+ }
96
+ })
97
+ handleUpdateExtProduct({
98
+ pId,
99
+ code: newListId,
100
+ OptionalProName: title,
101
+ required: setCheck.exState ? 1 : 0,
102
+ numbersOptionalOnly: numberLimit
103
+ })
104
+ setTitle('')
105
+ }
106
+ }
107
+ const handleChangeItems = ({ listID, value: e }) => {
108
+ const value = e.target.value
109
+ setValueItems(value)
110
+ const currentList = data.lists[listID]
111
+ return dataListIds.map((salesLine) => {
112
+ if (salesLine === listID) {
113
+ setData({
114
+ listIds: [...data.listIds],
115
+ lists: {
116
+ ...data.lists,
117
+ [listID]: {
118
+ ...currentList,
119
+ value: value,
120
+ }
121
+ }
122
+ })
123
+ }
124
+ })
125
+ }
126
+ return {
127
+ setCheck,
128
+ data,
129
+ dataListIds,
130
+ title,
131
+ handleChangeItems,
132
+ handleCheck,
133
+ addCard,
134
+ setTitle,
135
+ handleAddList,
136
+ handleAdd,
137
+ removeOneItem,
138
+ handleRemoveList,
139
+ setData,
140
+ }
141
+ }
@@ -1,57 +1,62 @@
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
- }
1
+ import { useEffect, useState } 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
+
37
+ export const useDrag = (x, y) => {
38
+ const [move, setMove] = useState(false)
39
+ const [moveTo, setMoveTo] = useState([x, y])
40
+ const moveIcon = e => {
41
+ const xcoord = moveTo[0] + e.movementX
42
+ const ycoord = moveTo[1] + e.movementY
43
+ setMoveTo([xcoord, ycoord])
44
+ }
45
+ const handleMove = e => {
46
+ move && moveIcon(e)
47
+ }
48
+ const handelDown = () => {
49
+ setMove(true)
50
+ }
51
+ const handelUp = () => {
52
+ setMove(false)
53
+ }
54
+
55
+ return {
56
+ move,
57
+ moveTo,
58
+ handelDown,
59
+ handelUp,
60
+ handleMove
61
+ }
62
+ }
@@ -1,33 +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
- };
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
+ };
@@ -1,25 +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
- }
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
25
  }