aboutsummaryrefslogtreecommitdiffstats
path: root/apps/Notes/components/List.js
blob: 7d88c8d56bd849308447c80594174cf898c1c554 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import styles from '../styles/Notes.module.scss'
import { useRouter } from 'next/router'
import React, { useState, useEffect } from 'react'
import useUser from 'hooks/useUser'
import useSettings from 'hooks/useSettings'
import useNotes from '../hooks/useNotes'
import useSort from '../hooks/useSort'
import ListItem from './ListItem'
import Actions from './Actions'
import { Splash } from 'components'
import handleLogout from 'helpers/logout'

const List = ({ logout }) => {
  const [fetchedNote, setFetchedNote] = useState()
  const [action, setAction] = useState('')
  const [loading, setLoading] = useState(false)
  const [showSort, setShowSort] = useState(false)
  const { notes, error } = useNotes()
  const [sortedBy, sortBy, sortFn] = useSort(3)
  const { t } = useSettings()
  const router = useRouter()
  const { user, mutateUser } = useUser({
    redirectToLogin: true,
    redirectToVerify: true
  })

  useEffect(() => {
    setLoading(false)
  }, [fetchedNote])

  if (error) return <Splash type='connection' />
  if (loading) return <Splash />
  if (!user || !user.isLoggedIn || !user.isVerified || !notes || !sortFn) {
    return <Splash />
  }

  return (
    action === ''
      ? (
        <>
          <div className='window__submenu'>
            <div>
              <div onClick={() => setAction('addNote')}>{t('notes_new')}</div>
              <div className='mobile-only' onClick={() => { setShowSort(s => !s) }}>{t('sort')}</div>
              <div onClick={() => setAction('importNotes')}>{t('import')}</div>
              <div onClick={() => setAction('exportNotes')}>{t('export')}</div>
              {logout && (
                <div onClick={e => handleLogout(e, router, mutateUser)}>
                  {t('logout')}
                </div>
              )}
            </div>
          </div>
          <div className='window__scroll'>
            <table className={styles.notesList}>
              <thead className={showSort ? styles.mobileSort : 'desktop-only'}>
                <tr className='mobile-only'>
                  <th>{t('sort_by')}</th>
                </tr>
                <tr>
                  <th onClick={() => sortBy(1)}>{t('title')} {sortedBy(1)}</th>
                  <th onClick={() => sortBy(2)}>{t('created')} {sortedBy(2)}</th>
                  <th onClick={() => sortBy(3)}>{t('modified')} {sortedBy(3)}</th>
                </tr>
                <tr className='mobile-only'>
                  <td className='window__button' onClick={() => setShowSort(false)}>{t('close')}</td>
                </tr>
              </thead>
              <tbody>
                {
                  notes.length > 0
                    ? (notes.sort(sortFn).map(note => (
                      <ListItem
                        key={note._id}
                        note={note}
                        setAction={setAction}
                        setFetchedNote={setFetchedNote}
                        setLoading={setLoading}
                      />
                      )))
                    : (
                      <tr>
                        <td>{t('notes_list_empty')}</td>
                      </tr>
                      )
                }
              </tbody>
            </table>
          </div>
        </>
        )
      : (
        <Actions
          action={action}
          setAction={setAction}
          fetchedNote={fetchedNote}
          setFetchedNote={setFetchedNote}
        />
        )
  )
}

export default List