Merge pull request #1530 from neelesh7singh/sort-sidebar-folders
Order files alphabetically in sidebar and nested folders.
This commit is contained in:
commit
2948412a33
1 changed files with 46 additions and 16 deletions
|
@ -45,7 +45,7 @@ const initialState = () => {
|
||||||
name: 'root',
|
name: 'root',
|
||||||
id: r,
|
id: r,
|
||||||
_id: r,
|
_id: r,
|
||||||
children: [a, b, c],
|
children: [b, a, c],
|
||||||
fileType: 'folder',
|
fileType: 'folder',
|
||||||
content: ''
|
content: ''
|
||||||
},
|
},
|
||||||
|
@ -110,6 +110,32 @@ function deleteMany(state, ids) {
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sortedChildrenId(state, children) {
|
||||||
|
const childrenArray = state.filter(file => children.includes(file.id));
|
||||||
|
childrenArray.sort((a, b) => (a.name > b.name ? 1 : -1));
|
||||||
|
return childrenArray.map(child => child.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateParent(state, action) {
|
||||||
|
return state.map((file) => {
|
||||||
|
if (file.id === action.parentId) {
|
||||||
|
const newFile = Object.assign({}, file);
|
||||||
|
newFile.children = [...newFile.children, action.id];
|
||||||
|
return newFile;
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renameFile(state, action) {
|
||||||
|
return state.map((file) => {
|
||||||
|
if (file.id !== action.id) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
return Object.assign({}, file, { name: action.name });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const files = (state, action) => {
|
const files = (state, action) => {
|
||||||
if (state === undefined) {
|
if (state === undefined) {
|
||||||
state = initialState(); // eslint-disable-line
|
state = initialState(); // eslint-disable-line
|
||||||
|
@ -138,15 +164,8 @@ const files = (state, action) => {
|
||||||
return initialState();
|
return initialState();
|
||||||
case ActionTypes.CREATE_FILE: // eslint-disable-line
|
case ActionTypes.CREATE_FILE: // eslint-disable-line
|
||||||
{
|
{
|
||||||
const newState = state.map((file) => {
|
const newState = [
|
||||||
if (file.id === action.parentId) {
|
...updateParent(state, action),
|
||||||
const newFile = Object.assign({}, file);
|
|
||||||
newFile.children = [...newFile.children, action.id];
|
|
||||||
return newFile;
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
});
|
|
||||||
return [...newState,
|
|
||||||
{
|
{
|
||||||
name: action.name,
|
name: action.name,
|
||||||
id: action.id,
|
id: action.id,
|
||||||
|
@ -156,15 +175,23 @@ const files = (state, action) => {
|
||||||
children: action.children,
|
children: action.children,
|
||||||
fileType: action.fileType || 'file'
|
fileType: action.fileType || 'file'
|
||||||
}];
|
}];
|
||||||
|
return newState.map((file) => {
|
||||||
|
if (file.id === action.parentId) {
|
||||||
|
file.children = sortedChildrenId(newState, file.children);
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
case ActionTypes.UPDATE_FILE_NAME:
|
case ActionTypes.UPDATE_FILE_NAME:
|
||||||
return state.map((file) => {
|
{
|
||||||
if (file.id !== action.id) {
|
const newState = renameFile(state, action);
|
||||||
return file;
|
return newState.map((file) => {
|
||||||
|
if (file.children.includes(action.id)) {
|
||||||
|
file.children = sortedChildrenId(newState, file.children);
|
||||||
}
|
}
|
||||||
|
return file;
|
||||||
return Object.assign({}, file, { name: action.name });
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
case ActionTypes.DELETE_FILE:
|
case ActionTypes.DELETE_FILE:
|
||||||
{
|
{
|
||||||
const newState = deleteMany(state, [action.id, ...getAllDescendantIds(state, action.id)]);
|
const newState = deleteMany(state, [action.id, ...getAllDescendantIds(state, action.id)]);
|
||||||
|
@ -200,7 +227,10 @@ const files = (state, action) => {
|
||||||
return file;
|
return file;
|
||||||
});
|
});
|
||||||
default:
|
default:
|
||||||
return state;
|
return state.map((file) => {
|
||||||
|
file.children = sortedChildrenId(state, file.children);
|
||||||
|
return file;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue