5
5
updateItemInArray ,
6
6
createReducer ,
7
7
preserveValuesFromState ,
8
+ pathToArr ,
8
9
} from '../utils/reducers' ;
9
10
10
11
const {
@@ -18,30 +19,54 @@ const {
18
19
} = actionTypes ;
19
20
20
21
/**
21
- * Case reducer for adding a document to a collection.
22
+ * Case reducer for modifying a document within a collection or
23
+ * subcollection. When storeAs is being used, subcollections are
24
+ * moved to the level of the storeAs (instead of on their parent doc).
22
25
* @param {Array } [collectionState=[]] - Redux state of current collection
23
26
* @param {Object } action - The action that was dispatched
24
27
* @return {Array } State with document modified
25
28
*/
26
- function addDoc ( array = [ ] , action ) {
27
- return [
28
- ...array . slice ( 0 , action . payload . ordered . newIndex ) ,
29
- { id : action . meta . doc , ...action . payload . data } ,
30
- ...array . slice ( action . payload . ordered . newIndex ) ,
31
- ] ;
29
+ function modifyDoc ( collectionState , action ) {
30
+ if ( ! action . meta . subcollections || action . meta . storeAs ) {
31
+ return updateItemInArray ( collectionState , action . meta . doc , item =>
32
+ // Merge is used to prevent the removal of existing subcollections
33
+ mergeObjects ( item , action . payload . data ) ,
34
+ ) ;
35
+ }
36
+
37
+ // TODO: make this recurisve so it will work multiple subcollections deep
38
+ const [ , docId , subcollectionName , subDocId ] = pathToArr ( action . meta . path ) ;
39
+
40
+ // Update document item within top arra
41
+ return updateItemInArray ( collectionState , docId , item => ( {
42
+ ...item , // preserve document (only updating subcollection)
43
+ [ subcollectionName ] : updateItemInArray (
44
+ get ( item , subcollectionName , [ ] ) ,
45
+ subDocId ,
46
+ // Merge with existing subcollection doc (only updates changed keys)
47
+ subitem => mergeObjects ( subitem , action . payload . data ) ,
48
+ ) ,
49
+ } ) ) ;
32
50
}
33
51
34
52
/**
35
- * Case reducer for modifying a document within a collection.
36
- * @param {Array } collectionState - Redux state of current collection
53
+ * Case reducer for adding a document to a collection or subcollection .
54
+ * @param {Array } [ collectionState=[]] - Redux state of current collection
37
55
* @param {Object } action - The action that was dispatched
38
56
* @return {Array } State with document modified
39
57
*/
40
- function modifyDoc ( collectionState , action ) {
41
- return updateItemInArray ( collectionState , action . meta . doc , item =>
42
- // Merge is used to prevent the removal of existing subcollections
43
- mergeObjects ( item , action . payload . data ) ,
44
- ) ;
58
+ function addDoc ( array = [ ] , action ) {
59
+ const { meta, payload } = action ;
60
+ if ( ! meta . subcollections || meta . storeAs ) {
61
+ return [
62
+ ...array . slice ( 0 , payload . ordered . newIndex ) ,
63
+ { id : meta . doc , ...payload . data } ,
64
+ ...array . slice ( payload . ordered . newIndex ) ,
65
+ ] ;
66
+ }
67
+
68
+ // Add doc to subcollection by modifying the existing doc at this level
69
+ return modifyDoc ( array , action ) ;
45
70
}
46
71
47
72
/**
0 commit comments