I have finished the assignment but would love some feedback. Data.json contains news articles data.
Requirements for assignment:
Fetch the data and render it on the page. Initially the page should show only a list of titles fetched from the source. On clicking the title it should show the relevant image and content of the article associated with the title as well as related stories, if available. There should be a “read more” link to the article source. Above the list of articles there should be a search field for filtering the articles. Upon typing either the title or a keyword from the article content the list should be filtered to show only relevant results from an already rendered content. The filtering should occur as the user is typing. Additionally the user should be able to filter by date published.
Restrictions:
You have one week to complete this task. You are not allowed to use jQuery or any other libraries, including helper libraries such as templating ones. Instead, use core Javascript features. You can use either ECMAScript5 or ECMAScript6 (ES2015) features. Code should be clean, modular and reusable.
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.json");
xhr.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
var responseParse = JSON.parse(this.responseText);
var articles = responseParse.results;
//sort articles by date published
var filterByLatestDate = document.getElementById('filterByLatestDate');
var filterByOldestDate = document.getElementById('filterByOldestDate');
filterByOldestDate.addEventListener("click", function() {
articles.sort(function(a, b){
return new Date(a.publishedDate) - new Date(b.publishedDate);
})
addDOM(articles);
})
filterByLatestDate.addEventListener("click", function() {
articles.sort(function(a, b){
return new Date(b.publishedDate) - new Date(a.publishedDate);
})
addDOM(articles);
})
addDOM(articles);
}
};
xhr.send();
//adding DOM element when data recieved
var addDOM = function(data) {
console.log(data);
var articleList = '<ul>';
data.forEach(function(article,index){
var relatedStories = article.relatedStories;
articleList += '<li id="article" onclick= "basanti('+index+')">';
articleList += '<input type="checkbox" name="article-heading" id="article-heading'+index+'"><label class="title" for="article-heading'+index+'">'+ article.titleNoFormatting +'</label>';
articleList += '<div id="article-detail'+index+'" style="display:none">';
articleList += '<img src="'+article.image.url+'" alt="'+ article.titleNoFormatting +'" style="height:'+article.image.tbHeight+';width:'+article.image.tbWidth+';" />';
articleList += '<p class="content">'+ article.content+'</p>';
articleList += '<a href="'+article.unescapedUrl+'"> Read more.. </a>';
if(relatedStories){
relatedStories.forEach(function(articleRelatedStories){
articleList += '<ul id="related-stories">';
articleList += '<li><a href="'+articleRelatedStories.unescapedUrl+'">'+articleRelatedStories.titleNoFormatting+'</a></li>';
articleList += '</ul>';
});
};
articleList += '</div>';
articleList += '</li>';
})
articleList += '</ul>';
document.getElementById('article-list').innerHTML = articleList;
}
//checkbox when clicked on title the details will open
var basanti = function (index){
var articleDetail = document.getElementById('article-detail'+index+'');
var articleHeading = document.getElementById('article-heading'+index+'');
if(articleHeading.checked){
articleDetail.style.display = 'block';
}else {
articleDetail.style.display = 'none';
}
}
//filtering by title and content
var filterByContentTitle = function() {
var searchNode = document.getElementById('searchFilter')
searchText = searchNode.value,
titles = document.getElementsByClassName('title'),
contents = document.getElementsByClassName('content')
regexp = new RegExp(searchText,'i');
for (var i = 0; i < titles.length; i++) {
var title = titles[i];
var content = contents[i];
(title.textContent.search(regexp) < 0 && content.textContent.search(regexp) < 0 ) ?
title.parentNode.style.display = 'none':
title.parentNode.style.display = 'block';
}
}
//html
<body>
<input type="search" id="searchFilter" onkeyup="filterByContentTitle()" />
<input type="radio" id="filterByLatestDate" name="filterByDate" />Filter By Latest Date
<input type="radio" id="filterByOldestDate" name="filterByDate" />Filter By OldestDate Date
<div id="article-list"></div>
<script type="text/javascript" src="app.js"></script>
</body>