ETHReport/components/searchResults/index.js

95 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-07-06 11:47:25 +00:00
import React from 'react';
2018-07-16 07:27:24 +00:00
import Parser from 'html-react-parser';
2018-07-09 14:29:57 +00:00
import { PropTypes } from 'prop-types';
2018-07-17 14:23:38 +00:00
import getConfig from 'next/config';
2018-07-06 11:47:25 +00:00
import './style.scss';
2018-07-17 14:23:38 +00:00
const { publicRuntimeConfig } = getConfig();
2018-07-09 14:29:57 +00:00
const SearchResults = (props) => {
2018-07-13 11:39:01 +00:00
if (!props.data || props.data[0] === null) {
2018-07-09 14:29:57 +00:00
return <div>Loading...</div>;
2018-07-13 11:39:01 +00:00
} else if (props.data.length < 1) {
2018-07-16 15:13:29 +00:00
return <div>No results found</div>;
2018-07-09 14:29:57 +00:00
}
2018-07-10 11:43:25 +00:00
// sort array alphabetically
2018-07-10 12:25:23 +00:00
const sortedInterviews = props.data.sort((a, b) => a.name.localeCompare(b.name));
2018-07-13 10:43:51 +00:00
2018-07-13 09:11:32 +00:00
const trimText = (text, length) => {
if (text === null) {
return '';
}
return text.length <= length ? text : `${text.substr(0, length)}...`;
};
const highlightTerm = (text) => {
const cleanTerm = props.term.replace(/[^a-zA-Z 0-9]+/g, '').toLowerCase();
const regex = new RegExp(cleanTerm, 'ig');
return text.replace(regex, `<span>${cleanTerm}</span>`);
};
2018-07-17 13:06:53 +00:00
const processText = (text, length = 500) => highlightTerm(trimText(text, length));
2018-07-13 09:45:34 +00:00
const findFirstQuestion = (interview) => {
let { answer } = interview.interview[interview.matchedIndex];
let id = interview.interview[interview.matchedIndex].question;
if (answer === null) {
const firstNonNullAnswer = interview.interview.find(question => question.answer !== null);
id = firstNonNullAnswer.question;
2018-07-16 07:39:44 +00:00
// eslint-disable-next-line
2018-07-13 09:45:34 +00:00
answer = firstNonNullAnswer.answer;
}
const { text } = props.questions.find(question => question.id === id);
return {
question: text,
answer: processText(answer),
};
};
2018-07-10 11:43:25 +00:00
2018-07-17 13:12:34 +00:00
const interviewNameContainsTerm = (name, searchTerm) =>
name.toLowerCase().includes(searchTerm.toLowerCase());
2018-07-17 13:06:53 +00:00
2018-07-09 14:29:57 +00:00
return (
<div className="search-results">
<ul>
2018-07-10 11:43:25 +00:00
{ sortedInterviews.map(interview => (
2018-07-17 14:23:38 +00:00
<li key={interview.id}>
<button id={interview.id} onClick={props.toggleSingleInterview}>
<div className="li-header">
<h3 className={interviewNameContainsTerm(interview.name, props.term) ? 'matched-name' : ''}>
{ Parser(interview.name) }
</h3>
<div>
<span>View</span>
<img src={`${publicRuntimeConfig.subDirPath}/static/img/right-chevron-icon.svg`} alt="right chevron icon" />
</div>
</div>
<h5>{interview.matchedIndex + 1})&nbsp;
{ findFirstQuestion(interview).question }
</h5>
<div>
{ Parser(findFirstQuestion(interview).answer) }
</div>
</button>
2018-07-09 14:29:57 +00:00
</li>
))
}
</ul>
</div>
);
};
SearchResults.propTypes = {
data: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
toggleSingleInterview: PropTypes.func.isRequired,
2018-07-13 09:11:32 +00:00
questions: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
term: PropTypes.string.isRequired,
2018-07-09 14:29:57 +00:00
};
2018-07-06 11:47:25 +00:00
export default SearchResults;