diff options
Diffstat (limited to 'components/List.jsx')
-rw-r--r-- | components/List.jsx | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/components/List.jsx b/components/List.jsx index b280d4a..8003187 100644 --- a/components/List.jsx +++ b/components/List.jsx @@ -10,6 +10,16 @@ import { handleRemove } from "../helpers"; const List = ({ session, setSession, showError, setEdit }) => { const [list, setList] = useState() const [loading, setLoading] = useState(true) + const [sort, setSort] = useState(0) + + const sortFn = (a, b) => { + const d = sort % 2 ? -1 : 1 + switch (sort) { + case 0: case 1: return d * (new Date(b.updated_at) - new Date(a.updated_at)) + case 2: case 3: return d * (new Date(b.created_at) - new Date(a.created_at)) + case 4: case 5: return d * a.title.toLowerCase().localeCompare(b.title.toLowerCase()) + } + } const fetchNotes = async () => { setLoading(true) @@ -35,13 +45,15 @@ const List = ({ session, setSession, showError, setEdit }) => { setSession={setSession} showError={showError} setEdit={setEdit} + sort={sort} + setSort={setSort} /> { loading || list === undefined ? <Text style={styles.text}>Loading notes...</Text> : ( <SwipeableFlatList - data={list.sort((a,b) => new Date(b.updated_at) - new Date(a.updated_at))} + data={list.sort(sortFn)} renderItem={({item}) => <Note note={item} setEdit={setEdit} />} keyExtractor={item => item._id} ListEmptyComponent={<Text style={styles.text}>You don't have any notes</Text>} |