2018-07-06 13:47:25 +02:00
|
|
|
import React from 'react';
|
2018-07-16 09:27:24 +02:00
|
|
|
import Parser from 'html-react-parser';
|
2018-07-09 16:29:57 +02:00
|
|
|
import { PropTypes } from 'prop-types';
|
2018-07-17 16:23:38 +02:00
|
|
|
import getConfig from 'next/config';
|
2018-07-06 13:47:25 +02:00
|
|
|
import './style.scss';
|
|
|
|
|
2018-07-17 16:23:38 +02:00
|
|
|
const { publicRuntimeConfig } = getConfig();
|
|
|
|
|
2018-07-09 16:29:57 +02:00
|
|
|
const SearchResults = (props) => {
|
2018-07-13 13:39:01 +02:00
|
|
|
if (!props.data || props.data[0] === null) {
|
2018-07-09 16:29:57 +02:00
|
|
|
return <div>Loading...</div>;
|
2018-07-13 13:39:01 +02:00
|
|
|
} else if (props.data.length < 1) {
|
2018-07-16 17:13:29 +02:00
|
|
|
return <div>No results found</div>;
|
2018-07-09 16:29:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:43:25 +02:00
|
|
|
// sort array alphabetically
|
2018-07-10 14:25:23 +02:00
|
|
|
const sortedInterviews = props.data.sort((a, b) => a.name.localeCompare(b.name));
|
2018-07-13 12:43:51 +02:00
|
|
|
|
2018-07-17 17:34:40 +02:00
|
|
|
const trimText = (text, strpos, length) => {
|
|
|
|
let offset = 0;
|
2018-07-18 10:24:12 +02:00
|
|
|
let firstEllipses = '';
|
|
|
|
let lastEllipses = '';
|
2018-07-19 10:50:11 +02:00
|
|
|
const allowance = props.term.length + 5;
|
2018-07-19 14:57:53 +02:00
|
|
|
const cleanText = text.replace(/<(?:.|\n)*?>/gm, '');
|
2018-07-17 17:34:40 +02:00
|
|
|
|
2018-07-13 11:11:32 +02:00
|
|
|
if (text === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-07-19 10:48:21 +02:00
|
|
|
/**
|
|
|
|
* We check if the position of the matched term is greater than the length of the trim
|
|
|
|
* Then (strpos - length) would place our matched term at the beginning of the last character
|
2018-07-19 10:50:11 +02:00
|
|
|
* We add a further x characters as an allowance (based on term length)
|
|
|
|
* for the search term to show fully
|
2018-07-19 10:48:21 +02:00
|
|
|
*/
|
|
|
|
|
2018-07-19 09:57:16 +02:00
|
|
|
if (strpos > length && strpos !== -1 && length > 50 && text.length > length) {
|
2018-07-19 10:50:11 +02:00
|
|
|
offset = strpos - (length - allowance);
|
2018-07-19 14:57:53 +02:00
|
|
|
firstEllipses = '...';
|
2018-07-17 17:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:57:53 +02:00
|
|
|
const offsetText = cleanText.substr(offset, length + offset);
|
2018-07-18 10:24:12 +02:00
|
|
|
|
2018-07-19 14:57:53 +02:00
|
|
|
if (offsetText.substr(offsetText.length - 1, 1) !== '.') {
|
2018-07-18 10:24:12 +02:00
|
|
|
lastEllipses = '...';
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:57:53 +02:00
|
|
|
return cleanText.length <= length ? `<p>${offsetText}</p>` : `<p>${firstEllipses}${offsetText}${lastEllipses}</p>`;
|
2018-07-13 11:11:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const highlightTerm = (text) => {
|
2018-07-18 11:33:11 +02:00
|
|
|
const cleanTerm = props.term.replace(/[^a-zA-Z 0-9]+/g, '');
|
2018-07-13 11:11:32 +02:00
|
|
|
const regex = new RegExp(cleanTerm, 'ig');
|
2018-07-18 11:41:10 +02:00
|
|
|
return text.replace(regex, match => `<span>${match}</span>`);
|
2018-07-13 11:11:32 +02:00
|
|
|
};
|
|
|
|
|
2018-07-17 17:34:40 +02:00
|
|
|
const processText = (text, strpos, length = 500) => highlightTerm(trimText(text, strpos, length));
|
2018-07-13 11:45:34 +02:00
|
|
|
|
2018-07-17 17:34:40 +02:00
|
|
|
const findQuestion = (answer) => {
|
|
|
|
const { id } = answer;
|
2018-07-13 11:45:34 +02:00
|
|
|
const { text } = props.questions.find(question => question.id === id);
|
2018-07-17 17:34:40 +02:00
|
|
|
return text;
|
2018-07-13 11:45:34 +02:00
|
|
|
};
|
2018-07-10 13:43:25 +02:00
|
|
|
|
2018-07-17 15:12:34 +02:00
|
|
|
const interviewNameContainsTerm = (name, searchTerm) =>
|
|
|
|
name.toLowerCase().includes(searchTerm.toLowerCase());
|
2018-07-17 15:06:53 +02:00
|
|
|
|
2018-07-09 16:29:57 +02:00
|
|
|
return (
|
|
|
|
<div className="search-results">
|
|
|
|
<ul>
|
2018-07-10 13:43:25 +02:00
|
|
|
{ sortedInterviews.map(interview => (
|
2018-07-17 16:23:38 +02: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>
|
2018-07-18 10:24:12 +02:00
|
|
|
{interview.matchingQuestionAnswerPositions ?
|
|
|
|
interview.matchingQuestionAnswerPositions.map(match => (
|
2018-07-18 13:10:58 +02:00
|
|
|
<div className="question-wrap" key={match.index + 1}>
|
2018-07-17 17:34:40 +02:00
|
|
|
<h5>{match.index + 1})
|
2018-07-18 12:34:35 +02:00
|
|
|
{ findQuestion(match) }
|
2018-07-17 17:34:40 +02:00
|
|
|
</h5>
|
|
|
|
<div>
|
|
|
|
{ Parser(processText(match.answer, match.strpos)) }
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-07-18 10:24:12 +02:00
|
|
|
)) : ''}
|
2018-07-17 16:23:38 +02:00
|
|
|
</button>
|
2018-07-09 16:29:57 +02:00
|
|
|
</li>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SearchResults.propTypes = {
|
|
|
|
data: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
|
|
|
|
toggleSingleInterview: PropTypes.func.isRequired,
|
2018-07-13 11:11:32 +02:00
|
|
|
questions: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
|
|
|
|
term: PropTypes.string.isRequired,
|
2018-07-09 16:29:57 +02:00
|
|
|
};
|
2018-07-06 13:47:25 +02:00
|
|
|
|
|
|
|
export default SearchResults;
|