aboutsummaryrefslogtreecommitdiffstats
path: root/apps/Notes/components/List.js
blob: 0f6221e80eb8a4ba4567094e00db741b5cdaed0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import styles from '../Notes.module.scss'
import React, {useState} from 'react'
import useUser from 'lib/useUser'
import useNotes from '../hooks/useNotes'
import useSort from '../hooks/useSort'
import ListItem from './ListItem'
import Actions from './Actions'
import Splash from './Splash'

const List = () => {
  const [fetchedNote, setFetchedNote] = useState()
  const [action, setAction] = useState('')
  const {notes, error} = useNotes()
  const [sortedBy, sortBy, sortFn] = useSort(2)
  const {user} = useUser({
    redirectToLogin: true,
    redirectToVerify: true,
  })

  if (error) return <Splash type='connection' />

  if (!user || !user.isLoggedIn || !user.isVerified || !notes || !sortFn) {
    return <Splash />
  }

  return (
    <>
      {
        action === '' ? (
          <>
            <div className='window__submenu'>
              <div onClick={() => setAction('addNote')}>New note</div>
              <div onClick={() => setAction('importNotes')}>Import</div>
              <div onClick={() => setAction('exportNotes')}>Export</div>
            </div>
            <table className={styles.notesList}>
              <thead>
                <tr>
                  <th onClick={() => sortBy(1)}>Title {sortedBy(1)}</th>
                  <th onClick={() => sortBy(2)}>Created {sortedBy(2)}</th>
                  <th onClick={() => sortBy(3)}>Modified {sortedBy(3)}</th>
                </tr>
              </thead>
              <tbody>
                {
                  notes.length > 0
                    ? (notes.sort(sortFn).map(note => (
                      <ListItem
                        key={note._id}
                        note={note}
                        setAction={setAction}
                        setFetchedNote={setFetchedNote}
                      />
                    ))) : (
                      <tr>
                        <td>Your notes list is empty.</td>
                      </tr>
                    )}
              </tbody>
            </table>
          </>
        ) : (
          <Actions
            action={action}
            setAction={setAction}
            fetchedNote={fetchedNote}
            setFetchedNote={setFetchedNote}
          />
        )
      }
    </>
  )
}

export default List