Skip to content

Commit

Permalink
Merge pull request #37 from vtex-apps/feat/translate-estimate-string
Browse files Browse the repository at this point in the history
Add getEstimateTranslation utility function
  • Loading branch information
lucasecdb authored Jun 23, 2021
2 parents 2ece4a6 + f55deb3 commit 03410b3
Show file tree
Hide file tree
Showing 12 changed files with 1,849 additions and 567 deletions.
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
react/node_modules/*
react/coverage/*
node_modules/
coverage/
*.snap.ts
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "vtex",
"root": true,
"env": {
"node": true
}
}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ docs/_book/
npm-debug.log
.build/
lib
.eslintrc
7 changes: 1 addition & 6 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"jsxBracketSameLine": true
}
"@vtex/prettier-config"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Utility `getEstimateTranslation` function.

## [1.1.6] - 2020-12-17

### Added
Expand Down
7 changes: 3 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
},
"mustUpdateAt": "2019-12-19",
"categories": [],
"registries": [
"smartcheckout"
],
"registries": ["smartcheckout"],
"settingsSchema": {},
"scripts": {
"postreleasy": "vtex publish --verbose"
}
},
"$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema"
}
56 changes: 35 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
{
"version": "1.1.4",
"name": "shipping-estimate-translator",
"private": true,
"license": "UNLICENSED",
"scripts": {
"lint": "eslint react/**/*.js",
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,json}\"",
"lint:locales": "intl-equalizer",
"test": "jest",
"test:watch": "jest --watch"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "yarn run lint && yarn run test && yarn run lint:locales"
}
},
"lint-staged": {
"*.{ts,js,tsx,jsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,graphql,gql}": [
"prettier --write"
]
},
"devDependencies": {
"@vtex/intl-equalizer": "^2.0.1",
"@vtex/prettier-config": "^0.1.4",
"babel-core": "^6.26.3",
"babel-eslint": "7",
"babel-jest": "^23.6.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "^3.1.1",
"eslint": "^5.15.3",
"eslint-config-prettier": "^3.3.0",
"eslint-config-vtex": "^8.0.0",
"eslint-config-vtex-react": "^3.0.3",
"eslint-plugin-import": "^2.14.0",
"eslint": "^7.12.1",
"eslint-config-vtex": "^12.3.2",
"eslint-config-vtex-react": "^8.0.0",
"eslint-plugin-lodash": "^5.1.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^1.3.0",
"husky": "^4.2.3",
"jest": "^22.0.4",
"prettier": "^1.15.3",
"lint-staged": "^10.1.1",
"prettier": "^2.0.2",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-intl": "^2.7.2",
"react-testing-library": "^5.4.2",
"regenerator-runtime": "^0.13.1"
},
"scripts": {
"lint": "eslint react/**/*.js",
"lint:locales": "intl-equalizer",
"test": "jest",
"test:watch": "jest --watch"
},
"husky": {
"hooks": {
"pre-push": "yarn run lint && yarn run test && yarn run lint:locales"
}
"regenerator-runtime": "^0.13.1",
"typescript": "^3.8.3"
},
"version": "1.1.4",
"jest": {
"collectCoverageFrom": [
"react/**/*.{js,jsx}"
Expand Down
6 changes: 6 additions & 0 deletions react/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "vtex-react",
"env": {
"browser": true
}
}
78 changes: 9 additions & 69 deletions react/TranslateEstimate.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,8 @@
import PropTypes from 'prop-types'
import { injectIntl, intlShape } from 'react-intl'
import { deliveryWindowShape } from './shapes'

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,
}
}
import { getEstimateTranslation } from './getEstimateTranslation'
import { deliveryWindowShape } from './shapes'

const TranslateEstimate = ({
intl,
Expand All @@ -48,36 +11,13 @@ const TranslateEstimate = ({
shippingEstimate,
scheduled,
}) => {
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
return getEstimateTranslation({
intl,
isPickup,
lowerCase,
shippingEstimate,
scheduled,
})
}

TranslateEstimate.defaultProps = {
Expand Down
11 changes: 6 additions & 5 deletions react/TranslateEstimate.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'

import TranslateEstimate from './TranslateEstimate'
import { renderWithIntl, cleanup } from '../test-utils' // eslint-disable-line import/named

Expand Down Expand Up @@ -121,7 +122,7 @@ describe('TranslateEstimate - Valid Values', () => {
<TranslateEstimate shippingEstimate="0d" />
)

const expectedLabel = 'Same day'
const expectedLabel = 'Today'

expect(getByText(expectedLabel).textContent).toBe(expectedLabel)
})
Expand Down Expand Up @@ -155,7 +156,7 @@ describe('TranslateEstimate - Valid Values', () => {
<TranslateEstimate shippingEstimate="0bd" />
)

const expectedLabel = 'Same day'
const expectedLabel = 'Today'

expect(getByText(expectedLabel).textContent).toBe(expectedLabel)
})
Expand Down Expand Up @@ -257,7 +258,7 @@ describe('TranslateEstimate - Valid Values', () => {
<TranslateEstimate shippingEstimate="0d" isPickup />
)

const expectedLabel = 'Ready in the same day'
const expectedLabel = 'Ready today'

expect(getByText(expectedLabel).textContent).toBe(expectedLabel)
})
Expand Down Expand Up @@ -291,7 +292,7 @@ describe('TranslateEstimate - Valid Values', () => {
<TranslateEstimate shippingEstimate="0bd" isPickup />
)

const expectedLabel = 'Ready in the same day'
const expectedLabel = 'Ready today'

expect(getByText(expectedLabel).textContent).toBe(expectedLabel)
})
Expand Down Expand Up @@ -336,7 +337,7 @@ describe('TranslateEstimate - Valid Values', () => {
})
})

describe('Scheduled delivery/pickup', () => {
describe('Scheduled delivery', () => {
afterEach(cleanup)

it('should return correct for a scheduled delivery with no delivery window', () => {
Expand Down
80 changes: 80 additions & 0 deletions react/getEstimateTranslation.js
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 }
Loading

0 comments on commit 03410b3

Please sign in to comment.