Skip to content

Commit

Permalink
Move unsyncednoteids state from flux to redux (#1871)
Browse files Browse the repository at this point in the history
Move unsyncednoteids state from flux to redux
  • Loading branch information
belcherj authored Feb 3, 2020
1 parent 3867114 commit 4b915b1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 50 deletions.
18 changes: 10 additions & 8 deletions lib/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {
pick,
values,
} from 'lodash';
import { toggleSimperiumConnectionStatus } from './state/ui/actions';
import {
setUnsyncedNoteIds,
toggleSimperiumConnectionStatus,
} from './state/ui/actions';

import * as settingsActions from './state/settings/actions';

Expand Down Expand Up @@ -103,6 +106,7 @@ const mapDispatchToProps: S.MapDispatch<
setSimperiumConnectionStatus: connected =>
dispatch(toggleSimperiumConnectionStatus(connected)),
selectNote: note => dispatch(actions.ui.selectNote(note)),
setUnsyncedNoteIds: noteIds => dispatch(setUnsyncedNoteIds(noteIds)),
};
};

Expand Down Expand Up @@ -303,11 +307,11 @@ export const App = connect(
};

onNotesIndex = () => {
const { noteBucket } = this.props;
const { loadNotes, setUnsyncedNoteIds } = this.props.actions;
const { noteBucket, setUnsyncedNoteIds } = this.props;
const { loadNotes } = this.props.actions;

loadNotes({ noteBucket });
setUnsyncedNoteIds({ noteIds: getUnsyncedNoteIds(noteBucket) });
setUnsyncedNoteIds(getUnsyncedNoteIds(noteBucket));
};

onNoteRemoved = () => this.onNotesIndex();
Expand Down Expand Up @@ -388,16 +392,14 @@ export const App = connect(
activityHooks(data, {
onIdle: () => {
const {
actions: { setUnsyncedNoteIds },
appState: { notes },
client,
noteBucket,
setUnsyncedNoteIds,
} = this.props;

nudgeUnsynced({ client, noteBucket, notes });
setUnsyncedNoteIds({
noteIds: getUnsyncedNoteIds(noteBucket),
});
setUnsyncedNoteIds(getUnsyncedNoteIds(noteBucket));
},
});
};
Expand Down
6 changes: 2 additions & 4 deletions lib/components/sync-status/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ class SyncStatus extends Component<StateProps> {
anchorEl={anchorEl}
id={popoverId}
onClose={this.handlePopoverClose}
unsyncedNoteIds={unsyncedNoteIds}
/>
</div>
);
}
}

const mapStateToProps: S.MapState<StateProps> = ({
appState: state,
ui: { simperiumConnected },
ui: { simperiumConnected, unsyncedNoteIds },
}) => ({
simperiumConnected,
unsyncedNoteIds: state.unsyncedNoteIds,
unsyncedNoteIds,
});

export default connect(mapStateToProps)(SyncStatus);
56 changes: 27 additions & 29 deletions lib/components/sync-status/popover.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classnames from 'classnames';
import formatDistanceToNow from 'date-fns/formatDistanceToNow';
Expand All @@ -8,17 +7,26 @@ import { Popover } from '@material-ui/core';
import { getLastSyncedTime } from '../../utils/sync/last-synced-time';
import getNoteTitles from './get-note-titles';

class SyncStatusPopover extends React.Component {
import * as S from '../../state';
import * as T from '../../types';

type StateProps = {
notes: T.NoteEntity[] | null;
theme: T.Theme;
unsyncedNoteIds: T.EntityId[];
};

type OwnProps = {
anchorEl: HTMLElement;
id: T.EntityId;
onClose: () => void;
};

type Props = StateProps & OwnProps;

class SyncStatusPopover extends React.Component<Props> {
render() {
const {
anchorEl,
classes = {},
id,
notes,
onClose,
theme,
unsyncedNoteIds,
} = this.props;
const { anchorEl, id, notes, onClose, theme, unsyncedNoteIds } = this.props;
const themeClass = `theme-${theme}`;
const open = Boolean(anchorEl);
const hasUnsyncedChanges = unsyncedNoteIds.length > 0;
Expand All @@ -34,19 +42,14 @@ class SyncStatusPopover extends React.Component {
return (
<Popover
id={id}
className={classnames(
'sync-status-popover',
classes.popover,
themeClass
)}
className={classnames('sync-status-popover', themeClass)}
classes={{
paper: classnames(
'sync-status-popover__paper',
'theme-color-bg',
'theme-color-border',
'theme-color-fg-dim',
{ 'has-unsynced-changes': hasUnsyncedChanges },
classes.paper
{ 'has-unsynced-changes': hasUnsyncedChanges }
),
}}
open={open}
Expand Down Expand Up @@ -98,19 +101,14 @@ class SyncStatusPopover extends React.Component {
}
}

SyncStatusPopover.propTypes = {
anchorEl: PropTypes.object,
classes: PropTypes.object,
id: PropTypes.string,
notes: PropTypes.array,
onClose: PropTypes.func.isRequired,
theme: PropTypes.string.isRequired,
unsyncedNoteIds: PropTypes.array.isRequired,
};

const mapStateToProps = ({ appState, settings }) => ({
const mapStateToProps: S.MapState<StateProps> = ({
appState,
settings,
ui: { unsyncedNoteIds },
}) => ({
notes: appState.notes,
theme: settings.theme,
unsyncedNoteIds,
});

export default connect(mapStateToProps)(SyncStatusPopover);
9 changes: 0 additions & 9 deletions lib/flux/app-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,6 @@ export const actionMap = new ActionMap({
};
},
},

setUnsyncedNoteIds(
state: AppState,
{ noteIds }: { noteIds: T.EntityId[] }
) {
return update(state, {
unsyncedNoteIds: { $set: noteIds },
});
},
},
});

Expand Down
5 changes: 5 additions & 0 deletions lib/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type SetWPToken = Action<'setWPToken', { token: string }>;
*/
export type FilterNotes = Action<'FILTER_NOTES', { notes: T.NoteEntity[] }>;
export type SetAuth = Action<'AUTH_SET', { status: AuthState }>;
export type SetUnsyncedNoteIds = Action<
'SET_UNSYNCED_NOTE_IDS',
{ noteIds: T.EntityId[] }
>;
export type ToggleSimperiumConnectionStatus = Action<
'SIMPERIUM_CONNECTION_STATUS_TOGGLE',
{ simperiumConnected: boolean }
Expand All @@ -77,6 +81,7 @@ export type ActionType =
| SetSortType
| SetSpellCheck
| SetTheme
| SetUnsyncedNoteIds
| SetWPToken
| ToggleSimperiumConnectionStatus
| ToggleTagDrawer;
Expand Down
7 changes: 7 additions & 0 deletions lib/state/ui/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export const filterNotes: A.ActionCreator<A.FilterNotes> = (
notes,
});

export const setUnsyncedNoteIds: A.ActionCreator<A.SetUnsyncedNoteIds> = (
noteIds: T.EntityId[]
) => ({
type: 'SET_UNSYNCED_NOTE_IDS',
noteIds,
});

export const toggleSimperiumConnectionStatus: A.ActionCreator<A.ToggleSimperiumConnectionStatus> = (
simperiumConnected: boolean
) => ({
Expand Down
6 changes: 6 additions & 0 deletions lib/state/ui/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const filteredNotes: A.Reducer<T.NoteEntity[]> = (
action
) => ('FILTER_NOTES' === action.type ? action.notes : state);

const unsyncedNoteIds: A.Reducer<T.EntityId[]> = (
state = emptyList as T.EntityId[],
action
) => ('SET_UNSYNCED_NOTE_IDS' === action.type ? action.noteIds : state);

const simperiumConnected: A.Reducer<boolean> = (state = false, action) =>
'SIMPERIUM_CONNECTION_STATUS_TOGGLE' === action.type
? action.simperiumConnected
Expand Down Expand Up @@ -54,5 +59,6 @@ export default combineReducers({
filteredNotes,
note,
simperiumConnected,
unsyncedNoteIds,
visiblePanes,
});

0 comments on commit 4b915b1

Please sign in to comment.