content creation internal + test_repo implementations

This commit is contained in:
Cássio Zen 2016-08-25 16:11:00 -03:00
parent b717874e7b
commit 2b70893e89
3 changed files with 47 additions and 11 deletions

View File

@ -73,19 +73,49 @@ class Backend {
}; };
} }
slugFormatter(template, entry) {
var date = new Date();
return template.replace(/\{\{([^\}]+)\}\}/g, function(_, name) {
switch (name) {
case 'year':
return date.getFullYear();
case 'month':
return ('0' + (date.getMonth() + 1)).slice(-2);
case 'day':
return ('0' + date.getDate()).slice(-2);
case 'slug':
return entry.getIn(['data', 'title']).trim().toLowerCase().replace(/[^a-z0-9\.\-\_]+/gi, '-');
default:
return entry.getIn(['data', name]);
}
});
}
persistEntry(collection, entryDraft, MediaFiles) { persistEntry(collection, entryDraft, MediaFiles) {
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
const entryData = entryDraft.getIn(['entry', 'data']).toObject(); const entryData = entryDraft.getIn(['entry', 'data']).toObject();
const entryObj = { let entryObj;
if (newEntry) {
const slug = this.slugFormatter(collection.get('slug'), entryDraft.get('entry'));
entryObj = {
path: `${collection.get('folder')}/${slug}.md`,
slug: slug,
raw: this.entryToRaw(collection, entryData)
};
} else {
entryObj = {
path: entryDraft.getIn(['entry', 'path']), path: entryDraft.getIn(['entry', 'path']),
slug: entryDraft.getIn(['entry', 'slug']), slug: entryDraft.getIn(['entry', 'slug']),
raw: this.entryToRaw(collection, entryData) raw: this.entryToRaw(collection, entryData)
}; };
}
const commitMessage = (entryDraft.getIn(['entry', 'newRecord']) ? 'Created ' : 'Updated ') + const commitMessage = (newEntry ? 'Created ' : 'Updated ') +
collection.get('label') + ' “' + collection.get('label') + ' “' +
entryDraft.getIn(['entry', 'data', 'title']) + '”'; entryDraft.getIn(['entry', 'data', 'title']) + '”';
return this.implementation.persistEntry(collection, entryObj, MediaFiles, { commitMessage }); return this.implementation.persistEntry(collection, entryObj, MediaFiles, { commitMessage }, newEntry);
} }
entryToRaw(collection, entry) { entryToRaw(collection, entry) {

View File

@ -45,10 +45,14 @@ export default class TestRepo {
)); ));
} }
persistEntry(collection, entry, mediaFiles = []) { persistEntry(collection, entry, mediaFiles = [], newEntry = false) {
const folder = entry.path.substring(0, entry.path.lastIndexOf('/')); const folder = entry.path.substring(0, entry.path.lastIndexOf('/'));
const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1); const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1);
if (newEntry) {
window.repoFiles[folder][fileName] = { content: entry.raw };
} else {
window.repoFiles[folder][fileName]['content'] = entry.raw; window.repoFiles[folder][fileName]['content'] = entry.raw;
}
mediaFiles.forEach(media => media.uploaded = true); mediaFiles.forEach(media => media.uploaded = true);
return Promise.resolve(); return Promise.resolve();
} }

View File

@ -10,12 +10,14 @@ const entryDraft = (state = Map(), action) => {
// Existing Entry // Existing Entry
return state.withMutations((state) => { return state.withMutations((state) => {
state.set('entry', action.payload); state.set('entry', action.payload);
state.setIn(['entry', 'newRecord'], false);
state.set('mediaFiles', List()); state.set('mediaFiles', List());
}); });
case DRAFT_CREATE_EMPTY: case DRAFT_CREATE_EMPTY:
// New Entry // New Entry
return state.withMutations((state) => { return state.withMutations((state) => {
state.set('entry', fromJS(action.payload)); state.set('entry', fromJS(action.payload));
state.setIn(['entry', 'newRecord'], true);
state.set('mediaFiles', List()); state.set('mediaFiles', List());
}); });
case DRAFT_DISCARD: case DRAFT_DISCARD: