Skip to content

Commit 0c30232

Browse files
committed
Use date-fns
1 parent 47ad9a7 commit 0c30232

2 files changed

Lines changed: 62 additions & 41 deletions

File tree

src/dashboard/categories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const categories = data.map(({ id, name, description, img }) => ({ id, na
99
export const getPapers = ({ id }) => {
1010
let papers = [];
1111
const category = data.find(cat => cat.id === id);
12-
papers = category && category.papers.map(toDate).sort(byDate).slice(1);
12+
papers = category && category.papers.map(toDate).sort(byDate);
1313
return papers;
1414
};
1515

src/dashboard/category-results-screen.js

Lines changed: 61 additions & 40 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 } from 'date-fns';
4+
import { format, sub, isWithinInterval } from 'date-fns';
55

66
function Paper ({ paper }) {
77
return h('div', { class: 'paper' }, [
@@ -18,57 +18,78 @@ 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 = format(now, 'yyyy-MM-dd');
2222
const displayPapers = [];
2323

24-
if (range === 'today') {
25-
for (const paper of papers) {
26-
const paperDate = format(paper.date, 'yyyy-MM-dd');
27-
if (paperDate === today) {
28-
displayPapers.push(paper);
29-
}
30-
}
31-
} else if (range === 'days') { // within the past 4 days
32-
const startOffset = { days: 4 };
33-
const start = format(sub(now, startOffset), 'yyyy-MM-dd');
24+
for (const paper of papers) {
25+
const paperDate = new Date(paper.date);
3426

35-
for (const paper of papers) {
36-
const paperDate = format(paper.date, 'yyyy-MM-dd');
37-
if (paperDate >= start && paperDate < today) {
38-
displayPapers.push(paper);
39-
}
27+
if (range === 'today' && isWithinInterval(paperDate, { start: now, end: now })) { // Today's papers
28+
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
30+
displayPapers.push(paper);
31+
} else if (range === 'week' && isWithinInterval(paperDate, { start: sub(now, { weeks: 1 }), end: sub(now, { days: 4 }) })) { // Papers from past week
32+
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
34+
displayPapers.push(paper);
35+
} else {
36+
console.log('didnt work');
4037
}
41-
} else if (range === 'week') {
42-
const startOffset = { weeks: 1 };
43-
const endOffset = { days: 4 };
44-
const start = format(sub(now, startOffset), 'yyyy-MM-dd');
45-
const end = format(sub(now, endOffset), 'yyyy-MM-dd');
38+
}
4639

47-
for (const paper of papers) {
48-
const paperDate = format(paper.date, 'yyyy-MM-dd');
49-
if (paperDate >= start && paperDate < end) { // within the past week
50-
displayPapers.push(paper);
51-
}
52-
}
53-
} else {
54-
const startOffset = { months: 1 };
55-
const endOffset = { weeks: 1 };
56-
const start = format(sub(now, startOffset), 'yyyy-MM-dd');
57-
const end = format(sub(now, endOffset), 'yyyy-MM-dd');
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');
5852

59-
for (const paper of papers) {
60-
const paperDate = format(paper.date, 'yyyy-MM-dd');
61-
if (paperDate >= start && paperDate < end) { // everything else within the past month
62-
displayPapers.push(paper);
63-
}
64-
}
65-
}
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+
// }
6684
return displayPapers;
6785
}
6886

6987
export default function CategoryResultsScreen ({ store }) {
7088
const { selectedPapers, selectedCategory } = store;
7189
const todayPapers = findRelativeDate(selectedPapers, 'today');
90+
console.log('TODAYS PAPERS: ');
91+
console.log(todayPapers);
92+
7293
const fewDaysPapers = findRelativeDate(selectedPapers, 'days');
7394
const weekPapers = findRelativeDate(selectedPapers, 'week');
7495
const monthPapers = findRelativeDate(selectedPapers, 'month');

0 commit comments

Comments
 (0)