1
- import _ from 'underscore' ;
2
1
import moment from 'moment' ;
3
- import { CLEAR_ERRORS , validationError , filterDeleted } from './utils' ;
2
+ import { clearErrors , validationError , filterDeleted } from './utils' ;
4
3
import { get , put , del } from '../utils/fetch' ;
5
4
import { validator } from '../utils/validation' ;
6
- import { slugify , trimObject , computeRelativePath } from '../utils/helpers' ;
5
+ import {
6
+ slugify ,
7
+ preparePayload ,
8
+ sanitizeFrontMatter ,
9
+ computeRelativePath ,
10
+ } from '../utils/helpers' ;
7
11
import {
8
12
collectionsAPIUrl ,
9
13
collectionAPIUrl ,
@@ -45,27 +49,20 @@ export const fetchCollections = () => dispatch => {
45
49
) ;
46
50
} ;
47
51
48
- export const fetchCollection = (
49
- collection_name ,
50
- directory = ''
51
- ) => dispatch => {
52
+ export const fetchCollection = ( collection , directory = '' ) => dispatch => {
52
53
dispatch ( { type : FETCH_COLLECTION_REQUEST } ) ;
53
54
return get (
54
- collectionAPIUrl ( collection_name , directory ) ,
55
+ collectionAPIUrl ( collection , directory ) ,
55
56
{ type : FETCH_COLLECTION_SUCCESS , name : 'entries' } ,
56
57
{ type : FETCH_COLLECTION_FAILURE , name : 'error' } ,
57
58
dispatch
58
59
) ;
59
60
} ;
60
61
61
- export const fetchDocument = (
62
- collection_name ,
63
- directory ,
64
- filename
65
- ) => dispatch => {
62
+ export const fetchDocument = ( collection , directory , filename ) => dispatch => {
66
63
dispatch ( { type : FETCH_DOCUMENT_REQUEST } ) ;
67
64
return get (
68
- documentAPIUrl ( collection_name , directory , filename ) ,
65
+ documentAPIUrl ( collection , directory , filename ) ,
69
66
{ type : FETCH_DOCUMENT_SUCCESS , name : 'doc' } ,
70
67
{ type : FETCH_DOCUMENT_FAILURE , name : 'error' } ,
71
68
dispatch
@@ -78,10 +75,11 @@ export const createDocument = (collection, directory) => (
78
75
) => {
79
76
// get edited fields from metadata state
80
77
const metadata = getState ( ) . metadata . metadata ;
81
- let { path, raw_content, title } = metadata ;
82
- // if path is not given or equals to directory, generate filename from the title
78
+ let { path, raw_content, title, date } = metadata ;
79
+ // if `path` is a falsy value or if appending a slash to it equals to
80
+ // `directory`, generate filename from `title`.
83
81
if ( ( ! path || `${ path } /` === directory ) && title ) {
84
- path = generateFilenameFromTitle ( metadata , collection ) ; // override empty path
82
+ path = generateFilenameFromTitle ( title , collection , date ) ;
85
83
} else {
86
84
// validate otherwise
87
85
const errors = validateDocument ( metadata , collection ) ;
@@ -90,14 +88,12 @@ export const createDocument = (collection, directory) => (
90
88
}
91
89
}
92
90
// clear errors
93
- dispatch ( { type : CLEAR_ERRORS } ) ;
94
- // omit raw_content, path and empty-value keys in metadata state from front_matter
95
- const front_matter = _ . omit ( metadata , ( value , key , object ) => {
96
- return key === 'raw_content' || key === 'path' || value === '' ;
97
- } ) ;
91
+ dispatch ( clearErrors ( ) ) ;
92
+
93
+ const front_matter = sanitizeFrontMatter ( metadata ) ;
94
+
98
95
// send the put request
99
96
return put (
100
- // create or update document according to filename existence
101
97
documentAPIUrl ( collection , directory , path ) ,
102
98
preparePayload ( { raw_content, front_matter } ) ,
103
99
{ type : PUT_DOCUMENT_SUCCESS , name : 'doc' } ,
@@ -112,10 +108,11 @@ export const putDocument = (collection, directory, filename) => (
112
108
) => {
113
109
// get edited fields from metadata state
114
110
const metadata = getState ( ) . metadata . metadata ;
115
- let { path, raw_content, title } = metadata ;
116
- // if path is not given or equals to directory, generate filename from the title
111
+ let { path, raw_content, title, date } = metadata ;
112
+ // if `path` is a falsy value or if appending a slash to it equals to
113
+ // `directory`, generate filename from `title`.
117
114
if ( ( ! path || `${ path } /` === directory ) && title ) {
118
- path = generateFilenameFromTitle ( metadata , collection ) ; // override empty path
115
+ path = generateFilenameFromTitle ( title , collection , date ) ;
119
116
} else {
120
117
// validate otherwise
121
118
const errors = validateDocument ( metadata , collection ) ;
@@ -124,18 +121,17 @@ export const putDocument = (collection, directory, filename) => (
124
121
}
125
122
}
126
123
// clear errors
127
- dispatch ( { type : CLEAR_ERRORS } ) ;
128
- // omit raw_content, path and empty-value keys in metadata state from front_matter
129
- const front_matter = _ . omit ( metadata , ( value , key , object ) => {
130
- return key === 'raw_content' || key === 'path' || value === '' ;
131
- } ) ;
124
+ dispatch ( clearErrors ( ) ) ;
125
+
126
+ const front_matter = sanitizeFrontMatter ( metadata ) ;
127
+
132
128
// add collection type prefix to relative path
133
129
const relative_path = directory
134
130
? `_${ collection } /${ directory } /${ path } `
135
131
: `_${ collection } /${ path } ` ;
132
+
136
133
// send the put request
137
134
return put (
138
- // create or update document according to filename existence
139
135
documentAPIUrl ( collection , directory , filename ) ,
140
136
preparePayload ( { path : relative_path , raw_content, front_matter } ) ,
141
137
{ type : PUT_DOCUMENT_SUCCESS , name : 'doc' } ,
@@ -154,18 +150,14 @@ export const deleteDocument = (collection, directory, filename) => dispatch => {
154
150
) ;
155
151
} ;
156
152
157
- const generateFilenameFromTitle = ( metadata , collection ) => {
153
+ const generateFilenameFromTitle = ( title , collection , date ) => {
154
+ const slugifiedTitle = slugify ( title ) ;
158
155
if ( collection === 'posts' ) {
159
156
// if date is provided, use it, otherwise generate it with today's date
160
- let date ;
161
- if ( metadata . date ) {
162
- date = metadata . date . split ( ' ' ) [ 0 ] ;
163
- } else {
164
- date = moment ( ) . format ( 'YYYY-MM-DD' ) ;
165
- }
166
- return `${ date } -${ slugify ( metadata . title ) } .md` ;
157
+ const docDate = date ? date . split ( ' ' ) [ 0 ] : moment ( ) . format ( 'YYYY-MM-DD' ) ;
158
+ return `${ docDate } -${ slugifiedTitle } .md` ;
167
159
}
168
- return `${ slugify ( metadata . title ) } .md` ;
160
+ return `${ slugifiedTitle } .md` ;
169
161
} ;
170
162
171
163
const validateDocument = ( metadata , collection ) => {
@@ -186,8 +178,6 @@ const validateDocument = (metadata, collection) => {
186
178
return validator ( metadata , validations , messages ) ;
187
179
} ;
188
180
189
- const preparePayload = obj => JSON . stringify ( trimObject ( obj ) ) ;
190
-
191
181
// Reducer
192
182
export default function collections (
193
183
state = {
@@ -264,17 +254,10 @@ export default function collections(
264
254
265
255
// Selectors
266
256
export const filterBySearchInput = ( list , input ) => {
267
- if ( ! list ) {
268
- return [ ] ;
257
+ if ( ! input ) {
258
+ return list ;
269
259
}
270
- if ( input ) {
271
- return _ . filter ( list , item => {
272
- if ( item . type ) {
273
- return item . name . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) ;
274
- } else {
275
- return item . title . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) ;
276
- }
277
- } ) ;
278
- }
279
- return list ;
260
+ return list . filter ( f =>
261
+ ( f . title || f . name ) . toLowerCase ( ) . includes ( input . toLowerCase ( ) )
262
+ ) ;
280
263
} ;
0 commit comments