136 lines
4.0 KiB
JavaScript
Raw Normal View History

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-18 10:41:29 +02:00
const getStartOffset = (text) => {
if (text.indexOf('>') === 0) {
return -2;
} else if (text.indexOf('/p>') === 0) {
return 3;
} else if (text.indexOf('p>') === 0) {
return -1;
} else if (text.indexOf('<p>') !== 0) {
return false;
2018-07-18 10:24:12 +02:00
}
2018-07-18 10:41:29 +02:00
return 0;
};
const getEndOffset = (text) => {
if (text.substr(text.length - 1, 1) === '<') {
return -1;
} else if (text.substr(text.length - 2, 2) === '<p') {
return -2;
} else if (text.substr(text.length - 3, 3) === '<p>') {
return -3;
2018-07-18 10:24:12 +02:00
}
2018-07-18 10:41:29 +02:00
return 0;
2018-07-18 10:24:12 +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-18 10:41:29 +02:00
let startOffset = 0;
let endOffset = 0;
2018-07-17 17:34:40 +02:00
2018-07-13 11:11:32 +02:00
if (text === null) {
return '';
}
2018-07-17 17:34:40 +02:00
if (strpos > length && strpos !== -1 && length > 50) {
offset = strpos - length;
2018-07-18 10:24:12 +02:00
firstEllipses = '<p>...</p>';
2018-07-17 17:34:40 +02:00
}
2018-07-18 10:41:29 +02:00
const offsetText = text.substr(offset, length + offset);
startOffset = getStartOffset(offsetText);
endOffset = getEndOffset(offsetText);
const newOffsetText = startOffset ?
text.substr(offset + startOffset, length + offset + endOffset) :
`<p>${text.substr(offset + 0, length + offset + endOffset)}`;
2018-07-18 10:24:12 +02:00
2018-07-18 10:41:29 +02:00
if (newOffsetText.substr(newOffsetText.length - 1, 1) !== '.' && newOffsetText.substr(newOffsetText.length - 1, 1) !== '>') {
2018-07-18 10:24:12 +02:00
lastEllipses = '...';
}
2018-07-18 10:41:29 +02:00
return text.length <= length ? text : `${firstEllipses}${newOffsetText}${lastEllipses}`;
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-17 17:34:40 +02:00
<div>
<h5>{match.index + 1})&nbsp;
{ findQuestion(match) }
</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;