ETHReport/components/browseArchives/index.js

167 lines
5.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
2018-07-09 14:29:57 +00:00
import _ from 'lodash';
2018-07-06 12:36:22 +00:00
import RelatedInterviewsList from '../interviews/relatedInterviewsList';
import InterviewsList from '../interviews/interviewsList';
import SingleInterview from '../interviews/singleInterview';
import TopicsList from '../topicsList';
import ProjectsList from '../projectsList';
import SearchBar from '../searchBar';
import SearchResults from '../searchResults';
2018-07-09 14:29:57 +00:00
import InterviewsData from '../../data/archives/interviews';
import './style.scss';
class BrowseArchives extends React.Component {
constructor(props) {
super(props);
this.state = {
term: '',
2018-07-09 14:29:57 +00:00
searchResults: [],
isSingleInterviewModalOpen: false,
isInterviewsListModalOpen: false,
activeSingleInterviewId: 1,
isSearchActive: false,
};
2018-07-06 13:49:24 +00:00
2018-07-09 14:29:57 +00:00
this.onSearchInputChange = this.onSearchInputChange.bind(this);
this.toggleSingleInterview = this.toggleSingleInterview.bind(this);
this.toggleInterviewsListModal = this.toggleInterviewsListModal.bind(this);
2018-07-09 14:29:57 +00:00
this.setSearchTerm = this.setSearchTerm.bind(this);
this.clearSearchInput = this.clearSearchInput.bind(this);
}
2018-07-09 14:29:57 +00:00
onSearchInputChange = (event) => {
const { term } = this.state;
const getSearchResults = _.debounce(() => { this.getSearchResults(term); }, 500);
this.setState({
term: event.target.value,
isSearchActive: true,
});
if (event.target.value.length === 0) {
2018-07-06 12:36:22 +00:00
this.setState({ isSearchActive: false });
}
2018-07-09 14:29:57 +00:00
// Throttle search result frequency with debounce
getSearchResults(term);
}
2018-07-09 14:29:57 +00:00
getSearchResults = (term) => {
// Edit function to do actual search based on term and update searchResults with dynamic data
// eslint-disable-next-line
console.log(`Get search results array based on searching for: ${term}`);
// Using static full interviews data array for now
2018-07-09 14:29:57 +00:00
this.setState({
searchResults: InterviewsData,
});
}
getSelectedInterview = () => {
2018-07-06 12:36:22 +00:00
const { activeSingleInterviewId } = this.state;
2018-07-09 14:29:57 +00:00
const selectedInterview = InterviewsData.find(item => item.id === activeSingleInterviewId);
2018-07-06 12:36:22 +00:00
return selectedInterview;
}
2018-07-09 14:29:57 +00:00
setSearchTerm = (event) => {
this.setState({
term: event.target.innerText,
isSearchActive: true,
});
}
clearSearchInput = () => {
this.setState({
isSearchActive: false,
term: '',
});
}
toggleInterviewsListModal = () => {
const { isInterviewsListModalOpen } = this.state;
2018-07-06 13:49:24 +00:00
this.setState({
isInterviewsListModalOpen: !isInterviewsListModalOpen,
isSingleInterviewModalOpen: false,
});
}
2018-07-09 14:29:57 +00:00
toggleSingleInterview = (event) => {
const { isSingleInterviewModalOpen, isInterviewsListModalOpen } = this.state;
2018-07-06 13:49:24 +00:00
2018-07-09 14:29:57 +00:00
let clickedInterview = event.target;
while (clickedInterview.id === '') {
clickedInterview = clickedInterview.parentNode;
}
const clickedSingleInterviewId = clickedInterview.id;
this.setState({
isInterviewsListModalOpen: !isInterviewsListModalOpen,
isSingleInterviewModalOpen: !isSingleInterviewModalOpen,
2018-07-09 14:29:57 +00:00
activeSingleInterviewId: Number(clickedSingleInterviewId),
});
}
render() {
2018-07-06 12:36:22 +00:00
const {
isSingleInterviewModalOpen,
isInterviewsListModalOpen,
isSearchActive,
activeSingleInterviewId,
term,
2018-07-09 14:29:57 +00:00
searchResults,
2018-07-06 12:36:22 +00:00
} = this.state;
return (
<div className="browse-wrap">
2018-07-09 14:29:57 +00:00
<SearchBar
onSearchInputChange={this.onSearchInputChange}
clearSearchInput={this.clearSearchInput}
isSearchActive={isSearchActive}
term={term}
/>
<div className="browse-content-wrap container">
<div className="browse-content-left">
2018-07-06 12:36:22 +00:00
{isSearchActive &&
(<RelatedInterviewsList
2018-07-09 14:29:57 +00:00
data={InterviewsData}
2018-07-06 12:36:22 +00:00
toggleSingleInterview={this.toggleSingleInterview}
/>)
}
</div>
<div className="browse-content-right">
2018-07-09 14:29:57 +00:00
{isSearchActive ? (
<SearchResults
data={searchResults}
toggleSingleInterview={this.toggleSingleInterview}
/>) :
2018-07-06 12:36:22 +00:00
(
<React.Fragment>
<InterviewsList
2018-07-09 14:29:57 +00:00
data={InterviewsData}
isInterviewsListModalOpen={isInterviewsListModalOpen}
toggleSingleInterview={this.toggleSingleInterview}
toggleInterviewsListModal={this.toggleInterviewsListModal}
/>
2018-07-09 14:29:57 +00:00
<TopicsList setSearchTerm={this.setSearchTerm} />
<ProjectsList setSearchTerm={this.setSearchTerm} />
</React.Fragment>
)
}
</div>
{isSingleInterviewModalOpen &&
(<SingleInterview
activeSingleInterviewId={activeSingleInterviewId}
selectedInterview={this.getSelectedInterview()}
toggleSingleInterview={this.toggleSingleInterview}
/>)
}
</div>
</div>
);
}
}
export default BrowseArchives;