static-cms/src/actions/entries.js

343 lines
9.1 KiB
JavaScript
Raw Normal View History

import { List } from 'immutable';
import { actions as notifActions } from 'redux-notifications';
import { serializeValues } from '../lib/serializeEntryValues';
import { closeEntry } from './editor';
import { currentBackend } from '../backends/backend';
import { getIntegrationProvider } from '../integrations';
2017-01-10 22:23:22 -02:00
import { getAsset, selectIntegration } from '../reducers';
2017-09-26 16:20:03 -04:00
import { selectFields } from '../reducers/collections';
2016-11-30 16:52:17 -02:00
import { createEntry } from '../valueObjects/Entry';
import ValidationErrorTypes from '../constants/validationErrorTypes';
const { notifSend } = notifActions;
/*
* Contant Declarations
*/
export const ENTRY_REQUEST = 'ENTRY_REQUEST';
export const ENTRY_SUCCESS = 'ENTRY_SUCCESS';
export const ENTRY_FAILURE = 'ENTRY_FAILURE';
export const ENTRIES_REQUEST = 'ENTRIES_REQUEST';
export const ENTRIES_SUCCESS = 'ENTRIES_SUCCESS';
export const ENTRIES_FAILURE = 'ENTRIES_FAILURE';
2016-07-19 17:11:22 -03:00
export const DRAFT_CREATE_FROM_ENTRY = 'DRAFT_CREATE_FROM_ENTRY';
2016-08-24 21:36:44 -03:00
export const DRAFT_CREATE_EMPTY = 'DRAFT_CREATE_EMPTY';
export const DRAFT_DISCARD = 'DRAFT_DISCARD';
export const DRAFT_CHANGE = 'DRAFT_CHANGE';
export const DRAFT_CHANGE_FIELD = 'DRAFT_CHANGE_FIELD';
export const DRAFT_VALIDATION_ERRORS = 'DRAFT_VALIDATION_ERRORS';
2016-06-10 00:16:01 -03:00
export const ENTRY_PERSIST_REQUEST = 'ENTRY_PERSIST_REQUEST';
export const ENTRY_PERSIST_SUCCESS = 'ENTRY_PERSIST_SUCCESS';
export const ENTRY_PERSIST_FAILURE = 'ENTRY_PERSIST_FAILURE';
export const ENTRY_DELETE_REQUEST = 'ENTRY_DELETE_REQUEST';
export const ENTRY_DELETE_SUCCESS = 'ENTRY_DELETE_SUCCESS';
export const ENTRY_DELETE_FAILURE = 'ENTRY_DELETE_FAILURE';
/*
* Simple Action Creators (Internal)
* We still need to export them for tests
*/
export function entryLoading(collection, slug) {
return {
type: ENTRY_REQUEST,
payload: {
collection: collection.get('name'),
2016-10-12 16:01:27 +02:00
slug,
},
};
}
export function entryLoaded(collection, entry) {
return {
type: ENTRY_SUCCESS,
payload: {
collection: collection.get('name'),
2016-10-12 16:01:27 +02:00
entry,
},
};
}
2017-01-10 22:23:22 -02:00
export function entryLoadError(error, collection, slug) {
return {
type: ENTRY_FAILURE,
payload: {
error,
collection: collection.get('name'),
slug,
},
};
}
export function entriesLoading(collection) {
return {
type: ENTRIES_REQUEST,
payload: {
2016-10-12 16:01:27 +02:00
collection: collection.get('name'),
},
};
}
export function entriesLoaded(collection, entries, pagination) {
return {
type: ENTRIES_SUCCESS,
payload: {
collection: collection.get('name'),
2016-10-12 16:01:27 +02:00
entries,
page: pagination,
},
};
}
export function entriesFailed(collection, error) {
return {
type: ENTRIES_FAILURE,
error: 'Failed to load entries',
payload: error.toString(),
2016-10-12 16:01:27 +02:00
meta: { collection: collection.get('name') },
};
}
export function entryPersisting(collection, entry) {
return {
type: ENTRY_PERSIST_REQUEST,
payload: {
collectionName: collection.get('name'),
entrySlug: entry.get('slug'),
2016-10-12 16:01:27 +02:00
},
};
}
export function entryPersisted(collection, entry) {
return {
type: ENTRY_PERSIST_SUCCESS,
payload: {
collectionName: collection.get('name'),
entrySlug: entry.get('slug'),
2016-10-12 16:01:27 +02:00
},
};
}
export function entryPersistFail(collection, entry, error) {
return {
type: ENTRY_PERSIST_FAILURE,
error: 'Failed to persist entry',
payload: {
collectionName: collection.get('name'),
entrySlug: entry.get('slug'),
error: error.toString(),
},
};
}
export function entryDeleting(collection, slug) {
return {
type: ENTRY_DELETE_REQUEST,
payload: {
collectionName: collection.get('name'),
entrySlug: slug,
},
};
}
export function entryDeleted(collection, slug) {
return {
type: ENTRY_DELETE_SUCCESS,
payload: {
collectionName: collection.get('name'),
entrySlug: slug,
},
};
}
export function entryDeleteFail(collection, slug, error) {
return {
type: ENTRY_DELETE_FAILURE,
payload: {
collectionName: collection.get('name'),
entrySlug: slug,
error: error.toString(),
},
};
}
export function emptyDraftCreated(entry) {
2016-08-24 21:36:44 -03:00
return {
type: DRAFT_CREATE_EMPTY,
2016-10-12 16:01:27 +02:00
payload: entry,
2016-08-24 21:36:44 -03:00
};
}
/*
* Exported simple Action Creators
*/
2016-07-19 17:11:22 -03:00
export function createDraftFromEntry(entry) {
return {
2016-07-19 17:11:22 -03:00
type: DRAFT_CREATE_FROM_ENTRY,
2016-10-12 16:01:27 +02:00
payload: entry,
};
}
export function discardDraft() {
return {
2016-10-12 16:01:27 +02:00
type: DRAFT_DISCARD,
};
}
export function changeDraft(entry) {
return {
type: DRAFT_CHANGE,
2016-10-12 16:01:27 +02:00
payload: entry,
};
}
export function changeDraftField(field, value, metadata) {
return {
type: DRAFT_CHANGE_FIELD,
payload: { field, value, metadata },
};
}
export function changeDraftFieldValidation(field, errors) {
return {
type: DRAFT_VALIDATION_ERRORS,
payload: { field, errors },
};
}
/*
* Exported Thunk Action Creators
*/
export function loadEntry(collection, slug) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(entryLoading(collection, slug));
2016-10-27 13:12:18 -02:00
return backend.getEntry(collection, slug)
.then(loadedEntry => {
return dispatch(entryLoaded(collection, loadedEntry))
})
2017-01-10 22:23:22 -02:00
.catch((error) => {
console.error(error);
2017-01-10 22:23:22 -02:00
dispatch(notifSend({
message: `Failed to load entry: ${ error.message }`,
kind: 'danger',
dismissAfter: 8000,
2017-01-10 22:23:22 -02:00
}));
dispatch(entryLoadError(error, collection, slug));
});
};
}
export function loadEntries(collection, page = 0) {
return (dispatch, getState) => {
if (collection.get('isFetching')) {
return;
}
const state = getState();
2017-01-10 22:23:22 -02:00
const backend = currentBackend(state.config);
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
2017-01-10 22:23:22 -02:00
const provider = integration ? getIntegrationProvider(state.integrations, backend.getToken, integration) : backend;
dispatch(entriesLoading(collection));
provider.listEntries(collection, page).then(
response => dispatch(entriesLoaded(collection, response.entries.reverse(), response.pagination)),
2016-10-12 16:01:27 +02:00
error => dispatch(entriesFailed(collection, error))
);
};
}
2016-08-24 21:36:44 -03:00
export function createEmptyDraft(collection) {
2016-11-30 16:52:17 -02:00
return (dispatch) => {
const dataFields = {};
collection.get('fields', List()).forEach((field) => {
dataFields[field.get('name')] = field.get('default', null);
});
const newEntry = createEntry(collection.get('name'), '', '', { data: dataFields });
dispatch(emptyDraftCreated(newEntry));
2016-08-24 21:36:44 -03:00
};
}
export function persistEntry(collection) {
return (dispatch, getState) => {
const state = getState();
const entryDraft = state.entryDraft;
const fieldsErrors = entryDraft.get('fieldsErrors');
// Early return if draft contains validation errors
if (!fieldsErrors.isEmpty()) {
const hasPresenceErrors = fieldsErrors
.some(errors => errors.some(error => error.type && error.type === ValidationErrorTypes.PRESENCE));
if (hasPresenceErrors) {
dispatch(notifSend({
message: 'Oops, you\'ve missed a required field. Please complete before saving.',
kind: 'danger',
dismissAfter: 8000,
}));
}
return Promise.reject();
}
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');
/**
* Serialize the values of any fields with registered serializers, and
* update the entry and entryDraft with the serialized values.
*/
2017-09-26 16:20:03 -04:00
const fields = selectFields(collection, entry.get('slug'));
const serializedData = serializeValues(entryDraft.getIn(['entry', 'data']), fields);
const serializedEntry = entry.set('data', serializedData);
const serializedEntryDraft = entryDraft.set('entry', serializedEntry);
dispatch(entryPersisting(collection, serializedEntry));
return backend
.persistEntry(state.config, collection, serializedEntryDraft, assetProxies.toJS())
.then(() => {
dispatch(notifSend({
message: 'Entry saved',
kind: 'success',
dismissAfter: 4000,
}));
return dispatch(entryPersisted(collection, serializedEntry));
})
.catch((error) => {
console.error(error);
dispatch(notifSend({
message: `Failed to persist entry: ${ error }`,
kind: 'danger',
dismissAfter: 8000,
}));
return Promise.reject(dispatch(entryPersistFail(collection, serializedEntry, error)));
});
};
}
export function deleteEntry(collection, slug) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(entryDeleting(collection, slug));
return backend.deleteEntry(state.config, collection, slug)
.then(() => {
return dispatch(entryDeleted(collection, slug));
})
.catch((error) => {
dispatch(notifSend({
message: `Failed to delete entry: ${ error }`,
kind: 'danger',
dismissAfter: 8000,
}));
console.error(error);
return Promise.reject(dispatch(entryDeleteFail(collection, slug, error)));
});
};
}