Skip to content

Commit

Permalink
Merge pull request #16 from vtex-apps/fix/xVtexTenant
Browse files Browse the repository at this point in the history
Use defaultLocale as xVtexTenant
  • Loading branch information
cesarocampov authored Oct 5, 2021
2 parents 3e7dae7 + 94ae856 commit 1b2b316
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Bulk Upload of product translations

### Fixed
- Use `defaultLocale` from tenant API as `xVtexTenant`.

## [1.1.0] - 2021-08-02

Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.ts",
"license": "UNLICENSED",
"dependencies": {
"@vtex/api": "6.44.0",
"@vtex/api": "6.45.4",
"JSONStream": "^1.3.5"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@
"@types/mime" "^1"
"@types/node" "*"

"@vtex/api@6.44.0":
version "6.44.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.44.0.tgz#8dc2a27c37635278117310bcc5cd16203c0a5849"
integrity sha512-7nPIwzQz55Mu0E1BOoW1noObZet9J34iKmeAJU3TRL7iiGmPZB0McSjC6KgDHkfqYgim2JGrnCsa1978ffbwKQ==
"@vtex/api@6.45.4":
version "6.45.4"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.4.tgz#58be7497c0c0f91a388fabd42149e48cb95e271d"
integrity sha512-DVAJr5BkSjXupjn2h5Z1In8C3Li9kZwCXPwRQbpIgyS7s9dN2ZEFQc6nQlJm6ZoDCoyYBg62LgD7Kurvz9jc3w==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down
8 changes: 4 additions & 4 deletions react/components/LocaleSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const BindingProvider: FC = ({ children }) => {
useEffect(() => {
// eslint-disable-next-line vtex/prefer-early-return
if (bindingData) {
const fetchedLocales = bindingData.tenantInfo.bindings
const filteredLocales = filterLocales(fetchedLocales)
const { defaultLocale, bindings: fetchedLocales } = bindingData.tenantInfo
const filteredLocales = filterLocales(fetchedLocales, defaultLocale)
setBindings(filteredLocales)
setSelectedLocale(filteredLocales[0].defaultLocale)
setXVtexTenant(filteredLocales[0].defaultLocale)
setSelectedLocale(defaultLocale)
setXVtexTenant(defaultLocale)
}
}, [bindingData])

Expand Down
1 change: 1 addition & 0 deletions react/graphql/accountLocales.gql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
query accountLocales {
tenantInfo @context(provider: "vtex.tenant-graphql") {
defaultLocale
bindings {
id
defaultLocale
Expand Down
1 change: 1 addition & 0 deletions react/typings/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Binding {

interface BindingsData {
tenantInfo: {
defaultLocale: string
bindings: Binding[]
}
}
25 changes: 23 additions & 2 deletions react/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ import { Translation } from 'vtex.messages'
export * from './fileParsers'
export * from './sanitizeImportJSON'

/**
* Keep the xVtexTenant in the top of the dropdown button
*/
const sortBindings = (bindings: Binding[], xVtexTenant: string): Binding[] => {
const xVtexTenantBinding = []
const otherBindings = []

for (const binding of bindings) {
if (binding.defaultLocale === xVtexTenant) {
xVtexTenantBinding.push(binding)
} else {
otherBindings.push(binding)
}
}

return [...xVtexTenantBinding, ...otherBindings]
}

/**
* Returns all the unique binding locales, excluding the first one provided by the api (admin)
*
* @param {array} Array received from api graphql
* @return {array} Array with only unique locales and without the admin binding.
*/

export const filterLocales = (bindings: Binding[]): Binding[] => {
export const filterLocales = (
bindings: Binding[],
xVtexTenant: string
): Binding[] => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [admin, ...otherBindings] = bindings
const uniqueBindings: { [id: string]: boolean } = {}
Expand All @@ -37,7 +58,7 @@ export const filterLocales = (bindings: Binding[]): Binding[] => {
}
}

return filteredBindings
return sortBindings(filteredBindings, xVtexTenant)
}

/**
Expand Down

0 comments on commit 1b2b316

Please sign in to comment.