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

[DO NOT MERGE] Add note's tags to history screen - Use action generator #2837

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export const middleware: S.Middleware = (store) => {
return next(action);
case 'IMPORT_NOTE_WITH_ID':
case 'REMOTE_NOTE_UPDATE':
case 'APPLY_NOTE_REVISION':
case 'RESTORE_NOTE_REVISION':
searchState.notes.set(action.noteId, toSearchNote(action.note ?? {}));
indexNote(action.noteId);
queueSearch();
Expand Down
9 changes: 0 additions & 9 deletions lib/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,6 @@ export type ReorderTag = Action<
export type RestoreNote = Action<'RESTORE_NOTE', { noteId: T.EntityId }>;
export type RestoreNoteRevision = Action<
'RESTORE_NOTE_REVISION',
{
noteId: T.EntityId;
version: number;
includeDeletedTags: boolean;
}
>;
export type ApplyNoteRevision = Action<
'APPLY_NOTE_REVISION',
{
noteId: T.EntityId;
note: T.Note;
Expand Down Expand Up @@ -349,7 +341,6 @@ export type ActionType =
| AcknowledgePendingChange
| AddCollaborator
| AddNoteTag
| ApplyNoteRevision
| ChangeConnectionStatus
| CloseNote
| CloseNoteActions
Expand Down
37 changes: 30 additions & 7 deletions lib/state/data/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as A from '../action-types';
import * as T from '../../types';
import * as S from '..';
import isEmailTag from '../../utils/is-email-tag';
import { tagHashOf } from '../../utils/tag-hash';

export const addCollaborator: A.ActionCreator<A.AddCollaborator> = (
noteId: T.EntityId,
Expand Down Expand Up @@ -77,13 +80,33 @@ export const toggleAnalytics: A.ActionCreator<A.ToggleAnalytics> = () => ({
type: 'TOGGLE_ANALYTICS',
});

export const restoreNoteRevision: A.ActionCreator<A.RestoreNoteRevision> = (
export const restoreNoteRevision: S.ActionGenerator = (
noteId: T.EntityId,
version: number,
includeDeletedTags: boolean
) => ({
type: 'RESTORE_NOTE_REVISION',
noteId,
version,
includeDeletedTags,
});
) => (state) => {
const revision = state.data.noteRevisions.get(noteId)?.get(version);

if (!revision) {
return;
}

const note = state.data.notes.get(noteId);
const noteEmailTags =
note?.tags.filter((tagName) => isEmailTag(tagName)) ?? [];

const revisionCanonicalTags = revision.tags.filter((tagName) => {
const tagHash = tagHashOf(tagName);
const hasTag = state.data.tags.has(tagHash);
return !isEmailTag(tagName) && (hasTag || includeDeletedTags);
});

return {
type: 'RESTORE_NOTE_REVISION',
noteId,
note: {
...revision,
tags: [...noteEmailTags, ...revisionCanonicalTags],
},
};
Comment on lines +88 to +111
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This action contains the logic that we had in the data middleware.

};
29 changes: 0 additions & 29 deletions lib/state/data/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,6 @@ export const middleware: S.Middleware = (store) => (
note: action.note,
});

case 'RESTORE_NOTE_REVISION': {
const revision = state.data.noteRevisions
.get(action.noteId)
?.get(action.version);

if (!revision) {
return;
}

const note = state.data.notes.get(action.noteId);
const noteEmailTags =
note?.tags.filter((tagName) => isEmailTag(tagName)) ?? [];

const revisionCanonicalTags = revision.tags.filter((tagName) => {
const tagHash = tagHashOf(tagName);
const hasTag = state.data.tags.has(tagHash);
return !isEmailTag(tagName) && (hasTag || action.includeDeletedTags);
});

return next({
type: 'APPLY_NOTE_REVISION',
noteId: action.noteId,
note: {
...revision,
tags: [...noteEmailTags, ...revisionCanonicalTags],
},
});
}

case 'RESTORE_OPEN_NOTE':
if (!state.ui.openedNote) {
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/state/data/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const notes: A.Reducer<Map<T.EntityId, T.Note>> = (

case 'NOTE_BUCKET_UPDATE':
case 'REMOTE_NOTE_UPDATE':
case 'APPLY_NOTE_REVISION':
case 'RESTORE_NOTE_REVISION':
return new Map(state).set(action.noteId, action.note);

case 'IMPORT_NOTE_WITH_ID': {
Expand Down Expand Up @@ -437,7 +437,7 @@ export const tags: A.Reducer<Map<T.TagHash, T.Tag>> = (
return next;
}

case 'APPLY_NOTE_REVISION': {
case 'RESTORE_NOTE_REVISION': {
const next = new Map(state);
action.note.tags.forEach((tagName) => {
const tagHash = t(tagName);
Expand Down
21 changes: 20 additions & 1 deletion lib/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ export type State = {

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const actionGeneratorMiddleware: Middleware = (store) => (
next: (action: A.ActionType) => A.ActionType
) => (action: A.ActionType | ReturnType<ActionGenerator>) => {
const state = store.getState();
if (typeof action === 'function') {
const result = action(state);
if (!result || !result.type) {
return;
}
Comment on lines +59 to +61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives control to the action generator to prevent the action dispatch, it could be useful if for example we determine that the Redux action shouldn't be dispatched.

return next(result);
}
return next(action);
};

export const makeStore = (
accountName: string | null,
...middlewares: Middleware[]
Expand Down Expand Up @@ -78,6 +92,7 @@ export const makeStore = (
}),
}),
applyMiddleware(
actionGeneratorMiddleware,
dataMiddleware,
analyticsMiddleware,
browserMiddleware,
Expand Down Expand Up @@ -115,7 +130,7 @@ export type MapDispatch<
| {
[P in keyof DispatchProps]: (
...args: Parameters<DispatchProps[P]>
) => A.ActionType;
) => A.ActionType | ReturnType<ActionGenerator>;
};

export type Dispatch = ReduxDispatch<A.ActionType>;
Expand All @@ -126,3 +141,7 @@ export type Middleware<Extension = {}> = ReduxMiddleware<
>;

export type Selector<T> = (state: State, ...args: any[]) => T;

export type ActionGenerator = (
...args: any[]
) => (state: State) => A.ActionType | void;
2 changes: 1 addition & 1 deletion lib/state/simperium/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export const initSimperium = (
return result;
}

case 'APPLY_NOTE_REVISION': {
case 'RESTORE_NOTE_REVISION': {
action.note.tags.map(t).forEach((tagHash) => {
if (!prevState.data.tags.has(tagHash)) {
queueTagUpdate(tagHash, 10);
Expand Down
4 changes: 2 additions & 2 deletions lib/state/ui/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ const openedRevision: A.Reducer<[T.EntityId, number] | null> = (
) => {
switch (action.type) {
case 'CLOSE_REVISION':
case 'APPLY_NOTE_REVISION':
case 'RESTORE_NOTE_REVISION':
return null;

case 'OPEN_REVISION':
Expand Down Expand Up @@ -384,7 +384,7 @@ const showRevisions: A.Reducer<boolean> = (state = false, action) => {
case 'OPEN_NOTE':
case 'SELECT_NOTE':
case 'CREATE_NOTE_WITH_ID':
case 'APPLY_NOTE_REVISION':
case 'RESTORE_NOTE_REVISION':
return false;
default:
return state;
Expand Down