-
Notifications
You must be signed in to change notification settings - Fork 2
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 #37 from vtex-apps/feat/translate-estimate-string
Add getEstimateTranslation utility function
- Loading branch information
Showing
12 changed files
with
1,849 additions
and
567 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,2 +1,3 @@ | ||
react/node_modules/* | ||
react/coverage/* | ||
node_modules/ | ||
coverage/ | ||
*.snap.ts |
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,7 @@ | ||
{ | ||
"extends": "vtex", | ||
"root": true, | ||
"env": { | ||
"node": true | ||
} | ||
} |
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 |
---|---|---|
|
@@ -33,4 +33,3 @@ docs/_book/ | |
npm-debug.log | ||
.build/ | ||
lib | ||
.eslintrc |
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,6 +1 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"jsxBracketSameLine": true | ||
} | ||
"@vtex/prettier-config" |
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
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,6 @@ | ||
{ | ||
"extends": "vtex-react", | ||
"env": { | ||
"browser": true | ||
} | ||
} |
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
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,80 @@ | ||
const getTranslateId = (shippingEstimate, isPickup) => { | ||
const shippingEstimateString = | ||
shippingEstimate && shippingEstimate.split(/[0-9]+/)[1] | ||
|
||
return ( | ||
shippingEstimate && | ||
shippingEstimateString && | ||
`shippingEstimate${isPickup ? 'Pickup' : ''}-${ | ||
shippingEstimate.split(/[0-9]+/)[1] | ||
}` | ||
) | ||
} | ||
|
||
const getTimeAmount = shippingEstimate => { | ||
return shippingEstimate && shippingEstimate.split(/\D+/)[0] | ||
} | ||
|
||
const getScheduledWindow = (scheduled, intl) => { | ||
return scheduled.startDateUtc && scheduled.endDateUtc | ||
? { | ||
date: intl.formatDate(scheduled.startDateUtc, { | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
}), | ||
startDate: intl.formatTime(scheduled.startDateUtc, { | ||
timeZone: 'UTC', | ||
}), | ||
endDate: intl.formatTime(scheduled.endDateUtc, { | ||
timeZone: 'UTC', | ||
}), | ||
} | ||
: { | ||
date: null, | ||
startDate: null, | ||
endDate: null, | ||
} | ||
} | ||
|
||
export function getEstimateTranslation({ | ||
intl, | ||
shippingEstimate, | ||
isPickup = false, | ||
lowerCase = false, | ||
scheduled = false, | ||
}) { | ||
if (scheduled) { | ||
const { date, startDate, endDate } = getScheduledWindow(scheduled, intl) | ||
const hasDeliveryWindow = !!(startDate && endDate) | ||
const translatedEstimate = hasDeliveryWindow | ||
? intl.formatMessage( | ||
{ | ||
id: 'shippingEstimate-scheduled', | ||
}, | ||
{ date, startDate, endDate } | ||
) | ||
: intl.formatMessage({ | ||
id: 'shippingEstimate-scheduled-no-dates', | ||
}) | ||
|
||
return lowerCase ? translatedEstimate.toLowerCase() : translatedEstimate | ||
} | ||
|
||
const id = getTranslateId(shippingEstimate, isPickup) | ||
const timeAmount = getTimeAmount(shippingEstimate) | ||
|
||
let translatedEstimate = '' | ||
|
||
if (id && timeAmount && intl) { | ||
translatedEstimate = intl.formatMessage({ id }, { timeAmount }) | ||
} | ||
|
||
if (lowerCase) { | ||
translatedEstimate = translatedEstimate.toLowerCase() | ||
} | ||
|
||
return translatedEstimate | ||
} | ||
|
||
export default { getEstimateTranslation } |
Oops, something went wrong.