npm-pkg-hook 1.7.7 → 1.7.9

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 CHANGED
@@ -44,5 +44,5 @@
44
44
  "rm": "rm -rf node_modules package-lock.json && npm i",
45
45
  "test": "echo \"Error: no test specified\" && exit 1"
46
46
  },
47
- "version": "1.7.7"
47
+ "version": "1.7.9"
48
48
  }
@@ -0,0 +1,14 @@
1
+ export function convertToMilitaryTime (time12Hour) {
2
+ const [time, period] = time12Hour.split(' ')
3
+ let [hours, minutes] = time.split(':')
4
+
5
+ if (period === 'PM' && hours !== '12') {
6
+ hours = String(Number(hours) + 12)
7
+ }
8
+
9
+ if (period === 'AM' && hours === '12') {
10
+ hours = '00'
11
+ }
12
+
13
+ return `${hours}:${minutes}`
14
+ }
@@ -0,0 +1,48 @@
1
+ export const getTotalHours = (days) => {
2
+ const totalMinutesArray = days.map((day) => {
3
+ const { schHoSta, schHoEnd } = day
4
+
5
+ // Handle potential invalid time strings
6
+ if (!isValidTimeString(schHoSta) || !isValidTimeString(schHoEnd)) {
7
+ return 0 // Ignore invalid time strings and return 0
8
+ }
9
+
10
+ const [startHours, startMinutes] = schHoSta.split(':')
11
+ const [endHours, endMinutes] = schHoEnd.split(':')
12
+
13
+ // Convert hours and minutes to integers for calculations
14
+ let totalHoursInt = parseInt(endHours, 10) - parseInt(startHours, 10)
15
+ const totalMinutesInt = parseInt(endMinutes, 10) - parseInt(startMinutes, 10)
16
+
17
+ // Handle negative total minutes (occurs when endMinutes < startMinutes)
18
+ let totalMinutes = totalMinutesInt
19
+ if (totalMinutes < 0) {
20
+ totalHoursInt-- // Decrement total hours for negative minutes
21
+ totalMinutes += 60 // Add 60 minutes to account for borrowing from previous hour
22
+ }
23
+
24
+ // Calculate total time in minutes
25
+ const totalTimeMinutes = totalHoursInt * 60 + totalMinutes
26
+
27
+ return totalTimeMinutes
28
+ })
29
+
30
+ // Calculate the sum of total minutes for all days
31
+ const totalMinutes = totalMinutesArray.reduce((acc, curr) => acc + curr, 0)
32
+
33
+ // Convert total minutes to hours and minutes format
34
+ const totalHours = Math.floor(totalMinutes / 60)
35
+ const remainingMinutes = totalMinutes % 60
36
+
37
+ // Format the total time as "00:00"
38
+ const formattedHours = totalHours.toString().padStart(2, '0')
39
+ const formattedMinutes = remainingMinutes.toString().padStart(2, '0')
40
+
41
+ return `${formattedHours}:${formattedMinutes}`
42
+ }
43
+
44
+ // Function to validate time string format (HH:MM)
45
+ export const isValidTimeString = (timeString) => {
46
+ const timeRegex = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/
47
+ return timeRegex.test(timeString)
48
+ }
@@ -6,6 +6,8 @@ export * from './useCategoryStore'
6
6
  export * from './useCatWithProduct'
7
7
  export * from './useManageQueryParams'
8
8
  export * from './useDeliveryTime'
9
+ export * from './getTotalHours'
10
+ export * from './convertToMilitaryTime'
9
11
  export * from './statusOpenStores'
10
12
  export * from './completeSchedules'
11
13
  export * from './useLogout/helpers/BroadcastChannel'
@@ -15,6 +17,8 @@ export * from './useCreateDeliveryTime'
15
17
  export * from './addTenMinutes'
16
18
  export * from './useCategoriesProduct'
17
19
  export * from './useLogout'
20
+ export * from './useSetupSchedule'
21
+ export * from './useMouse'
18
22
  export * from './useStatusOpenStore'
19
23
  export * from './usePushNotificationOrder'
20
24
  export * from './newStoreOrderSubscription'
@@ -0,0 +1,51 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import type { MouseEvent } from "react";
3
+
4
+ export function useMouse<T extends HTMLElement = any>(
5
+ options: { resetOnExit?: boolean } = { resetOnExit: false }
6
+ ) {
7
+ const [position, setPosition] = useState({ x: 0, y: 0 });
8
+
9
+ const ref = useRef<T>();
10
+
11
+ const setMousePosition = (event: MouseEvent<HTMLElement>) => {
12
+ if (ref.current) {
13
+ const rect = event.currentTarget.getBoundingClientRect();
14
+
15
+ const x = Math.max(
16
+ 0,
17
+ Math.round(
18
+ event.pageX - rect.left - (window.pageXOffset || window.scrollX)
19
+ )
20
+ );
21
+
22
+ const y = Math.max(
23
+ 0,
24
+ Math.round(
25
+ event.pageY - rect.top - (window.pageYOffset || window.scrollY)
26
+ )
27
+ );
28
+
29
+ setPosition({ x, y });
30
+ } else {
31
+ setPosition({ x: event.clientX, y: event.clientY });
32
+ }
33
+ };
34
+
35
+ const resetMousePosition = () => setPosition({ x: 0, y: 0 });
36
+
37
+ useEffect(() => {
38
+ const element = ref?.current ? ref.current : document;
39
+ element.addEventListener("mousemove", setMousePosition as any);
40
+ if (options.resetOnExit)
41
+ element.addEventListener("mouseleave", resetMousePosition as any);
42
+
43
+ return () => {
44
+ element.removeEventListener("mousemove", setMousePosition as any);
45
+ if (options.resetOnExit)
46
+ element.removeEventListener("mouseleave", resetMousePosition as any);
47
+ };
48
+ }, [ref.current]);
49
+
50
+ return { ref, ...position };
51
+ }
@@ -51,12 +51,18 @@ export const useSetScheduleOpenAll = () => {
51
51
  return [handleSetStoreSchedule, { loading, error }]
52
52
  }
53
53
 
54
- export const useSchedules = ({ schDay = 1, idStore = '' }) => {
54
+ export const useSchedules = ({ schDay = 1, idStore = '', onCompleted = (data) => { return data } }) => {
55
55
  const {
56
56
  data,
57
57
  loading,
58
58
  error
59
- } = useQuery(GET_SCHEDULE_STORE, { variables: { schDay, idStore } })
59
+ } = useQuery(GET_SCHEDULE_STORE, {
60
+ variables: { schDay, idStore },
61
+ onCompleted: (data) => {
62
+ onCompleted(data)
63
+ }
64
+ }
65
+ )
60
66
 
61
67
  return [data?.getStoreSchedules, { loading, error }]
62
68
  }
@@ -0,0 +1,85 @@
1
+ export const dateEnum = {
2
+ schHoEnd: 'schHoEnd',
3
+ schHoSta: 'schHoSta'
4
+
5
+ }
6
+ export const initialDays = [
7
+ {
8
+ name: 'Lunes',
9
+ day: 1,
10
+ checked: false,
11
+ schHoSta: '00:00',
12
+ schHoEnd: '00:00',
13
+ selected: false,
14
+ loading: false
15
+ },
16
+ {
17
+ name: 'Martes',
18
+ day: 2,
19
+ checked: false,
20
+ schHoSta: '00:00',
21
+ schHoEnd: '00:00',
22
+ selected: false,
23
+ loading: false
24
+ },
25
+ {
26
+ name: 'Miércoles',
27
+ day: 3,
28
+ checked: false,
29
+ schHoSta: '00:00',
30
+ schHoEnd: '00:00',
31
+ selected: false,
32
+ loading: false
33
+ },
34
+ {
35
+ name: 'Jueves',
36
+ day: 4,
37
+ checked: false,
38
+ schHoSta: '00:00',
39
+ schHoEnd: '00:00',
40
+ selected: false,
41
+ loading: false
42
+ },
43
+ {
44
+ name: 'Viernes',
45
+ day: 5,
46
+ checked: false,
47
+ schHoSta: '00:00',
48
+ schHoEnd: '00:00',
49
+ selected: false,
50
+ loading: false
51
+ },
52
+ {
53
+ name: 'Sábado',
54
+ day: 6,
55
+ checked: false,
56
+ schHoSta: '00:00',
57
+ schHoEnd: '00:00',
58
+ selected: false,
59
+ loading: false
60
+ },
61
+ {
62
+ name: 'Domingo',
63
+ day: 0,
64
+ checked: false,
65
+ schHoSta: '00:00',
66
+ schHoEnd: '00:00',
67
+ selected: false,
68
+ loading: false
69
+ }
70
+ ]
71
+
72
+ export const timeSuggestions = [
73
+ '00:00', '00:15', '00:30', '00:45', '01:00', '01:15', '01:30', '01:45',
74
+ '02:00', '02:15', '02:30', '02:45', '03:00', '03:15', '03:30', '03:45',
75
+ '04:00', '04:15', '04:30', '04:45', '05:00', '05:15', '05:30', '05:45',
76
+ '06:00', '06:15', '06:30', '06:45', '07:00', '07:15', '07:30', '07:45',
77
+ '08:00', '08:15', '08:30', '08:45', '09:00', '09:15', '09:30', '09:45',
78
+ '10:00', '10:15', '10:30', '10:45', '11:00', '11:15', '11:30', '11:45',
79
+ '12:00', '12:15', '12:30', '12:45', '13:00', '13:15', '13:30', '13:45',
80
+ '14:00', '14:15', '14:30', '14:45', '15:00', '15:15', '15:30', '15:45',
81
+ '16:00', '16:15', '16:30', '16:45', '17:00', '17:15', '17:30', '17:45',
82
+ '18:00', '18:15', '18:30', '18:45', '19:00', '19:15', '19:30', '19:45',
83
+ '20:00', '20:15', '20:30', '20:45', '21:00', '21:15', '21:30', '21:45',
84
+ '22:00', '22:15', '22:30', '22:45', '23:00', '23:15', '23:30', '23:45'
85
+ ]
@@ -0,0 +1,230 @@
1
+ import { useEffect, useState } from 'react'
2
+ import { dateEnum, initialDays, timeSuggestions } from './helpers'
3
+ import { useCreateSchedules, useSchedules } from '../useSchedule'
4
+ import { days as NameDays } from '../../utils'
5
+ import { convertToMilitaryTime } from '../convertToMilitaryTime'
6
+ import { GET_ONE_SCHEDULE_STORE, GET_SCHEDULE_STORE } from '../useSchedule/queries'
7
+ export * from './helpers/index'
8
+
9
+ export const useSetupSchedule = ({
10
+ idStore = null, sendNotification = (args) => {
11
+ return args
12
+ }
13
+ } = {}) => {
14
+ const [days, setDays] = useState(initialDays)
15
+ const [alertModal, setAlertModal] = useState(false)
16
+ const [selectedDay, setSelectedDay] = useState({})
17
+ const [setStoreSchedule, { loading }] = useCreateSchedules()
18
+
19
+ const onCompleted = (data) => {
20
+ if (Array.isArray(data) && data.length > 0) {
21
+ // Mapeamos los datos recibidos y los convertimos en un objeto con la estructura deseada
22
+ const newSchedules = data.map((day) => ({
23
+ day: day.schDay,
24
+ name: NameDays[day.schDay],
25
+ selected: day.schHoEnd !== '00:00' || day.schHoSta !== '00:00',
26
+ ...day
27
+ }))
28
+
29
+ // Actualizamos el estado days
30
+ setDays(prevDays => {
31
+ // Creamos un nuevo array combinando los elementos existentes en days con los nuevos datos
32
+ const updatedDays = prevDays.map(existingDay => {
33
+ // Buscamos si hay un elemento con el mismo día en los nuevos datos
34
+ const newData = newSchedules.find(newDay => newDay.day === existingDay.day)
35
+ // Si encontramos el día en los nuevos datos, lo fusionamos con el día existente
36
+ if (newData) {
37
+ return { ...existingDay, ...newData }
38
+ }
39
+ // Si no encontramos el día en los nuevos datos, simplemente devolvemos el día existente
40
+ return existingDay
41
+ })
42
+
43
+ // Ahora, buscamos los nuevos días que no están en days y los agregamos al array
44
+ newSchedules.forEach(newDay => {
45
+ if (!updatedDays.some(existingDay => existingDay.day === newDay.day)) {
46
+ updatedDays.push(newDay)
47
+ }
48
+ })
49
+
50
+ // Devolvemos el nuevo array actualizado
51
+ return updatedDays
52
+ })
53
+ }
54
+ }
55
+
56
+ const [data, { loading: lsc }] = useSchedules({ schDay: 1 })
57
+
58
+ const toggleCheck = (index) => {
59
+ const updatedDays = [...days]
60
+ updatedDays[index].checked = !updatedDays[index].checked
61
+ setDays(updatedDays)
62
+ }
63
+
64
+ const duplicateDay = (index) => {
65
+ const selectedDay = days[index]
66
+ // Lógica para desplegar el modal y seleccionar los días donde se replicará
67
+ const selectedDaysToDuplicate = days.filter((day) => day.checked)
68
+ const updatedDays = [...days]
69
+ selectedDaysToDuplicate.forEach((dayToDuplicate) => {
70
+ const duplicatedDay = { ...selectedDay, checked: false }
71
+ updatedDays.push(duplicatedDay)
72
+ })
73
+ setDays(updatedDays)
74
+ }
75
+
76
+ const handleSelectedDay = (day) => {
77
+ const isSaved = days.find((data) => {
78
+ return data.day === day
79
+ })
80
+ if (isSaved?.schId && isSaved?.selected) {
81
+ setSelectedDay(isSaved)
82
+ return setAlertModal(true)
83
+ }
84
+ const updatedDays = days.map((d) => {
85
+ if (d.day === day) {
86
+ return { ...d, selected: !d.selected }
87
+ } else {
88
+ return { ...d }
89
+ }
90
+ })
91
+ setDays(updatedDays)
92
+ }
93
+ const selectedDays = days?.filter((day) => Boolean(day.selected)).map((day) => {
94
+ return day.day
95
+ })
96
+
97
+ const onChangeSaveHour = async ({ time, name, day }) => {
98
+ setDays(prevDays => {
99
+ const updatedDays = prevDays.map((d) => {
100
+ if (d.day === day) {
101
+ return { ...d, [name]: time, loading: Boolean(name === dateEnum.schHoEnd) }
102
+ } else {
103
+ return { ...d }
104
+ }
105
+ })
106
+
107
+ if (name === dateEnum.schHoEnd) {
108
+ const findHour = updatedDays.find((d) => {
109
+ return d.day === day
110
+ })
111
+ const schHoSta = findHour?.schHoSta
112
+ const schHoEnd = findHour?.schHoEnd
113
+ const startHour = convertToMilitaryTime(schHoSta)
114
+ const endHour = convertToMilitaryTime(schHoEnd)
115
+ // Comparar solo las horas y minutos
116
+ if (startHour === endHour) {
117
+ // eslint-disable-next-line consistent-return
118
+ sendNotification({
119
+ description: 'Error, la hora de salida no debe ser igual a la hora final',
120
+ title: 'Error',
121
+ backgroundColor: 'error'
122
+ })
123
+ }
124
+ if (startHour > endHour) {
125
+ // eslint-disable-next-line consistent-return
126
+ sendNotification({
127
+ description: 'Error, la hora de salida debe ser mayor que la de entrada',
128
+ title: 'Error',
129
+ backgroundColor: 'error'
130
+ })
131
+ }
132
+ if (startHour !== endHour && startHour < endHour) {
133
+ setStoreSchedule({
134
+ variables: {
135
+ input: {
136
+ schHoSta: schHoSta ?? '00:00',
137
+ schHoEnd: schHoEnd ?? '00:00',
138
+ schState: 1,
139
+ schDay: day
140
+ }
141
+ },
142
+ update (cache) {
143
+ cache.modify({
144
+ fields: {
145
+ getStoreSchedules (dataOld = []) {
146
+ return cache.writeQuery({ query: GET_SCHEDULE_STORE, data: dataOld })
147
+ },
148
+ getOneStoreSchedules (dataOld = []) {
149
+ return cache.writeQuery({ query: GET_ONE_SCHEDULE_STORE, data: dataOld })
150
+ }
151
+ }
152
+ })
153
+ }
154
+ })
155
+ }
156
+ const newUpdatedDays = updatedDays.map((d) => {
157
+ if (d.day === day) {
158
+ return { ...d, loading: false }
159
+ } else {
160
+ return { ...d }
161
+ }
162
+ })
163
+ // Devolver el nuevo estado actualizado
164
+ return newUpdatedDays
165
+ }
166
+ // Devolver el nuevo estado
167
+ return updatedDays
168
+ })
169
+ }
170
+
171
+ const handleDeleteSchedule = async (day) => {
172
+ await setStoreSchedule({
173
+ variables: {
174
+ input: {
175
+ schHoSta: '00:00',
176
+ schHoEnd: '00:00',
177
+ schState: 1,
178
+ schDay: day,
179
+ idStore
180
+ }
181
+ },
182
+ update (cache) {
183
+ cache.modify({
184
+ fields: {
185
+ getStoreSchedules (dataOld = []) {
186
+ return cache.writeQuery({ query: GET_SCHEDULE_STORE, data: dataOld })
187
+ },
188
+ getOneStoreSchedules (dataOld = []) {
189
+ return cache.writeQuery({ query: GET_ONE_SCHEDULE_STORE, data: dataOld })
190
+ }
191
+ }
192
+ })
193
+ }
194
+ })
195
+ setDays(prevDays => {
196
+ const updatedDays = prevDays.map((d) => {
197
+ if (d.day === day) {
198
+ return {
199
+ ...d,
200
+ [dateEnum.schHoEnd]: dateEnum.schHoEnd,
201
+ [dateEnum.schHoSta]: dateEnum.schHoSta,
202
+ selected: false
203
+ }
204
+ } else {
205
+ return { ...d }
206
+ }
207
+ })
208
+ return updatedDays
209
+ })
210
+ setAlertModal(false)
211
+ }
212
+ useEffect(() => {
213
+ onCompleted(data)
214
+ }, [data])
215
+
216
+ return {
217
+ duplicateDay,
218
+ alertModal,
219
+ toggleCheck,
220
+ handleSelectedDay,
221
+ onChangeSaveHour,
222
+ handleDeleteSchedule,
223
+ setAlertModal,
224
+ days,
225
+ selectedDays,
226
+ selectedDay,
227
+ loading: lsc || loading,
228
+ times: timeSuggestions
229
+ }
230
+ }
@@ -164,6 +164,16 @@ export const SPANISH_MONTHS = {
164
164
  11: 'Diciembre'
165
165
  }
166
166
 
167
+ export const days = {
168
+ 1: 'Lunes',
169
+ 2: 'Martes',
170
+ 3: 'Miércoles',
171
+ 4: 'Jueves',
172
+ 5: 'Viernes',
173
+ 6: 'Sábado',
174
+ 0: 'Domingo'
175
+ }
176
+
167
177
  export const convertBase64 = file => {
168
178
  return new Promise((resolve, reject) => {
169
179
  const reader = new FileReader()