Skip to content
Merged
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
16 changes: 14 additions & 2 deletions browser/main/SideNav/StorageItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class StorageItem extends React.Component {
constructor (props) {
super(props)

const { storage } = this.props

this.state = {
isOpen: true
isOpen: !!storage.isOpen
}
}

Expand Down Expand Up @@ -68,8 +70,18 @@ class StorageItem extends React.Component {
}

handleToggleButtonClick (e) {
const { storage, dispatch } = this.props
const isOpen = !this.state.isOpen
dataApi.toggleStorage(storage.key, isOpen)
.then((storage) => {
dispatch({
type: 'EXPAND_STORAGE',
storage,
isOpen
})
})
this.setState({
isOpen: !this.state.isOpen
isOpen: isOpen
})
}

Expand Down
6 changes: 4 additions & 2 deletions browser/main/lib/dataApi/addStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ function addStorage (input) {
key,
name: input.name,
type: input.type,
path: input.path
path: input.path,
isOpen: false
}

return Promise.resolve(newStorage)
Expand All @@ -48,7 +49,8 @@ function addStorage (input) {
key: newStorage.key,
type: newStorage.type,
name: newStorage.name,
path: newStorage.path
path: newStorage.path,
isOpen: false
})

localStorage.setItem('storages', JSON.stringify(rawStorages))
Expand Down
1 change: 1 addition & 0 deletions browser/main/lib/dataApi/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const dataApi = {
init: require('./init'),
toggleStorage: require('./toggleStorage'),
addStorage: require('./addStorage'),
renameStorage: require('./renameStorage'),
removeStorage: require('./removeStorage'),
Expand Down
3 changes: 2 additions & 1 deletion browser/main/lib/dataApi/resolveStorageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function resolveStorageData (storageCache) {
key: storageCache.key,
name: storageCache.name,
type: storageCache.type,
path: storageCache.path
path: storageCache.path,
isOpen: storageCache.isOpen
}

const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json')
Expand Down
28 changes: 28 additions & 0 deletions browser/main/lib/dataApi/toggleStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const _ = require('lodash')
const resolveStorageData = require('./resolveStorageData')

/**
* @param {String} key
* @param {Boolean} isOpen
* @return {Object} Storage meta data
*/
function toggleStorage (key, isOpen) {
let cachedStorageList
try {
cachedStorageList = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(cachedStorageList)) throw new Error('invalid storages')
} catch (err) {
console.log('error got')
console.error(err)
return Promise.reject(err)
}
const targetStorage = _.find(cachedStorageList, {key: key})
if (targetStorage == null) return Promise.reject('Storage')

targetStorage.isOpen = isOpen
localStorage.setItem('storages', JSON.stringify(cachedStorageList))

return resolveStorageData(targetStorage)
}

module.exports = toggleStorage
6 changes: 6 additions & 0 deletions browser/main/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ function data (state = defaultDataMap(), action) {
state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage)
return state
case 'EXPAND_STORAGE':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
action.storage.isOpen = action.isOpen
state.storageMap.set(action.storage.key, action.storage)
return state
}
return state
}
Expand Down
38 changes: 38 additions & 0 deletions tests/dataApi/toggleStorage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const test = require('ava')
const toggleStorage = require('browser/main/lib/dataApi/toggleStorage')

global.document = require('jsdom').jsdom('<body></body>')
global.window = document.defaultView
global.navigator = window.navigator

const Storage = require('dom-storage')
const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true })
const path = require('path')
const _ = require('lodash')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')

const storagePath = path.join(os.tmpdir(), 'test/toggle-storage')

test.beforeEach((t) => {
t.context.storage = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
})

test.serial('Toggle a storage location', (t) => {
const storageKey = t.context.storage.cache.key
return Promise.resolve()
.then(function doTest () {
return toggleStorage(storageKey, true)
})
.then(function assert (data) {
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
t.true(_.find(cachedStorageList, {key: storageKey}).isOpen === true)
})
})

test.after(function after () {
localStorage.clear()
sander.rimrafSync(storagePath)
})