@@ -36,20 +36,39 @@ const bookmarkFoldersState = {
36
36
37
37
getFolder : ( state , folderKey ) => {
38
38
state = validateState ( state )
39
+
40
+ if ( folderKey == null ) {
41
+ return Immutable . Map ( )
42
+ }
43
+
39
44
folderKey = folderKey . toString ( )
40
45
return state . getIn ( [ STATE_SITES . BOOKMARK_FOLDERS , folderKey ] , Immutable . Map ( ) )
41
46
} ,
42
47
43
48
getFoldersByParentId : ( state , parentFolderId ) => {
44
49
state = validateState ( state )
45
50
51
+ if ( parentFolderId == null ) {
52
+ return Immutable . List ( )
53
+ }
54
+
46
55
const folders = bookmarkOrderCache . getFoldersByParentId ( state , parentFolderId )
47
56
return folders . map ( folder => bookmarkFoldersState . getFolder ( state , folder . get ( 'key' ) ) )
48
57
} ,
49
58
50
59
addFolder : ( state , folderDetails , destinationKey ) => {
51
60
state = validateState ( state )
52
61
62
+ if ( folderDetails == null ) {
63
+ return state
64
+ }
65
+
66
+ folderDetails = makeImmutable ( folderDetails )
67
+
68
+ if ( folderDetails . get ( 'key' ) == null ) {
69
+ return state
70
+ }
71
+
53
72
state = state . setIn ( [ STATE_SITES . BOOKMARK_FOLDERS , folderDetails . get ( 'key' ) ] , folderDetails )
54
73
state = bookmarkOrderCache . addFolderToCache ( state , folderDetails . get ( 'parentFolderId' ) , folderDetails . get ( 'key' ) , destinationKey )
55
74
return state
@@ -78,24 +97,23 @@ const bookmarkFoldersState = {
78
97
} ,
79
98
80
99
removeFolder : ( state , folderKey ) => {
81
- const bookmarksState = require ( './bookmarksState' )
82
- const folders = bookmarkFoldersState . getFolders ( state )
100
+ state = validateState ( state )
83
101
const folder = bookmarkFoldersState . getFolder ( state , folderKey )
84
102
85
103
if ( folder . isEmpty ( ) ) {
86
104
return state
87
105
}
88
106
107
+ const bookmarksState = require ( './bookmarksState' )
108
+ const folders = bookmarkFoldersState . getFolders ( state )
109
+
89
110
if ( getSetting ( settings . SYNC_ENABLED ) === true ) {
90
111
syncActions . removeSites ( [ folder . toJS ( ) ] )
91
112
}
92
113
93
114
folders . filter ( folder => folder . get ( 'parentFolderId' ) === Number ( folderKey ) )
94
115
. map ( folder => {
95
- state = bookmarksState . removeBookmarksByParentId ( state , folder . get ( 'folderId' ) )
96
116
state = bookmarkFoldersState . removeFolder ( state , folder . get ( 'folderId' ) )
97
- state = bookmarkOrderCache . removeCacheParent ( state , folder . get ( 'folderId' ) )
98
- state = bookmarkOrderCache . removeCacheKey ( state , folder . get ( 'parentFolderId' ) , folderKey )
99
117
} )
100
118
101
119
state = bookmarksState . removeBookmarksByParentId ( state , folderKey )
@@ -105,14 +123,15 @@ const bookmarkFoldersState = {
105
123
} ,
106
124
107
125
/**
108
- * Get all folders except provided folder
126
+ * Get a list of all folders except provided folder
109
127
* @param state
110
- * @param folderKey
128
+ * @param { number } folderKey
111
129
* @param parentFolderId
112
130
* @param labelPrefix
113
- * @returns {Array }
131
+ * @returns {Array } - each entry with folder id and label (title)
114
132
*/
115
133
getFoldersWithoutKey : ( state , folderKey , parentFolderId = 0 , labelPrefix = '' ) => {
134
+ state = validateState ( state )
116
135
let folders = [ ]
117
136
const results = bookmarkFoldersState . getFoldersByParentId ( state , parentFolderId )
118
137
@@ -128,27 +147,36 @@ const bookmarkFoldersState = {
128
147
folderId : folder . get ( 'folderId' ) ,
129
148
label
130
149
} )
131
- const subSites = bookmarkFoldersState . getFoldersWithoutKey ( state , folderKey , folder . get ( 'folderId' ) , ( label || '' ) + ' / ' )
132
- folders = folders . concat ( subSites )
150
+ const subFolders = bookmarkFoldersState . getFoldersWithoutKey ( state , folderKey , folder . get ( 'folderId' ) , ( label || '' ) + ' / ' )
151
+ folders = folders . concat ( subFolders )
133
152
}
134
153
135
154
return folders
136
155
} ,
137
156
138
157
moveFolder : ( state , folderKey , destinationKey , append , moveIntoParent ) => {
139
- const bookmarksState = require ( './bookmarksState' )
158
+ state = validateState ( state )
140
159
let folder = bookmarkFoldersState . getFolder ( state , folderKey )
141
- let destinationItem = bookmarksState . findBookmark ( state , destinationKey )
142
-
143
160
if ( folder . isEmpty ( ) ) {
144
161
return state
145
162
}
146
163
164
+ const bookmarksState = require ( './bookmarksState' )
165
+ let destinationItem = bookmarksState . findBookmark ( state , destinationKey )
166
+ const numKey = Number ( destinationKey )
167
+ if ( destinationItem . isEmpty ( ) && numKey !== - 1 && numKey !== 0 ) {
168
+ return state
169
+ }
170
+
147
171
if ( moveIntoParent || destinationItem . get ( 'parentFolderId' ) !== folder . get ( 'parentFolderId' ) ) {
148
- const parentFolderId = destinationItem . get ( 'type' ) === siteTags . BOOKMARK
172
+ let parentFolderId = destinationItem . get ( 'type' ) === siteTags . BOOKMARK
149
173
? destinationItem . get ( 'parentFolderId' )
150
174
: destinationItem . get ( 'folderId' )
151
175
176
+ if ( parentFolderId == null ) {
177
+ parentFolderId = destinationKey
178
+ }
179
+
152
180
state = bookmarkOrderCache . removeCacheKey ( state , folder . get ( 'parentFolderId' ) , folderKey )
153
181
folder = folder . set ( 'parentFolderId' , Number ( parentFolderId ) )
154
182
const newKey = bookmarkFoldersUtil . getKey ( folder )
@@ -169,7 +197,14 @@ const bookmarkFoldersState = {
169
197
} ,
170
198
171
199
setWidth : ( state , key , width ) => {
172
- return state . setIn ( [ STATE_SITES . BOOKMARK_FOLDERS , key , 'width' ] , parseFloat ( width ) )
200
+ state = validateState ( state )
201
+ width = parseFloat ( width )
202
+
203
+ if ( key == null || isNaN ( width ) ) {
204
+ return state
205
+ }
206
+
207
+ return state . setIn ( [ STATE_SITES . BOOKMARK_FOLDERS , key , 'width' ] , width )
173
208
}
174
209
}
175
210
0 commit comments