Listing 6-49: Filtering the Reminders List
function filterList(reminders,selectedFilter){
if (selectedFilter === "all"){
return reminders;
} else {
let numberOfDays;
switch(selectedFilter){
case "2day":
numberOfDays = 2;
break;
case "1week":
numberOfDays = 7;
break;
case "30days":
numberOfDays = 30;
break;
default:
numberOfDays = 0;
break;
}
const result = reminders.filter(reminder=>{
const todaysDate = new Date().toISOString().substr(0,10);
const todayTime = new Date(todaysDate).getTime();
const dueTime = new Date(reminder.dueDate).getTime();
return dueTime < (todayTime + (numberOfDays * 86400000));
});
return result;
}
}
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.