@@ -45,7 +45,7 @@ const initialState = () => {
45
45
name : 'root' ,
46
46
id : r ,
47
47
_id : r ,
48
- children : [ a , b , c ] ,
48
+ children : [ b , a , c ] ,
49
49
fileType : 'folder' ,
50
50
content : ''
51
51
} ,
@@ -110,6 +110,32 @@ function deleteMany(state, ids) {
110
110
return newState ;
111
111
}
112
112
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
+
113
139
const files = ( state , action ) => {
114
140
if ( state === undefined ) {
115
141
state = initialState ( ) ; // eslint-disable-line
@@ -138,15 +164,8 @@ const files = (state, action) => {
138
164
return initialState ( ) ;
139
165
case ActionTypes . CREATE_FILE : // eslint-disable-line
140
166
{
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 ) ,
150
169
{
151
170
name : action . name ,
152
171
id : action . id ,
@@ -156,15 +175,23 @@ const files = (state, action) => {
156
175
children : action . children ,
157
176
fileType : action . fileType || 'file'
158
177
} ] ;
178
+ return newState . map ( ( file ) => {
179
+ if ( file . id === action . parentId ) {
180
+ file . children = sortedChildrenId ( newState , file . children ) ;
181
+ }
182
+ return file ;
183
+ } ) ;
159
184
}
160
185
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 ) ;
164
191
}
165
-
166
- return Object . assign ( { } , file , { name : action . name } ) ;
192
+ return file ;
167
193
} ) ;
194
+ }
168
195
case ActionTypes . DELETE_FILE :
169
196
{
170
197
const newState = deleteMany ( state , [ action . id , ...getAllDescendantIds ( state , action . id ) ] ) ;
@@ -200,7 +227,10 @@ const files = (state, action) => {
200
227
return file ;
201
228
} ) ;
202
229
default :
203
- return state ;
230
+ return state . map ( ( file ) => {
231
+ file . children = sortedChildrenId ( state , file . children ) ;
232
+ return file ;
233
+ } ) ;
204
234
}
205
235
} ;
206
236
0 commit comments