1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/components/shared/reduxSharedMiddleware.js
Abijeet Patro ae3a278ac4 Desktop: New: Display selected tags under a note title (#2217)
Follow up to #893

Now using middleware to set the tags when a note is selected

This avoids the ugly code in the NoteTextComponent where we determine
if tags are to be fetched, identify if they have been modified, fetch
them  and then dispatch an action to update the store which might
again re-render the component.

Also implements style related fixes from #1000

Signed-off-by: Abijeet <abijeetpatro@gmail.com>
Fixes: #469
2020-01-06 21:23:22 +00:00

61 lines
1.6 KiB
JavaScript

const Setting = require('lib/models/Setting');
const Tag = require('lib/models/Tag');
const { reg } = require('lib/registry.js');
const ResourceFetcher = require('lib/services/ResourceFetcher');
const reduxSharedMiddleware = async function(store, next, action) {
const newState = store.getState();
let refreshTags = false;
if (action.type == 'FOLDER_SET_COLLAPSED' || action.type == 'FOLDER_TOGGLE') {
Setting.setValue('collapsedFolderIds', newState.collapsedFolderIds);
}
if (action.type === 'SETTING_UPDATE_ONE' && !!action.key.match(/^sync\.\d+\.path$/)) {
reg.resetSyncTarget();
}
if (action.type === 'SETTING_UPDATE_ONE' && action.key === 'sync.resourceDownloadMode') {
ResourceFetcher.instance().autoAddResources();
}
if (action.type == 'NOTE_DELETE' ||
action.type == 'NOTE_UPDATE_ONE' ||
action.type == 'NOTE_UPDATE_ALL' ||
action.type == 'NOTE_TAG_REMOVE' ||
action.type == 'TAG_UPDATE_ONE') {
refreshTags = true;
}
if (action.type === 'NOTE_SELECT' ||
action.type === 'NOTE_SELECT_TOGGLE' ||
action.type === 'NOTE_SET_NEW_ONE') {
let noteTags = [];
// We don't need to show tags unless only one note is selected.
// For new notes, the old note is still selected, but we don't want to show any tags.
if (action.type !== 'NOTE_SET_NEW_ONE' &&
newState.selectedNoteIds &&
newState.selectedNoteIds.length === 1) {
noteTags = await Tag.tagsByNoteId(newState.selectedNoteIds[0]);
}
store.dispatch({
type: 'SET_NOTE_TAGS',
items: noteTags,
});
}
if (refreshTags) {
store.dispatch({
type: 'TAG_UPDATE_ALL',
items: await Tag.allWithNotes(),
});
}
};
module.exports = reduxSharedMiddleware;