Skip to content

Commit 7bccd39

Browse files
committed
Finalize findRelativeDate function
1 parent 0c30232 commit 7bccd39

2 files changed

Lines changed: 11 additions & 58 deletions

File tree

src/dashboard/category-results-screen.js

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { h } from 'preact';
22
import { Link } from 'preact-router/match';
33
import CategoryCard from './category-card.js';
4-
import { format, sub, isWithinInterval } from 'date-fns';
4+
import { sub } from 'date-fns';
55

66
function Paper ({ paper }) {
77
return h('div', { class: 'paper' }, [
@@ -18,78 +18,31 @@ function Paper ({ paper }) {
1818

1919
function findRelativeDate (papers, range) {
2020
const now = new Date();
21-
// const today = format(now, 'yyyy-MM-dd');
21+
const today = now.toISOString().split('T')[0];
22+
const daysAgo = sub(now, { days: 4 }).toISOString();
23+
const weekAgo = sub(now, { weeks: 1 }).toISOString();
24+
const monthAgo = sub(now, { months: 1 }).toISOString();
2225
const displayPapers = [];
2326

2427
for (const paper of papers) {
25-
const paperDate = new Date(paper.date);
28+
const paperDate = paper.date.toISOString().split('T')[0];
2629

27-
if (range === 'today' && isWithinInterval(paperDate, { start: now, end: now })) { // Today's papers
30+
if (range === 'today' && paperDate === today) { // Today's papers
2831
displayPapers.push(paper);
29-
} else if (range === 'days' && isWithinInterval(paperDate, { start: sub(now, { days: 4 }), end: sub(now, { days: 1 }) })) { // Papers from 4 days ago
32+
} else if (range === 'days' && paperDate >= daysAgo && paperDate < today) { // Papers from 4 days ago
3033
displayPapers.push(paper);
31-
} else if (range === 'week' && isWithinInterval(paperDate, { start: sub(now, { weeks: 1 }), end: sub(now, { days: 4 }) })) { // Papers from past week
34+
} else if (range === 'week' && paperDate >= weekAgo && paperDate < daysAgo) { // Papers from past week
3235
displayPapers.push(paper);
33-
} else if (range === 'month' && isWithinInterval(paperDate, { start: sub(now, { months: 1 }), end: sub(now, { weeks: 1 }) })) { // Rest of the papers from the past month
36+
} else if (range === 'month' && paperDate >= monthAgo && paperDate < weekAgo) { // Rest of the papers from the past month
3437
displayPapers.push(paper);
35-
} else {
36-
console.log('didnt work');
3738
}
3839
}
39-
40-
// if (range === 'today') {
41-
// console.log('today date: ' + today);
42-
// for (const paper of papers) {
43-
// const paperDate = format(paper.date, 'yyyy-MM-dd');
44-
// console.log('PAPER DATE: ' + paperDate);
45-
// if (paperDate === today) {
46-
// displayPapers.push(paper);
47-
// }
48-
// }
49-
// } else if (range === 'days') { // within the past 4 days
50-
// const startOffset = { days: 4 };
51-
// const start = format(sub(now, startOffset), 'yyyy-MM-dd');
52-
53-
// for (const paper of papers) {
54-
// const paperDate = format(paper.date, 'yyyy-MM-dd');
55-
// if (paperDate >= start && paperDate < today) {
56-
// displayPapers.push(paper);
57-
// }
58-
// }
59-
// } else if (range === 'week') {
60-
// const startOffset = { weeks: 1 };
61-
// const endOffset = { days: 4 };
62-
// const start = format(sub(now, startOffset), 'yyyy-MM-dd');
63-
// const end = format(sub(now, endOffset), 'yyyy-MM-dd');
64-
65-
// for (const paper of papers) {
66-
// const paperDate = format(paper.date, 'yyyy-MM-dd');
67-
// if (paperDate >= start && paperDate < end) { // within the past week
68-
// displayPapers.push(paper);
69-
// }
70-
// }
71-
// } else {
72-
// const startOffset = { months: 1 };
73-
// const endOffset = { weeks: 1 };
74-
// const start = format(sub(now, startOffset), 'yyyy-MM-dd');
75-
// const end = format(sub(now, endOffset), 'yyyy-MM-dd');
76-
77-
// for (const paper of papers) {
78-
// const paperDate = format(paper.date, 'yyyy-MM-dd');
79-
// if (paperDate >= start && paperDate < end) { // everything else within the past month
80-
// displayPapers.push(paper);
81-
// }
82-
// }
83-
// }
8440
return displayPapers;
8541
}
8642

8743
export default function CategoryResultsScreen ({ store }) {
8844
const { selectedPapers, selectedCategory } = store;
8945
const todayPapers = findRelativeDate(selectedPapers, 'today');
90-
console.log('TODAYS PAPERS: ');
91-
console.log(todayPapers);
92-
9346
const fewDaysPapers = findRelativeDate(selectedPapers, 'days');
9447
const weekPapers = findRelativeDate(selectedPapers, 'week');
9548
const monthPapers = findRelativeDate(selectedPapers, 'month');

src/dashboard/category-selection-screen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function CategorySelectionScreen ({ store }) {
88

99
return h('div', { class: 'category-selection-screen' }, [
1010
h('div', { class: 'app-name' }, 'The Digest'),
11-
h('div', { class: 'app-tagline' }, 'Quick acess to the latest papers in the biomedicine.'),
11+
h('div', { class: 'app-tagline' }, 'Quick access to the latest papers in the biomedicine.'),
1212
h('div', { class: 'categories' }, (haveCategories
1313
? categories.map(category => h(CategoryCard, { category, store }))
1414
: h('div', {}, 'No categories!')

0 commit comments

Comments
 (0)