From 2f2c1ef4029dee17be0d16acdd60b7d1718d519f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 19 Jul 2017 02:06:56 +0200 Subject: refactor file browser stuff --- client/reducers/folders.js | 25 ++++++++++++++++++++++--- client/reducers/index.js | 2 ++ client/reducers/newFolder.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 client/reducers/newFolder.js (limited to 'client/reducers') 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 -- cgit v1.2.3-70-g09d2