@@ -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,12 @@ 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+
113119const files = ( state , action ) => {
114120 if ( state === undefined ) {
115121 state = initialState ( ) ; // eslint-disable-line
@@ -138,33 +144,48 @@ const files = (state, action) => {
138144 return initialState ( ) ;
139145 case ActionTypes . CREATE_FILE : // eslint-disable-line
140146 {
141- const newState = state . map ( ( file ) => {
147+ let newState = state . map ( ( file ) => {
142148 if ( file . id === action . parentId ) {
143149 const newFile = Object . assign ( { } , file ) ;
144150 newFile . children = [ ...newFile . children , action . id ] ;
145151 return newFile ;
146152 }
147153 return file ;
148154 } ) ;
149- return [ ...newState ,
155+ newState = [
156+ ...newState ,
150157 {
151158 name : action . name ,
152159 id : action . id ,
153160 _id : action . _id ,
154161 content : action . content ,
155162 url : action . url ,
156163 children : action . children ,
157- fileType : action . fileType || 'file'
158- } ] ;
164+ fileType : action . fileType || 'file' ,
165+ } ,
166+ ] ;
167+ return newState . map ( ( file ) => {
168+ if ( file . id === action . parentId ) {
169+ file . children = sortedChildrenId ( newState , file . children ) ;
170+ }
171+ return file ;
172+ } ) ;
159173 }
160174 case ActionTypes . UPDATE_FILE_NAME :
161- return state . map ( ( file ) => {
175+ {
176+ const newState = state . map ( ( file ) => {
162177 if ( file . id !== action . id ) {
163178 return file ;
164179 }
165-
166180 return Object . assign ( { } , file , { name : action . name } ) ;
167181 } ) ;
182+ return newState . map ( ( file ) => {
183+ if ( file . children . includes ( action . id ) ) {
184+ file . children = sortedChildrenId ( newState , file . children ) ;
185+ }
186+ return file ;
187+ } ) ;
188+ }
168189 case ActionTypes . DELETE_FILE :
169190 {
170191 const newState = deleteMany ( state , [ action . id , ...getAllDescendantIds ( state , action . id ) ] ) ;
0 commit comments