npm-pkg-hook 1.7.9 → 1.8.0

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.9"
47
+ "version": "1.8.0"
48
48
  }
@@ -7,6 +7,7 @@ export * from './useCatWithProduct'
7
7
  export * from './useManageQueryParams'
8
8
  export * from './useDeliveryTime'
9
9
  export * from './getTotalHours'
10
+ export * from './useFreeSubscriptionValidation'
10
11
  export * from './convertToMilitaryTime'
11
12
  export * from './statusOpenStores'
12
13
  export * from './completeSchedules'
@@ -0,0 +1,41 @@
1
+ import { useEffect, useState } from 'react'
2
+ import { gql, useQuery } from '@apollo/client'
3
+
4
+ const VALIDATE_SUBSCRIPTION_QUERY = gql`
5
+ query validateFreeSubscription($idStore: ID) {
6
+ validateFreeSubscription(idStore: $idStore) {
7
+ subscriptionId
8
+ status
9
+ businessName
10
+ currentPeriodEnd
11
+ currentPeriodStart
12
+ }
13
+ }
14
+ `
15
+
16
+ export const useFreeSubscriptionValidation = (idStore) => {
17
+ const { loading, error, data } = useQuery(VALIDATE_SUBSCRIPTION_QUERY, {
18
+ variables: { idStore }
19
+ })
20
+ const [daysRemaining, setDaysRemaining] = useState(null)
21
+ const [daysElapsed, setDaysElapsed] = useState(null)
22
+
23
+ useEffect(() => {
24
+ if (data && data.validateFreeSubscription) {
25
+ const { currentPeriodEnd, currentPeriodStart } = data.validateFreeSubscription
26
+ const currentDate = new Date()
27
+ const endOfPeriod = new Date(parseInt(currentPeriodEnd))
28
+ const startOfPeriod = new Date(parseInt(currentPeriodStart))
29
+
30
+ const differenceInTimeUntilEnd = endOfPeriod.getTime() - currentDate.getTime()
31
+ const differenceInDaysUntilEnd = Math.ceil(differenceInTimeUntilEnd / (1000 * 3600 * 24))
32
+ setDaysRemaining(isNaN(differenceInDaysUntilEnd) ? 0 : differenceInDaysUntilEnd)
33
+
34
+ const differenceInTimeSinceStart = currentDate.getTime() - startOfPeriod.getTime()
35
+ const differenceInDaysSinceStart = Math.ceil(differenceInTimeSinceStart / (1000 * 3600 * 24))
36
+ setDaysElapsed(isNaN(differenceInDaysSinceStart) ? 0 : differenceInDaysSinceStart)
37
+ }
38
+ }, [data])
39
+
40
+ return { loading, error, subscriptionData: data?.validateFreeSubscription, daysRemaining, daysElapsed }
41
+ }