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.
Files changed (84) 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 +46 -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 +168 -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/useReport/index.js +35 -0
  58. package/src/hooks/useReport/queries.js +122 -0
  59. package/src/hooks/useRestaurant/index.js +19 -19
  60. package/src/hooks/useRestaurant/queries.js +79 -69
  61. package/src/hooks/useSales/index.js +589 -489
  62. package/src/hooks/useSales/queries.js +290 -229
  63. package/src/hooks/useSales/useGetSale.js +12 -0
  64. package/src/hooks/useSales/useTotalSales.js +17 -0
  65. package/src/hooks/useSaveAvailableProduct/helpers/index.js +30 -0
  66. package/src/hooks/useSaveAvailableProduct/index.js +26 -0
  67. package/src/hooks/useSaveAvailableProduct/queries.js +10 -0
  68. package/src/hooks/useSchedule/index.jsx +23 -0
  69. package/src/hooks/useSetState/index.js +24 -24
  70. package/src/hooks/useStore/index.js +17 -17
  71. package/src/hooks/useStore/queries.js +135 -135
  72. package/src/hooks/useStoreCalendar/index.js +7 -0
  73. package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
  74. package/src/hooks/useUpdateCart/index.js +124 -124
  75. package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +37 -0
  76. package/src/hooks/useUser/index.js +7 -3
  77. package/src/hooks/useUser/queries.js +69 -0
  78. package/src/hooks/useWindowSize/index.js +37 -37
  79. package/src/index.jsx +2 -1
  80. package/src/mock/dessert/index.js +16 -0
  81. package/src/mock/index.js +2 -0
  82. package/src/utils/index.js +106 -54
  83. package/src/hooks/useGetCategorieStore/index.js +0 -21
  84. package/src/hooks/useGetProductsFood/index.js +0 -46
@@ -1,114 +1,114 @@
1
- import { useCallback, useState } from 'react'
2
-
3
- /**
4
- * Hook for managing multiple checkbox states
5
- *
6
- * @param {Array<String | Number>} elem - list of all elem
7
- * @param {Array<String | Number>} selectedIds - list of selected elem (optional)
8
- *
9
- * @returns {Object}
10
- * - checkboxStates (state object),
11
- * - numSelectedItems (number),
12
- * - handleChangeCheck (callback),
13
- * - toggleAll (callback),
14
- * - selectAll (callback),
15
- * - clearAll (callback)
16
- */
17
-
18
- export const useCheckboxState = (elem, selectedIds = [], disabledIds = []) => {
19
- const numTotalItems = elem?.length
20
- const [checkedItems, setCheckedItems] = useState(new Set(selectedIds))
21
- const [disabledItems, setDisabledItems] = useState(new Set(disabledIds))
22
-
23
- const handleChangeCheck = useCallback((event, id) => {
24
- const target = event.target
25
- setCheckedItems(prevState => {
26
- const newState = new Set(prevState)
27
- if (target.checked) {
28
- newState.add(id)
29
- } else {
30
- newState.delete(id)
31
- }
32
- return newState
33
- })
34
- }, [])
35
-
36
- const setAll = useCallback(
37
- isChecked => {
38
- setCheckedItems(prevState => {
39
- const newState = new Set(prevState)
40
- for (const id of elem) {
41
- if (disabledItems.has(id)) {
42
- continue
43
- }
44
- if (isChecked) {
45
- newState.add(id)
46
- } else {
47
- newState.delete(id)
48
- }
49
- }
50
- return newState
51
- })
52
- },
53
- [elem, disabledItems]
54
- )
55
-
56
- const selectAll = useCallback(() => {
57
- if (checkedItems.size === numTotalItems) {
58
- return
59
- }
60
- setAll(true)
61
- }, [checkedItems, numTotalItems, setAll])
62
-
63
- const clearAll = useCallback(() => {
64
- if (checkedItems.size === 0) {
65
- return
66
- }
67
- setAll(false)
68
- }, [checkedItems, setAll])
69
-
70
- const toggleAll = useCallback(() => {
71
- const numDisabledAndChecked = [...disabledItems].reduce((count, id) => {
72
- if (checkedItems.has(id)) {
73
- return count + 1
74
- }
75
- return count
76
- }, 0)
77
- if (checkedItems.size - numDisabledAndChecked === 0) {
78
- selectAll()
79
- } else {
80
- clearAll()
81
- }
82
- }, [checkedItems, disabledItems, selectAll, clearAll])
83
-
84
- const enableCheckboxes = useCallback((...elem) => {
85
- setDisabledItems(prevState => {
86
- const newState = new Set(prevState)
87
- for (const id of elem) {
88
- newState.delete(id)
89
- }
90
- return newState
91
- })
92
- }, [])
93
-
94
- const disableCheckboxes = useCallback((...elem) => {
95
- setDisabledItems(prevState => {
96
- const newState = new Set(prevState)
97
- for (const id of elem) {
98
- newState.add(id)
99
- }
100
- return newState
101
- })
102
- }, [])
103
-
104
- return {
105
- checkedItems,
106
- disabledItems,
107
- handleChangeCheck,
108
- toggleAll,
109
- selectAll,
110
- clearAll,
111
- enableCheckboxes,
112
- disableCheckboxes
113
- }
114
- }
1
+ import { useCallback, useState } from 'react'
2
+
3
+ /**
4
+ * Hook for managing multiple checkbox states
5
+ *
6
+ * @param {Array<String | Number>} elem - list of all elem
7
+ * @param {Array<String | Number>} selectedIds - list of selected elem (optional)
8
+ *
9
+ * @returns {Object}
10
+ * - checkboxStates (state object),
11
+ * - numSelectedItems (number),
12
+ * - handleChangeCheck (callback),
13
+ * - toggleAll (callback),
14
+ * - selectAll (callback),
15
+ * - clearAll (callback)
16
+ */
17
+
18
+ export const useCheckboxState = (elem, selectedIds = [], disabledIds = []) => {
19
+ const numTotalItems = elem?.length
20
+ const [checkedItems, setCheckedItems] = useState(new Set(selectedIds))
21
+ const [disabledItems, setDisabledItems] = useState(new Set(disabledIds))
22
+
23
+ const handleChangeCheck = useCallback((event, id) => {
24
+ const target = event.target
25
+ setCheckedItems(prevState => {
26
+ const newState = new Set(prevState)
27
+ if (target.checked) {
28
+ newState.add(id)
29
+ } else {
30
+ newState.delete(id)
31
+ }
32
+ return newState
33
+ })
34
+ }, [])
35
+
36
+ const setAll = useCallback(
37
+ isChecked => {
38
+ setCheckedItems(prevState => {
39
+ const newState = new Set(prevState)
40
+ for (const id of elem) {
41
+ if (disabledItems.has(id)) {
42
+ continue
43
+ }
44
+ if (isChecked) {
45
+ newState.add(id)
46
+ } else {
47
+ newState.delete(id)
48
+ }
49
+ }
50
+ return newState
51
+ })
52
+ },
53
+ [elem, disabledItems]
54
+ )
55
+
56
+ const selectAll = useCallback(() => {
57
+ if (checkedItems.size === numTotalItems) {
58
+ return
59
+ }
60
+ setAll(true)
61
+ }, [checkedItems, numTotalItems, setAll])
62
+
63
+ const clearAll = useCallback(() => {
64
+ if (checkedItems.size === 0) {
65
+ return
66
+ }
67
+ setAll(false)
68
+ }, [checkedItems, setAll])
69
+
70
+ const toggleAll = useCallback(() => {
71
+ const numDisabledAndChecked = [...disabledItems].reduce((count, id) => {
72
+ if (checkedItems.has(id)) {
73
+ return count + 1
74
+ }
75
+ return count
76
+ }, 0)
77
+ if (checkedItems.size - numDisabledAndChecked === 0) {
78
+ selectAll()
79
+ } else {
80
+ clearAll()
81
+ }
82
+ }, [checkedItems, disabledItems, selectAll, clearAll])
83
+
84
+ const enableCheckboxes = useCallback((...elem) => {
85
+ setDisabledItems(prevState => {
86
+ const newState = new Set(prevState)
87
+ for (const id of elem) {
88
+ newState.delete(id)
89
+ }
90
+ return newState
91
+ })
92
+ }, [])
93
+
94
+ const disableCheckboxes = useCallback((...elem) => {
95
+ setDisabledItems(prevState => {
96
+ const newState = new Set(prevState)
97
+ for (const id of elem) {
98
+ newState.add(id)
99
+ }
100
+ return newState
101
+ })
102
+ }, [])
103
+
104
+ return {
105
+ checkedItems,
106
+ disabledItems,
107
+ handleChangeCheck,
108
+ toggleAll,
109
+ selectAll,
110
+ clearAll,
111
+ enableCheckboxes,
112
+ disableCheckboxes
113
+ }
114
+ }
@@ -1,13 +1,13 @@
1
- import { useQuery } from '@apollo/client'
2
- import { useEffect, useState } from 'react'
3
- import { GET_ALL_CLIENTS } from './queries'
4
-
5
- export const useGetClients = () => {
6
- const [clientes, setClients] = useState([])
7
- const { loading, error } = useQuery(GET_ALL_CLIENTS, {
8
- onCompleted: (data) => {
9
- setClients(data)
10
- }
11
- })
12
- return [clientes?.getAllClients, { loading, error }]
13
- }
1
+ import { useQuery } from '@apollo/client'
2
+ import { useState } from 'react'
3
+ import { GET_ALL_CLIENTS } from './queries'
4
+
5
+ export const useGetClients = () => {
6
+ const [clientes, setClients] = useState([])
7
+ const { loading, error } = useQuery(GET_ALL_CLIENTS, {
8
+ onCompleted: (data) => {
9
+ setClients(data)
10
+ }
11
+ })
12
+ return [clientes?.getAllClients || [], { loading, error }]
13
+ }
@@ -1,118 +1,118 @@
1
- import { gql } from '@apollo/client'
2
-
3
- export const CREATE_CLIENTS = gql`
4
- mutation createClients ($input: IClients) {
5
- createClients(input: $input) {
6
- cliId
7
- idStore
8
- idUser
9
- clState
10
- ClientAddress
11
- clientNumber
12
- ccClient
13
- gender
14
- clientLastName
15
- clientName
16
- createAt
17
- updateAt
18
- }
19
- }
20
- `
21
- export const DELETE_ONE_CLIENTS = gql`
22
- mutation deleteClient($cliId: ID, $clState: Int!) {
23
- deleteClient(cliId: $cliId, clState: $clState) {
24
- success
25
- message
26
- }
27
- }
28
- `
29
- export const GET_ALL_CLIENTS = gql`
30
- query getAllClients($idStore: ID, $cId: ID, $dId: ID, $ctId: ID, $search: String, $min: Int, $max: Int, $fromDate: DateTime, $toDate: DateTime ) {
31
- getAllClients(idStore: $idStore, cId: $cId, dId: $dId, ctId: $ctId, search: $search, min: $min, max: $max, fromDate: $fromDate, toDate: $toDate) {
32
- cliId
33
- idStore
34
- gender
35
- # idUser
36
- clState
37
- clientNumber
38
- ccClient
39
- clientLastName
40
- clientName
41
- ClientAddress
42
- createAt
43
- updateAt
44
-
45
- }
46
- }
47
- `
48
- export const GET_ONE_CLIENT = gql`
49
- query getOneClients($cliId: ID) {
50
- getOneClients(cliId: $cliId) {
51
- cliId
52
- idStore
53
- idUser
54
- clState
55
- clientNumber
56
- ClientAddress
57
- gender
58
- ccClient
59
- clientLastName
60
- clientName
61
- createAt
62
- updateAt
63
-
64
- }
65
- }
66
- `
67
-
68
- export const CREATE_SHOPPING_CARD = gql`
69
- mutation registerShoppingCard($input: IShoppingCard, $idSubArray: IID_SUB_ITEMS){
70
- registerShoppingCard(input: $input, idSubArray: $idSubArray){
71
- ShoppingCard
72
- id
73
- pId
74
- subProductsId
75
- ShoppingCardRefCode
76
- uuid
77
- discountCardProduct
78
- idUser
79
- cName
80
- idStore
81
- cState
82
- cDatCre
83
- cDatMod
84
- csDescription
85
- cantProducts
86
- comments
87
- # idSubArray
88
- }
89
- }
90
- `
91
- export const CREATE_SHOPPING_CARD_TO_USER_STORE = gql`
92
- mutation registerSalesStore($input: [IShoppingCard], $id: ID, $idStore: ID, $pCodeRef: String, $change: String, $valueDelivery: Float, $payMethodPState: Int, $pickUp: Int, $totalProductsPrice: Float, $idSubArray: IID_SUB_ITEMS){
93
- registerSalesStore(input: $input, id: $id, idStore: $idStore, pCodeRef: $pCodeRef, change: $change, valueDelivery: $valueDelivery, payMethodPState: $payMethodPState, pickUp: $pickUp, totalProductsPrice: $totalProductsPrice, idSubArray: $idSubArray){
94
- ShoppingCard {
95
- ShoppingCard
96
- id
97
- pId
98
- subProductsId
99
- ShoppingCardRefCode
100
- uuid
101
- discountCardProduct
102
- idUser
103
- cName
104
- idStore
105
- cState
106
- cDatCre
107
- cDatMod
108
- csDescription
109
- cantProducts
110
- comments
111
- }
112
- Response {
113
- success
114
- message
115
- }
116
- }
117
- }
1
+ import { gql } from '@apollo/client'
2
+
3
+ export const CREATE_CLIENTS = gql`
4
+ mutation createClients ($input: IClients) {
5
+ createClients(input: $input) {
6
+ cliId
7
+ idStore
8
+ idUser
9
+ clState
10
+ ClientAddress
11
+ clientNumber
12
+ ccClient
13
+ gender
14
+ clientLastName
15
+ clientName
16
+ createAt
17
+ updateAt
18
+ }
19
+ }
20
+ `
21
+ export const DELETE_ONE_CLIENTS = gql`
22
+ mutation deleteClient($cliId: ID, $clState: Int!) {
23
+ deleteClient(cliId: $cliId, clState: $clState) {
24
+ success
25
+ message
26
+ }
27
+ }
28
+ `
29
+ export const GET_ALL_CLIENTS = gql`
30
+ query getAllClients($idStore: ID, $cId: ID, $dId: ID, $ctId: ID, $search: String, $min: Int, $max: Int, $fromDate: DateTime, $toDate: DateTime ) {
31
+ getAllClients(idStore: $idStore, cId: $cId, dId: $dId, ctId: $ctId, search: $search, min: $min, max: $max, fromDate: $fromDate, toDate: $toDate) {
32
+ cliId
33
+ idStore
34
+ gender
35
+ # idUser
36
+ clState
37
+ clientNumber
38
+ ccClient
39
+ clientLastName
40
+ clientName
41
+ ClientAddress
42
+ createAt
43
+ updateAt
44
+
45
+ }
46
+ }
47
+ `
48
+ export const GET_ONE_CLIENT = gql`
49
+ query getOneClients($cliId: ID) {
50
+ getOneClients(cliId: $cliId) {
51
+ cliId
52
+ idStore
53
+ idUser
54
+ clState
55
+ clientNumber
56
+ ClientAddress
57
+ gender
58
+ ccClient
59
+ clientLastName
60
+ clientName
61
+ createAt
62
+ updateAt
63
+
64
+ }
65
+ }
66
+ `
67
+
68
+ export const CREATE_SHOPPING_CARD = gql`
69
+ mutation registerShoppingCard($input: IShoppingCard, $idSubArray: IID_SUB_ITEMS){
70
+ registerShoppingCard(input: $input, idSubArray: $idSubArray){
71
+ ShoppingCard
72
+ id
73
+ pId
74
+ subProductsId
75
+ ShoppingCardRefCode
76
+ uuid
77
+ discountCardProduct
78
+ idUser
79
+ cName
80
+ idStore
81
+ cState
82
+ cDatCre
83
+ cDatMod
84
+ csDescription
85
+ cantProducts
86
+ comments
87
+ # idSubArray
88
+ }
89
+ }
90
+ `
91
+ export const CREATE_SHOPPING_CARD_TO_USER_STORE = gql`
92
+ mutation registerSalesStore($input: [IShoppingCard], $id: ID, $idStore: ID, $pCodeRef: String, $change: String, $valueDelivery: Float, $payMethodPState: Int, $pickUp: Int, $totalProductsPrice: Float, $idSubArray: IID_SUB_ITEMS){
93
+ registerSalesStore(input: $input, id: $id, idStore: $idStore, pCodeRef: $pCodeRef, change: $change, valueDelivery: $valueDelivery, payMethodPState: $payMethodPState, pickUp: $pickUp, totalProductsPrice: $totalProductsPrice, idSubArray: $idSubArray){
94
+ ShoppingCard {
95
+ ShoppingCard
96
+ id
97
+ pId
98
+ subProductsId
99
+ ShoppingCardRefCode
100
+ uuid
101
+ discountCardProduct
102
+ idUser
103
+ cName
104
+ idStore
105
+ cState
106
+ cDatCre
107
+ cDatMod
108
+ csDescription
109
+ cantProducts
110
+ comments
111
+ }
112
+ Response {
113
+ success
114
+ message
115
+ }
116
+ }
117
+ }
118
118
  `
@@ -0,0 +1,23 @@
1
+ export const useConnection = ({ setConnectionStatus }) => {
2
+ async function updateConnectionStatus() {
3
+ if (navigator.onLine) {
4
+ setConnectionStatus(navigator.onLine);
5
+ } else {
6
+ setConnectionStatus(navigator.onLine);
7
+ }
8
+ }
9
+
10
+ // Attaching event handler for the load event
11
+ // window.addEventListener('load', updateConnectionStatus);
12
+
13
+ // Attaching event handler for the online event
14
+ window.addEventListener('online', function(e) {
15
+ updateConnectionStatus();
16
+ });
17
+
18
+ // Attaching event handler for the offline event
19
+ window.addEventListener('offline', function(e) {
20
+ updateConnectionStatus();
21
+ });
22
+ };
23
+
@@ -0,0 +1,165 @@
1
+ import { useRef, useState } from 'react'
2
+
3
+ export const useEditImageProduct = ({ sendNotification = () => { }, initialState }) => {
4
+ const [openEditImage, setopenEditImage] = useState(false)
5
+ const [tags, setTags] = useState([])
6
+ const [errors, setErrors] = useState({})
7
+ const handleOpenEditImage = () => {
8
+ setopenEditImage(!openEditImage)
9
+ }
10
+ // eslint-disable-next-line
11
+ const changeHandler = (name, value) => {
12
+ if (name === 'tags') {
13
+ setTags(value)
14
+ if (value.length > 0 && errors.tags) {
15
+ setErrors(prev => {
16
+ const prevErrors = { ...prev }
17
+ delete prevErrors.tags
18
+ return prevErrors
19
+ })
20
+ }
21
+ }
22
+ }
23
+ const [existImage, setExistImage] = useState(false)
24
+ const [crop, setCrop] = useState({ x: 0, y: 0 })
25
+ const [zoom, setZoom] = useState(0)
26
+ const imageRef = useRef(null)
27
+ const onCropChange = (crop) => {
28
+ setCrop(crop)
29
+ }
30
+
31
+ const onZoomChange = (zoom) => {
32
+ setZoom(zoom)
33
+ }
34
+
35
+ const onCropComplete = (crop, pixelCrop) => {
36
+ const canvas = document.createElement('canvas')
37
+ const img = imageRef.current
38
+
39
+ canvas.width = pixelCrop.width
40
+ canvas.height = pixelCrop.height
41
+
42
+ const ctx = canvas.getContext('2d')
43
+ ctx.drawImage(
44
+ img,
45
+ pixelCrop.x,
46
+ pixelCrop.y,
47
+ pixelCrop.width,
48
+ pixelCrop.height,
49
+ 0, 0,
50
+ pixelCrop.width,
51
+ pixelCrop.height)
52
+ // ctx.drawImage(
53
+ // img,
54
+ // pixelCrop.x,
55
+ // pixelCrop.y,
56
+ // pixelCrop.width,
57
+ // pixelCrop.height,
58
+ // 0,
59
+ // 0,
60
+ // canvas.width,
61
+ // canvas.height
62
+ // )
63
+ const croppedUrl = canvas.toDataURL()
64
+ // console.log(croppedUrl)
65
+ // You can use the croppedUrl for whatever you want
66
+ // for example, you can set it as the src of an img element
67
+ // or send it to a server
68
+ // Convert the dataURL to a blob
69
+ fetch(croppedUrl)
70
+ .then(response => { return response.blob() })
71
+ .then(blob => {
72
+ // Create a new File
73
+ const file = new File([blob], 'cropped_image.jpg', { type: blob.type })
74
+ setPreviewImg(
75
+ file
76
+ ? {
77
+ src: URL.createObjectURL(file),
78
+ alt: ''
79
+ }
80
+ : initialState
81
+ )
82
+ // You can use the file object to send it to a server or to download it
83
+ sendNotification({ title: 'Exito', description: 'Imagen editada' })
84
+ setExistImage(true)
85
+ handleOpenEditImage()
86
+ })
87
+ .catch(() => {
88
+ sendNotification({ title: 'Error', description: 'Ha ocurrido un error!' })
89
+ })
90
+ // You can use the file object to send it to a server or to download it
91
+ }
92
+ function getPixelCrop (image, crop, zoom) {
93
+ const imgWidth = image.naturalWidth
94
+ const imgHeight = image.naturalHeight
95
+
96
+ const scaledWidth = imgWidth * zoom
97
+ const scaledHeight = imgHeight * zoom
98
+
99
+ const x = (imgWidth * crop.x) / 100
100
+ const y = (imgHeight * crop.y) / 100
101
+
102
+ return {
103
+ x,
104
+ y,
105
+ width: scaledWidth,
106
+ height: scaledHeight
107
+ }
108
+ }
109
+ const handleEditImage = () => {
110
+ handleOpenEditImage()
111
+ }
112
+
113
+ const [Dimage, setImage] = useState('')
114
+ const [cropData, setCropData] = useState('#')
115
+ const [cropper, setCropper] = useState()
116
+ const onChange = (e) => {
117
+ e.preventDefault()
118
+ let files
119
+ if (e.dataTransfer) {
120
+ files = e.dataTransfer.files
121
+ } else if (e.target) {
122
+ files = e.target.files
123
+ }
124
+ const reader = new FileReader()
125
+ reader.onload = () => {
126
+ setImage(reader.result)
127
+ }
128
+ reader.readAsDataURL(files[0])
129
+ }
130
+
131
+ const getCropData = () => {
132
+ if (typeof cropper !== 'undefined') {
133
+ setCropData(cropper.getCroppedCanvas().toDataURL())
134
+ fetch(cropper)
135
+ .then(response => { return response.blob() })
136
+ .then(blob => {
137
+ // Create a new File
138
+ const file = new File([blob], 'cropped_image.jpg', { type: blob.type })
139
+ setPreviewImg(
140
+ file
141
+ ? {
142
+ src: URL.createObjectURL(file),
143
+ alt: ''
144
+ }
145
+ : initialState
146
+ )
147
+ // You can use the file object to send it to a server or to download it
148
+ sendNotification({ title: 'Exito', description: 'Imagen editada' })
149
+ setExistImage(true)
150
+ handleOpenEditImage()
151
+ })
152
+ .catch(() => {
153
+ sendNotification({ title: 'Error', description: 'Ha ocurrido un error!' })
154
+ })
155
+ }
156
+ }
157
+ return {
158
+ img: null,
159
+ crop,
160
+ existImage,
161
+ zoom,
162
+ handleEditImage,
163
+ onCropChange
164
+ }
165
+ }