npm-pkg-hook 1.4.1 → 1.4.3
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/package.json +1 -1
- package/src/hooks/generateStoreURL/index.js +32 -0
- package/src/hooks/index.js +2 -0
- package/src/hooks/useCreateProduct/helpers/index.js +1 -1
- package/src/hooks/usePushNotifications/helpers/index.js +114 -0
- package/src/hooks/usePushNotifications/index.js +149 -0
- package/src/hooks/useSales/index.js +36 -32
package/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate the store URL based on given parameters
|
|
3
|
+
* @param {Object} city - The city object with name
|
|
4
|
+
* @param {Object} department - The department object with name
|
|
5
|
+
* @param {string} storeName - The name of the store
|
|
6
|
+
* @param {string} idStore - The ID of the store
|
|
7
|
+
* @returns {string} - The generated URL or null if any field is missing
|
|
8
|
+
*/
|
|
9
|
+
export const generateStoreURL = ({ city, department, storeName, idStore }) => {
|
|
10
|
+
try {
|
|
11
|
+
// Validate all necessary fields
|
|
12
|
+
if (
|
|
13
|
+
!process.env.MAIN_URL_BASE ||
|
|
14
|
+
!city?.cName ||
|
|
15
|
+
!department?.dName ||
|
|
16
|
+
!storeName ||
|
|
17
|
+
!idStore
|
|
18
|
+
) {
|
|
19
|
+
return null // Return null or any default case you'd prefer
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const cityName = city.cName.toLocaleLowerCase()
|
|
23
|
+
const departmentName = department.dName.toLocaleLowerCase()
|
|
24
|
+
|
|
25
|
+
// Replace spaces in storeName with hyphens
|
|
26
|
+
const formattedStoreName = storeName.replace(/\s+/g, '-')
|
|
27
|
+
|
|
28
|
+
return `${process.env.MAIN_URL_BASE}/delivery/${cityName}-${departmentName}/${formattedStoreName}/${idStore}`
|
|
29
|
+
} catch (_) {
|
|
30
|
+
return null
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/hooks/index.js
CHANGED
|
@@ -10,9 +10,11 @@ export * from './useLogout'
|
|
|
10
10
|
export * from './useStatusOpenStore'
|
|
11
11
|
export * from './useScheduleData'
|
|
12
12
|
export * from './useOrders'
|
|
13
|
+
export * from './usePushNotifications'
|
|
13
14
|
export * from './useLocationManager'
|
|
14
15
|
export * from './useFingerprintjs'
|
|
15
16
|
export * from './useCountries'
|
|
17
|
+
export * from './generateStoreURL'
|
|
16
18
|
export * from './useCreateStorePendingToRegister'
|
|
17
19
|
export * from './useDepartments'
|
|
18
20
|
export * from './useRoads'
|
|
@@ -14,7 +14,7 @@ export const verifyPriceInRange = ({ values = [], sendNotification }) => {
|
|
|
14
14
|
sendNotification({
|
|
15
15
|
backgroundColor: 'warning',
|
|
16
16
|
title: 'Alerta',
|
|
17
|
-
description: 'El valor
|
|
17
|
+
description: 'El valor es muy elevado'
|
|
18
18
|
})
|
|
19
19
|
return false
|
|
20
20
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/* eslint-disable consistent-return */
|
|
2
|
+
const pushServerPublicKey = 'BIN2Jc5Vmkmy-S3AUrcMlpKxJpLeVRAfu9WBqUbJ70SJOCWGCGXKY-Xzyh7HDr6KbRDGYHjqZ06OcS3BjD7uAm8'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* checks if Push notification and service workers are supported by your browser
|
|
6
|
+
*/
|
|
7
|
+
function isPushNotificationSupported () {
|
|
8
|
+
if (typeof window !== 'undefined' && window.Notification && window.ServiceWorker) {
|
|
9
|
+
return 'serviceWorker' in navigator && 'PushManager' in window
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* asks user consent to receive push notifications and returns the response of the user, one of granted, default, denied
|
|
15
|
+
*/
|
|
16
|
+
async function askUserPermission () {
|
|
17
|
+
return await Notification.requestPermission()
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* shows a notification
|
|
21
|
+
*/
|
|
22
|
+
function sendNotification () {
|
|
23
|
+
const img = '/images/jason-leung-HM6TMmevbZQ-unsplash.jpg'
|
|
24
|
+
const text = 'Take a look at this brand new t-shirt!'
|
|
25
|
+
const title = 'New Product Available'
|
|
26
|
+
const options = {
|
|
27
|
+
body: text,
|
|
28
|
+
icon: '/images/jason-leung-HM6TMmevbZQ-unsplash.jpg',
|
|
29
|
+
vibrate: [200, 100, 200],
|
|
30
|
+
tag: 'new-product',
|
|
31
|
+
image: img,
|
|
32
|
+
badge: 'https://spyna.it/icons/android-icon-192x192.png',
|
|
33
|
+
actions: [{ action: 'Detail', title: 'View', icon: 'https://via.placeholder.com/128/ff0000' }]
|
|
34
|
+
}
|
|
35
|
+
navigator.serviceWorker.ready.then(function (serviceWorker) {
|
|
36
|
+
serviceWorker.showNotification(title, options)
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
function registerServiceWorker () {
|
|
44
|
+
return navigator.serviceWorker.register('/sw.js')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* using the registered service worker creates a push notification subscription and returns it
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
async function createNotificationSubscription () {
|
|
53
|
+
// wait for service worker installation to be ready
|
|
54
|
+
const serviceWorker = await navigator.serviceWorker.ready
|
|
55
|
+
// subscribe and return the subscription
|
|
56
|
+
return await serviceWorker.pushManager.subscribe({
|
|
57
|
+
userVisibleOnly: true,
|
|
58
|
+
applicationServerKey: pushServerPublicKey
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* returns the subscription if present or nothing
|
|
64
|
+
*/
|
|
65
|
+
function getUserSubscription () {
|
|
66
|
+
// wait for service worker installation to be ready, and then
|
|
67
|
+
return navigator.serviceWorker.ready
|
|
68
|
+
.then(function (serviceWorker) {
|
|
69
|
+
return serviceWorker.pushManager.getSubscription()
|
|
70
|
+
})
|
|
71
|
+
.then(function (pushSubscription) {
|
|
72
|
+
return pushSubscription
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const host = process.env.NODE_ENV === 'production' ? 'http://localhost:4000' : 'http://localhost:4000'
|
|
77
|
+
|
|
78
|
+
function post (path, body) {
|
|
79
|
+
return fetch(`${host}${path}`, {
|
|
80
|
+
body: JSON.stringify(body),
|
|
81
|
+
method: 'POST',
|
|
82
|
+
mode: 'cors'
|
|
83
|
+
})
|
|
84
|
+
.then(function (response) {
|
|
85
|
+
return response.json()
|
|
86
|
+
})
|
|
87
|
+
.then(function (data) {
|
|
88
|
+
return data
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function get (path) {
|
|
93
|
+
return fetch(`${host}${path}`, {
|
|
94
|
+
method: 'GET',
|
|
95
|
+
mode: 'cors'
|
|
96
|
+
})
|
|
97
|
+
.then(function (response) {
|
|
98
|
+
return response.json()
|
|
99
|
+
})
|
|
100
|
+
.then(function (data) {
|
|
101
|
+
return data
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
isPushNotificationSupported,
|
|
107
|
+
askUserPermission,
|
|
108
|
+
post,
|
|
109
|
+
get,
|
|
110
|
+
registerServiceWorker,
|
|
111
|
+
sendNotification,
|
|
112
|
+
createNotificationSubscription,
|
|
113
|
+
getUserSubscription
|
|
114
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import { useState, useEffect } from 'react'
|
|
3
|
+
import {
|
|
4
|
+
isPushNotificationSupported,
|
|
5
|
+
post,
|
|
6
|
+
get,
|
|
7
|
+
askUserPermission,
|
|
8
|
+
registerServiceWorker,
|
|
9
|
+
createNotificationSubscription,
|
|
10
|
+
getUserSubscription
|
|
11
|
+
} from './helpers'
|
|
12
|
+
// import all the function created to manage the push notifications
|
|
13
|
+
|
|
14
|
+
const pushNotificationSupported = isPushNotificationSupported()
|
|
15
|
+
// first thing to do: check if the push notifications are supported by the browser
|
|
16
|
+
|
|
17
|
+
export const usePushNotifications = () => {
|
|
18
|
+
const [userConsent, setSuserConsent] = useState()
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (typeof Notification !== 'undefined') {
|
|
21
|
+
setSuserConsent(Notification.permission)
|
|
22
|
+
}
|
|
23
|
+
}, [])
|
|
24
|
+
|
|
25
|
+
// to manage the user consent: Notification.permission is a JavaScript native function that return the current state of the permission
|
|
26
|
+
// We initialize the userConsent with that value
|
|
27
|
+
const [userSubscription, setUserSubscription] = useState(null)
|
|
28
|
+
// to manage the use push notification subscription
|
|
29
|
+
const [pushServerSubscriptionId, setPushServerSubscriptionId] = useState()
|
|
30
|
+
// to manage the push server subscription
|
|
31
|
+
const [error, setError] = useState(null)
|
|
32
|
+
// to manage errors
|
|
33
|
+
const [loading, setLoading] = useState(true)
|
|
34
|
+
// to manage async actions
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (pushNotificationSupported) {
|
|
38
|
+
setLoading(true)
|
|
39
|
+
setError(false)
|
|
40
|
+
registerServiceWorker().then(() => {
|
|
41
|
+
setLoading(false)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
}, [])
|
|
45
|
+
// if the push notifications are supported, registers the service worker
|
|
46
|
+
// this effect runs only the first render
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
setLoading(true)
|
|
50
|
+
setError(false)
|
|
51
|
+
const getExixtingSubscription = async () => {
|
|
52
|
+
const existingSubscription = await getUserSubscription()
|
|
53
|
+
setUserSubscription(existingSubscription)
|
|
54
|
+
setLoading(false)
|
|
55
|
+
}
|
|
56
|
+
getExixtingSubscription()
|
|
57
|
+
}, [])
|
|
58
|
+
// Retrieve if there is any push notification subscription for the registered service worker
|
|
59
|
+
// this use effect runs only in the first render
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* define a click handler that asks the user permission,
|
|
63
|
+
* it uses the setSuserConsent state, to set the consent of the user
|
|
64
|
+
* If the user denies the consent, an error is created with the setError hook
|
|
65
|
+
*/
|
|
66
|
+
const onClickAskUserPermission = () => {
|
|
67
|
+
setLoading(true)
|
|
68
|
+
setError(false)
|
|
69
|
+
askUserPermission().then(consent => {
|
|
70
|
+
setSuserConsent(consent)
|
|
71
|
+
if (consent !== 'granted') {
|
|
72
|
+
setError({
|
|
73
|
+
name: 'consentimiento denegado',
|
|
74
|
+
message: 'Negaste el consentimiento para recibir notificaciones',
|
|
75
|
+
code: 0
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
setLoading(false)
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
//
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* define a click handler that creates a push notification subscription.
|
|
85
|
+
* Once the subscription is created, it uses the setUserSubscription hook
|
|
86
|
+
*/
|
|
87
|
+
const onClickSusbribeToPushNotification = () => {
|
|
88
|
+
setLoading(true)
|
|
89
|
+
setError(false)
|
|
90
|
+
createNotificationSubscription()
|
|
91
|
+
.then(function (subscrition) {
|
|
92
|
+
setUserSubscription(subscrition)
|
|
93
|
+
setLoading(false)
|
|
94
|
+
})
|
|
95
|
+
.catch(err => {
|
|
96
|
+
console.error("Couldn't create the notification subscription", err, 'name:', err.name, 'message:', err.message, 'code:', err.code)
|
|
97
|
+
setError(err)
|
|
98
|
+
setLoading(false)
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* define a click handler that sends the push susbcribtion to the push server.
|
|
104
|
+
* Once the subscription ics created on the server, it saves the id using the hook setPushServerSubscriptionId
|
|
105
|
+
*/
|
|
106
|
+
const onClickSendSubscriptionToPushServer = () => {
|
|
107
|
+
setLoading(true)
|
|
108
|
+
setError(false)
|
|
109
|
+
post('/subscription2', userSubscription)
|
|
110
|
+
.then(function (response) {
|
|
111
|
+
setPushServerSubscriptionId(response.id)
|
|
112
|
+
setLoading(false)
|
|
113
|
+
})
|
|
114
|
+
.catch(err => {
|
|
115
|
+
setLoading(false)
|
|
116
|
+
setError(err)
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* define a click handler that request the push server to send a notification, passing the id of the saved subscription
|
|
122
|
+
*/
|
|
123
|
+
const onClickSendNotification = async () => {
|
|
124
|
+
setLoading(true)
|
|
125
|
+
setError(false)
|
|
126
|
+
console.log(pushServerSubscriptionId)
|
|
127
|
+
await get(`/subscription/${pushServerSubscriptionId}`).catch(err => {
|
|
128
|
+
setLoading(false)
|
|
129
|
+
setError(err)
|
|
130
|
+
})
|
|
131
|
+
setLoading(false)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* returns all the stuff needed by a Component
|
|
136
|
+
*/
|
|
137
|
+
return {
|
|
138
|
+
onClickAskUserPermission,
|
|
139
|
+
onClickSusbribeToPushNotification,
|
|
140
|
+
onClickSendSubscriptionToPushServer,
|
|
141
|
+
pushServerSubscriptionId,
|
|
142
|
+
onClickSendNotification,
|
|
143
|
+
userConsent,
|
|
144
|
+
pushNotificationSupported,
|
|
145
|
+
userSubscription,
|
|
146
|
+
error,
|
|
147
|
+
loading
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -27,8 +27,9 @@ import {
|
|
|
27
27
|
import { updateExistingOrders } from '../useUpdateExistingOrders'
|
|
28
28
|
import { useGetSale } from './useGetSale'
|
|
29
29
|
import { convertToIntegerOrFloat } from './helpers'
|
|
30
|
-
import { useCatWithProduct } from './../useCatWithProduct/index'
|
|
31
|
-
import { useCheckboxState } from '../useCheckbox'
|
|
30
|
+
import { useCatWithProduct } from './../useCatWithProduct/index'
|
|
31
|
+
import { useCheckboxState } from '../useCheckbox'
|
|
32
|
+
import { useLogout } from '../useLogout'
|
|
32
33
|
export * from './useGetAllSales'
|
|
33
34
|
export { GET_ALL_COUNT_SALES } from './queries'
|
|
34
35
|
|
|
@@ -62,6 +63,8 @@ export const useSales = ({
|
|
|
62
63
|
const domain = getCurrentDomain()
|
|
63
64
|
const [loadingSale, setLoadingSale] = useState(false)
|
|
64
65
|
const [errorSale, setErrorSale] = useState(false)
|
|
66
|
+
const [onClickLogout] = useLogout({})
|
|
67
|
+
|
|
65
68
|
const [modalItem, setModalItem] = useState(false)
|
|
66
69
|
const [openCommentModal, setOpenCommentModal] = useState(false)
|
|
67
70
|
const keyToSaveData = process.env.LOCAL_SALES_STORE
|
|
@@ -74,7 +77,7 @@ export const useSales = ({
|
|
|
74
77
|
setCheckedItems,
|
|
75
78
|
handleChangeCheck
|
|
76
79
|
} = useCheckboxState(datCat, [], [])
|
|
77
|
-
|
|
80
|
+
const arr = checkedItems ? Array.from(checkedItems) : []
|
|
78
81
|
const [totalProductPrice, setTotalProductPrice] = useState(0)
|
|
79
82
|
const [showMore, setShowMore] = useState(100)
|
|
80
83
|
const [inputValue, setInputValue] = useState('')
|
|
@@ -117,6 +120,9 @@ export const useSales = ({
|
|
|
117
120
|
description: message
|
|
118
121
|
})
|
|
119
122
|
setAlertBox({ message, type: 'success' })
|
|
123
|
+
if (message === 'Token expired') {
|
|
124
|
+
onClickLogout()
|
|
125
|
+
}
|
|
120
126
|
setOpenCurrentSale(data?.registerSalesStore?.Response.success)
|
|
121
127
|
},
|
|
122
128
|
onError: (error) => {
|
|
@@ -188,12 +194,12 @@ export const useSales = ({
|
|
|
188
194
|
return setSearch(e.target.value)
|
|
189
195
|
}
|
|
190
196
|
const handleChange = (e, error) => {
|
|
191
|
-
const { name, value } = e.target
|
|
197
|
+
const { name, value } = e.target
|
|
192
198
|
setErrors({ ...errors, [e.target.name]: error })
|
|
193
199
|
setValues((prevValues) => ({
|
|
194
200
|
...prevValues,
|
|
195
|
-
[name]: value
|
|
196
|
-
}))
|
|
201
|
+
[name]: value
|
|
202
|
+
}))
|
|
197
203
|
}
|
|
198
204
|
const onChangeInput = (e) => {
|
|
199
205
|
return setValuesDates({ ...valuesDates, [e.target.name]: e.target.value })
|
|
@@ -462,15 +468,15 @@ export const useSales = ({
|
|
|
462
468
|
return { ...obj, ExtProductFoodsSubOptionalAll: filteredSubOptions }
|
|
463
469
|
})
|
|
464
470
|
.filter((obj) => obj !== null) // Elimine todos los objetos nulos del arreglo
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
471
|
+
const filteredDataExtra = dataExtra?.filter((p) => p?.quantity !== undefined && p?.quantity !== 0)
|
|
472
|
+
if (product?.PRODUCT?.pId) {
|
|
473
|
+
dispatch({
|
|
474
|
+
type: 'PUT_EXTRA_PRODUCTS_AND_OPTIONAL_PRODUCT',
|
|
475
|
+
payload: product.PRODUCT.pId,
|
|
476
|
+
dataOptional: filteredDataOptional,
|
|
477
|
+
dataExtra: filteredDataExtra
|
|
478
|
+
})
|
|
479
|
+
}
|
|
474
480
|
} catch (_error) {
|
|
475
481
|
return sendNotification({
|
|
476
482
|
title: 'Error',
|
|
@@ -480,31 +486,30 @@ export const useSales = ({
|
|
|
480
486
|
}
|
|
481
487
|
}
|
|
482
488
|
|
|
483
|
-
function handleIncrementExtra({ Adicionales, index }) {
|
|
484
|
-
const { pId } = product?.PRODUCT || {}
|
|
485
|
-
const exPid = Adicionales.exPid || null
|
|
489
|
+
function handleIncrementExtra ({ Adicionales, index }) {
|
|
490
|
+
const { pId } = product?.PRODUCT || {}
|
|
491
|
+
const exPid = Adicionales.exPid || null
|
|
486
492
|
|
|
487
493
|
if (exPid && pId) {
|
|
488
494
|
const newExtra = dataExtra.map((producto) => {
|
|
489
495
|
if (exPid === producto.exPid) {
|
|
490
|
-
const initialQuantity = producto?.quantity ? producto?.quantity : 0
|
|
491
|
-
const newQuantity = initialQuantity + 1
|
|
492
|
-
const newExtraPrice = producto.extraPrice * newQuantity
|
|
496
|
+
const initialQuantity = producto?.quantity ? producto?.quantity : 0
|
|
497
|
+
const newQuantity = initialQuantity + 1
|
|
498
|
+
const newExtraPrice = producto.extraPrice * newQuantity
|
|
493
499
|
|
|
494
500
|
return {
|
|
495
501
|
...producto,
|
|
496
502
|
quantity: newQuantity,
|
|
497
|
-
newExtraPrice
|
|
498
|
-
}
|
|
503
|
+
newExtraPrice
|
|
504
|
+
}
|
|
499
505
|
}
|
|
500
|
-
return producto
|
|
501
|
-
})
|
|
506
|
+
return producto
|
|
507
|
+
})
|
|
502
508
|
|
|
503
|
-
setDataExtra(newExtra)
|
|
509
|
+
setDataExtra(newExtra)
|
|
504
510
|
}
|
|
505
511
|
}
|
|
506
512
|
|
|
507
|
-
|
|
508
513
|
function handleDecrementExtra ({ Adicionales, index }) {
|
|
509
514
|
const { pId } = product?.PRODUCT || {}
|
|
510
515
|
const exPid = Adicionales.exPid || null
|
|
@@ -823,8 +828,7 @@ export const useSales = ({
|
|
|
823
828
|
const client = useApolloClient()
|
|
824
829
|
const { getOnePedidoStore } = useGetSale()
|
|
825
830
|
const handleSubmit = () => {
|
|
826
|
-
|
|
827
|
-
if (errors?.change || errors?.valueDelivery) {
|
|
831
|
+
if (errors?.change || errors?.valueDelivery) {
|
|
828
832
|
return sendNotification({
|
|
829
833
|
title: 'error',
|
|
830
834
|
backgroundColor: 'warning',
|
|
@@ -834,12 +838,12 @@ export const useSales = ({
|
|
|
834
838
|
setLoadingSale(true)
|
|
835
839
|
const code = RandomCode(10)
|
|
836
840
|
setCode(code)
|
|
837
|
-
function convertirAEntero(cadena) {
|
|
841
|
+
function convertirAEntero (cadena) {
|
|
838
842
|
if (typeof cadena === 'string') {
|
|
839
|
-
const numeroEntero = parseInt(cadena?.replace('.', ''))
|
|
843
|
+
const numeroEntero = parseInt(cadena?.replace('.', ''))
|
|
840
844
|
return numeroEntero
|
|
841
845
|
}
|
|
842
|
-
return cadena ||
|
|
846
|
+
return cadena || 0
|
|
843
847
|
}
|
|
844
848
|
return registerSalesStore({
|
|
845
849
|
variables: {
|