1- import _ from 'underscore' ;
21import moment from 'moment' ;
3- import { CLEAR_ERRORS , validationError , filterDeleted } from './utils' ;
2+ import { clearErrors , validationError , filterDeleted } from './utils' ;
43import { get , put , del } from '../utils/fetch' ;
54import { 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' ;
711import {
812 collectionsAPIUrl ,
913 collectionAPIUrl ,
@@ -45,27 +49,20 @@ export const fetchCollections = () => dispatch => {
4549 ) ;
4650} ;
4751
48- export const fetchCollection = (
49- collection_name ,
50- directory = ''
51- ) => dispatch => {
52+ export const fetchCollection = ( collection , directory = '' ) => dispatch => {
5253 dispatch ( { type : FETCH_COLLECTION_REQUEST } ) ;
5354 return get (
54- collectionAPIUrl ( collection_name , directory ) ,
55+ collectionAPIUrl ( collection , directory ) ,
5556 { type : FETCH_COLLECTION_SUCCESS , name : 'entries' } ,
5657 { type : FETCH_COLLECTION_FAILURE , name : 'error' } ,
5758 dispatch
5859 ) ;
5960} ;
6061
61- export const fetchDocument = (
62- collection_name ,
63- directory ,
64- filename
65- ) => dispatch => {
62+ export const fetchDocument = ( collection , directory , filename ) => dispatch => {
6663 dispatch ( { type : FETCH_DOCUMENT_REQUEST } ) ;
6764 return get (
68- documentAPIUrl ( collection_name , directory , filename ) ,
65+ documentAPIUrl ( collection , directory , filename ) ,
6966 { type : FETCH_DOCUMENT_SUCCESS , name : 'doc' } ,
7067 { type : FETCH_DOCUMENT_FAILURE , name : 'error' } ,
7168 dispatch
@@ -78,10 +75,11 @@ export const createDocument = (collection, directory) => (
7875) => {
7976 // get edited fields from metadata state
8077 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`.
8381 if ( ( ! path || `${ path } /` === directory ) && title ) {
84- path = generateFilenameFromTitle ( metadata , collection ) ; // override empty path
82+ path = generateFilenameFromTitle ( title , collection , date ) ;
8583 } else {
8684 // validate otherwise
8785 const errors = validateDocument ( metadata , collection ) ;
@@ -90,14 +88,12 @@ export const createDocument = (collection, directory) => (
9088 }
9189 }
9290 // 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+
9895 // send the put request
9996 return put (
100- // create or update document according to filename existence
10197 documentAPIUrl ( collection , directory , path ) ,
10298 preparePayload ( { raw_content, front_matter } ) ,
10399 { type : PUT_DOCUMENT_SUCCESS , name : 'doc' } ,
@@ -112,10 +108,11 @@ export const putDocument = (collection, directory, filename) => (
112108) => {
113109 // get edited fields from metadata state
114110 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`.
117114 if ( ( ! path || `${ path } /` === directory ) && title ) {
118- path = generateFilenameFromTitle ( metadata , collection ) ; // override empty path
115+ path = generateFilenameFromTitle ( title , collection , date ) ;
119116 } else {
120117 // validate otherwise
121118 const errors = validateDocument ( metadata , collection ) ;
@@ -124,18 +121,17 @@ export const putDocument = (collection, directory, filename) => (
124121 }
125122 }
126123 // 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+
132128 // add collection type prefix to relative path
133129 const relative_path = directory
134130 ? `_${ collection } /${ directory } /${ path } `
135131 : `_${ collection } /${ path } ` ;
132+
136133 // send the put request
137134 return put (
138- // create or update document according to filename existence
139135 documentAPIUrl ( collection , directory , filename ) ,
140136 preparePayload ( { path : relative_path , raw_content, front_matter } ) ,
141137 { type : PUT_DOCUMENT_SUCCESS , name : 'doc' } ,
@@ -154,18 +150,14 @@ export const deleteDocument = (collection, directory, filename) => dispatch => {
154150 ) ;
155151} ;
156152
157- const generateFilenameFromTitle = ( metadata , collection ) => {
153+ const generateFilenameFromTitle = ( title , collection , date ) => {
154+ const slugifiedTitle = slugify ( title ) ;
158155 if ( collection === 'posts' ) {
159156 // 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` ;
167159 }
168- return `${ slugify ( metadata . title ) } .md` ;
160+ return `${ slugifiedTitle } .md` ;
169161} ;
170162
171163const validateDocument = ( metadata , collection ) => {
@@ -186,8 +178,6 @@ const validateDocument = (metadata, collection) => {
186178 return validator ( metadata , validations , messages ) ;
187179} ;
188180
189- const preparePayload = obj => JSON . stringify ( trimObject ( obj ) ) ;
190-
191181// Reducer
192182export default function collections (
193183 state = {
@@ -264,17 +254,10 @@ export default function collections(
264254
265255// Selectors
266256export const filterBySearchInput = ( list , input ) => {
267- if ( ! list ) {
268- return [ ] ;
257+ if ( ! input ) {
258+ return list ;
269259 }
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+ ) ;
280263} ;
0 commit comments