Skip to content

Commit

Permalink
refactor: New reducer model
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed May 22, 2019
1 parent 5a4517d commit 3259473
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/drive/web/modules/navigation/RealtimeFiles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export class RealtimeFiles extends React.Component {
}

onDocumentDeletion = doc => {
if (this.isInCurrentView(doc)) this.props.deleteFile(this.normalizeId(doc))
//Ne pas faire un deleteFile quand on vide la corbeille car on a déjà une action pour cela
if (this.isInCurrentView(doc) && doc.dir_id !== 'io.cozy.files.trash-dir')
this.props.deleteFile(this.normalizeId(doc))
}

isInCurrentView(doc) {
Expand Down
14 changes: 7 additions & 7 deletions src/drive/web/modules/navigation/duck/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ export const fetchRecentFiles = () => {
const path = parentFolder ? parentFolder.doc.path : ''
return { ...file, path, id: file._id }
})
if (lastestActionSelector(getState()) === action) {
return dispatch({
type: FETCH_RECENT_SUCCESS,
fileCount: filesWithPath.length,
files: filesWithPath
})
}
// if (lastestActionSelector(getState()) === action) {
return dispatch({
type: FETCH_RECENT_SUCCESS,
fileCount: filesWithPath.length,
files: filesWithPath
})
//}
} catch (err) {
logException(err, {
context: FETCH_RECENT_FAILURE
Expand Down
121 changes: 102 additions & 19 deletions src/drive/web/modules/navigation/duck/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ const displayedFolder = (state = null, action) => {
}
}

const currentView = (state = '', action) => {
switch (action.type) {
case OPEN_FOLDER:
if (action.folderId === 'io.cozy.files.trash-dir') {
return 'trash'
}
return 'folder'
case FETCH_RECENT:
return 'recent'
case FETCH_SHARINGS:
return 'sharings'
default:
return state
}
}
const latestAction = (state, action) => {
return action.type
}
Expand Down Expand Up @@ -193,45 +208,97 @@ const indexFor = (file, array, compareFn, start = 0, end = array.length) => {
}

// reducer for the full file list of the currently displayed folder
const files = (state = [], action) => {
const files = (
state = { shared: [], recent: [], trashed: [], folder: [] },
action
) => {
switch (action.type) {
case OPEN_FOLDER_SUCCESS:
if (action.folder.id === 'io.cozy.files.trash-dir') {
return {
...state,
trashed: action.files
}
}
return {
...state,
folder: action.files
}
case FETCH_RECENT_SUCCESS:
return {
...state,
recent: action.files
}
case FETCH_SHARINGS_SUCCESS:
return {
...state,
shared: action.files
}
case SORT_FOLDER_SUCCESS:
return action.files
return {
...state,
folder: action.files
}
case FETCH_MORE_FILES_SUCCESS: {
const clone = state.slice(0)
const clone = state.folder.slice(0)
action.files.forEach((f, i) => {
clone[action.skip + i] = f
})
return clone

return {
...state,
folder: clone
}
}
case RENAME_SUCCESS:
case UPDATE_FILE:
return updateItem(action.file, state)
case ADD_FILE:
return insertItem(
case UPDATE_FILE: {
const newStateFile = updateItem(action.file, state.folder)
return {
...state,
folder: newStateFile
}
}
case ADD_FILE: {
const newFolderState = insertItem(
action.file,
state,
state.folder,
action.currentFileCount,
action.currentSort
)
case CREATE_FOLDER_SUCCESS:
return insertItem(
return {
...state,
folder: newFolderState
}
}
case CREATE_FOLDER_SUCCESS: {
const newFolderState = insertItem(
action.folder,
state,
state.folder,
action.currentFileCount,
action.currentSort
)
return {
...state,
folder: newFolderState
}
}
case TRASH_FILES_SUCCESS:
case RESTORE_FILES_SUCCESS:
case DESTROY_FILES_SUCCESS:
return state.filter(f => action.ids.indexOf(f.id) === -1)
return {
...state,
trashed: state.trashed.filter(f => action.ids.indexOf(f.id) === -1)
}
case DELETE_FILE:
return state.filter(f => action.file.id !== f.id)
return {
...state,
folder: state.folder.filter(f => action.file.id !== f.id)
}
case EMPTY_TRASH_SUCCESS:
return []
return {
...state,
trashed: []
}
default:
return state
}
Expand Down Expand Up @@ -341,20 +408,36 @@ export default combineReducers({
sort,
files,
fetchStatus,
lastFetch
lastFetch,
currentView
})

// TODO: temp
export const getFilesWithLinks = ({ view }, folderId) =>
view.filesWithLinks[folderId]

export const getVisibleFiles = ({ view }) =>
view.files.map(f => ensureFileHavePath({ view }, f))
export const getVisibleFiles = ({ view }) => {
return getCurrentFilesIndex(view).map(f => ensureFileHavePath({ view }, f))
}

export const getSort = ({ view }) => view.sort

export const getCurrentFilesIndex = view => {
switch (view.currentView) {
case 'folder':
return view.files.folder
case 'recent':
return view.files.recent
case 'sharings':
return view.files.shared
case 'trash':
return view.files.trashed
default:
return view.files.folder
}
}
export const getFileById = ({ view }, id) => {
const file = view.files.find(f => f && f.id && f.id === id)
const file = getCurrentFilesIndex(view).find(f => f && f.id && f.id === id)
if (!file) return null
// we need the path for some actions, like selection download
// but the stack only provides the path for folders...
Expand Down

0 comments on commit 3259473

Please sign in to comment.