Skip to content

Commit ae80d08

Browse files
authored
Merge pull request #374 from vtex-apps/fix/locale-selector
Fix/locale selector
2 parents bdf655a + f65f1aa commit ae80d08

File tree

9 files changed

+85
-14
lines changed

9 files changed

+85
-14
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Block selector not working after a locale change.
12+
13+
### Added
14+
- Block selector status(wait).
15+
16+
### Changed
17+
- Hides query strings from site editor top bar.
18+
1019
## [4.40.0] - 2021-04-13
1120

1221
### Fixed

manifest.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
},
2727
"mustUpdateAt": "2018-09-05",
2828
"categories": [],
29-
"registries": [
30-
"smartcheckout"
31-
],
29+
"registries": ["smartcheckout"],
3230
"settingsSchema": {},
3331
"scripts": {
3432
"postreleasy": "vtex publish -r vtex --verbose"

react/components/EditorContainer/StoreIframe.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ const StoreIframe: React.FunctionComponent<Props> = ({ path }) => {
3636
src += `${getJoiner(src)}__bindingAddress=${binding.canonicalBaseAddress}`
3737
}
3838

39-
src += `${getJoiner(src)}__siteEditor`
39+
if (!src.includes('__siteEditor')) {
40+
src += `${getJoiner(src)}__siteEditor=true`
41+
}
4042

4143
return (
4244
<iframe

react/components/EditorContainer/Topbar/BlockPicker.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { InjectedIntlProps, injectIntl } from 'react-intl'
44
import { Tooltip } from 'vtex.styleguide'
55

66
import { useEditorContext } from '../../EditorContext'
7-
87
import { useHover } from './hooks'
98
import IconPicker from './icons/IconPicker'
109

@@ -26,13 +25,17 @@ const BlockPicker: React.FC<InjectedIntlProps> = ({ intl }) => {
2625
position="bottom"
2726
>
2827
<button
28+
style={editor.mode === 'disabled' ? { cursor: 'wait' } : {}}
2929
className={`w2 h2 bg-white br2 b--transparent outline-0 pointer flex justify-center items-center ${
30-
editor.editMode || hover ? 'c-action-primary' : 'c-on-disabled'
30+
editor.editMode || (hover && editor.mode !== 'disabled')
31+
? 'c-action-primary'
32+
: 'c-on-disabled'
3133
}`}
3234
onClick={handleEditModeToggle}
3335
onKeyPress={handleKeyPress}
3436
onMouseEnter={handleMouseEnter}
3537
onMouseLeave={handleMouseLeave}
38+
disabled={editor.mode === 'disabled'}
3639
>
3740
<IconPicker />
3841
</button>

react/components/EditorContainer/Topbar/ContextSelectors/LocaleSelector.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,44 @@ const LocaleSelector: React.FC<Props> = ({
2020
}) => {
2121
const { culture, emitter } = iframeRuntime
2222

23+
const editor = useEditorContext()
24+
2325
const [locale, setLocale] = React.useState(culture.locale)
2426

2527
const handleChange = React.useCallback(
2628
(e: React.ChangeEvent<HTMLSelectElement>) => {
27-
setLocale(e.target.value)
29+
editor.setMode('disabled')
30+
31+
setLocale(e?.target?.value)
32+
33+
emitter.emit(
34+
'localesChanged',
35+
e?.target?.value,
36+
null,
37+
(locale: string) => {
38+
let bindingQueryString
39+
40+
if (editor.iframeWindow) {
41+
const searchParams = new URLSearchParams(
42+
editor?.iframeWindow?.location.search
43+
)
44+
45+
const bindingAddress = searchParams.get('__bindingAddress')
2846

29-
emitter.emit('localesChanged', e.target.value)
47+
if (bindingAddress) {
48+
bindingQueryString = `__bindingAddress=${decodeURIComponent(
49+
bindingAddress
50+
)}`
51+
}
52+
53+
if (e?.target?.value) {
54+
editor.iframeWindow.location.search = `${bindingQueryString}&__locale=${e.target.value}&__siteEditor=true`
55+
} else {
56+
editor.iframeWindow.location.search = `${bindingQueryString}&__locale=${locale}&__siteEditor=true`
57+
}
58+
}
59+
}
60+
)
3061
},
3162
[emitter]
3263
)
@@ -38,6 +69,12 @@ const LocaleSelector: React.FC<Props> = ({
3869
// eslint-disable-next-line react-hooks/exhaustive-deps
3970
}, [culture.locale])
4071

72+
React.useEffect(() => {
73+
if (editor.mode !== 'content') {
74+
editor.setMode('content')
75+
}
76+
}, [culture.locale, culture.country])
77+
4178
const Selector = React.useCallback(
4279
() => (
4380
<div className={className}>
@@ -53,7 +90,6 @@ const LocaleSelector: React.FC<Props> = ({
5390
[className, handleChange, isDisabled, locale, options]
5491
)
5592

56-
const editor = useEditorContext()
5793
const intl = useIntl()
5894

5995
return editor.editTreePath ? (

react/components/EditorContainer/Topbar/ContextSelectors/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ const ContextSelectors: React.FC<WithApolloClient<Props>> = ({
9696
if (newBinding && editor.iframeWindow) {
9797
setBinding(newBinding)
9898

99-
editor.iframeWindow.location.search = `__bindingAddress=${newBinding.canonicalBaseAddress}&__siteEditor`
99+
editor.iframeWindow.location.search = `__bindingAddress=${newBinding.canonicalBaseAddress}&__siteEditor=true`
100100
}
101+
102+
editor.setMode('disabled')
101103
}
102104
},
103105
[bindings, setBinding, editor.iframeWindow]

react/components/EditorContainer/Topbar/UrlInput.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,30 @@ const UrlInput = () => {
88

99
const editor = useEditorContext()
1010

11+
const resolveUrlPath = (pathname: string, searchQueries: string) => {
12+
const searchParams = new URLSearchParams(searchQueries)
13+
const SEARCH_QUERIES_TO_HIDE = [
14+
'__siteEditor',
15+
'__bindingAddress',
16+
'__locale',
17+
]
18+
19+
SEARCH_QUERIES_TO_HIDE.forEach(query => {
20+
searchParams.delete(query)
21+
})
22+
23+
if (searchParams.toString().length) {
24+
return pathname + decodeURIComponent(`?${searchParams.toString()}`)
25+
}
26+
27+
return pathname
28+
}
29+
1130
const urlPath = editor.iframeWindow
12-
? editor.iframeWindow.location.pathname +
13-
editor.iframeWindow.location.search
31+
? resolveUrlPath(
32+
editor.iframeWindow.location.pathname,
33+
editor.iframeWindow.location.search
34+
)
1435
: ''
1536

1637
const [url, setUrl] = React.useState(urlPath)

react/components/EditorProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class EditorProvider extends Component<Props, State> {
306306
}
307307

308308
public handleSetMode = (mode: EditorMode) => {
309-
this.setState({ mode })
309+
this.setState({ mode, editMode: mode === 'content' ? false : true })
310310
}
311311

312312
public handleChangeIframeUrl = (url: string) => {

react/typings/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ declare global {
170170

171171
type ConfigurationDevice = 'any' | 'desktop' | 'mobile'
172172

173-
type EditorMode = 'content' | 'layout'
173+
type EditorMode = 'content' | 'layout' | 'disabled'
174174

175175
interface EditorConditionSection {
176176
activeConditions: string[]

0 commit comments

Comments
 (0)