-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
import { useLocalStorage } from 'usehooks-ts'; | ||
|
||
export interface DefaultOfferExpiry { | ||
enabled: boolean; | ||
days: string; | ||
hours: string; | ||
minutes: string; | ||
enabled: boolean; | ||
days: string; | ||
hours: string; | ||
minutes: string; | ||
} | ||
|
||
const validateNumber = (value: string): string => { | ||
if (!value) return ''; | ||
const num = parseInt(value); | ||
if (isNaN(num) || num < 0) return ''; | ||
return num.toString(); | ||
if (!value) return ''; | ||
const num = parseInt(value); | ||
if (isNaN(num) || num < 0) return ''; | ||
return num.toString(); | ||
}; | ||
|
||
const validateExpiry = (expiry: DefaultOfferExpiry): DefaultOfferExpiry => { | ||
return { | ||
enabled: expiry.enabled, | ||
days: validateNumber(expiry.days), | ||
hours: validateNumber(expiry.hours), | ||
minutes: validateNumber(expiry.minutes) | ||
}; | ||
return { | ||
enabled: expiry.enabled, | ||
days: validateNumber(expiry.days), | ||
hours: validateNumber(expiry.hours), | ||
minutes: validateNumber(expiry.minutes), | ||
}; | ||
}; | ||
|
||
export function useDefaultOfferExpiry() { | ||
const [expiry, setExpiry] = useLocalStorage<DefaultOfferExpiry>( | ||
'default-offer-expiry', | ||
{ | ||
enabled: false, | ||
days: '1', | ||
hours: '', | ||
minutes: '', | ||
} | ||
); | ||
|
||
// Ensure stored values are valid on load | ||
const validatedExpiry = validateExpiry(expiry); | ||
|
||
const setValidatedExpiry = (newExpiry: DefaultOfferExpiry) => { | ||
setExpiry(validateExpiry(newExpiry)); | ||
}; | ||
|
||
// Calculate total seconds | ||
const getTotalSeconds = (): number | null => { | ||
if (!validatedExpiry.enabled) return null; | ||
|
||
// the || operates on the result of parseInt, so if parseInt returns NaN, | ||
// it will return 1 etc. because NaN is falsy | ||
const days = parseInt(validatedExpiry.days) || 1; | ||
const hours = parseInt(validatedExpiry.hours) || 0; | ||
const minutes = parseInt(validatedExpiry.minutes) || 0; | ||
|
||
return days * 24 * 60 * 60 + hours * 60 * 60 + minutes * 60; | ||
}; | ||
|
||
return { | ||
expiry: validatedExpiry, | ||
setExpiry: setValidatedExpiry, | ||
getTotalSeconds | ||
}; | ||
} | ||
const [expiry, setExpiry] = useLocalStorage<DefaultOfferExpiry>( | ||
'default-offer-expiry', | ||
{ | ||
enabled: false, | ||
days: '1', | ||
hours: '', | ||
minutes: '', | ||
}, | ||
); | ||
|
||
// Ensure stored values are valid on load | ||
const validatedExpiry = validateExpiry(expiry); | ||
|
||
const setValidatedExpiry = (newExpiry: DefaultOfferExpiry) => { | ||
setExpiry(validateExpiry(newExpiry)); | ||
}; | ||
|
||
// Calculate total seconds | ||
const getTotalSeconds = (): number | null => { | ||
if (!validatedExpiry.enabled) return null; | ||
|
||
// the || operates on the result of parseInt, so if parseInt returns NaN, | ||
// it will return 1 etc. because NaN is falsy | ||
const days = parseInt(validatedExpiry.days) || 1; | ||
const hours = parseInt(validatedExpiry.hours) || 0; | ||
const minutes = parseInt(validatedExpiry.minutes) || 0; | ||
|
||
return days * 24 * 60 * 60 + hours * 60 * 60 + minutes * 60; | ||
}; | ||
|
||
return { | ||
expiry: validatedExpiry, | ||
setExpiry: setValidatedExpiry, | ||
getTotalSeconds, | ||
}; | ||
} |