From 538ee2524160a773c191564016e2d2779ef9d624 Mon Sep 17 00:00:00 2001 From: dan-searle Date: Thu, 24 Jan 2019 13:48:07 +0000 Subject: [PATCH] Extend Component class instead of using x-interaction --- components/x-topic-search/package.json | 1 - .../x-topic-search/src/ResultContainer.jsx | 10 +- components/x-topic-search/src/TopicSearch.jsx | 154 +++++++++--------- 3 files changed, 84 insertions(+), 81 deletions(-) diff --git a/components/x-topic-search/package.json b/components/x-topic-search/package.json index fd441f3b8..22c52604e 100644 --- a/components/x-topic-search/package.json +++ b/components/x-topic-search/package.json @@ -19,7 +19,6 @@ "dependencies": { "@financial-times/x-engine": "file:../../packages/x-engine", "@financial-times/x-follow-button": "0.0.11", - "@financial-times/x-interaction": "^1.0.0-beta.6", "classnames": "^2.2.6", "debounce-promise": "^3.1.0" }, diff --git a/components/x-topic-search/src/ResultContainer.jsx b/components/x-topic-search/src/ResultContainer.jsx index 9fd251ba0..64f7051ce 100644 --- a/components/x-topic-search/src/ResultContainer.jsx +++ b/components/x-topic-search/src/ResultContainer.jsx @@ -29,24 +29,24 @@ const arrayToSentence = followedSuggestions => { }; -export default ({ result, searchTerm, csrfToken, followedTopicIds }) => { +export default ({ followedSuggestions, searchTerm, csrfToken, followedTopicIds, unfollowedSuggestions }) => { - const hasFollowedSuggestions = result.followedSuggestions.length > 0; - const hasUnfollowedSuggestions = result.unfollowedSuggestions.length > 0; + const hasFollowedSuggestions = followedSuggestions.length > 0; + const hasUnfollowedSuggestions = unfollowedSuggestions.length > 0; return (
{ hasUnfollowedSuggestions && } { !hasUnfollowedSuggestions && hasFollowedSuggestions &&
- You already follow { arrayToSentence(result.followedSuggestions) } + You already follow { arrayToSentence(followedSuggestions) }
} { !hasUnfollowedSuggestions && !hasFollowedSuggestions && diff --git a/components/x-topic-search/src/TopicSearch.jsx b/components/x-topic-search/src/TopicSearch.jsx index 4a5e93a9c..cb00d8cbd 100644 --- a/components/x-topic-search/src/TopicSearch.jsx +++ b/components/x-topic-search/src/TopicSearch.jsx @@ -1,5 +1,4 @@ -import { h } from '@financial-times/x-engine'; -import { withActions } from '@financial-times/x-interaction'; +import { h, Component } from '@financial-times/x-engine'; import styles from './TopicSearch.scss'; import classNames from 'classnames'; import getSuggestions from './lib/get-suggestions.js'; @@ -7,89 +6,94 @@ import debounce from 'debounce-promise'; import ResultContainer from './ResultContainer'; -const debounceGetSuggestions = debounce(getSuggestions, 150); +class TopicSearch extends Component { + constructor(props) { + super(props); + + this.minSearchLength = props.minSearchLength || 2; + this.maxSuggestions = props.maxSuggestions || 5; + this.apiUrl = props.apiUrl; + this.getSuggestions = debounce(getSuggestions, 150); + this.handleInputChange = this.handleInputChange.bind(this); + this.handleInputClickOrFocus = this.handleInputClickOrFocus.bind(this); + + this.state = { + followedTopicIds: [], + searchTerm: '', + showResult: false, + followedSuggestions: [], + unFollowedSuggestions: [] + }; + } -let resultExists = false; + handleInputChange(event) { + const searchTerm = event.target.value.trim(); -const topicSearchActions = withActions(({ minSearchLength = 2, maxSuggestions = 5, apiUrl, followedTopicIds = [] }) => ({ - async checkInput(event) { - const searchTerm = event.target.value && event.target.value.trim(); + this.setState({ searchTerm }); - if (searchTerm.length >= minSearchLength) { - return debounceGetSuggestions(searchTerm, maxSuggestions, apiUrl, followedTopicIds) - .then(result => { - resultExists = true; - return { showResult: true, result, searchTerm }; + if (searchTerm.length >= this.minSearchLength) { + this.getSuggestions(searchTerm, this.maxSuggestions, this.apiUrl, this.state.followedTopicIds) + .then(({ followedSuggestions, unfollowedSuggestions }) => { + this.setState({ + followedSuggestions, + unfollowedSuggestions, + showResult: true + }); }) .catch(() => { - resultExists = false; - return { showResult: false }; + this.setState({ + showResult: false + }); }); } else { - resultExists = false; - return Promise.resolve({ showResult: false }); - } - }, - - topicFollowed (subjectId) { - if (!followedTopicIds.includes(subjectId)) { - followedTopicIds.push(subjectId); - } - - return { followedTopicIds }; - }, - - topicUnfollowed (subjectId) { - const targetIdIndex = followedTopicIds.indexOf(subjectId); - - if (targetIdIndex > -1) { - followedTopicIds.splice(targetIdIndex, 1); + this.setState({ + showResult: false + }); } - - return { followedTopicIds }; - }, - - selectInput (event) { - event.target.select(); - return { showResult: resultExists }; - }, - - hideResult() { - return { showResult: false }; } -})); - -const TopicSearch = topicSearchActions(({ searchTerm, showResult, result, actions, isLoading, csrfToken, followedTopicIds }) => ( -
-

- Search for topics, authors, companies, or other areas of interest -

- -
- - -
- - { showResult && !isLoading && - } + handleInputClickOrFocus() { + console.log('handleInputClickOrFocus'); + // this.setState({ + // showResult: true + // }); + } -
-)); + render() { + const { csrfToken, followedSuggestions, followedTopicIds, isLoading, searchTerm, showResult, unfollowedSuggestions } = this.state; + + return ( +
+

+ Search for topics, authors, companies, or other areas of interest +

+ + +
+ + +
+ + { showResult && !isLoading && + } +
+ ); + } +} export { TopicSearch };