sort notes by created and done state

This commit is contained in:
Jonathan Jenne 2022-06-20 21:40:53 +02:00
parent 476a4c4634
commit 0dc14812de
2 changed files with 26 additions and 9 deletions

View File

@ -4,13 +4,13 @@
import Actions from './lib/Actions.svelte'
import Storage from './storage'
import {createNote, toggleDoneState, applyFilter} from './note'
import {createNote, toggleDoneState, applyFilterSort} from './note'
const storage = new Storage()
let notes = storage.getNotes()
let filter = storage.getFilter()
let theme = storage.getTheme()
let shownNotes = applyFilter(notes, filter)
let shownNotes = applyFilterSort(notes, filter)
let hasNotes = notes.length > 0
$: hasFinishedNotes = notes.filter(note => !note.done).length === notes.length
@ -20,21 +20,22 @@
function handleNewNote(e) {
notes.push(createNote(e.detail))
notes = notes // Trigger reactiveness
shownNotes = applyFilter(notes, filter)
shownNotes = applyFilterSort(notes, filter)
storage.saveNotes(notes)
}
function handleToggle(e) {
notes = toggleDoneState(notes, e.detail.created)
shownNotes = applyFilter(notes, filter)
shownNotes = applyFilterSort(notes, filter)
storage.saveNotes(notes)
}
function handleFilter(e) {
filter = e.detail
shownNotes = applyFilter(notes, filter)
shownNotes = applyFilterSort(notes, filter)
storage.saveFilter(filter)
}
@ -45,7 +46,8 @@
const noteDone = note.done
return !(noteDone && noteShown)
})
shownNotes = applyFilter(notes, filter)
shownNotes = applyFilterSort(notes, filter)
storage.saveNotes(notes)
}
@ -59,6 +61,7 @@
function handleToggleDarkMode(e) {
console.log("Dark Mode: " + !darkMode)
theme = theme === "dark" ? "" : "dark"
storage.saveTheme(theme)
}
</script>

View File

@ -15,11 +15,25 @@ export function createNote(noteTitle) {
}
export function applyFilter(notes, filter) {
if (!filter) return notes
if (!filter || filter.length === 0) return notes
return notes.filter(note => note.tags.includes(filter))
}
return notes.filter(note => {
return note.tags.includes(filter)
export function applySort(notes) {
const sorted = notes
sorted.sort((a, b) => {
return b.created - a.created
})
sorted.sort((a, b) => {
if (a.done && !b.done) return 1
if (!a.done && b.done) return -1
return 0
})
return sorted
}
export function applyFilterSort(notes, filter) {
return applySort(applyFilter(notes, filter))
}
export function toggleDoneState(notes, id) {