From 4b990bf8193855b9fa27c01590ce8c24b9942c42 Mon Sep 17 00:00:00 2001 From: Konstantin Markov Date: Fri, 23 Aug 2024 11:47:38 +0300 Subject: [PATCH 1/4] Store location translations, render location name for selected language --- .../GeoLookupInput/AddGeoLookupInput.tsx | 2 ++ .../GeoLookupInput/LocationItem.tsx | 9 ++++++-- client/components/UI/List/ActionMenu.tsx | 22 +++++++++---------- client/components/fields/editor/Location.tsx | 2 +- client/interfaces.ts | 1 + 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/client/components/GeoLookupInput/AddGeoLookupInput.tsx b/client/components/GeoLookupInput/AddGeoLookupInput.tsx index e8f46906b..1ae491ad1 100644 --- a/client/components/GeoLookupInput/AddGeoLookupInput.tsx +++ b/client/components/GeoLookupInput/AddGeoLookupInput.tsx @@ -230,6 +230,7 @@ export class AddGeoLookupInput extends React.Component { qcode: location.guid, address: location.address, details: location.details, + translations: location.translations, }; // external address might not be there. @@ -274,6 +275,7 @@ export class AddGeoLookupInput extends React.Component { {initialValue?.name == null ? null : ( { @@ -23,6 +24,10 @@ export class LocationItem extends React.PureComponent { const {gettext} = superdeskApi.localization; const location = this.props.location; + const locationNameComputed = this.props.languageCode + ? location.translations?.name[`name:${this.props.languageCode}`] ?? location.name + : location.name; + return ( { ( +export const ActionMenu = ({children, className, row = true}: IProps) => (
{children}
); - -ActionMenu.propTypes = { - children: PropTypes.node.isRequired, - row: PropTypes.bool, -}; - -ActionMenu.defaultProps = {row: true}; diff --git a/client/components/fields/editor/Location.tsx b/client/components/fields/editor/Location.tsx index 4a6ca1b8a..6761bde71 100644 --- a/client/components/fields/editor/Location.tsx +++ b/client/components/fields/editor/Location.tsx @@ -22,7 +22,7 @@ export class EditorFieldLocation extends React.PureComponent { {...this.props} field={field} label={this.props.label ?? gettext('Location')} - value={get(this.props.item, field, this.props.defaultValue)} + value={this.props.item[field] ?? this.props.defaultValue} disableSearch={!this.props.enableExternalSearch} disableAddLocation={this.props.disableAddLocation} readOnly={this.props.disabled} diff --git a/client/interfaces.ts b/client/interfaces.ts index 07a5e666a..0b1188fd2 100644 --- a/client/interfaces.ts +++ b/client/interfaces.ts @@ -389,6 +389,7 @@ export interface IEventLocation { lat: number; lon: number; }; + translations?: ILocation['translations']; } export interface IItemAction { From 7a2715fa78aaf0ae554a896d4964e63c51fff05f Mon Sep 17 00:00:00 2001 From: Konstantin Markov Date: Fri, 23 Aug 2024 12:01:21 +0300 Subject: [PATCH 2/4] Remove lodash.get --- .../AddGeoLookupResultsPopUp.tsx | 36 +++++++++---------- .../GeoLookupInput/LocationItem.tsx | 4 +-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx index 637ca71f3..f88af22a9 100644 --- a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx +++ b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx @@ -79,20 +79,22 @@ export class AddGeoLookupResultsPopUp extends React.Component { } handleEnterKey() { + const {localSuggests, suggests} = this.props; + const localSuggestLen = localSuggests.length ?? 0; + if (this.state.activeOptionIndex > -1) { - if (this.state.activeOptionIndex < get(this.props.localSuggests, 'length', -1)) { - this.props.onChange(this.props.localSuggests[this.state.activeOptionIndex]); + if (this.state.activeOptionIndex < (localSuggests.length ?? -1)) { + this.props.onChange(localSuggests[this.state.activeOptionIndex]); return; } - if (this.state.activeOptionIndex === get(this.props.localSuggests, 'length', 0)) { + if (this.state.activeOptionIndex === localSuggestLen) { this.onSearch(); return; } - if (this.state.activeOptionIndex >= get(this.props.localSuggests, 'length', 0) + 1) { - this.props.onChange(this.props.suggests[ - this.state.activeOptionIndex - (get(this.props.localSuggests, 'length', 0) + 1)]); + if (this.state.activeOptionIndex >= localSuggestLen + 1) { + this.props.onChange(suggests[this.state.activeOptionIndex - localSuggestLen + 1]); } } } @@ -100,8 +102,8 @@ export class AddGeoLookupResultsPopUp extends React.Component { handleDownArrowKey() { if (this.state.activeOptionIndex < (1 + // External search button - get(this.props.localSuggests, 'length', 0) + - get(this.props.suggests, 'length', 0)) - 1 + (this.props.localSuggests.length ?? 0) + + (this.props.suggests.length ?? 0)) - 1 ) { this.setState({activeOptionIndex: this.state.activeOptionIndex + 1}); @@ -141,10 +143,8 @@ export class AddGeoLookupResultsPopUp extends React.Component { render() { const {gettext} = superdeskApi.localization; - const localSuggests = get(this.props.localSuggests, 'length') > 0 ? - this.props.localSuggests : []; - const suggests = get(this.props.suggests, 'length') > 0 ? - this.props.suggests : []; + const localSuggests = this.props.localSuggests.length > 0 ? this.props.localSuggests : []; + const suggests = this.props.suggests.length > 0 ? this.props.suggests : []; const tabLabels = [( { active={index === this.state.activeOptionIndex} /> ))} - {get(localSuggests, 'length') === 0 && ( + {localSuggests.length === 0 && (
  • {gettext('No results found')}
  • @@ -220,14 +220,12 @@ export class AddGeoLookupResultsPopUp extends React.Component { key={index} location={suggest} onClick={this.props.onChange.bind(null, suggest)} - active={( - index + - get(this.props.localSuggests, 'length', 0) + - 1 - ) === this.state.activeOptionIndex} + active={(index + localSuggests.length + 1) + === this.state.activeOptionIndex + } /> ))} - {get(suggests, 'length') === 0 && ( + {suggests.length === 0 && (
  • {gettext('No results found')}
  • diff --git a/client/components/GeoLookupInput/LocationItem.tsx b/client/components/GeoLookupInput/LocationItem.tsx index 507fcf93d..01165d1f4 100644 --- a/client/components/GeoLookupInput/LocationItem.tsx +++ b/client/components/GeoLookupInput/LocationItem.tsx @@ -9,7 +9,7 @@ import {Button} from '../UI'; import {Location} from '../Location'; import {onEventCapture} from '../../utils'; -import {formatLocationToAddress, getLocationsShortName} from '../../utils/locations'; +import {formatLocationToAddress} from '../../utils/locations'; interface IProps { location?: ILocation | IEventLocation; @@ -41,7 +41,7 @@ export class LocationItem extends React.PureComponent { name={locationNameComputed} address={formatLocationToAddress(this.props.location)} multiLine={true} - details={get(location, 'details[0]')} + details={location.details?.[0]} /> {(this.props.readOnly || this.props.onRemoveLocation == null) ? null : ( From 054bde3092e82d36c753dfe0e09b57c1820fd6e8 Mon Sep 17 00:00:00 2001 From: Konstantin Markov Date: Fri, 23 Aug 2024 12:04:03 +0300 Subject: [PATCH 3/4] Fix fallback --- client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx index f88af22a9..ebb139c47 100644 --- a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx +++ b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx @@ -143,8 +143,8 @@ export class AddGeoLookupResultsPopUp extends React.Component { render() { const {gettext} = superdeskApi.localization; - const localSuggests = this.props.localSuggests.length > 0 ? this.props.localSuggests : []; - const suggests = this.props.suggests.length > 0 ? this.props.suggests : []; + const localSuggests = this.props.localSuggests ?? []; + const suggests = this.props.suggests ?? []; const tabLabels = [( Date: Fri, 23 Aug 2024 14:56:30 +0300 Subject: [PATCH 4/4] Fix undefined.length --- .../components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx index ebb139c47..142ae1edd 100644 --- a/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx +++ b/client/components/GeoLookupInput/AddGeoLookupResultsPopUp.tsx @@ -80,7 +80,7 @@ export class AddGeoLookupResultsPopUp extends React.Component { handleEnterKey() { const {localSuggests, suggests} = this.props; - const localSuggestLen = localSuggests.length ?? 0; + const localSuggestLen = localSuggests?.length ?? 0; if (this.state.activeOptionIndex > -1) { if (this.state.activeOptionIndex < (localSuggests.length ?? -1)) { @@ -102,8 +102,8 @@ export class AddGeoLookupResultsPopUp extends React.Component { handleDownArrowKey() { if (this.state.activeOptionIndex < (1 + // External search button - (this.props.localSuggests.length ?? 0) + - (this.props.suggests.length ?? 0)) - 1 + (this.props.localSuggests?.length ?? 0) + + (this.props.suggests?.length ?? 0)) - 1 ) { this.setState({activeOptionIndex: this.state.activeOptionIndex + 1});