fix: remove algolia indices and urls from translations (#41043)

This commit is contained in:
Kristofer Koishigawa
2021-02-11 03:41:51 +09:00
committed by GitHub
parent 149cf95310
commit 1e9dc8cea5
10 changed files with 115 additions and 37 deletions

View File

@ -356,8 +356,6 @@
"heres-a-quote": "我们未找到你搜索的信息。这里分享一句名言:" "heres-a-quote": "我们未找到你搜索的信息。这里分享一句名言:"
}, },
"search": { "search": {
"index-name": "news-zh",
"search-page-url": "https://chinese.freecodecamp.org/news/search/?query={{searchQuery}}",
"label": "搜索", "label": "搜索",
"placeholder": "搜索 300+ 篇教程", "placeholder": "搜索 300+ 篇教程",
"see-results": "查看 {{searchQuery}} 的所有结果", "see-results": "查看 {{searchQuery}} 的所有结果",

View File

@ -356,8 +356,6 @@
"heres-a-quote": "We couldn't find what you were looking for, but here is a quote:" "heres-a-quote": "We couldn't find what you were looking for, but here is a quote:"
}, },
"search": { "search": {
"index-name": "news",
"search-page-url": "https://www.freecodecamp.org/news/search/?query={{searchQuery}}",
"label": "Search", "label": "Search",
"placeholder": "Search 6,000+ tutorials", "placeholder": "Search 6,000+ tutorials",
"see-results": "See all results for {{searchQuery}}", "see-results": "See all results for {{searchQuery}}",

View File

@ -356,8 +356,6 @@
"heres-a-quote": "No pudimos encontrar lo que estabas buscando, pero aquí hay una cita:" "heres-a-quote": "No pudimos encontrar lo que estabas buscando, pero aquí hay una cita:"
}, },
"search": { "search": {
"index-name": "noticias",
"search-page-url": "https://www.freecodecamp.org/espanol/news/search/?query={{searchQuery}}",
"label": "Buscar", "label": "Buscar",
"placeholder": "Buscar en más de 6.000 tutoriales", "placeholder": "Buscar en más de 6.000 tutoriales",
"see-results": "Ver todos los resultados de {{searchQuery}}", "see-results": "Ver todos los resultados de {{searchQuery}}",

View File

@ -426,9 +426,6 @@ const translationsSchema = {
"We couldn't find what you were looking for, but here is a quote:" "We couldn't find what you were looking for, but here is a quote:"
}, },
search: { search: {
'index-name': 'news',
'search-page-url':
'https://www.freecodecamp.org/news/search/?query={{searchQuery}}',
label: 'Search', label: 'Search',
placeholder: 'Search 6,000+ tutorials', placeholder: 'Search 6,000+ tutorials',
'see-results': 'See all results for {{searchQuery}}', 'see-results': 'See all results for {{searchQuery}}',

View File

@ -7,7 +7,7 @@ import qs from 'query-string';
import { navigate } from 'gatsby'; import { navigate } from 'gatsby';
import Media from 'react-responsive'; import Media from 'react-responsive';
import algoliasearch from 'algoliasearch/lite'; import algoliasearch from 'algoliasearch/lite';
import { withTranslation } from 'react-i18next'; import { newsIndex } from '../../utils/algolia-locale-setup';
import { import {
isSearchDropdownEnabledSelector, isSearchDropdownEnabledSelector,
@ -32,7 +32,6 @@ const propTypes = {
isDropdownEnabled: PropTypes.bool, isDropdownEnabled: PropTypes.bool,
location: PropTypes.object.isRequired, location: PropTypes.object.isRequired,
query: PropTypes.string, query: PropTypes.string,
t: PropTypes.func.isRequired,
toggleSearchDropdown: PropTypes.func.isRequired, toggleSearchDropdown: PropTypes.func.isRequired,
updateSearchQuery: PropTypes.func.isRequired updateSearchQuery: PropTypes.func.isRequired
}; };
@ -126,11 +125,11 @@ class InstantSearchRoot extends Component {
}; };
render() { render() {
const { query, t } = this.props; const { query } = this.props;
const MAX_MOBILE_HEIGHT = 768; const MAX_MOBILE_HEIGHT = 768;
return ( return (
<InstantSearch <InstantSearch
indexName={t('search.index-name')} indexName={newsIndex}
onSearchStateChange={this.onSearchStateChange} onSearchStateChange={this.onSearchStateChange}
searchClient={searchClient} searchClient={searchClient}
searchState={{ query }} searchState={{ query }}
@ -159,7 +158,7 @@ InstantSearchRoot.propTypes = propTypes;
const InstantSearchRootConnected = connect( const InstantSearchRootConnected = connect(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)(withTranslation()(InstantSearchRoot)); )(InstantSearchRoot);
const WithInstantSearch = ({ children }) => ( const WithInstantSearch = ({ children }) => (
<Location> <Location>

View File

@ -7,6 +7,7 @@ import { SearchBox } from 'react-instantsearch-dom';
import { HotKeys, ObserveKeys } from 'react-hotkeys'; import { HotKeys, ObserveKeys } from 'react-hotkeys';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
import { withTranslation } from 'react-i18next'; import { withTranslation } from 'react-i18next';
import { searchPageUrl } from '../../../utils/algolia-locale-setup';
import { import {
isSearchDropdownEnabledSelector, isSearchDropdownEnabledSelector,
@ -93,7 +94,7 @@ export class SearchBar extends Component {
handleSearch(e, query) { handleSearch(e, query) {
e.preventDefault(); e.preventDefault();
const { toggleSearchDropdown, updateSearchQuery, t } = this.props; const { toggleSearchDropdown, updateSearchQuery } = this.props;
const { index, hits } = this.state; const { index, hits } = this.state;
const selectedHit = hits[index]; const selectedHit = hits[index];
@ -116,9 +117,7 @@ export class SearchBar extends Component {
// are hits besides the footer // are hits besides the footer
return query && hits.length > 1 return query && hits.length > 1
? window.location.assign( ? window.location.assign(
t('search.search-page-url', { `${searchPageUrl}?query=${encodeURIComponent(query)}`
searchQuery: encodeURIComponent(query)
})
) )
: false; : false;
} }

View File

@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { connectStateResults, connectHits } from 'react-instantsearch-dom'; import { connectStateResults, connectHits } from 'react-instantsearch-dom';
import isEmpty from 'lodash/isEmpty'; import isEmpty from 'lodash/isEmpty';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { searchPageUrl } from '../../../utils/algolia-locale-setup';
import Suggestion from './SearchSuggestion'; import Suggestion from './SearchSuggestion';
import NoHitsSuggestion from './NoHitsSuggestion'; import NoHitsSuggestion from './NoHitsSuggestion';
@ -25,9 +26,7 @@ const CustomHits = connectHits(
query: searchQuery, query: searchQuery,
url: noHits url: noHits
? null ? null
: t('search.search-page-url', { : `${searchPageUrl}?query=${encodeURIComponent(searchQuery)}`,
searchQuery: encodeURIComponent(searchQuery)
}),
title: t('search.see-results', { searchQuery: searchQuery }), title: t('search.see-results', { searchQuery: searchQuery }),
_highlightResult: { _highlightResult: {
query: { query: {

View File

@ -0,0 +1,19 @@
import { clientLocale } from '../../../config/env.json';
const algoliaIndices = {
english: {
name: 'news',
searchPage: 'https://www.freecodecamp.org/news/search/'
},
espanol: {
name: 'news-es',
searchPage: 'https://www.freecodecamp.org/espanol/news/search/'
},
chinese: {
name: 'news-zh',
searchPage: 'https://chinese.freecodecamp.org/news/search/'
}
};
export const newsIndex = algoliaIndices[clientLocale].name;
export const searchPageUrl = algoliaIndices[clientLocale].searchPage;

View File

@ -57,22 +57,6 @@ describe('Navbar', () => {
} }
); );
it('Should be able to search on navbar search field', () => {
cy.get('.ais-SearchBox').within(() => {
cy.get('input').type('Learn');
});
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of.at.least', 1);
cy.get('.ais-SearchBox').within(() => {
cy.get('input').clear();
});
cy.get('div.ais-Hits').should('not.exist');
});
it('Should have a "Sign in" button', () => { it('Should have a "Sign in" button', () => {
cy.contains("[data-test-label='landing-small-cta']", 'Sign in'); cy.contains("[data-test-label='landing-small-cta']", 'Sign in');
}); });

View File

@ -0,0 +1,87 @@
/* global cy */
const search = query => {
cy.get('.ais-SearchBox').within(() => {
cy.get('input').type(query);
});
cy.wait(300);
};
const clear = () => {
cy.get('.ais-SearchBox').within(() => {
cy.get('input').clear();
});
};
describe('Search bar', () => {
before(() => {
cy.visit('/');
});
beforeEach(() => {
clear();
});
it('Should render properly', () => {
cy.get('.ais-SearchBox').should('be.visible');
});
it('Should accept input and display hits', () => {
search('freeCodeCamp');
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of.at.least', 1);
});
it('Should clear hits when input is cleared', () => {
search('freeCodeCamp');
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of.at.least', 1);
clear();
cy.get('div.ais-Hits').should('not.exist');
});
it('Should show up to 8 hits when height >= 768px', () => {
cy.viewport(1300, 768);
search('freeCodeCamp');
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of', 8);
});
it('Should show up to 5 hits when height < 768px', () => {
cy.viewport(1300, 767);
search('freeCodeCamp');
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of', 5);
});
it('Should show no hits for queries that do not exist in the Algolia index', () => {
search('testtttt');
cy.get('.ais-Hits-list')
.children()
.should('to.have.length.of', 0);
cy.contains('No tutorials found');
});
it('Should not redirect to the News search page if there are no hits', () => {
search('testtttt');
cy.get('.ais-SearchBox-form').submit();
cy.url('/');
});
});