import { finishEnumSubFolders } from '../actions/index' ; import { Folder , AppState } from '../RendererTypes' ; param old param current returns export const merge = ( old , current ) : Folder [ ] => { if ( old . length === 0 || current . length === 0 ) { return current ; } const exists = old . filter ( ( a ) => current . some ( ( b ) => a . path === b . path ) ) ; const added = current . filter ( ( a ) => ! old . some ( ( b ) => a . path === b . path ) ) ; return exists . concat ( added ) . sort ( ( a , b ) => ( a . name . toLowerCase ( ) < b . name . toLowerCase ( ) ? - 1 : 1 ) ) ; } ; param folderPath param newSubFolders param updateTargetFolders returns const checkFolders = ( folderPath , newSubFolders , updateTargetFolders ) : boolean => { for ( let targetFolder of updateTargetFolders ) { if ( ! folderPath . startsWith ( targetFolder . path ) ) { continue ; } else if ( targetFolder . path === folderPath ) { targetFolder . subFolders = merge ( targetFolder . subFolders , newSubFolders ) ; return true ; } if ( checkFolders ( folderPath , newSubFolders , targetFolder . subFolders ) ) { return true ; } } return false ; } ; param state param action returns export const checkEnumSubFolders = ( state , action ) : AppState => { if ( ! action . payload . subFolders || action . payload . subFolders . length === 0 ) { return state ; } const updateTargetFolders = state . folders . concat ( ) ; updateTargetFolders . forEach ( ( rootFolder ) => { if ( ! action . payload . folderPath . startsWith ( rootFolder . path ) ) { return ; } const subFolders = action . payload . subFolders . map ( ( subFolder ) : Folder => Object . assign ( { } , subFolder , { treeId : rootFolder . treeId } ) ) ; checkFolders ( action . payload . folderPath , subFolders , [ rootFolder ] ) ; } ) ; return Object . assign ( { } , state , { folders : updateTargetFolders } ) ; } ;