From bd4aca06d7a4ac1aac94708427433127bcdc523c Mon Sep 17 00:00:00 2001 From: brian Date: Mon, 15 Mar 2021 17:22:52 -0400 Subject: [PATCH] sorting works, on mock api server --- api-stub.js | 8 +++++++- handlers/App.js | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/api-stub.js b/api-stub.js index e8024f8..b704c18 100644 --- a/api-stub.js +++ b/api-stub.js @@ -20,7 +20,13 @@ endpoints.post['/load-notes'] = function(resolve, reject, body){ }; endpoints.post['/search'] = function(resolve, reject, body){ - const notes = state.notes.filter(n => n.text.indexOf(body.search_term) !== -1 ); + const notes = state.notes.filter(n => n.text.indexOf(body.search_term) !== -1 ) + if(body.sorting==='new->old'){ + notes.sort((a,b) => a.modified-b.modified); + } + else{ + notes.sort((a,b) => b.modified-a.modified); + } resolve( {status: 'ok', notes} ); }; diff --git a/handlers/App.js b/handlers/App.js index b68396c..7c48668 100644 --- a/handlers/App.js +++ b/handlers/App.js @@ -9,7 +9,7 @@ const load_notes = function(state, dispatch){ }); }; const runSearch = function(state, dispatch){ - api.post('/search', {search_term: state.search_term}) + api.post('/search', {search_term: state.search_term, sorting: state.sorting}) .then(res=>{ dispatch({type:'update-search-results', notes: res.notes}); }); @@ -26,6 +26,7 @@ const search_term_change_handler = function(state, dispatch, e){ } }; const sorting_change_handler = function(state, dispatch, e){ + runSearch(state, dispatch); dispatch({type:'update-sorting', sorting: e.target.value}); };