Skip to content

Commit 54f59e0

Browse files
Leslie RoseLeslie Rose
Leslie Rose
authored and
Leslie Rose
committed
fixing articles.length on alert
1 parent 47e3aec commit 54f59e0

11 files changed

+25
-41
lines changed

client/public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
99
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
1010
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/4.0.0/journal/bootstrap.min.css"/>
11-
<title>NYT ARTICLE SCRUBBER</title>
11+
<title>📰 New York Times Article Scrubber 📰</title>
1212
</head>
1313

1414
<body>

client/src/pages/search/SearchArticles.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Col, Row, Container } from "../../components/Grid";
66
import { List, ListItem } from "../../components/List";
77
import { Input, FormBtn } from "../../components/Form";
88

9-
109
class SearchArticles extends Component {
1110
state = {
1211
search: "",
@@ -17,20 +16,23 @@ class SearchArticles extends Component {
1716
title: "",
1817
};
1918

20-
21-
2219
searchArticle = (event) => {
2320
event.preventDefault();
24-
21+
2522
nytapi.getArticles(this.state.search, this.state.startYear, this.state.endYear)
2623
.then(res => {
2724
console.log(res.data.response.docs)
2825
this.setState({ articles: res.data.response.docs });
2926
})
30-
.then(res =>
31-
alert("📰 Articles found! 📰"))
32-
.catch(err => console.log(err));
33-
}
27+
.then(res => {
28+
if (this.state.articles.length > 0) {
29+
alert("📰 Articles found! 📰")
30+
} else {
31+
alert("😢 No articles found! Try another search! 😢")
32+
}
33+
})
34+
.catch(err => console.log(err));
35+
}
3436

3537
deleteArticleFunction = (event) => {
3638
event.preventDefault();
@@ -46,8 +48,9 @@ class SearchArticles extends Component {
4648

4749
//Find the article from the articles array with the ID matching the saved button
4850
const articleSaveButtonContent = (this.state.articles.filter(element => element._id === event.target.id)[0]);
49-
nytapi.saveArticle({ title: articleSaveButtonContent.headline.main, author: articleSaveButtonContent.source, summary: articleSaveButtonContent.snippet, articleDate: articleSaveButtonContent.pub_date, link: articleSaveButtonContent.web_url
50-
})
51+
nytapi.saveArticle({
52+
title: articleSaveButtonContent.headline.main, author: articleSaveButtonContent.source, summary: articleSaveButtonContent.snippet, articleDate: articleSaveButtonContent.pub_date, link: articleSaveButtonContent.web_url
53+
})
5154
.then(res =>
5255
alert("📰 Article saved! 📰"))
5356
.catch(err => console.log(err));
@@ -117,26 +120,27 @@ class SearchArticles extends Component {
117120
<List>
118121
{this.state.articles.map(article => (
119122
<ListItem key={article._id}>
120-
<strong>
121-
<h2>{article.headline.main}</h2>
122-
</strong>
123-
<h5>Date published: {article.pub_date}</h5>
124-
<h6>{article.snippet}</h6>
123+
<strong>
124+
<h2>{article.headline.main}</h2>
125+
</strong>
126+
<h5>Date published: {article.pub_date}</h5>
127+
<h6>{article.snippet}</h6>
125128

126-
<a href={article.web_url} target="blank"> <span role="img" aria-label="newspaper1">📰</span> Read more here <span role="img" aria-label="newspaper2">📰</span></a>
129+
<a href={article.web_url} target="blank"> <span role="img" aria-label="newspaper1">📰</span> Read more here <span role="img" aria-label="newspaper2">📰</span></a>
127130
<SaveButton id={article._id} onClick={this.saveArticleFunction} />
128131
</ListItem>
129132
))}
130133
</List>
131-
) : (
134+
) :
135+
(
132136
<h3>No Results to Display</h3>
133137
)}
134138
</Col>
135139
</Row>
136140

137-
</Container>
138-
);
139-
}
141+
</Container>
142+
);
143+
}
140144
}
141145

142146

controllers/articlesController.js

-20
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,12 @@ module.exports = {
1515
.then(dbModel => res.json(dbModel))
1616
.catch(err => res.status(422).json(err));
1717
},
18-
update: function(req, res) {
19-
db.Article
20-
.findOneAndUpdate({ _id: req.params.id }, req.body)
21-
.then(dbModel => res.json(dbModel))
22-
.catch(err => res.status(422).json(err));
23-
},
2418
create: function (req, res) {
2519
db.Article
2620
.create(req.body)
2721
.then(dbModel => res.json(dbModel))
2822
.catch(err => res.status(422).json(err));
2923
},
30-
addNote: function (req, res) {
31-
db.Note
32-
.create(req.body)
33-
.then(dbNote => db.Article.findOneAndUpdate({ _id: req.params.id }, { note: dbNote._id }, { new: true }))
34-
.then(dbArticle => res.json(dbArticle))
35-
.catch(err => res.status(422).json(err));
36-
},
37-
deleteNote: function (req, res) {
38-
db.Note
39-
.remove(req.body)
40-
.then(dbNote => db.Article.findOneAndUpdate({ _id: req.params.id }, { note: dbNote._id }))
41-
.then(dbArticle => res.json(dbArticle))
42-
.catch(err => res.status(422).json(err));
43-
},
4424
remove: function (req, res) {
4525
db.Article
4626
.findById({ _id: req.params.id })

0 commit comments

Comments
 (0)