Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/GeolocationEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ const GeolocationEditor = forwardRef(({ isMobile, config: propsConfig, ...props
const config = useMemo(() => ({ ...window?.dtable, ...propsConfig, }), [propsConfig]);

const getLocationData = useCallback(() => {
if (window?.app?.location) return new Promise((resolve, reject) => {
if (window?.app?.location) return new Promise((resolve) => {
resolve(window.app.location);
});
const { server = '', mediaUrl = '' } = config || {};
return fetch(`${server}${mediaUrl}geo-data/cn-location.json`.replaceAll('//', '/')).then((res) => { // get locations from server
return res.json();
}).catch(() => {
// get locations from local
// get locations from local, fall back to empty object if unavailable
return fetch('./geo-data/cn-location.json').then(res => {
return res.json();
});
}).catch(() => null);
});
}, [config]);

const getCountryData = useCallback((lang) => {
if (lang === 'cn' && window.app.countryListCn) return new Promise((resolve, reject) => {
if (lang === 'cn' && window.app.countryListCn) return new Promise((resolve) => {
resolve(window.app.countryListCn);
});
if (lang !== 'cn' && window.app.countryListEn) return new Promise((resolve, reject) => {
if (lang !== 'cn' && window.app.countryListEn) return new Promise((resolve) => {
resolve(window.app.countryListEn);
});

const { mediaUrl = '', server = '' } = config;
const { mediaUrl = '', server = '' } = config || {};
const geoFileName = lang === 'cn' ? 'cn-region-location' : 'en-region-location';
return fetch(`${server}${mediaUrl}geo-data/${geoFileName}.json`.replaceAll('//', '/'))
.then(res => {
return res.json();
}).catch(() => {
// get locations from local
// get locations from local, fall back to null if unavailable
return fetch(`./geo-data/${geoFileName}.json`).then(res => {
return res.json();
});
}).catch(() => null);
}).then(res => {
const data = res || {};
if (lang === 'cn') {
Expand Down
7 changes: 5 additions & 2 deletions src/GeolocationEditor/mb-editor/country-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ const CountryEditor = ({

useEffect(() => {
getData().then(data => {
locations.current = data;
const continent = data.find(a => a.children.find(b => b.value === oldValue?.country_region || ''));
const safeData = data || [];
locations.current = safeData;
const continent = safeData.find(a => a.children && a.children.find(b => b.value === oldValue?.country_region || ''));
const value = [continent?.value || '', oldValue?.country_region || ''];
setValue(value);
setLoading(false);
}).catch(() => {
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
4 changes: 3 additions & 1 deletion src/GeolocationEditor/mb-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import MapSelectionEditor from './map-selection-editor';
import './index.css';

const transLocationData = (data) => {
if (!data) return [];
if (Object.prototype.toString.call(data) === '[object Object]') {
const name = data.name;
data.label = name;
Expand All @@ -22,10 +23,11 @@ const transLocationData = (data) => {
});
}
}
return data.children;
return data.children || [];
};

const transCountryData = (data) => {
if (!data) return [];
let _data = [];
// eslint-disable-next-line
for (let key in data) {
Expand Down
4 changes: 3 additions & 1 deletion src/GeolocationEditor/mb-editor/location-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const LocationEditor = ({

useEffect(() => {
getData().then(data => {
locations.current = data;
locations.current = data || [];
setLoading(false);
}).catch(() => {
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
4 changes: 3 additions & 1 deletion src/GeolocationEditor/mb-editor/province-city-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const ProvinceCityEditor = ({

useEffect(() => {
getData().then(data => {
locations.current = data;
locations.current = data || [];
setLoading(false);
}).catch(() => {
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
4 changes: 3 additions & 1 deletion src/GeolocationEditor/mb-editor/province-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const ProvinceEditor = ({

useEffect(() => {
getData().then(data => {
locations.current = data;
locations.current = data || [];
setLoading(false);
}).catch(() => {
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
17 changes: 10 additions & 7 deletions src/GeolocationEditor/pc-editor/location-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LocationEditor extends Component {

UNSAFE_componentWillMount() {
this.props.getData().then(data => {
this.locations = data;
this.locations = data || {};
const { selectedProvince, selectedCity, selectedCounty, selectedItem } = this.initLocationSelecting(this.value);
this.setState({
isLoadingData: false,
Expand All @@ -53,28 +53,31 @@ class LocationEditor extends Component {
selectedCounty,
selectedItem
});
}).catch(() => {
this.locations = {};
this.setState({ isLoadingData: false });
});
}

initLocationSelecting = (value) => {
let selectedProvince = this.locations.children.find((province) => {
return value.province && value.province.length > 0 && province.name.includes(value.province);
let selectedProvince = (this.locations.children || []).find((province) => {
return value.province && value.province.length > 0 && province.name && province.name.includes(value.province);
});

if (!selectedProvince) {
return { selectedProvince: null, selectedCity: null, selectedCounty: null, selectedItem: 'province' };
}

let selectedCity = selectedProvince.children.find((city) => {
return value.city && value.city.length > 0 && city.name.includes(value.city);
let selectedCity = (selectedProvince.children || []).find((city) => {
return value.city && value.city.length > 0 && city.name && city.name.includes(value.city);
});

if (!selectedCity) {
return { selectedProvince, selectedCity: null, selectedCounty: null, selectedItem: 'city' };
}

let selectedCounty = selectedCity.children.find((county) => {
return value.district && value.district.length > 0 && county.name.includes(value.district);
let selectedCounty = (selectedCity.children || []).find((county) => {
return value.district && value.district.length > 0 && county.name && county.name.includes(value.district);
});

if (!selectedCounty) {
Expand Down
13 changes: 8 additions & 5 deletions src/GeolocationEditor/pc-editor/province-city-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,31 @@ class ProvinceCityEditor extends Component {

componentDidMount() {
this.props.getData().then(data => {
this.locations = data;
this.locations = data || {};
const { selectedProvince, selectedCity, selectedItem } = this.initLocationSelecting(this.value);
this.setState({
isLoadingData: false,
selectedProvince,
selectedCity,
selectedItem
});
}).catch(() => {
this.locations = {};
this.setState({ isLoadingData: false });
});
}

initLocationSelecting = (value) => {
let selectedProvince = this.locations.children.find((province) => {
return value.province && value.province.length > 0 && province.name.includes(value.province);
let selectedProvince = (this.locations.children || []).find((province) => {
return value.province && value.province.length > 0 && province.name && province.name.includes(value.province);
});

if (!selectedProvince) {
return { selectedProvince: null, selectedCity: null, selectedItem: 'province' };
}

let selectedCity = selectedProvince.children.find((city) => {
return value.city && value.city.length > 0 && city.name.includes(value.city);
let selectedCity = (selectedProvince.children || []).find((city) => {
return value.city && value.city.length > 0 && city.name && city.name.includes(value.city);
});

if (!selectedCity) {
Expand Down
10 changes: 7 additions & 3 deletions src/GeolocationEditor/pc-editor/province-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ class ProvinceEditor extends Component {

componentDidMount() {
this.props.getData().then(data => {
this.locations = data;
this.filteredProvince = this.locations.children;
this.locations = data || {};
this.filteredProvince = this.locations.children || [];
this.setState({
isLoadingData: false,
});
}).catch(() => {
this.locations = {};
this.filteredProvince = [];
this.setState({ isLoadingData: false });
});
document.addEventListener('keydown', this.onHotKey, true);
}
Expand Down Expand Up @@ -120,7 +124,7 @@ class ProvinceEditor extends Component {
if (value.length > 0) {
this.provinceReg = new RegExp(value, 'i');
}
this.filteredProvince = this.locations.children.filter((item) => {
this.filteredProvince = (this.locations.children || []).filter((item) => {
if (!this.provinceReg) {
return true;
}
Expand Down