From c4eaef975377c2e7025dbb4495c941fa6044f0d4 Mon Sep 17 00:00:00 2001 From: Mark Peace Date: Thu, 19 Oct 2017 14:30:35 +0100 Subject: [PATCH 1/4] Persist grass values in neo4j sync --- src/browser/modules/Sync/BrowserSync.jsx | 6 +++ src/shared/modules/grass/grassDuck.js | 63 +++++++++++++++++++++++- src/shared/modules/sync/syncDuck.js | 39 +++++++++++++++ src/shared/rootEpic.js | 6 ++- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/src/browser/modules/Sync/BrowserSync.jsx b/src/browser/modules/Sync/BrowserSync.jsx index 7d7205e21ce..cde7bf91416 100644 --- a/src/browser/modules/Sync/BrowserSync.jsx +++ b/src/browser/modules/Sync/BrowserSync.jsx @@ -264,6 +264,12 @@ export class BrowserSync extends Component { return ( + {headerContent} {offlineContent} {onlineContent} diff --git a/src/shared/modules/grass/grassDuck.js b/src/shared/modules/grass/grassDuck.js index 3bd4bb10c7a..8dba266d20a 100644 --- a/src/shared/modules/grass/grassDuck.js +++ b/src/shared/modules/grass/grassDuck.js @@ -18,14 +18,36 @@ * along with this program. If not, see . */ +import { getBrowserName } from 'services/utils' import { APP_START } from 'shared/modules/app/appDuck' export const NAME = 'grass' export const UPDATE_GRAPH_STYLE_DATA = 'grass/UPDATE_GRAPH_STYLE_DATA' +export const SYNC_GRASS = 'grass/SYNC_GRASS' + export const getGraphStyleData = state => state[NAME] +const versionSize = 20 const initialState = null +export const composeGrassToSync = (store, syncValue) => { + const grassFromSync = syncValue.syncObj.grass || [] + const grassFromState = getGraphStyleData(store.getState()) + const stringifyedGrassFromState = JSON.stringify(grassFromState) + + if (grassFromSync[0].data !== stringifyedGrassFromState) { + return [ + { + client: getBrowserName(), + data: stringifyedGrassFromState, + syncedAt: Date.now() + } + ].concat(grassFromSync.slice(0, versionSize)) + } + + return grassFromSync +} + function updateStyleData (state, styleData) { return styleData } @@ -34,7 +56,6 @@ export default function visualization (state = initialState, action) { if (action.type === APP_START) { state = !state ? state : { ...initialState, ...state } } - switch (action.type) { case UPDATE_GRAPH_STYLE_DATA: return updateStyleData(state, action.styleData) @@ -49,3 +70,43 @@ export const updateGraphStyleData = graphStyleData => { styleData: graphStyleData } } +export function syncGrass (grass) { + return { + type: SYNC_GRASS, + grass + } +} + +export const grassToLoad = (action, store) => { + const grassFromSync = + action.obj.syncObj && + action.obj.syncObj.grass && + action.obj.syncObj.grass.length > 0 + ? action.obj.syncObj.grass[0].data || {} + : null + + const existingGrass = getGraphStyleData(store.getState()) + const grassHasChanged = grassFromSync !== JSON.stringify(existingGrass) + + if (grassFromSync) { + if (grassHasChanged) { + return { + grass: grassFromSync, + syncGrass: false, + loadGrass: true + } + } else { + return { + grass: existingGrass, + syncGrass: false, + loadGrass: false + } + } + } else { + return { + grass: existingGrass, + syncGrass: true, + loadGrass: false + } + } +} diff --git a/src/shared/modules/sync/syncDuck.js b/src/shared/modules/sync/syncDuck.js index 2f0e2aa1f47..234a7d5e9ff 100644 --- a/src/shared/modules/sync/syncDuck.js +++ b/src/shared/modules/sync/syncDuck.js @@ -19,6 +19,7 @@ */ import { syncResourceFor } from 'services/browserSyncService' + import { setItem } from 'services/localstorage' import { APP_START } from 'shared/modules/app/appDuck' import { @@ -41,6 +42,14 @@ import { loadFolders, syncFolders } from 'shared/modules/favorites/foldersDuck' +import { + grassToLoad, + updateGraphStyleData, + composeGrassToSync, + syncGrass, + SYNC_GRASS, + UPDATE_GRAPH_STYLE_DATA +} from 'shared/modules/grass/grassDuck' import { CLEAR_LOCALSTORAGE } from 'shared/modules/localstorage/localstorageDuck' export const NAME = 'sync' @@ -275,6 +284,7 @@ export const clearSyncEpic = (action$, store) => setItem('documents', null) setItem('folders', null) setItem('syncConsent', false) + setItem('grass', null) }) .mapTo({ type: CLEAR_LOCALSTORAGE }) @@ -313,6 +323,20 @@ export const loadFavoritesFromSyncEpic = (action$, store) => }) .mapTo({ type: 'NOOP' }) +export const loadGrassFromSyncEpic = (action$, store) => + action$ + .ofType(SET_SYNC_DATA) + .do(action => { + const grass = grassToLoad(action, store) + if (grass.loadGrass) { + store.dispatch(updateGraphStyleData(grass.grass)) + } + if (grass.syncGrass) { + store.dispatch(syncGrass(grass.grass)) + } + }) + .mapTo({ type: 'NOOP' }) + export const syncFoldersEpic = (action$, store) => action$ .filter(action => @@ -330,6 +354,21 @@ export const syncFoldersEpic = (action$, store) => return { type: 'NOOP' } }) +export const syncGrassEpic = (action$, store) => + action$ + .filter(action => + [SYNC_GRASS, UPDATE_GRAPH_STYLE_DATA].includes(action.type) + ) + .map(action => { + const syncValue = getSync(store.getState()) + + if (syncValue && syncValue.syncObj) { + const grass = composeGrassToSync(store, syncValue) + return syncItems('grass', grass) + } + return { type: 'NOOP' } + }) + export const loadFoldersFromSyncEpic = (action$, store) => action$ .ofType(SET_SYNC_DATA) diff --git a/src/shared/rootEpic.js b/src/shared/rootEpic.js index db7d6fdb3ce..31646a15f57 100644 --- a/src/shared/rootEpic.js +++ b/src/shared/rootEpic.js @@ -63,8 +63,10 @@ import { clearSyncEpic, syncFavoritesEpic, loadFavoritesFromSyncEpic, + loadGrassFromSyncEpic, loadFoldersFromSyncEpic, - syncFoldersEpic + syncFoldersEpic, + syncGrassEpic } from './modules/sync/syncDuck' import { credentialsTimeoutEpic } from './modules/credentialsPolicy/credentialsPolicyDuck' import { @@ -109,10 +111,12 @@ export default combineEpics( featuresDiscoveryEpic, syncFavoritesEpic, loadFavoritesFromSyncEpic, + loadGrassFromSyncEpic, syncItemsEpic, clearSyncEpic, loadFoldersFromSyncEpic, syncFoldersEpic, + syncGrassEpic, credentialsTimeoutEpic, bootEpic, udcStartupEpic, From c85a9ff9daaf2cb28dc5bbc771b401a0c54d6b42 Mon Sep 17 00:00:00 2001 From: Mark Peace Date: Mon, 18 Dec 2017 15:34:10 +0000 Subject: [PATCH 2/4] Remove button that forces grass sync --- src/browser/modules/Sync/BrowserSync.jsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/browser/modules/Sync/BrowserSync.jsx b/src/browser/modules/Sync/BrowserSync.jsx index cde7bf91416..7d7205e21ce 100644 --- a/src/browser/modules/Sync/BrowserSync.jsx +++ b/src/browser/modules/Sync/BrowserSync.jsx @@ -264,12 +264,6 @@ export class BrowserSync extends Component { return ( - {headerContent} {offlineContent} {onlineContent} From 42e029b398ec8e71f7a20c2e2d7279cd97d363c6 Mon Sep 17 00:00:00 2001 From: Mark Peace Date: Wed, 20 Dec 2017 14:17:30 +0000 Subject: [PATCH 3/4] Create first grass entry in firebase if grass property doesn't exist --- src/shared/modules/grass/grassDuck.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/shared/modules/grass/grassDuck.js b/src/shared/modules/grass/grassDuck.js index 8dba266d20a..56ec8b5119f 100644 --- a/src/shared/modules/grass/grassDuck.js +++ b/src/shared/modules/grass/grassDuck.js @@ -35,7 +35,10 @@ export const composeGrassToSync = (store, syncValue) => { const grassFromState = getGraphStyleData(store.getState()) const stringifyedGrassFromState = JSON.stringify(grassFromState) - if (grassFromSync[0].data !== stringifyedGrassFromState) { + if ( + grassFromSync.length < 1 || + grassFromSync[0].data !== stringifyedGrassFromState + ) { return [ { client: getBrowserName(), From 1d5a55de693605ae6a0da22fcfa12fd907de9050 Mon Sep 17 00:00:00 2001 From: Mark Peace Date: Thu, 21 Dec 2017 17:10:59 +0000 Subject: [PATCH 4/4] Fix issue where grass was being recursively encoded --- src/shared/modules/grass/grassDuck.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/shared/modules/grass/grassDuck.js b/src/shared/modules/grass/grassDuck.js index 56ec8b5119f..e3747b3575b 100644 --- a/src/shared/modules/grass/grassDuck.js +++ b/src/shared/modules/grass/grassDuck.js @@ -37,7 +37,9 @@ export const composeGrassToSync = (store, syncValue) => { if ( grassFromSync.length < 1 || - grassFromSync[0].data !== stringifyedGrassFromState + (grassFromSync[0].data && + (grassFromSync[0].data !== stringifyedGrassFromState && + grassFromSync[0].data !== grassFromState)) ) { return [ {