Load unpublished entries

This commit is contained in:
Cássio Zen 2016-09-06 17:18:27 -03:00
parent 76693c71bd
commit 90d4b39fc1
6 changed files with 54 additions and 13 deletions

View File

@ -41,8 +41,7 @@ function unpublishedEntriesFailed(error) {
export function loadUnpublishedEntries() { export function loadUnpublishedEntries() {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
if (state.publish_mode !== EDITORIAL_WORKFLOW) return; if (state.config.get('publish_mode') !== EDITORIAL_WORKFLOW) return;
const backend = currentBackend(state.config); const backend = currentBackend(state.config);
dispatch(unpublishedEntriesLoading()); dispatch(unpublishedEntriesLoading());
backend.unpublishedEntries().then( backend.unpublishedEntries().then(

View File

@ -100,7 +100,7 @@ export default class API {
}); });
} }
retrieveMetadata(key, data) { retrieveMetadata(key) {
const cache = LocalForage.getItem(`gh.meta.${key}`); const cache = LocalForage.getItem(`gh.meta.${key}`);
return cache.then((cached) => { return cache.then((cached) => {
if (cached && cached.expires > Date.now()) { return cached.data; } if (cached && cached.expires > Date.now()) { return cached.data; }
@ -109,7 +109,9 @@ export default class API {
params: { ref: 'refs/meta/_netlify_cms' }, params: { ref: 'refs/meta/_netlify_cms' },
headers: { Accept: 'application/vnd.github.VERSION.raw' }, headers: { Accept: 'application/vnd.github.VERSION.raw' },
cache: 'no-store', cache: 'no-store',
}).then((result) => { })
.then(response => JSON.parse(response))
.then((result) => {
LocalForage.setItem(`gh.meta.${key}`, { LocalForage.setItem(`gh.meta.${key}`, {
expires: Date.now() + 300000, // In 5 minutes expires: Date.now() + 300000, // In 5 minutes
data: result, data: result,
@ -119,20 +121,19 @@ export default class API {
}).catch(error => null); }).catch(error => null);
} }
readFile(path, sha) { readFile(path, sha, branch = this.branch) {
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null); const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
return cache.then((cached) => { return cache.then((cached) => {
if (cached) { return cached; } if (cached) { return cached; }
return this.request(`${this.repoURL}/contents/${path}`, { return this.request(`${this.repoURL}/contents/${path}`, {
headers: { Accept: 'application/vnd.github.VERSION.raw' }, headers: { Accept: 'application/vnd.github.VERSION.raw' },
params: { ref: this.branch }, params: { ref: branch },
cache: false cache: false
}).then((result) => { }).then((result) => {
if (sha) { if (sha) {
LocalForage.setItem(`gh.${sha}`, result); LocalForage.setItem(`gh.${sha}`, result);
} }
return result; return result;
}); });
}); });
@ -144,6 +145,22 @@ export default class API {
}); });
} }
readUnpublishedBranchFile(contentKey) {
let metaData;
return this.retrieveMetadata(contentKey)
.then(data => {
metaData = data;
return this.readFile(data.objects.entry, null, data.branch);
})
.then(file => {
return { metaData, file };
});
}
listUnpublishedBranches() {
return this.request(`${this.repoURL}/git/refs/heads/cms`);
}
persistFiles(entry, mediaFiles, options) { persistFiles(entry, mediaFiles, options) {
let filename, part, parts, subtree; let filename, part, parts, subtree;
const fileTree = {}; const fileTree = {};
@ -180,7 +197,10 @@ export default class API {
collection: options.collectionName, collection: options.collectionName,
title: options.parsedData.title, title: options.parsedData.title,
description: options.parsedData.description, description: options.parsedData.description,
objects: files.map(file => file.path) objects: {
entry: entry.path,
files: mediaFiles.map(file => file.path)
}
})) }))
.then(this.createPR(options.commitMessage, `cms/${contentKey}`)); .then(this.createPR(options.commitMessage, `cms/${contentKey}`));
} else { } else {

View File

@ -64,9 +64,30 @@ export default class GitHub {
} }
unpublishedEntries() { unpublishedEntries() {
return Promise.resolve({ return this.api.listUnpublishedBranches().then((branches) => {
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
const promises = [];
branches.map((branch) => {
promises.push(new Promise((resolve, reject) => {
const contentKey = branch.ref.split('refs/heads/cms/').pop();
return sem.take(() => this.api.readUnpublishedBranchFile(contentKey).then((data) => {
const entryPath = data.metaData.objects.entry;
const entry = createEntry(entryPath, entryPath.split('/').pop().replace(/\.[^\.]+$/, ''), data.file);
entry.metaData = data.metaData;
resolve(entry);
sem.leave();
}).catch((err) => {
sem.leave();
reject(err);
}));
}));
});
return Promise.all(promises);
}).then((entries) => {
return {
pagination: {}, pagination: {},
entries: [] entries
};
}); });
} }
} }

View File

@ -10,7 +10,7 @@ import EntryListing from '../components/EntryListing';
class DashboardPage extends React.Component { class DashboardPage extends React.Component {
componentDidMount() { componentDidMount() {
const { collection, dispatch } = this.props; const { collection, dispatch } = this.props;
dispatch(loadUnpublishedEntries); dispatch(loadUnpublishedEntries());
if (collection) { if (collection) {
dispatch(loadEntries(collection)); dispatch(loadEntries(collection));
} }

View File

@ -12,7 +12,7 @@ const unpublishedEntries = (state = Map({ entities: Map(), pages: Map() }), acti
const { entries, pages } = action.payload; const { entries, pages } = action.payload;
return state.withMutations((map) => { return state.withMutations((map) => {
entries.forEach((entry) => ( entries.forEach((entry) => (
map.setIn(['entities', `${entry.metadata.status}.${entry.slug}`], fromJS(entry).set('isFetching', false)) map.setIn(['entities', `${entry.metaData.status}.${entry.slug}`], fromJS(entry).set('isFetching', false))
)); ));
map.set('pages', Map({ map.set('pages', Map({
...pages, ...pages,

View File

@ -4,5 +4,6 @@ export function createEntry(path = '', slug = '', raw = '') {
returnObj.slug = slug; returnObj.slug = slug;
returnObj.raw = raw; returnObj.raw = raw;
returnObj.data = {}; returnObj.data = {};
returnObj.metaData = {};
return returnObj; return returnObj;
} }