Skip to content

Commit

Permalink
Merge pull request neo4j#678 from pe4cey/3.0-sync-grass
Browse files Browse the repository at this point in the history
Persist grass to neo4j sync
  • Loading branch information
oskarhane authored Feb 23, 2018
2 parents 2be5a37 + 1d5a55d commit 690e993
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
68 changes: 67 additions & 1 deletion src/shared/modules/grass/grassDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,41 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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.length < 1 ||
(grassFromSync[0].data &&
(grassFromSync[0].data !== stringifyedGrassFromState &&
grassFromSync[0].data !== grassFromState))
) {
return [
{
client: getBrowserName(),
data: stringifyedGrassFromState,
syncedAt: Date.now()
}
].concat(grassFromSync.slice(0, versionSize))
}

return grassFromSync
}

function updateStyleData (state, styleData) {
return styleData
}
Expand All @@ -34,7 +61,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)
Expand All @@ -49,3 +75,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
}
}
}
39 changes: 39 additions & 0 deletions src/shared/modules/sync/syncDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import { syncResourceFor } from 'services/browserSyncService'

import { setItem } from 'services/localstorage'
import { APP_START } from 'shared/modules/app/appDuck'
import {
Expand All @@ -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'
Expand Down Expand Up @@ -278,6 +287,7 @@ export const clearSyncEpic = (action$, store) =>
setItem('documents', null)
setItem('folders', null)
setItem('syncConsent', false)
setItem('grass', null)
})
.mapTo({ type: CLEAR_LOCALSTORAGE })

Expand Down Expand Up @@ -316,6 +326,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 =>
Expand All @@ -333,6 +357,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)
Expand Down
6 changes: 5 additions & 1 deletion src/shared/rootEpic.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ import {
clearSyncEpic,
syncFavoritesEpic,
loadFavoritesFromSyncEpic,
loadGrassFromSyncEpic,
loadFoldersFromSyncEpic,
syncFoldersEpic
syncFoldersEpic,
syncGrassEpic
} from './modules/sync/syncDuck'
import { credentialsTimeoutEpic } from './modules/credentialsPolicy/credentialsPolicyDuck'
import {
Expand Down Expand Up @@ -109,10 +111,12 @@ export default combineEpics(
featuresDiscoveryEpic,
syncFavoritesEpic,
loadFavoritesFromSyncEpic,
loadGrassFromSyncEpic,
syncItemsEpic,
clearSyncEpic,
loadFoldersFromSyncEpic,
syncFoldersEpic,
syncGrassEpic,
credentialsTimeoutEpic,
bootEpic,
udcStartupEpic,
Expand Down

0 comments on commit 690e993

Please sign in to comment.