diff --git a/assets/js/dashboard/constants.js b/assets/js/dashboard/constants.js deleted file mode 100644 index 07e3d10d5..000000000 --- a/assets/js/dashboard/constants.js +++ /dev/null @@ -1,253 +0,0 @@ -export const country_codes = { - "AFG": "Afghanistan", - "ALA": "Aland Islands", - "ALB": "Albania", - "DZA": "Algeria", - "ASM": "American Samoa", - "AND": "Andorra", - "AGO": "Angola", - "AIA": "Anguilla", - "ATA": "Antarctica", - "ATG": "Antigua And Barbuda", - "ARG": "Argentina", - "ARM": "Armenia", - "ABW": "Aruba", - "AUS": "Australia", - "AUT": "Austria", - "AZE": "Azerbaijan", - "BHS": "Bahamas", - "BHR": "Bahrain", - "BGD": "Bangladesh", - "BRB": "Barbados", - "BLR": "Belarus", - "BEL": "Belgium", - "BLZ": "Belize", - "BEN": "Benin", - "BMU": "Bermuda", - "BTN": "Bhutan", - "BOL": "Bolivia", - "BIH": "Bosnia And Herzegovina", - "BWA": "Botswana", - "BVT": "Bouvet Island", - "BRA": "Brazil", - "IOT": "British Indian Ocean Territory", - "BRN": "Brunei Darussalam", - "BGR": "Bulgaria", - "BFA": "Burkina Faso", - "BDI": "Burundi", - "KHM": "Cambodia", - "CMR": "Cameroon", - "CAN": "Canada", - "CPV": "Cape Verde", - "CYM": "Cayman Islands", - "CAF": "Central African Republic", - "TCD": "Chad", - "CHL": "Chile", - "CHN": "China", - "CXR": "Christmas Island", - "CCK": "Cocos (Keeling) Islands", - "COL": "Colombia", - "COM": "Comoros", - "COG": "Congo", - "COD": "Congo, Democratic Republic", - "COK": "Cook Islands", - "CRI": "Costa Rica", - "CIV": "Cote D'Ivoire", - "HRV": "Croatia", - "CUB": "Cuba", - "CYP": "Cyprus", - "CZE": "Czech Republic", - "DNK": "Denmark", - "DJI": "Djibouti", - "DMA": "Dominica", - "DOM": "Dominican Republic", - "ECU": "Ecuador", - "EGY": "Egypt", - "SLV": "El Salvador", - "GNQ": "Equatorial Guinea", - "ERI": "Eritrea", - "EST": "Estonia", - "ETH": "Ethiopia", - "FLK": "Falkland Islands (Malvinas)", - "FRO": "Faroe Islands", - "FJI": "Fiji", - "FIN": "Finland", - "FRA": "France", - "GUF": "French Guiana", - "PYF": "French Polynesia", - "ATF": "French Southern Territories", - "GAB": "Gabon", - "GMB": "Gambia", - "GEO": "Georgia", - "DEU": "Germany", - "GHA": "Ghana", - "GIB": "Gibraltar", - "GRC": "Greece", - "GRL": "Greenland", - "GRD": "Grenada", - "GLP": "Guadeloupe", - "GUM": "Guam", - "GTM": "Guatemala", - "GGY": "Guernsey", - "GIN": "Guinea", - "GNB": "Guinea-Bissau", - "GUY": "Guyana", - "HTI": "Haiti", - "HMD": "Heard Island & Mcdonald Islands", - "VAT": "Holy See (Vatican City State)", - "HND": "Honduras", - "HKG": "Hong Kong", - "HUN": "Hungary", - "ISL": "Iceland", - "IND": "India", - "IDN": "Indonesia", - "IRN": "Iran, Islamic Republic Of", - "IRQ": "Iraq", - "IRL": "Ireland", - "IMN": "Isle Of Man", - "ISR": "Israel", - "ITA": "Italy", - "JAM": "Jamaica", - "JPN": "Japan", - "JEY": "Jersey", - "JOR": "Jordan", - "KAZ": "Kazakhstan", - "KEN": "Kenya", - "KIR": "Kiribati", - "KOR": "Korea", - "KWT": "Kuwait", - "KGZ": "Kyrgyzstan", - "LAO": "Lao People's Democratic Republic", - "LVA": "Latvia", - "LBN": "Lebanon", - "LSO": "Lesotho", - "LBR": "Liberia", - "LBY": "Libyan Arab Jamahiriya", - "LIE": "Liechtenstein", - "LTU": "Lithuania", - "LUX": "Luxembourg", - "MAC": "Macao", - "MKD": "Macedonia", - "MDG": "Madagascar", - "MWI": "Malawi", - "MYS": "Malaysia", - "MDV": "Maldives", - "MLI": "Mali", - "MLT": "Malta", - "MHL": "Marshall Islands", - "MTQ": "Martinique", - "MRT": "Mauritania", - "MUS": "Mauritius", - "MYT": "Mayotte", - "MEX": "Mexico", - "FSM": "Micronesia, Federated States Of", - "MDA": "Moldova", - "MCO": "Monaco", - "MNG": "Mongolia", - "MNE": "Montenegro", - "MSR": "Montserrat", - "MAR": "Morocco", - "MOZ": "Mozambique", - "MMR": "Myanmar", - "NAM": "Namibia", - "NRU": "Nauru", - "NPL": "Nepal", - "NLD": "Netherlands", - "NCL": "New Caledonia", - "NZL": "New Zealand", - "NIC": "Nicaragua", - "NER": "Niger", - "NGA": "Nigeria", - "NIU": "Niue", - "NFK": "Norfolk Island", - "MNP": "Northern Mariana Islands", - "NOR": "Norway", - "OMN": "Oman", - "PAK": "Pakistan", - "PLW": "Palau", - "PSE": "Palestinian Territory, Occupied", - "PAN": "Panama", - "PNG": "Papua New Guinea", - "PRY": "Paraguay", - "PER": "Peru", - "PHL": "Philippines", - "PCN": "Pitcairn", - "POL": "Poland", - "PRT": "Portugal", - "PRI": "Puerto Rico", - "QAT": "Qatar", - "REU": "Reunion", - "ROU": "Romania", - "RUS": "Russian Federation", - "RWA": "Rwanda", - "BLM": "Saint Barthelemy", - "SHN": "Saint Helena", - "KNA": "Saint Kitts And Nevis", - "LCA": "Saint Lucia", - "MAF": "Saint Martin", - "SPM": "Saint Pierre And Miquelon", - "VCT": "Saint Vincent And Grenadines", - "WSM": "Samoa", - "SMR": "San Marino", - "STP": "Sao Tome And Principe", - "SAU": "Saudi Arabia", - "SEN": "Senegal", - "SRB": "Serbia", - "SYC": "Seychelles", - "SLE": "Sierra Leone", - "SGP": "Singapore", - "SVK": "Slovakia", - "SVN": "Slovenia", - "SLB": "Solomon Islands", - "SOM": "Somalia", - "ZAF": "South Africa", - "SGS": "South Georgia And Sandwich Isl.", - "ESP": "Spain", - "LKA": "Sri Lanka", - "SDN": "Sudan", - "SUR": "Suriname", - "SJM": "Svalbard And Jan Mayen", - "SWZ": "Swaziland", - "SWE": "Sweden", - "CHE": "Switzerland", - "SYR": "Syrian Arab Republic", - "TWN": "Taiwan", - "TJK": "Tajikistan", - "TZA": "Tanzania", - "THA": "Thailand", - "TLS": "Timor-Leste", - "TGO": "Togo", - "TKL": "Tokelau", - "TON": "Tonga", - "TTO": "Trinidad And Tobago", - "TUN": "Tunisia", - "TUR": "Turkey", - "TKM": "Turkmenistan", - "TCA": "Turks And Caicos Islands", - "TUV": "Tuvalu", - "UGA": "Uganda", - "UKR": "Ukraine", - "ARE": "United Arab Emirates", - "GBR": "United Kingdom", - "USA": "United States", - "UMI": "United States Outlying Islands", - "URY": "Uruguay", - "UZB": "Uzbekistan", - "VUT": "Vanuatu", - "VEN": "Venezuela", - "VNM": "Viet Nam", - "VGB": "Virgin Islands, British", - "VIR": "Virgin Islands, U.S.", - "WLF": "Wallis And Futuna", - "ESH": "Western Sahara", - "YEM": "Yemen", - "ZMB": "Zambia", - "ZWE": "Zimbabwe" -} - -export const screen_sizes = [ - 'Desktop', - 'Mobile', - 'Laptop', - 'Tablet' -] diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index dd434113f..4aea6e78a 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -3,9 +3,9 @@ import { withRouter, Redirect } from 'react-router-dom' import Modal from './modal' import { parseQuery, formattedFilters, navigateToQuery } from '../../query' -import { country_codes, screen_sizes } from "../../constants"; import Transition from "../../../transition"; import * as api from '../../api' +import Datamap from 'datamaps' class FilterModal extends React.Component { constructor(props) { @@ -18,8 +18,8 @@ class FilterModal extends React.Component { filterSaved: false, suggestions: [], fetchSuggestionsTimeout: undefined, - initialSuggest: true, - focusedSuggestionIndex: -1 + focusedSuggestionIndex: -1, + preventSuggestions: true } this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['goal', 'props'].includes(filter)) @@ -35,15 +35,24 @@ class FilterModal extends React.Component { } componentDidUpdate(prevProps, prevState) { - const { query, selectedFilter, updatedValue, preventSuggestions } = this.state + const { query, selectedFilter, updatedValue, preventSuggestions, fetchSuggestionsTimeout } = this.state if (prevState.selectedFilter !== selectedFilter) { const negated = query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter) - const updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "") + let updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "") + clearTimeout(fetchSuggestionsTimeout) - clearTimeout(this.state.fetchSuggestionsTimeout) + if (selectedFilter == 'country') { + const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; + const selectedCountry = allCountries.find((c) => c.id === updatedValue) || { properties: { name: updatedValue } }; + updatedValue = selectedCountry.properties.name + } - this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false }, () => { + if (selectedFilter) { + this.fetchSuggestions() + } + }) } if (prevState.updatedValue !== updatedValue) { @@ -51,13 +60,11 @@ class FilterModal extends React.Component { let fetchSuggestionsTimeout; clearTimeout(this.state.fetchSuggestionsTimeout) - if (!['country', 'screen'].includes(selectedFilter)) { - fetchSuggestionsTimeout = setTimeout(this.fetchSuggestions, 1000) - } - - this.setState({ suggestions: [], fetchSuggestionsTimeout, preventSuggestions: false, focusedSuggestionIndex: -1 }) + this.setState({ suggestions: [], preventSuggestions: false, focusedSuggestionIndex: -1, noSuggestions: false }, () => { + this.setState({ fetchSuggestionsTimeout: setTimeout(this.fetchSuggestions, 200) }) + }) } else { - this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false }) } } } @@ -68,7 +75,7 @@ class FilterModal extends React.Component { handleKeydown(e) { const { suggestions } = this.state - let { focusedSuggestionIndex, updatedValue, preventSuggestions } = this.state + let { focusedSuggestionIndex, updatedValue, preventSuggestions, selectedFilter } = this.state const navigationKeys = ['ArrowUp', undefined, 'ArrowDown'] if (e.target.id !== 'suggestions_input') return true @@ -82,7 +89,7 @@ class FilterModal extends React.Component { } if (navigationKeys.includes(e.key)) { - focusedSuggestionIndex += navigationKeys.indexOf(e.key)-1 + focusedSuggestionIndex += navigationKeys.indexOf(e.key) - 1 if (focusedSuggestionIndex < 0) { focusedSuggestionIndex = suggestions.length - 1 } else if (focusedSuggestionIndex >= suggestions.length) { @@ -100,47 +107,30 @@ class FilterModal extends React.Component { fetchSuggestions() { const { query, updatedValue, selectedFilter } = this.state - - query.filters[selectedFilter] = null - - api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, query, { q: updatedValue }) - .then((res) => this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 })) + const updatedQuery = { ...query, filters: { ...query.filters, [selectedFilter]: null } } + + if (selectedFilter == 'country') { + const matchedCountries = Datamap.prototype.worldTopo.objects.world.geometries.filter(c => c.properties.name.includes(updatedValue.trim())) + const matches = matchedCountries.map(c => c.id) + + api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/country`, updatedQuery, { q: matches }) + .then((res) => { + const suggestions = res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name) + + this.setState({ suggestions, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 }) + }) + } else { + api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: updatedValue.trim() }) + .then((res) => { + this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 }) + }) + } } renderSelector(filter) { - const { updatedValue, suggestions, initialSuggest, inputFocused, focusedSuggestionIndex } = this.state; + const { updatedValue, suggestions, inputFocused, focusedSuggestionIndex, selectedFilter } = this.state; - if (!filter) { return null; } - - if (filter === 'country') { - return () - } - - if (filter === 'screen') { - return ( - <> - - > - ) - } + if (!filter) return null return