@@ -45,7 +45,7 @@ const initialState = () => {
4545 name : 'root' ,
4646 id : r ,
4747 _id : r ,
48- children : [ a , b , c ] ,
48+ children : [ b , a , c ] ,
4949 fileType : 'folder' ,
5050 content : ''
5151 } ,
@@ -110,6 +110,32 @@ function deleteMany(state, ids) {
110110 return newState ;
111111}
112112
113+ function sortedChildrenId ( state , children ) {
114+ const childrenArray = state . filter ( file => children . includes ( file . id ) ) ;
115+ childrenArray . sort ( ( a , b ) => ( a . name > b . name ? 1 : - 1 ) ) ;
116+ return childrenArray . map ( child => child . id ) ;
117+ }
118+
119+ function updateParent ( 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+
113139const files = ( state , action ) => {
114140 if ( state === undefined ) {
115141 state = initialState ( ) ; // eslint-disable-line
@@ -138,15 +164,8 @@ const files = (state, action) => {
138164 return initialState ( ) ;
139165 case ActionTypes . CREATE_FILE : // eslint-disable-line
140166 {
141- const newState = state . map ( ( file ) => {
142- if ( file . id === action . parentId ) {
143- const newFile = Object . assign ( { } , file ) ;
144- newFile . children = [ ...newFile . children , action . id ] ;
145- return newFile ;
146- }
147- return file ;
148- } ) ;
149- return [ ...newState ,
167+ const newState = [
168+ ...updateParent ( state , action ) ,
150169 {
151170 name : action . name ,
152171 id : action . id ,
@@ -156,15 +175,23 @@ const files = (state, action) => {
156175 children : action . children ,
157176 fileType : action . fileType || 'file'
158177 } ] ;
178+ return newState . map ( ( file ) => {
179+ if ( file . id === action . parentId ) {
180+ file . children = sortedChildrenId ( newState , file . children ) ;
181+ }
182+ return file ;
183+ } ) ;
159184 }
160185 case ActionTypes . UPDATE_FILE_NAME :
161- return state . map ( ( file ) => {
162- if ( file . id !== action . id ) {
163- return file ;
186+ {
187+ const newState = renameFile ( state , action ) ;
188+ return newState . map ( ( file ) => {
189+ if ( file . children . includes ( action . id ) ) {
190+ file . children = sortedChildrenId ( newState , file . children ) ;
164191 }
165-
166- return Object . assign ( { } , file , { name : action . name } ) ;
192+ return file ;
167193 } ) ;
194+ }
168195 case ActionTypes . DELETE_FILE :
169196 {
170197 const newState = deleteMany ( state , [ action . id , ...getAllDescendantIds ( state , action . id ) ] ) ;
@@ -200,7 +227,10 @@ const files = (state, action) => {
200227 return file ;
201228 } ) ;
202229 default :
203- return state ;
230+ return state . map ( ( file ) => {
231+ file . children = sortedChildrenId ( state , file . children ) ;
232+ return file ;
233+ } ) ;
204234 }
205235} ;
206236
0 commit comments