Skip to content

Commit 4b022dd

Browse files
committed
Refactored CREATE_FILE and UPDATE_FILE_NAME case into smaller functions
1 parent 855dcea commit 4b022dd

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

client/modules/IDE/reducers/files.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ function sortedChildrenId(state, children) {
116116
return childrenArray.map(child => child.id);
117117
}
118118

119+
function udpateParent(state, action) {
120+
return state.map((file) => {
121+
if (file.id === action.parentId) {
122+
const newFile = Object.assign({}, file);
123+
newFile.children = [...newFile.children, action.id];
124+
return newFile;
125+
}
126+
return file;
127+
});
128+
}
129+
130+
function renameFile(state, action) {
131+
return state.map((file) => {
132+
if (file.id !== action.id) {
133+
return file;
134+
}
135+
return Object.assign({}, file, { name: action.name });
136+
});
137+
}
138+
119139
const files = (state, action) => {
120140
if (state === undefined) {
121141
state = initialState(); // eslint-disable-line
@@ -144,26 +164,17 @@ const files = (state, action) => {
144164
return initialState();
145165
case ActionTypes.CREATE_FILE: // eslint-disable-line
146166
{
147-
let newState = state.map((file) => {
148-
if (file.id === action.parentId) {
149-
const newFile = Object.assign({}, file);
150-
newFile.children = [...newFile.children, action.id];
151-
return newFile;
152-
}
153-
return file;
154-
});
155-
newState = [
156-
...newState,
167+
const newState = [
168+
...udpateParent(state, action),
157169
{
158170
name: action.name,
159171
id: action.id,
160172
_id: action._id,
161173
content: action.content,
162174
url: action.url,
163175
children: action.children,
164-
fileType: action.fileType || 'file',
165-
},
166-
];
176+
fileType: action.fileType || 'file'
177+
}];
167178
return newState.map((file) => {
168179
if (file.id === action.parentId) {
169180
file.children = sortedChildrenId(newState, file.children);
@@ -173,12 +184,7 @@ const files = (state, action) => {
173184
}
174185
case ActionTypes.UPDATE_FILE_NAME:
175186
{
176-
const newState = state.map((file) => {
177-
if (file.id !== action.id) {
178-
return file;
179-
}
180-
return Object.assign({}, file, { name: action.name });
181-
});
187+
const newState = renameFile(state, action);
182188
return newState.map((file) => {
183189
if (file.children.includes(action.id)) {
184190
file.children = sortedChildrenId(newState, file.children);

0 commit comments

Comments
 (0)