@@ -3,15 +3,9 @@ import _ from 'underscore';
33import { validationError } from '../actions/utils' ;
44import { get , put } from '../utils/fetch' ;
55import { validator } from '../utils/validation' ;
6- import { slugify } from '../utils/helpers' ;
7- import {
8- getTitleRequiredMessage ,
9- getFilenameNotValidMessage
10- } from '../constants/lang' ;
11- import {
12- pagesAPIUrl ,
13- pageAPIUrl
14- } from '../constants/api' ;
6+ import { slugify , trimObject } from '../utils/helpers' ;
7+ import { getTitleRequiredMessage , getFilenameNotValidMessage } from '../constants/lang' ;
8+ import { pagesAPIUrl , pageAPIUrl } from '../constants/api' ;
159
1610export function fetchPages ( directory = '' ) {
1711 return ( dispatch ) => {
@@ -55,12 +49,12 @@ export function createPage(directory) {
5549 dispatch ( { type : ActionTypes . CLEAR_ERRORS } ) ;
5650 // omit raw_content, path and empty-value keys in metadata state from front_matter
5751 const front_matter = _ . omit ( metadata , ( value , key , object ) => {
58- return key == 'raw_content' || key == 'path' || value == '' ;
52+ return key == 'raw_content' || key == 'path' || value === '' ;
5953 } ) ;
6054 //send the put request
6155 return put (
6256 pageAPIUrl ( directory , path ) ,
63- JSON . stringify ( { front_matter, raw_content } ) ,
57+ preparePayload ( { front_matter, raw_content } ) ,
6458 { type : ActionTypes . PUT_PAGE_SUCCESS , name : "page" } ,
6559 { type : ActionTypes . PUT_PAGE_FAILURE , name : "error" } ,
6660 dispatch
@@ -86,33 +80,21 @@ export function putPage(directory, filename) {
8680 dispatch ( { type : ActionTypes . CLEAR_ERRORS } ) ;
8781 // omit raw_content, path and empty-value keys in metadata state from front_matter
8882 const front_matter = _ . omit ( metadata , ( value , key , object ) => {
89- return key == 'raw_content' || key == 'path' || value == '' ;
83+ return key == 'raw_content' || key == 'path' || value === '' ;
9084 } ) ;
91- const relative_path = directory ?
92- `${ directory } /${ path } ` : `${ path } ` ;
85+ const relative_path = directory ? `${ directory } /${ path } ` : `${ path } ` ;
9386 //send the put request
9487 return put (
9588 // create or update page according to filename existence
9689 pageAPIUrl ( directory , filename ) ,
97- JSON . stringify ( { path : relative_path , front_matter, raw_content } ) ,
90+ preparePayload ( { path : relative_path , front_matter, raw_content } ) ,
9891 { type : ActionTypes . PUT_PAGE_SUCCESS , name : "page" } ,
9992 { type : ActionTypes . PUT_PAGE_FAILURE , name : "error" } ,
10093 dispatch
10194 ) ;
10295 } ;
10396}
10497
105- function validatePage ( metadata ) {
106- return validator (
107- metadata ,
108- { 'path' : 'required|filename' } ,
109- {
110- 'path.required' : getTitleRequiredMessage ( ) ,
111- 'path.filename' : getFilenameNotValidMessage ( )
112- }
113- ) ;
114- }
115-
11698export function deletePage ( directory , filename ) {
11799 return ( dispatch ) => {
118100 return fetch ( pageAPIUrl ( directory , filename ) , {
@@ -128,3 +110,16 @@ export function deletePage(directory, filename) {
128110 } ) ) ;
129111 } ;
130112}
113+
114+ const validatePage = ( metadata ) => {
115+ return validator (
116+ metadata ,
117+ { 'path' : 'required|filename' } ,
118+ {
119+ 'path.required' : getTitleRequiredMessage ( ) ,
120+ 'path.filename' : getFilenameNotValidMessage ( )
121+ }
122+ ) ;
123+ } ;
124+
125+ const preparePayload = ( obj ) => JSON . stringify ( trimObject ( obj ) ) ;
0 commit comments