From 4b022dd0e861ce312e6eb48ec0190e7362cd03d3 Mon Sep 17 00:00:00 2001 From: Neelesh Date: Tue, 4 Aug 2020 10:28:21 +0530 Subject: [PATCH] Refactored CREATE_FILE and UPDATE_FILE_NAME case into smaller functions --- client/modules/IDE/reducers/files.js | 44 ++++++++++++++++------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 9c07cbbc..1f03e8de 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -116,6 +116,26 @@ function sortedChildrenId(state, children) { return childrenArray.map(child => child.id); } +function udpateParent(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) => { if (state === undefined) { state = initialState(); // eslint-disable-line @@ -144,16 +164,8 @@ const files = (state, action) => { return initialState(); case ActionTypes.CREATE_FILE: // eslint-disable-line { - let newState = state.map((file) => { - if (file.id === action.parentId) { - const newFile = Object.assign({}, file); - newFile.children = [...newFile.children, action.id]; - return newFile; - } - return file; - }); - newState = [ - ...newState, + const newState = [ + ...udpateParent(state, action), { name: action.name, id: action.id, @@ -161,9 +173,8 @@ const files = (state, action) => { content: action.content, url: action.url, 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); @@ -173,12 +184,7 @@ const files = (state, action) => { } case ActionTypes.UPDATE_FILE_NAME: { - const newState = state.map((file) => { - if (file.id !== action.id) { - return file; - } - return Object.assign({}, file, { name: action.name }); - }); + const newState = renameFile(state, action); return newState.map((file) => { if (file.children.includes(action.id)) { file.children = sortedChildrenId(newState, file.children);