static-cms/src/actions/editorialWorkflow.js

235 lines
7.5 KiB
JavaScript
Raw Normal View History

import uuid from 'uuid';
import { actions as notifActions } from 'redux-notifications';
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
2016-09-06 13:04:17 -03:00
import { currentBackend } from '../backends/backend';
2017-01-10 22:23:22 -02:00
import { getAsset } from '../reducers';
import { status, EDITORIAL_WORKFLOW } from '../constants/publishModes';
const { notifSend } = notifActions;
2016-09-06 13:04:17 -03:00
/*
* Contant Declarations
*/
export const UNPUBLISHED_ENTRY_REQUEST = 'UNPUBLISHED_ENTRY_REQUEST';
export const UNPUBLISHED_ENTRY_SUCCESS = 'UNPUBLISHED_ENTRY_SUCCESS';
2016-09-06 13:04:17 -03:00
export const UNPUBLISHED_ENTRIES_REQUEST = 'UNPUBLISHED_ENTRIES_REQUEST';
export const UNPUBLISHED_ENTRIES_SUCCESS = 'UNPUBLISHED_ENTRIES_SUCCESS';
export const UNPUBLISHED_ENTRIES_FAILURE = 'UNPUBLISHED_ENTRIES_FAILURE';
export const UNPUBLISHED_ENTRY_PERSIST_REQUEST = 'UNPUBLISHED_ENTRY_PERSIST_REQUEST';
export const UNPUBLISHED_ENTRY_PERSIST_SUCCESS = 'UNPUBLISHED_ENTRY_PERSIST_SUCCESS';
export const UNPUBLISHED_ENTRY_PERSIST_FAILURE = 'UNPUBLISHED_ENTRY_PERSIST_FAILURE';
2016-09-06 13:04:17 -03:00
2016-09-13 16:00:24 -03:00
export const UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST = 'UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST';
export const UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS = 'UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS';
export const UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE = 'UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE';
2016-09-14 18:25:45 -03:00
export const UNPUBLISHED_ENTRY_PUBLISH_REQUEST = 'UNPUBLISHED_ENTRY_PUBLISH_REQUEST';
export const UNPUBLISHED_ENTRY_PUBLISH_SUCCESS = 'UNPUBLISHED_ENTRY_PUBLISH_SUCCESS';
export const UNPUBLISHED_ENTRY_PUBLISH_FAILURE = 'UNPUBLISHED_ENTRY_PUBLISH_FAILURE';
2016-09-14 18:25:45 -03:00
2016-09-06 13:04:17 -03:00
/*
* Simple Action Creators (Internal)
*/
2016-09-13 04:09:52 -03:00
function unpublishedEntryLoading(status, slug) {
return {
type: UNPUBLISHED_ENTRY_REQUEST,
payload: { status, slug },
};
}
2016-09-13 04:09:52 -03:00
function unpublishedEntryLoaded(status, entry) {
return {
type: UNPUBLISHED_ENTRY_SUCCESS,
payload: { status, entry },
};
}
2016-09-06 13:04:17 -03:00
function unpublishedEntriesLoading() {
return {
type: UNPUBLISHED_ENTRIES_REQUEST,
2016-09-06 13:04:17 -03:00
};
}
function unpublishedEntriesLoaded(entries, pagination) {
return {
type: UNPUBLISHED_ENTRIES_SUCCESS,
payload: {
entries,
pages: pagination,
},
2016-09-06 13:04:17 -03:00
};
}
function unpublishedEntriesFailed(error) {
return {
type: UNPUBLISHED_ENTRIES_FAILURE,
error: 'Failed to load entries',
payload: error,
2016-09-06 13:04:17 -03:00
};
}
function unpublishedEntryPersisting(collection, entry, transactionID) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_REQUEST,
payload: { collection, entry },
optimist: { type: BEGIN, id: transactionID },
};
}
function unpublishedEntryPersisted(collection, entry, transactionID) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_SUCCESS,
payload: { collection, entry },
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryPersistedFail(error, transactionID) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_FAILURE,
payload: { error },
optimist: { type: REVERT, id: transactionID },
2016-09-13 16:00:24 -03:00
};
}
function unpublishedEntryStatusChangeRequest(collection, slug, oldStatus, newStatus, transactionID) {
2016-09-13 16:00:24 -03:00
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST,
payload: { collection, slug, oldStatus, newStatus },
optimist: { type: BEGIN, id: transactionID },
2016-09-13 16:00:24 -03:00
};
}
function unpublishedEntryStatusChangePersisted(collection, slug, oldStatus, newStatus, transactionID) {
2016-09-13 16:00:24 -03:00
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS,
payload: { collection, slug, oldStatus, newStatus },
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryStatusChangeError(collection, slug, transactionID) {
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE,
payload: { collection, slug },
optimist: { type: REVERT, id: transactionID },
};
}
function unpublishedEntryPublishRequest(collection, slug, status, transactionID) {
2016-09-14 18:25:45 -03:00
return {
type: UNPUBLISHED_ENTRY_PUBLISH_REQUEST,
payload: { collection, slug, status },
optimist: { type: BEGIN, id: transactionID },
2016-09-14 18:25:45 -03:00
};
}
function unpublishedEntryPublished(collection, slug, status, transactionID) {
2016-09-14 18:25:45 -03:00
return {
type: UNPUBLISHED_ENTRY_PUBLISH_SUCCESS,
payload: { collection, slug, status },
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryPublishError(collection, slug, transactionID) {
return {
type: UNPUBLISHED_ENTRY_PUBLISH_FAILURE,
payload: { collection, slug },
optimist: { type: REVERT, id: transactionID },
2016-09-14 18:25:45 -03:00
};
}
2016-09-06 13:04:17 -03:00
/*
* Exported Thunk Action Creators
*/
2016-09-13 04:09:52 -03:00
export function loadUnpublishedEntry(collection, status, slug) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
2016-09-13 04:09:52 -03:00
dispatch(unpublishedEntryLoading(status, slug));
backend.unpublishedEntry(collection, slug)
.then(entry => dispatch(unpublishedEntryLoaded(status, entry)));
};
}
2016-09-06 13:04:17 -03:00
export function loadUnpublishedEntries() {
return (dispatch, getState) => {
const state = getState();
2016-09-06 17:18:27 -03:00
if (state.config.get('publish_mode') !== EDITORIAL_WORKFLOW) return;
2016-09-06 13:04:17 -03:00
const backend = currentBackend(state.config);
dispatch(unpublishedEntriesLoading());
backend.unpublishedEntries().then(
response => dispatch(unpublishedEntriesLoaded(response.entries, response.pagination)),
error => dispatch(unpublishedEntriesFailed(error))
2016-09-06 13:04:17 -03:00
);
};
}
export function persistUnpublishedEntry(collection, entryDraft, existingUnpublishedEntry) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
2017-01-10 22:23:22 -02:00
const assetProxies = entryDraft.get('mediaFiles').map(path => getAsset(state, path));
const entry = entryDraft.get('entry');
const transactionID = uuid.v4();
dispatch(unpublishedEntryPersisting(collection, entry, transactionID));
const persistAction = existingUnpublishedEntry ? backend.persistUnpublishedEntry : backend.persistEntry;
2017-01-10 22:23:22 -02:00
persistAction.call(backend, state.config, collection, entryDraft, assetProxies.toJS())
.then(() => {
dispatch(notifSend({
message: 'Entry saved',
kind: 'success',
dismissAfter: 4000,
}));
dispatch(unpublishedEntryPersisted(collection, entry, transactionID));
})
.catch((error) => {
dispatch(notifSend({
message: 'Failed to persist entry',
kind: 'danger',
dismissAfter: 4000,
}));
dispatch(unpublishedEntryPersistedFail(error, transactionID));
});
};
}
2016-09-13 16:00:24 -03:00
export function updateUnpublishedEntryStatus(collection, slug, oldStatus, newStatus) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid.v4();
dispatch(unpublishedEntryStatusChangeRequest(collection, slug, oldStatus, newStatus, transactionID));
2016-09-13 16:00:24 -03:00
backend.updateUnpublishedEntryStatus(collection, slug, newStatus)
.then(() => {
dispatch(unpublishedEntryStatusChangePersisted(collection, slug, oldStatus, newStatus, transactionID));
})
.catch(() => {
dispatch(unpublishedEntryStatusChangeError(collection, slug, transactionID));
2016-09-13 16:00:24 -03:00
});
};
}
2016-09-14 18:25:45 -03:00
export function publishUnpublishedEntry(collection, slug, status) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid.v4();
2016-10-18 15:18:29 -02:00
dispatch(unpublishedEntryPublishRequest(collection, slug, status, transactionID));
2016-09-14 18:25:45 -03:00
backend.publishUnpublishedEntry(collection, slug, status)
.then(() => {
dispatch(unpublishedEntryPublished(collection, slug, status, transactionID));
})
.catch(() => {
dispatch(unpublishedEntryPublishError(collection, slug, transactionID));
2016-09-14 18:25:45 -03:00
});
};
}