Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move previous index store to Redux #1919

Merged
merged 7 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ export const App = connect(

// gets the index of the note located before the currently selected one
getPreviousNoteIndex = note => {
const noteIndex = this.props.ui.filteredNotes.findIndex(
const previousIndex = this.props.ui.filteredNotes.findIndex(
({ id }) => note.id === id
);

return Math.max(noteIndex - 1, 0);
return Math.max(previousIndex - 1, 0);
};

syncActivityHooks = data => {
Expand Down
12 changes: 4 additions & 8 deletions lib/flux/app-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const toggleSystemTag = (
};

const initialState: AppState = {
previousIndex: -1,
notes: null,
tags: [],
dialogs: [],
Expand Down Expand Up @@ -70,14 +69,12 @@ export const actionMap = new ActionMap({
showAllNotes(state: AppState) {
return update(state, {
tag: { $set: null },
previousIndex: { $set: -1 },
});
},

selectTrash(state: AppState) {
return update(state, {
tag: { $set: null },
previousIndex: { $set: -1 },
});
},

Expand All @@ -97,7 +94,6 @@ export const actionMap = new ActionMap({
selectTag(state: AppState, { tag }: { tag: T.TagEntity }) {
return update(state, {
tag: { $set: tag },
previousIndex: { $set: -1 },
});
},

Expand Down Expand Up @@ -320,12 +316,12 @@ export const actionMap = new ActionMap({
}: {
noteBucket: T.Bucket<T.Note>;
note: T.NoteEntity;
previousIndex: number;
}) {
return () => {
return dispatch => {
if (note) {
note.data.deleted = true;
noteBucket.update(note.id, note.data);
dispatch(actions.ui.trashNote(previousIndex));
}
};
},
Expand All @@ -339,12 +335,12 @@ export const actionMap = new ActionMap({
}: {
noteBucket: T.Bucket<T.Note>;
note: T.NoteEntity;
previousIndex: number;
}) {
return dispatch => {
if (note) {
note.data.deleted = false;
noteBucket.update(note.id, note.data);
dispatch(actions.ui.restoreNote(previousIndex));
}
};
},
Expand All @@ -358,11 +354,11 @@ export const actionMap = new ActionMap({
}: {
noteBucket: T.Bucket<T.Note>;
note: T.NoteEntity;
previousIndex: number;
}) {
return dispatch => {
noteBucket.remove(note.id);
dispatch(this.action('loadNotes', { noteBucket }));
dispatch(actions.ui.deleteNoteForever(previousIndex));
};
},
},
Expand Down
11 changes: 4 additions & 7 deletions lib/note-toolbar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { connect } from 'react-redux';

import analytics from './analytics';
import appState from './flux/app-state';
import { closeNote } from './state/ui/actions';
import { toggleFocusMode } from './state/settings/actions';
import DialogTypes from '../shared/dialog-types';

Expand All @@ -28,10 +27,8 @@ type NoteChanger = {
type ListChanger = NoteChanger & { previousIndex: number };

type DispatchProps = {
closeNote: () => any;
deleteNoteForever: (args: ListChanger) => any;
restoreNote: (args: ListChanger) => any;
toggleRevisions: () => any;
shareNote: () => any;
toggleFocusMode: () => any;
trashNote: (args: ListChanger) => any;
Expand All @@ -42,15 +39,16 @@ type Props = OwnProps & StateProps & DispatchProps;
export class NoteToolbarContainer extends Component<Props> {
// Gets the index of the note located before the currently selected one
getPreviousNoteIndex = (note: T.NoteEntity) => {
const noteIndex = this.props.notes.findIndex(({ id }) => note.id === id);
const previousIndex = this.props.notes.findIndex(
({ id }) => note.id === id
);

return Math.max(noteIndex - 1, 0);
return Math.max(previousIndex - 1, 0);
};

onTrashNote = (note: T.NoteEntity) => {
const { noteBucket } = this.props;
const previousIndex = this.getPreviousNoteIndex(note);
this.props.closeNote();
this.props.trashNote({ noteBucket, note, previousIndex });
analytics.tracks.recordEvent('editor_note_deleted');
};
Expand Down Expand Up @@ -107,7 +105,6 @@ const {
} = appState.actionCreators;

const mapDispatchToProps: S.MapDispatch<DispatchProps> = dispatch => ({
closeNote: () => dispatch(closeNote()),
deleteNoteForever: args => dispatch(deleteNoteForever(args)),
restoreNote: args => dispatch(restoreNote(args)),
shareNote: () => dispatch(showDialog({ dialog: DialogTypes.SHARE })),
Expand Down
16 changes: 14 additions & 2 deletions lib/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ export type SetWPToken = Action<'setWPToken', { token: string }>;
*/
export type CreateNote = Action<'CREATE_NOTE'>;
export type CloseNote = Action<'CLOSE_NOTE'>;
export type FilterNotes = Action<'FILTER_NOTES', { notes: T.NoteEntity[] }>;
export type DeleteNoteForever = Action<
'DELETE_NOTE_FOREVER',
{ previousIndex: number }
>;
export type FilterNotes = Action<
'FILTER_NOTES',
{ notes: T.NoteEntity[]; previousIndex: number }
>;
export type FocusSearchField = Action<'FOCUS_SEARCH_FIELD'>;
export type Search = Action<'SEARCH', { searchQuery: string }>;
export type SelectRevision = Action<
'SELECT_REVISION',
{ revision: T.NoteEntity }
>;
export type RestoreNote = Action<'RESTORE_NOTE', { previousIndex: number }>;
export type SetAuth = Action<'AUTH_SET', { status: AuthState }>;
export type SetUnsyncedNoteIds = Action<
'SET_UNSYNCED_NOTE_IDS',
Expand All @@ -79,6 +87,7 @@ export type ToggleEditMode = Action<'TOGGLE_EDIT_MODE'>;
export type ToggleRevisions = Action<'REVISIONS_TOGGLE'>;
export type ToggleTagDrawer = Action<'TAG_DRAWER_TOGGLE', { show: boolean }>;
export type ToggleTagEditing = Action<'TAG_EDITING_TOGGLE'>;
export type TrashNote = Action<'TRASH_NOTE', { previousIndex: number }>;
export type SelectNote = Action<
'SELECT_NOTE',
{ note: T.NoteEntity; options?: { hasRemoteUpdate: boolean } }
Expand All @@ -87,9 +96,11 @@ export type SelectNote = Action<
export type ActionType =
| CreateNote
| CloseNote
| DeleteNoteForever
| LegacyAction
| FilterNotes
| FocusSearchField
| RestoreNote
| Search
| SelectNote
| SelectRevision
Expand All @@ -115,7 +126,8 @@ export type ActionType =
| ToggleRevisions
| ToggleSimperiumConnectionStatus
| ToggleTagDrawer
| ToggleTagEditing;
| ToggleTagEditing
| TrashNote;

export type ActionCreator<A extends ActionType> = (...args: any[]) => A;
export type Reducer<S> = (state: S | undefined, action: ActionType) => S;
Expand Down
1 change: 0 additions & 1 deletion lib/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export type AppState = {
nextDialogKey: number;
notes: T.NoteEntity[] | null;
preferences?: T.Preferences;
previousIndex: number;
revision: T.NoteEntity | null;
showNavigation: boolean;
tags: T.TagEntity[];
Expand Down
25 changes: 24 additions & 1 deletion lib/state/ui/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,33 @@ export const closeNote: A.ActionCreator<A.CloseNote> = () => ({
type: 'CLOSE_NOTE',
});

export const deleteNoteForever: A.ActionCreator<A.DeleteNoteForever> = (
previousIndex: number
) => ({
type: 'DELETE_NOTE_FOREVER',
previousIndex,
});

export const filterNotes: A.ActionCreator<A.FilterNotes> = (
notes: T.NoteEntity[]
notes: T.NoteEntity[],
previousIndex: number
) => ({
type: 'FILTER_NOTES',
notes,
previousIndex,
});

export const focusSearchField: A.ActionCreator<A.FocusSearchField> = () => ({
type: 'FOCUS_SEARCH_FIELD',
});

export const restoreNote: A.ActionCreator<A.RestoreNote> = (
previousIndex: number
) => ({
type: 'RESTORE_NOTE',
previousIndex,
});

export const selectRevision: A.ActionCreator<A.SelectRevision> = (
revision: T.NoteEntity
) => ({
Expand Down Expand Up @@ -85,3 +101,10 @@ export const toggleTagDrawer: A.ActionCreator<A.ToggleTagDrawer> = (
export const toggleTagEditing: A.ActionCreator<A.ToggleTagEditing> = () => ({
type: 'TAG_EDITING_TOGGLE',
});

export const trashNote: A.ActionCreator<A.TrashNote> = (
previousIndex: number
) => ({
type: 'TRASH_NOTE',
previousIndex,
});
19 changes: 15 additions & 4 deletions lib/state/ui/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ let searchTimeout: NodeJS.Timeout;

export const middleware: S.Middleware = store => {
const updateNotes = () =>
store.dispatch(filterAction(filterNotes(store.getState())));
store.dispatch(
filterAction(
filterNotes(store.getState()),
store.getState().ui.previousIndex
)
);

return next => (action: A.ActionType) => {
const result = next(action);

switch (action.type) {
// on clicks re-filter "immediately"
// on clicks re-filter immediately
case 'DELETE_NOTE_FOREVER':
case 'RESTORE_NOTE':
case 'TRASH_NOTE':
clearTimeout(searchTimeout);
updateNotes();
break;

// on events re-filter "immediately"
case 'App.authChanged':
case 'App.deleteNoteForever':
case 'App.notesLoaded':
case 'App.restoreNote':
case 'App.selectTag':
case 'App.selectTrash':
case 'App.showAllNotes':
Expand Down
28 changes: 23 additions & 5 deletions lib/state/ui/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ const selectedRevision: A.Reducer<T.NoteEntity | null> = (
}
};

const previousIndex: A.Reducer<number> = (state = -1, action) => {
switch (action.type) {
case 'DELETE_NOTE_FOREVER':
case 'RESTORE_NOTE':
case 'TRASH_NOTE':
return action.previousIndex || state;
case 'App.selectTag':
case 'App.selectTrash':
case 'App.showAllNotes':
return -1;
default:
return state;
}
};

const showNoteList: A.Reducer<boolean> = (state = false, action) => {
switch (action.type) {
case 'CLOSE_NOTE':
Expand Down Expand Up @@ -170,12 +185,14 @@ const showTrash: A.Reducer<boolean> = (state = false, action) => {

const note: A.Reducer<T.NoteEntity | null> = (state = null, action) => {
switch (action.type) {
case 'CLOSE_NOTE':
case 'App.trashNote':
case 'App.emptyTrash':
case 'App.showAllNotes':
case 'App.selectTrash':
case 'App.selectTag':
case 'App.selectTrash':
case 'App.showAllNotes':
case 'CLOSE_NOTE':
case 'DELETE_NOTE_FOREVER':
case 'RESTORE_NOTE':
case 'TRASH_NOTE':
return null;
case 'SELECT_NOTE':
return action.options
Expand All @@ -188,7 +205,7 @@ const note: A.Reducer<T.NoteEntity | null> = (state = null, action) => {
// keep note if still in new filtered list otherwise try to choose first note in list
return state && action.notes.some(({ id }) => id === state.id)
? state
: action.notes[0] || null;
: action.notes[Math.max(action.previousIndex, 0)] || null;
default:
return state;
}
Expand All @@ -201,6 +218,7 @@ export default combineReducers({
listTitle,
note,
noteRevisions,
previousIndex,
searchQuery,
selectedRevision,
showNavigation,
Expand Down