npm-pkg-hook 1.5.0 → 1.5.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/package.json CHANGED
@@ -43,5 +43,5 @@
43
43
  "rm": "rm -rf node_modules package-lock.json && npm i",
44
44
  "test": "echo \"Error: no test specified\" && exit 1"
45
45
  },
46
- "version": "1.5.0"
46
+ "version": "1.5.1"
47
47
  }
@@ -0,0 +1,29 @@
1
+ export function filterAndSortByDate (array = []) {
2
+ const isError = !Array.isArray(array) || !array.length
3
+ try {
4
+ if (isError) return []
5
+
6
+ const currentDate = new Date()
7
+ const sevenDaysAgo = currentDate.getTime() - 7 * 24 * 60 * 60 * 1000 // Calculating timestamp for 7 days ago
8
+
9
+ const filteredAndSorted = array.map(item => {
10
+ const createdAtDate = new Date(item.createdAt)
11
+ const isNew = createdAtDate.getTime() >= sevenDaysAgo
12
+ return { ...item, isNew }
13
+ }).sort((a, b) => {
14
+ // Ordenar primero por 'open' en 1 y luego por 'createdAt'
15
+ if (a.open !== b.open) {
16
+ return b.open - a.open // Orden descendente para 'open' en 1 primero
17
+ } else {
18
+ const dateA = new Date(a.createdAt).getTime()
19
+ const dateB = new Date(b.createdAt).getTime()
20
+ return dateA - dateB
21
+ }
22
+ })
23
+
24
+ return filteredAndSorted
25
+ } catch (error) {
26
+ if (isError) return []
27
+ if (Array.isArray(array)) return array
28
+ }
29
+ }
@@ -1,19 +1,61 @@
1
- import { useQuery } from '@apollo/client'
1
+ import { useEffect, useState } from 'react'
2
+ import { useLazyQuery } from '@apollo/client'
2
3
  import { GET_ALL_RESTAURANT } from './queries'
4
+ import { filterAndSortByDate } from './helpers'
5
+ import { useManageQueryParams } from '../useManageQueryParams'
3
6
 
4
- export const useRestaurant = () => {
5
- const {
7
+ export const useRestaurant = ({
8
+ location = {
9
+ pathname: ''
10
+ }
11
+ } = {}) => {
12
+ const [loadingFilter, setLoadingFilter] = useState(false)
13
+ const { handleQuery, handleCleanQuery } = useManageQueryParams()
14
+
15
+ const [getAllStoreInStore, {
6
16
  data,
7
17
  loading,
8
18
  error,
9
19
  fetchMore
10
- } = useQuery(GET_ALL_RESTAURANT, {
20
+ }] = useLazyQuery(GET_ALL_RESTAURANT, {
11
21
  fetchPolicy: 'cache-and-network',
12
22
  notifyOnNetworkStatusChange: true,
13
23
  nextFetchPolicy: 'cache-first',
14
24
  refetchWritePolicy: 'merge',
15
25
  context: { clientName: 'admin-store' }
16
26
  })
17
- const dataRestaurant = data?.getAllStoreInStore
18
- return [dataRestaurant, { loading, error, fetchMore }]
27
+
28
+ useEffect(() => {
29
+ getAllStoreInStore({})
30
+ }, [location])
31
+ const handleSendQueries = (name, value) => {
32
+ if (value) handleQuery(name, value)
33
+ }
34
+
35
+ const handleCleanQueries = (name) => {
36
+ handleCleanQuery(name)
37
+ }
38
+ const handleFilterStore = async () => {
39
+ setLoadingFilter(true)
40
+ try {
41
+ getAllStoreInStore({
42
+
43
+ }).then(() => {
44
+ setLoadingFilter(false)
45
+ })
46
+ } catch (error) {
47
+ setLoadingFilter(false)
48
+ }
49
+ }
50
+ const dataRestaurant = data?.getAllStoreInStore || []
51
+ const dataSort = filterAndSortByDate(dataRestaurant)
52
+ return [dataSort, {
53
+ loading,
54
+ loadingFilter,
55
+ error,
56
+ fetchMore,
57
+ handleSendQueries,
58
+ handleFilterStore,
59
+ handleCleanQueries
60
+ }]
19
61
  }
@@ -75,6 +75,5 @@ query getAllStoreInStore($search: String, $min: Int, $max: Int){
75
75
  createAt
76
76
  }
77
77
  }
78
-
79
78
  }
80
- `
79
+ `