-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1424 from beckn/feature/common-utils
feat: common utils
- Loading branch information
Showing
10 changed files
with
852 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { areObjectPropertiesEqual } from './general' | ||
import { ShippingFormInitialValuesType } from '@beckn-ui/becknified-components' | ||
import { CartRetailItem, InitResponseModel, StatusResponseModel } from '../../lib/types' | ||
|
||
export const extractAddressComponents = (result: google.maps.GeocoderResult) => { | ||
let country = 'IN', | ||
state = 'Karnataka', | ||
city = 'Bengaluru' | ||
|
||
for (const component of result.address_components) { | ||
if (component.types.includes('country')) { | ||
country = component.short_name | ||
} else if (component.types.includes('administrative_area_level_1')) { | ||
state = component.long_name | ||
} else if (component.types.includes('locality')) { | ||
city = component.long_name | ||
} | ||
} | ||
return { country, state, city } | ||
} | ||
|
||
export const geocodeFromPincode = async (pincode: string) => { | ||
const geocoder = new window.google.maps.Geocoder() | ||
try { | ||
const response = await geocoder.geocode({ address: pincode }) | ||
console.log('respnse from the map', response) | ||
if (response.results.length > 0) { | ||
const { country, state, city } = extractAddressComponents( | ||
response.results[1] ? response.results[1] : response.results[0] | ||
) | ||
const lat = response.results[0].geometry.location.lat() | ||
const lng = response.results[0].geometry.location.lng() | ||
return { country, state, city, lat, lng } | ||
} else { | ||
console.log('No results found') | ||
return { country: '', state: '', city: '' } | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
return { country: '', state: '', city: '' } | ||
} | ||
} | ||
|
||
export const getPaymentBreakDown = (initData: InitResponseModel[] | StatusResponseModel[]) => { | ||
const quote = initData[0].message.order.quote | ||
const breakUp = quote.breakup | ||
const totalPricewithCurrent = { | ||
currency: quote.price.currency, | ||
value: quote.price.value | ||
} | ||
|
||
const breakUpMap: Record<string, any> = {} | ||
|
||
breakUp.forEach(item => { | ||
const { | ||
title, | ||
price: { currency, value } | ||
} = item | ||
|
||
breakUpMap[title] = { | ||
currency: currency, | ||
value: value | ||
} | ||
}) | ||
|
||
return { breakUpMap, totalPricewithCurrent } | ||
} | ||
|
||
export const getSubTotalAndDeliveryCharges = (initData: InitResponseModel[]) => { | ||
let subTotal: number | string = 0 | ||
let currencySymbol | ||
|
||
if (initData && initData.length > 0) { | ||
initData.forEach(data => { | ||
subTotal = parseFloat(data.message.order.quote.price.value).toFixed(2) | ||
|
||
currencySymbol = data.message.order.quote.price.currency | ||
}) | ||
} | ||
|
||
return { subTotal, currencySymbol } | ||
} | ||
|
||
export const getTotalCartItems = (cartItems: CartRetailItem[]) => { | ||
let quantity = 0 | ||
|
||
cartItems.forEach(item => { | ||
quantity += item.quantity | ||
}) | ||
|
||
return quantity | ||
} | ||
|
||
export const areShippingAndBillingDetailsSame = ( | ||
isBillingAddressComplete: boolean, | ||
formData: ShippingFormInitialValuesType, | ||
billingFormData: ShippingFormInitialValuesType | ||
) => { | ||
if (isBillingAddressComplete) { | ||
return areObjectPropertiesEqual(formData, billingFormData) | ||
} | ||
return !isBillingAddressComplete | ||
} |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export const getOrderPlacementTimeline = (timeStamp: string) => { | ||
const localDateAndTime = new Date(timeStamp) | ||
const localTime = localDateAndTime.toLocaleTimeString() | ||
const localDate = localDateAndTime.toDateString() | ||
const localDateWithoutDay = localDate.split(' ').slice(1).join(' ') | ||
|
||
return `${localDateWithoutDay}, ${localTime}` | ||
} | ||
|
||
export function convertTimestampToDdMmYyyyHhMmPM(timestamp: string) { | ||
const date = new Date(timestamp) | ||
|
||
const day = date.getDate() | ||
const month = date.getMonth() + 1 | ||
const year = date.getFullYear() | ||
|
||
let hour = date.getHours() | ||
const minute = date.getMinutes() | ||
const second = date.getSeconds() | ||
|
||
const ampm = hour >= 12 ? 'pm' : 'am' | ||
hour = hour % 12 | ||
if (hour === 0) { | ||
hour = 12 | ||
} | ||
|
||
const formattedTimestamp = `${day}/${month}/${year}, ${hour}:${minute} ${ampm}` | ||
|
||
return formattedTimestamp | ||
} | ||
|
||
function getOrdinalSuffix(day: number) { | ||
if (day >= 11 && day <= 13) { | ||
return 'th' | ||
} | ||
switch (day % 10) { | ||
case 1: | ||
return 'st' | ||
case 2: | ||
return 'nd' | ||
case 3: | ||
return 'rd' | ||
default: | ||
return 'th' | ||
} | ||
} | ||
|
||
export function formatTimestamp(timestamp: string) { | ||
const date = new Date(timestamp) | ||
|
||
const day = date.getDate() | ||
const month = date.toLocaleString('default', { month: 'short' }) | ||
const year = date.getFullYear() | ||
const hours = date.getHours() % 12 || 12 | ||
const minutes = date.getMinutes().toString().padStart(2, '0') | ||
const period = date.getHours() < 12 ? 'am' : 'pm' | ||
|
||
const ordinalSuffix = getOrdinalSuffix(day) | ||
|
||
const formattedDate = `${day}${ordinalSuffix} ${month} ${year}, ${hours}:${minutes}${period}` | ||
|
||
return formattedDate | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Rial currency format | ||
export const irrCurrencyFormat = (price: number | undefined) => { | ||
return price ? new Intl.NumberFormat('fa-IR').format(price) : null | ||
} | ||
|
||
//pound currency format | ||
export const gbpCurrencyFormat = (price: number | undefined) => { | ||
return price ? new Intl.NumberFormat('en-GB').format(price) : null | ||
} |
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { FormErrors, SignInProps, SignUpProps, ProfileProps } from '@beckn-ui/common/lib/types' | ||
import { ShippingFormData } from '../../lib/types' | ||
|
||
export const validateForm = (formData: ShippingFormData): FormErrors => { | ||
const errors: FormErrors = {} | ||
|
||
if (formData.name.trim() === '') { | ||
errors.name = 'errorName' | ||
} | ||
|
||
if (formData.mobileNumber.trim() === '') { | ||
errors.mobileNumber = 'errorNumber' | ||
} else if (!/^\d{10}$/.test(formData.mobileNumber)) { | ||
errors.mobileNumber = 'errorNumber2' | ||
} | ||
|
||
if (formData.email.trim() === '') { | ||
errors.email = 'errorEmail' | ||
} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formData.email)) { | ||
errors.email = 'errorEmail2' | ||
} | ||
|
||
if (formData.address.trim() === '') { | ||
errors.address = 'errorAddress' | ||
} | ||
|
||
if (formData.zipCode.trim() === '') { | ||
errors.zipCode = 'errorZipcode' | ||
} else if (!/^\d{6}$/.test(formData.zipCode)) { | ||
errors.zipCode = 'errorZipcode2' | ||
} | ||
|
||
return errors | ||
} | ||
|
||
export const signInValidateForm = (formData: SignInProps): FormErrors => { | ||
const errors: FormErrors = {} | ||
|
||
if (formData.email.trim() === '') { | ||
errors.email = 'errorEmail' | ||
} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formData.email)) { | ||
errors.email = 'errorEmail' | ||
} | ||
if (formData.password.trim() === '') { | ||
errors.password = 'errorPassword' | ||
} else if (formData.password.length < 8) { | ||
errors.password = 'errorPassword2' | ||
} else if (!/[A-Z]/.test(formData.password)) { | ||
errors.password = 'errorPassword3' | ||
} else if (!/[$&+,:;=?@#|'<>.^*()%!-]/.test(formData.password)) { | ||
errors.password = 'errorPassword4' | ||
} else if (/^\d+$/.test(formData.password)) { | ||
errors.password = 'errorPassword5' | ||
} else if (!/[0-9]/.test(formData.password)) { | ||
errors.password = 'errorPassword6' | ||
} | ||
|
||
return errors | ||
} | ||
export const signUpValidateForm = (formData: SignUpProps): FormErrors => { | ||
const errors: FormErrors = {} | ||
|
||
if (formData.name.trim() === '') { | ||
errors.name = 'errorName' | ||
} else if (!/^[A-Za-z\s]*$/.test(formData.name)) { | ||
errors.name = 'errorName2' | ||
} else if (formData.name.length < 3) { | ||
errors.name = 'errorName3' | ||
} | ||
|
||
if (formData.email.trim() === '') { | ||
errors.email = 'errorEmail' | ||
} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formData.email)) { | ||
errors.email = 'errorEmail' | ||
} | ||
if (formData.password.trim() === '') { | ||
errors.password = 'errorPassword' | ||
} else if (formData.password.length < 8) { | ||
errors.password = 'errorPassword2' | ||
} else if (/^\d+$/.test(formData.password)) { | ||
errors.password = 'errorPassword3' | ||
} else if (!/[A-Z]/.test(formData.password)) { | ||
errors.password = 'errorPassword4' | ||
} else if (!/[$&+,:;=?@#|'<>.^*()%!-]/.test(formData.password)) { | ||
errors.password = 'errorPassword5' | ||
} else if (!/[0-9]/.test(formData.password)) { | ||
errors.password = 'errorPassword6' | ||
} | ||
if (formData.mobileNumber.trim() === '') { | ||
errors.mobileNumber = 'errorNumber' | ||
} else if (!/^\d{10}$/.test(formData.mobileNumber)) { | ||
errors.mobileNumber = 'errorNumber2' | ||
} | ||
return errors | ||
} | ||
export const profileValidateForm = (formData: ProfileProps): FormErrors => { | ||
const errors: FormErrors = {} | ||
|
||
if (formData.name.trim() === '') { | ||
errors.name = 'errorName' | ||
} else if (!/^[A-Za-z\s]*$/.test(formData.name)) { | ||
errors.name = 'errorName2' | ||
} else if (formData.name.length < 3) { | ||
errors.name = 'errorName3' | ||
} | ||
|
||
if (formData.mobileNumber.trim() === '') { | ||
errors.mobileNumber = 'errorNumber' | ||
} else if (!/^\d{10}$/.test(formData.mobileNumber)) { | ||
errors.mobileNumber = 'errorNumber2' | ||
} | ||
if (formData.zipCode.trim() === '') { | ||
errors.zipCode = 'errorZipcode' | ||
} else if (!/^\d{6}$/.test(formData.zipCode)) { | ||
errors.zipCode = 'errorZipcode2' | ||
} | ||
if (formData.country.trim() === '') { | ||
errors.country = 'errorCountry' | ||
} else if (!/^[A-Za-z\s]*$/.test(formData.country)) { | ||
errors.country = 'errorCountry1' | ||
} | ||
if (formData.state.trim() === '') { | ||
errors.state = 'errorState' | ||
} else if (!/^[A-Za-z\s]*$/.test(formData.state)) { | ||
errors.state = 'errorState1' | ||
} | ||
if (formData.city.trim() === '') { | ||
errors.city = 'errorCity' | ||
} else if (!/^[A-Za-z\s]*$/.test(formData.city)) { | ||
errors.city = 'errorCity1' | ||
} | ||
return errors | ||
} |
Oops, something went wrong.