Skip to content

Commit 38788dd

Browse files
authored
Merge pull request #45 from PathwayCommons/27-categorize-article-collection-by-date
Categorize papers by recency
2 parents 41afe32 + 531e02f commit 38788dd

4 files changed

Lines changed: 73 additions & 8 deletions

File tree

.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@
7575
],
7676
"program": "${workspaceFolder}/src/data-search.js",
7777
"console": "integratedTerminal"
78+
},
79+
{
80+
"type": "node",
81+
"request": "launch",
82+
"name": "App",
83+
"skipFiles": [
84+
"<node_internals>/**"
85+
],
86+
"program": "${workspaceFolder}/src/dashboard/app.js",
87+
"console": "integratedTerminal"
7888
}
7989
]
8090
}

src/dashboard/categories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const toDate = o => {
66
};
77
const byDate = (a, b) => { return b.date - a.date; };
88
export const categories = data.map(({ id, name, description, img }) => ({ id, name, description, img }));
9-
export const getPapers = ({ id, limit = 100 }) => {
9+
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, limit);
12+
papers = category && category.papers.map(toDate).sort(byDate);
1313
return papers;
1414
};
1515

src/dashboard/category-results-screen.js

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

56
function Paper ({ paper }) {
67
return h('div', { class: 'paper' }, [
@@ -15,19 +16,73 @@ function Paper ({ paper }) {
1516
]);
1617
}
1718

19+
function findRelativeDate (papers, range) {
20+
const now = new Date();
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();
25+
const displayPapers = [];
26+
27+
for (const paper of papers) {
28+
const paperDate = paper.date.toISOString().split('T')[0];
29+
30+
if (range === 'today' && paperDate === today) { // Today's papers
31+
displayPapers.push(paper);
32+
} else if (range === 'days' && paperDate >= daysAgo && paperDate < today) { // Papers from 4 days ago
33+
displayPapers.push(paper);
34+
} else if (range === 'week' && paperDate >= weekAgo && paperDate < daysAgo) { // Papers from past week
35+
displayPapers.push(paper);
36+
} else if (range === 'month' && paperDate >= monthAgo && paperDate < weekAgo) { // Rest of the papers from the past month
37+
displayPapers.push(paper);
38+
}
39+
}
40+
return displayPapers;
41+
}
42+
1843
export default function CategoryResultsScreen ({ store }) {
1944
const { selectedPapers, selectedCategory } = store;
45+
const todayPapers = findRelativeDate(selectedPapers, 'today');
46+
const fewDaysPapers = findRelativeDate(selectedPapers, 'days');
47+
const weekPapers = findRelativeDate(selectedPapers, 'week');
48+
const monthPapers = findRelativeDate(selectedPapers, 'month');
2049

21-
return h('div', { class: 'category-results-screen' }, [
22-
h(Link, { href: '/', class: 'category-results-reset link' }, '< Select a different topic'), // TODO: < should be a nice SVG icon
50+
const resultsScreenElements = [
51+
h(Link, { href: '/', class: 'category-results-reset link' }, '< Select a different topic'),
2352
h('div', { class: 'category-results-info' }, [ // TODO: better css for mobile
2453
CategoryCard({ category: selectedCategory, store, selectable: false, includeName: false }),
2554
h('div', { class: 'category-results-details' }, [
2655
h('div', { class: 'category-results-title' }, `${selectedCategory.name}`),
2756
h('div', { class: 'category-results-date' }, 'Updated yesterday'),
2857
h('div', { class: 'category-results-description' }, selectedCategory.description)
2958
])
30-
]),
31-
h('div', { class: 'papers' }, selectedPapers.map(paper => h(Paper, { paper })))
32-
]);
59+
])
60+
];
61+
62+
if (todayPapers.length !== 0) {
63+
resultsScreenElements.push(h('div', { class: 'papers-today' }, [
64+
h('h2', { class: 'papers-today-title' }, 'New papers today'),
65+
h('div', { class: 'papers' }, todayPapers.map(paper => h(Paper, { paper })))
66+
]));
67+
}
68+
if (fewDaysPapers.length !== 0) {
69+
resultsScreenElements.push(h('div', { class: 'papers-last-few-days' }, [
70+
h('h2', { class: 'papers-last-few-days-title' }, 'Papers in the last few days'),
71+
h('div', { class: 'papers' }, fewDaysPapers.map(paper => h(Paper, { paper })))
72+
]));
73+
}
74+
if (weekPapers.length !== 0) {
75+
resultsScreenElements.push(h('div', { class: 'papers-last-week' }, [
76+
h('h2', { class: 'papers-last-week-title' }, 'Papers in the last week'),
77+
h('div', { class: 'papers' }, weekPapers.map(paper => h(Paper, { paper })))
78+
]));
79+
}
80+
if (monthPapers.length !== 0) {
81+
resultsScreenElements.push(h('div', { class: 'category-results-papers-this-month' }, [
82+
h('h2', { class: 'papers-this-month-title' }, 'Papers in the past month'),
83+
h('div', { class: 'papers' }, monthPapers.map(paper => h(Paper, { paper })))
84+
]));
85+
}
86+
87+
return h('div', { class: 'category-results-screen' }, resultsScreenElements);
3388
}

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)