summaryrefslogtreecommitdiff
path: root/client/reducers
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2017-07-19 02:06:56 +0200
committerJules Laplace <julescarbon@gmail.com>2017-07-19 02:06:56 +0200
commit2f2c1ef4029dee17be0d16acdd60b7d1718d519f (patch)
tree61e982c3a5bc6e8de8feddf423b8aeee72aa0c95 /client/reducers
parent64e8c03dea044752bf3f2f228462721fe565f950 (diff)
refactor file browser stuff
Diffstat (limited to 'client/reducers')
-rw-r--r--client/reducers/folders.js25
-rw-r--r--client/reducers/index.js2
-rw-r--r--client/reducers/newFolder.js32
3 files changed, 56 insertions, 3 deletions
diff --git a/client/reducers/folders.js b/client/reducers/folders.js
index a69df74..bec4ad3 100644
--- a/client/reducers/folders.js
+++ b/client/reducers/folders.js
@@ -2,7 +2,7 @@
import client from '../client'
const folders = (state = {}, action) => {
- let file, files, folders, folder, openFolders, folder_id
+ let file, files, folders, folder, openFolders, folder_id, filesAreLoaded
console.log(action)
switch (action.type) {
case 'LOAD_FOLDERS':
@@ -24,7 +24,14 @@ const folders = (state = {}, action) => {
})
return {
...state,
- folders: folders,
+ folders
+ }
+
+ case 'ADD_FOLDER':
+ folders = [ action.folder ].concat(state.folders)
+ return {
+ ...state,
+ folders
}
case 'LOAD_OPEN_FOLDERS':
@@ -36,10 +43,22 @@ const folders = (state = {}, action) => {
case 'OPEN_FOLDER':
openFolders = state.openFolders
folder = action.folder
+ folder_id = folder.id
if (openFolders.indexOf(folder.id) === -1) {
openFolders = openFolders.concat(folder.id)
localStorage['openFolders'] = JSON.stringify(openFolders)
}
+ filesAreLoaded = state.folders.some( (folder) => {
+ if (folder.id === folder_id && folder.files) {
+ return true
+ }
+ return false
+ })
+ if (! filesAreLoaded) {
+ client.file.index({ folder_id }).then( (files) => {
+ action.cb && action.cb(files)
+ })
+ }
return {
...state,
openFolders,
@@ -57,7 +76,7 @@ const folders = (state = {}, action) => {
case 'ADD_FILES':
files = action.files
- folder_id = Number(files[0].folder_id)
+ folder_id = Number(files[0] && files[0].folder_id)
folders = state.folders.map( (folder) => {
if (folder.id === folder_id) {
folder.files = ( folder.files || [] ).concat(files)
diff --git a/client/reducers/index.js b/client/reducers/index.js
index 8fa287b..8a6ea2f 100644
--- a/client/reducers/index.js
+++ b/client/reducers/index.js
@@ -4,12 +4,14 @@ import audioPlayer from './audioPlayer'
import currentTask from './currentTask'
import tasks from './tasks'
import folders from './folders'
+import newFolder from './newFolder'
const cortexApp = combineReducers({
audioPlayer,
currentTask,
tasks,
folders,
+ newFolder,
})
export default cortexApp
diff --git a/client/reducers/newFolder.js b/client/reducers/newFolder.js
new file mode 100644
index 0000000..5c0f40d
--- /dev/null
+++ b/client/reducers/newFolder.js
@@ -0,0 +1,32 @@
+import client from '../client'
+
+const newFolder = (state = {}, action) => {
+ switch (action.type) {
+ case 'INIT_NEW_FOLDER':
+ return {
+ name: '',
+ visible: true,
+ }
+
+ case 'CANCEL_NEW_FOLDER':
+ return {
+ name: '',
+ visible: false,
+ }
+
+ case 'CREATE_NEW_FOLDER':
+ if (action.name) {
+ client.folder.create({ name: action.name })
+ .then(action.cb)
+ }
+ return {
+ name: '',
+ visible: false,
+ }
+
+ default:
+ return state
+ }
+}
+
+export default newFolder