Skip to content

Commit

Permalink
Merge 900a276 into b296b18
Browse files Browse the repository at this point in the history
  • Loading branch information
taniarascia committed Oct 7, 2020
2 parents b296b18 + 900a276 commit 2ce2d11
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 377 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
],
'no-console': 1, // Warning to reduce console logs used throughout app
'react/prop-types': 0, // Not using prop-types because we have TypeScript
'newline-before-return': 1,
'no-useless-return': 1,
'prefer-const': 1,
},
settings: {
'import/resolver': {
Expand Down
13 changes: 7 additions & 6 deletions src/client/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { NoteItem, SyncStatePayload, SettingsState } from '../types'
import { NoteItem, SyncPayload, SettingsState } from '../types'

import { welcomeNote } from '@/api/welcomeNote'
import { scratchpadNote } from '@/api/scratchpadNote'

type PromiseCallback = (value?: any) => void
type GetLocalStorage = (
key: string,
errorMsg?: string
errorMessage?: string
) => (resolve: PromiseCallback, reject: PromiseCallback) => void
const getLocalStorage: GetLocalStorage = (key, errorMsg = 'Something went wrong') => (

const getLocalStorage: GetLocalStorage = (key, errorMessage = 'Something went wrong') => (
resolve,
reject
) => {
Expand All @@ -18,7 +19,7 @@ const getLocalStorage: GetLocalStorage = (key, errorMsg = 'Something went wrong'
resolve(JSON.parse(data))
} else {
reject({
message: errorMsg,
message: errorMessage,
})
}
}
Expand Down Expand Up @@ -50,9 +51,9 @@ export const requestNotes = () => new Promise(getUserNotes())
export const requestCategories = () => new Promise(getLocalStorage('categories'))

export const requestSettings = () =>
new Promise(getLocalStorage('settings', 'Could not load code mirror options. An error occurred'))
new Promise(getLocalStorage('settings', 'Could not load settings'))

export const saveState = ({ categories, notes }: SyncStatePayload) =>
export const saveState = ({ categories, notes }: SyncPayload) =>
new Promise((resolve) => {
localStorage.setItem('categories', JSON.stringify(categories))
localStorage.setItem('notes', JSON.stringify(notes))
Expand Down
26 changes: 13 additions & 13 deletions src/client/containers/AppSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
addNote,
swapFolder,
updateActiveNote,
addFavoriteNote,
addTrashedNote,
assignFavoriteToNotes,
assignTrashToNotes,
updateSelectedNotes,
restoreTrashedNote,
unassignTrashFromNotes,
} from '@/slices/note'
import { toggleSettingsModal, togglePreviewMarkdown } from '@/slices/settings'
import { syncState } from '@/slices/sync'
import { sync } from '@/slices/sync'
import { getSettings, getNotes, getCategories, getSync } from '@/selectors'
import { CategoryItem, NoteItem } from '@/types'
import { newNoteHandlerHelper, getActiveNote } from '@/utils/helpers'
Expand Down Expand Up @@ -50,13 +50,13 @@ export const AppSidebar: React.FC = () => {
const _updateSelectedNotes = (noteId: string, multiSelect: boolean) =>
dispatch(updateSelectedNotes({ noteId, multiSelect }))
const _swapFolder = (folder: Folder) => dispatch(swapFolder(folder))
const _syncState = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(syncState({ notes, categories }))
const _sync = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(sync({ notes, categories }))
const _toggleSettingsModal = () => dispatch(toggleSettingsModal())
const _togglePreviewMarkdown = () => dispatch(togglePreviewMarkdown())
const _addTrashedNote = (noteId: string) => dispatch(addTrashedNote(noteId))
const _restoreTrashedNote = (noteId: string) => dispatch(restoreTrashedNote(noteId))
const _addFavoriteNote = (noteId: string) => dispatch(addFavoriteNote(noteId))
const _assignTrashToNotes = (noteId: string) => dispatch(assignTrashToNotes(noteId))
const _unassignTrashFromNotes = (noteId: string) => dispatch(unassignTrashFromNotes(noteId))
const _assignFavoriteToNotes = (noteId: string) => dispatch(assignFavoriteToNotes(noteId))

// ===========================================================================
// Handlers
Expand All @@ -75,7 +75,7 @@ export const AppSidebar: React.FC = () => {
_updateSelectedNotes
)

const syncNotesHandler = () => _syncState(notes, categories)
const syncNotesHandler = () => _sync(notes, categories)
const settingsHandler = () => _toggleSettingsModal()

return (
Expand Down Expand Up @@ -109,23 +109,23 @@ export const AppSidebar: React.FC = () => {
text={LabelText.NOTES}
dataTestID={TestID.FOLDER_NOTES}
folder={Folder.ALL}
addNoteType={_restoreTrashedNote}
addNoteType={_unassignTrashFromNotes}
/>
<FolderOption
active={activeFolder === Folder.FAVORITES}
text={LabelText.FAVORITES}
dataTestID={TestID.FOLDER_FAVORITES}
folder={Folder.FAVORITES}
swapFolder={_swapFolder}
addNoteType={_addFavoriteNote}
addNoteType={_assignFavoriteToNotes}
/>
<FolderOption
active={activeFolder === Folder.TRASH}
text={LabelText.TRASH}
dataTestID={TestID.FOLDER_TRASH}
folder={Folder.TRASH}
swapFolder={_swapFolder}
addNoteType={_addTrashedNote}
addNoteType={_assignTrashToNotes}
/>
<div className="category-title">
<h2>Categories</h2>
Expand Down
12 changes: 6 additions & 6 deletions src/client/containers/ContextMenuOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { ContextMenuOption } from '@/components/NoteList/ContextMenuOption'
import { downloadNotes } from '@/utils/helpers'
import {
deleteNotes,
toggleFavoriteNote,
toggleTrashedNote,
toggleFavoriteNotes,
toggleTrashNotes,
addCategoryToNote,
updateActiveCategoryId,
updateActiveNote,
Expand Down Expand Up @@ -108,8 +108,8 @@ const NotesOptions: React.FC<NotesOptionsProps> = ({ clickedNote }) => {
const dispatch = useDispatch()

const _deleteNotes = (noteIds: string[]) => dispatch(deleteNotes(noteIds))
const _toggleTrashedNote = (noteId: string) => dispatch(toggleTrashedNote(noteId))
const _toggleFavoriteNote = (noteId: string) => dispatch(toggleFavoriteNote(noteId))
const _toggleTrashNotes = (noteId: string) => dispatch(toggleTrashNotes(noteId))
const _toggleFavoriteNotes = (noteId: string) => dispatch(toggleFavoriteNotes(noteId))
const _addCategoryToNote = (categoryId: string, noteId: string) =>
dispatch(addCategoryToNote({ categoryId, noteId }))
const _updateActiveNote = (noteId: string, multiSelect: boolean) =>
Expand All @@ -129,8 +129,8 @@ const NotesOptions: React.FC<NotesOptionsProps> = ({ clickedNote }) => {
: [clickedNote],
categories
)
const favoriteNoteHandler = () => _toggleFavoriteNote(clickedNote.id)
const trashNoteHandler = () => _toggleTrashedNote(clickedNote.id)
const favoriteNoteHandler = () => _toggleFavoriteNotes(clickedNote.id)
const trashNoteHandler = () => _toggleTrashNotes(clickedNote.id)
const removeCategoryFromNoteHandler = () => {
_addCategoryToNote('', clickedNote.id)
_updateActiveNote(clickedNote.id, false)
Expand Down
14 changes: 7 additions & 7 deletions src/client/containers/KeyboardShortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { useKey } from '@/utils/hooks'
import {
addNote,
swapFolder,
toggleTrashedNote,
toggleTrashNotes,
updateActiveNote,
updateSelectedNotes,
updateNote,
} from '@/slices/note'
import { syncState } from '@/slices/sync'
import { sync } from '@/slices/sync'
import { getCategories, getNotes, getSettings } from '@/selectors'
import { CategoryItem, NoteItem } from '@/types'
import { toggleDarkTheme, togglePreviewMarkdown, updateCodeMirrorOption } from '@/slices/settings'
Expand Down Expand Up @@ -49,9 +49,9 @@ export const KeyboardShortcuts: React.FC = () => {
const _updateSelectedNotes = (noteId: string, multiSelect: boolean) =>
dispatch(updateSelectedNotes({ noteId, multiSelect }))
const _swapFolder = (folder: Folder) => dispatch(swapFolder(folder))
const _toggleTrashedNote = (noteId: string) => dispatch(toggleTrashedNote(noteId))
const _syncState = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(syncState({ notes, categories }))
const _toggleTrashNotes = (noteId: string) => dispatch(toggleTrashNotes(noteId))
const _sync = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(sync({ notes, categories }))
const _togglePreviewMarkdown = () => dispatch(togglePreviewMarkdown())
const _toggleDarkTheme = () => dispatch(toggleDarkTheme())
const _updateCodeMirrorOption = (key: string, value: string) =>
Expand Down Expand Up @@ -80,8 +80,8 @@ export const KeyboardShortcuts: React.FC = () => {
_updateSelectedNotes
)
const newTempCategoryHandler = () => !addingTempCategory && setAddingTempCategory(true)
const trashNoteHandler = () => _toggleTrashedNote(activeNote!.id)
const syncNotesHandler = () => _syncState(notes, categories)
const trashNoteHandler = () => _toggleTrashNotes(activeNote!.id)
const syncNotesHandler = () => _sync(notes, categories)
const downloadNotesHandler = () =>
downloadNotes(
selectedNotesIds.includes(activeNote!.id)
Expand Down
11 changes: 5 additions & 6 deletions src/client/containers/NoteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ContextMenu } from '@/containers/ContextMenu'
import { getNoteTitle, shouldOpenContextMenu, debounceEvent } from '@/utils/helpers'
import { useKey } from '@/utils/hooks'
import {
emptyTrash,
permanentlyEmptyTrash,
pruneNotes,
updateActiveNote,
searchNotes,
Expand Down Expand Up @@ -41,7 +41,7 @@ export const NoteList: React.FC = () => {

const _updateSelectedNotes = (noteId: string, multiSelect: boolean) =>
dispatch(updateSelectedNotes({ noteId, multiSelect }))
const _emptyTrash = () => dispatch(emptyTrash())
const _permanentlyEmptyTrash = () => dispatch(permanentlyEmptyTrash())
const _toggleSidebarVisibility = () => dispatch(toggleSidebarVisibility())
const _pruneNotes = () => dispatch(pruneNotes())
const _updateActiveNote = (noteId: string, multiSelect: boolean) =>
Expand Down Expand Up @@ -96,7 +96,7 @@ export const NoteList: React.FC = () => {
const handleNoteOptionsClick = (event: ReactMouseEvent, noteId: string = '') => {
const clicked = event.target

// Make sure we aren't getting any null values .. any element clicked should be a sub-class of element
// Make sure we aren't getting any null values. Any element clicked should be a sub-class of element
if (!clicked) return

// Ensure the clicked target is supposed to open the context menu
Expand All @@ -110,7 +110,6 @@ export const NoteList: React.FC = () => {
event.stopPropagation()

if (contextMenuRef.current && contextMenuRef.current.contains(clicked as HTMLDivElement)) {
return
} else {
setOptionsId(!optionsId || optionsId !== noteId ? noteId : '')
}
Expand Down Expand Up @@ -140,7 +139,6 @@ export const NoteList: React.FC = () => {
event.stopPropagation()

if (contextMenuRef.current && contextMenuRef.current.contains(clicked as HTMLDivElement)) {
return
} else {
setOptionsId(!optionsId || optionsId !== noteId ? noteId : '')
}
Expand All @@ -154,6 +152,7 @@ export const NoteList: React.FC = () => {

useEffect(() => {
document.addEventListener('mousedown', handleNoteOptionsClick)

return () => {
document.removeEventListener('mousedown', handleNoteOptionsClick)
}
Expand All @@ -172,7 +171,7 @@ export const NoteList: React.FC = () => {
<NoteListButton
dataTestID={TestID.EMPTY_TRASH_BUTTON}
label="Empty"
handler={() => _emptyTrash()}
handler={() => _permanentlyEmptyTrash()}
>
Empty Trash
</NoteListButton>
Expand Down
1 change: 1 addition & 0 deletions src/client/containers/SettingsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const SettingsModal: React.FC = () => {
useEffect(() => {
document.addEventListener('mousedown', handleDomClick)
document.addEventListener('keydown', handleEscPress)

return () => {
document.removeEventListener('mousedown', handleDomClick)
document.removeEventListener('keydown', handleEscPress)
Expand Down
8 changes: 4 additions & 4 deletions src/client/containers/TakeNoteApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useInterval, useBeforeUnload } from '@/utils/hooks'
import { getWebsiteTitle, determineAppClass, getActiveCategory } from '@/utils/helpers'
import { loadCategories, swapCategories } from '@/slices/category'
import { loadNotes } from '@/slices/note'
import { syncState } from '@/slices/sync'
import { sync } from '@/slices/sync'
import { loadSettings } from '@/slices/settings'
import { NoteItem, CategoryItem } from '@/types'
import { getSettings, getNotes, getCategories, getSync } from '@/selectors'
Expand Down Expand Up @@ -41,8 +41,8 @@ export const TakeNoteApp: React.FC = () => {
const _loadSettings = () => dispatch(loadSettings())
const _swapCategories = (categoryId: number, destinationId: number) =>
dispatch(swapCategories({ categoryId, destinationId }))
const _syncState = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(syncState({ notes, categories }))
const _sync = (notes: NoteItem[], categories: CategoryItem[]) =>
dispatch(sync({ notes, categories }))

// ===========================================================================
// Handlers
Expand Down Expand Up @@ -71,7 +71,7 @@ export const TakeNoteApp: React.FC = () => {
}, [])

useInterval(() => {
_syncState(notes, categories)
_sync(notes, categories)
}, 20000)

useBeforeUnload((event: BeforeUnloadEvent) => (pendingSync ? event.preventDefault() : null))
Expand Down
39 changes: 21 additions & 18 deletions src/client/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { all, put, takeLatest, select } from 'redux-saga/effects'
import dayjs from 'dayjs'
import axios from 'axios'

import { requestCategories, requestNotes, saveState, saveSettings, requestSettings } from '@/api'
import { requestCategories, requestNotes, requestSettings, saveState, saveSettings } from '@/api'
import { loadCategories, loadCategoriesError, loadCategoriesSuccess } from '@/slices/category'
import { loadNotes, loadNotesError, loadNotesSuccess } from '@/slices/note'
import { syncState, syncStateError, syncStateSuccess } from '@/slices/sync'
import { sync, syncError, syncSuccess } from '@/slices/sync'
import { login, loginSuccess, loginError, logout, logoutSuccess } from '@/slices/auth'
import {
updateCodeMirrorOption,
Expand All @@ -18,7 +18,7 @@ import {
toggleSettingsModal,
updateNotesSortStrategy,
} from '@/slices/settings'
import { SyncStateAction } from '@/types'
import { SyncAction } from '@/types'
import { getSettings } from '@/selectors'

/**
Expand Down Expand Up @@ -79,12 +79,25 @@ function* fetchCategories() {
}
}

function* postState({ payload }: SyncStateAction) {
/**
* Get settings from API
*/
function* fetchSettings() {
try {
const settings = yield requestSettings()

yield put(loadSettingsSuccess(settings))
} catch (error) {
yield put(loadSettingsError())
}
}

function* syncData({ payload }: SyncAction) {
try {
yield saveState(payload)
yield put(syncStateSuccess(dayjs().format()))
yield put(syncSuccess(dayjs().format()))
} catch (error) {
yield put(syncStateError(error.message))
yield put(syncError(error.message))
}
}

Expand All @@ -93,17 +106,7 @@ function* syncSettings() {
const settings = yield select(getSettings)

yield saveSettings(settings)
} catch {}
}

function* fetchSettings() {
try {
const settings = yield requestSettings()

yield put(loadSettingsSuccess(settings))
} catch {
yield put(loadSettingsError())
}
} catch (error) {}
}

// If any of these functions are dispatched, invoke the appropriate saga
Expand All @@ -114,7 +117,7 @@ function* rootSaga() {
takeLatest(loadNotes.type, fetchNotes),
takeLatest(loadCategories.type, fetchCategories),
takeLatest(loadSettings.type, fetchSettings),
takeLatest(syncState.type, postState),
takeLatest(sync.type, syncData),
takeLatest(
[
toggleDarkTheme.type,
Expand Down
Loading

0 comments on commit 2ce2d11

Please sign in to comment.