Skip to content

Commit

Permalink
fixed search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangrunest committed May 26, 2019
1 parent 789d047 commit 385812e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 5 additions & 1 deletion models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const ArticleSchema = new Schema({
type: String,
required: true
},
identifier: {
category: {
type: String,
required: true
},
criteria: {
type: String,
required: true
},
Expand Down
13 changes: 6 additions & 7 deletions public/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// grab articles from db based on localstorage terms
console.log(localStorage.getItem('category'));
console.log(localStorage.getItem('criteria'));
// console.log(localStorage.getItem('category'));
// console.log(localStorage.getItem('criteria'));

if ( localStorage.getItem('category') != null ) {

console.log('getting data!');
$.getJSON(`/articles/${localStorage.getItem('category')}/${localStorage.getItem('criteria')}`, data => {
console.log(data);
// append articles to page on load
for (var i = 0; i < data.length; i++) {
$("#articles").append(`<p data-id='${data[i]._id}'>${data[i].title}<br /><a href="${data[i].link}"> Link to Ad</a><br /> ${data[i].price}</p>`);
Expand All @@ -16,7 +19,7 @@ checkCategory = category => {
let searchCategory;
if (category === null) {
searchCategory = '';
} else if (category === 'Motorycles') {
} else if (category === 'Motorcycles') {
searchCategory = 'mca';
} else if (category === 'Furniture') {
searchCategory = 'fua';
Expand All @@ -28,13 +31,9 @@ checkCategory = category => {

// ajax request for scraping craigslist
$('#srch-submit').on('click', () => {
console.log('button clicked!');
event.preventDefault();
console.log($('#srch-category').val());
const category = checkCategory($('#srch-category').val());
const criteria = $('#srch-criteria').val();
console.log(`category is ${category}`);
console.log(criteria);

// set localstorage values so when page reloads, ajax request gets search fields from db
localStorage.setItem('category', category);
Expand Down

0 comments on commit 385812e

Please sign in to comment.