Skip to content

Commit

Permalink
Merge pull request #16 from galtenberg/issue-1
Browse files Browse the repository at this point in the history
Server-side PARA subsets, closes #1
  • Loading branch information
galtenberg authored Nov 18, 2017
2 parents 0d63cf0 + 1fe2e1d commit f2163d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions server/lib/evernote/evernote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
const Evernote = require('evernote')
const enAuth = require('./auth')

function notebooks(token) {
async function notebooks(token) {
const client = enAuth.createAuthenticatedClient(token)
return client.getNoteStore().listNotebooks()
const notebooks = await client.getNoteStore().listNotebooks()
return addParaNotebooks(notebooks)
}

function addParaNotebooks(notebooks) {
const paraNotebooks = notebooks.filter(n => ['projects', 'areas', 'resources', 'archives'].includes(n.name.toLowerCase()))
const parNotebooks = notebooks.filter(n => ['projects', 'areas', 'resources'].includes(n.name.toLowerCase()))
return notebooks.concat([
{ name: 'Any PARA', guid: paraNotebooks.map(n => n.guid) },
{ name: 'Any PAR', guid: parNotebooks.map(n => n.guid) },
{ name: 'Any', guid: notebooks.map(n => n.guid) }
])
}

async function randomNotebook(token) {
Expand All @@ -19,7 +30,10 @@ async function randomNote(token, notebookGuid) {
const noteStore = client.getNoteStore()

const filter = new Evernote.NoteStore.NoteFilter()
filter.notebookGuid = notebookGuid

filter.notebookGuid = notebookGuid.includes(',') ?
getRandomElement(notebookGuid.split(',')) :
notebookGuid

const noteCount = noteStore.findNoteCounts(token, filter)
.then(count => count['notebookCounts'][filter.notebookGuid])
Expand All @@ -40,9 +54,8 @@ async function randomNote(token, notebookGuid) {
}

const noteGuids = notesMetadata.notes.map(n => n.guid)
const randomNoteIndex = getRandomInt(0, noteGuids.length)

return noteStore.getNote(noteGuids[randomNoteIndex], true, true, true, true)
return noteStore.getNote(getRandomElement(noteGuids), true, true, true, true)
.then(note => note)
.catch(err => err)
}
Expand All @@ -54,3 +67,5 @@ exports.notebooks = notebooks
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

const getRandomElement = array => array[getRandomInt(0, array.length)]
2 changes: 1 addition & 1 deletion src/components/Notes/Notebooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Notebooks extends Component {
renderNotebook = (notebook) =>
<Notebook
name={notebook.name}
key={notebook.guid}
key={notebook.name+notebook.guid}
guid={notebook.guid}
notebookChanged={this.notebookChanged}
/>
Expand Down

0 comments on commit f2163d8

Please sign in to comment.