2017-06-21 13:49:43 -04:00
|
|
|
import { List, Map } from 'immutable';
|
|
|
|
import { isArray, isObject, isEmpty, isNil } from 'lodash';
|
2016-10-17 12:35:31 +02:00
|
|
|
import { actions as notifActions } from 'redux-notifications';
|
2017-01-11 20:58:15 -02:00
|
|
|
import { closeEntry } from './editor';
|
2016-02-25 20:40:35 -08:00
|
|
|
import { currentBackend } from '../backends/backend';
|
2016-10-10 15:34:21 -03:00
|
|
|
import { getIntegrationProvider } from '../integrations';
|
2017-01-10 22:23:22 -02:00
|
|
|
import { getAsset, selectIntegration } from '../reducers';
|
2016-11-30 16:52:17 -02:00
|
|
|
import { createEntry } from '../valueObjects/Entry';
|
2017-06-22 17:35:47 -04:00
|
|
|
import registry from '../lib/registry';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-10-17 12:35:31 +02:00
|
|
|
const { notifSend } = notifActions;
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Contant Declarations
|
|
|
|
*/
|
2016-06-05 01:52:18 -07:00
|
|
|
export const ENTRY_REQUEST = 'ENTRY_REQUEST';
|
|
|
|
export const ENTRY_SUCCESS = 'ENTRY_SUCCESS';
|
|
|
|
export const ENTRY_FAILURE = 'ENTRY_FAILURE';
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
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';
|
2016-06-08 04:42:24 -03:00
|
|
|
export const DRAFT_DISCARD = 'DRAFT_DISCARD';
|
|
|
|
export const DRAFT_CHANGE = 'DRAFT_CHANGE';
|
2016-12-29 17:18:24 -02:00
|
|
|
export const DRAFT_CHANGE_FIELD = 'DRAFT_CHANGE_FIELD';
|
2017-01-13 19:30:40 -02:00
|
|
|
export const DRAFT_VALIDATION_ERRORS = 'DRAFT_VALIDATION_ERRORS';
|
2016-06-10 00:16:01 -03:00
|
|
|
|
2016-06-06 21:53:22 -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';
|
|
|
|
|
2017-07-21 23:40:33 -07:00
|
|
|
export const ENTRY_DELETE_REQUEST = 'ENTRY_DELETE_REQUEST';
|
|
|
|
export const ENTRY_DELETE_SUCCESS = 'ENTRY_DELETE_SUCCESS';
|
|
|
|
export const ENTRY_DELETE_FAILURE = 'ENTRY_DELETE_FAILURE';
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Simple Action Creators (Internal)
|
2016-09-20 14:00:03 +02:00
|
|
|
* We still need to export them for tests
|
2016-06-08 04:42:24 -03:00
|
|
|
*/
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entryLoading(collection, slug) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
slug,
|
|
|
|
},
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entryLoaded(collection, entry) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
entry,
|
|
|
|
},
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entriesLoading(collection) {
|
2016-06-08 04:42:24 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_REQUEST,
|
|
|
|
payload: {
|
2016-10-12 16:01:27 +02:00
|
|
|
collection: collection.get('name'),
|
|
|
|
},
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entriesLoaded(collection, entries, pagination) {
|
2016-06-08 04:42:24 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
entries,
|
|
|
|
page: pagination,
|
|
|
|
},
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entriesFailed(collection, error) {
|
2016-06-08 04:42:24 -03:00
|
|
|
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') },
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entryPersisting(collection, entry) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-06-06 21:53:22 -03:00
|
|
|
type: ENTRY_PERSIST_REQUEST,
|
2016-02-25 20:40:35 -08:00
|
|
|
payload: {
|
2016-10-12 19:19:05 +02:00
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
2016-10-12 16:01:27 +02:00
|
|
|
},
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entryPersisted(collection, entry) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
|
|
|
type: ENTRY_PERSIST_SUCCESS,
|
|
|
|
payload: {
|
2016-10-12 19:19:05 +02:00
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
2016-10-12 16:01:27 +02:00
|
|
|
},
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function entryPersistFail(collection, entry, error) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
2016-10-12 19:19:05 +02:00
|
|
|
type: ENTRY_PERSIST_FAILURE,
|
2016-06-06 21:53:22 -03:00
|
|
|
error: 'Failed to persist entry',
|
2016-10-12 19:19:05 +02:00
|
|
|
payload: {
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
|
|
|
error: error.toString(),
|
|
|
|
},
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-21 23:40:33 -07:00
|
|
|
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(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-29 17:18:24 -02:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Exported simple Action Creators
|
|
|
|
*/
|
2016-07-19 17:11:22 -03:00
|
|
|
export function createDraftFromEntry(entry) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-07-19 17:11:22 -03:00
|
|
|
type: DRAFT_CREATE_FROM_ENTRY,
|
2016-10-12 16:01:27 +02:00
|
|
|
payload: entry,
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:30:40 -02:00
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export function discardDraft() {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
2016-10-12 16:01:27 +02:00
|
|
|
type: DRAFT_DISCARD,
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export function changeDraft(entry) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-06-08 04:42:24 -03:00
|
|
|
type: DRAFT_CHANGE,
|
2016-10-12 16:01:27 +02:00
|
|
|
payload: entry,
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-29 17:18:24 -02:00
|
|
|
export function changeDraftField(field, value, metadata) {
|
|
|
|
return {
|
|
|
|
type: DRAFT_CHANGE_FIELD,
|
|
|
|
payload: { field, value, metadata },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:30:40 -02:00
|
|
|
export function changeDraftFieldValidation(field, errors) {
|
|
|
|
return {
|
|
|
|
type: DRAFT_VALIDATION_ERRORS,
|
|
|
|
payload: { field, errors },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Exported Thunk Action Creators
|
|
|
|
*/
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
export function loadEntry(collection, slug) {
|
2016-06-05 01:52:18 -07:00
|
|
|
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)
|
2017-06-21 13:49:43 -04:00
|
|
|
.then(loadedEntry => {
|
|
|
|
const deserializeValues = (values, fields) => {
|
|
|
|
return fields.reduce((acc, field) => {
|
|
|
|
const fieldName = field.get('name');
|
|
|
|
const value = values[fieldName];
|
2017-06-22 17:35:47 -04:00
|
|
|
const serializer = registry.getWidgetValueSerializer(field.get('widget'));
|
2017-06-21 13:49:43 -04:00
|
|
|
if (isArray(value) && !isEmpty(value)) {
|
|
|
|
acc[fieldName] = value.map(val => deserializeValues(val, field.get('fields')));
|
|
|
|
} else if (isObject(value) && !isEmpty(value)) {
|
|
|
|
acc[fieldName] = deserializeValues(value, field.get('fields'));
|
|
|
|
} else if (serializer && !isNil(value)) {
|
|
|
|
acc[fieldName] = serializer.deserialize(value);
|
|
|
|
} else if (!isNil(value)) {
|
|
|
|
acc[fieldName] = value;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
loadedEntry.data = deserializeValues(loadedEntry.data, collection.get('fields'));
|
|
|
|
return dispatch(entryLoaded(collection, loadedEntry))
|
|
|
|
})
|
2017-01-10 22:23:22 -02:00
|
|
|
.catch((error) => {
|
|
|
|
dispatch(notifSend({
|
|
|
|
message: `Failed to load entry: ${ error.message }`,
|
|
|
|
kind: 'danger',
|
2017-01-11 20:58:15 -02:00
|
|
|
dismissAfter: 8000,
|
2017-01-10 22:23:22 -02:00
|
|
|
}));
|
|
|
|
dispatch(entryLoadError(error, collection, slug));
|
|
|
|
});
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-10 15:34:21 -03:00
|
|
|
export function loadEntries(collection, page = 0) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return (dispatch, getState) => {
|
2016-10-17 12:35:31 +02:00
|
|
|
if (collection.get('isFetching')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-25 20:40:35 -08:00
|
|
|
const state = getState();
|
2017-01-10 22:23:22 -02:00
|
|
|
const backend = currentBackend(state.config);
|
2016-10-10 15:34:21 -03:00
|
|
|
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;
|
2016-02-25 20:40:35 -08:00
|
|
|
dispatch(entriesLoading(collection));
|
2016-10-10 15:34:21 -03:00
|
|
|
provider.listEntries(collection, page).then(
|
2017-04-05 23:02:13 +01:00
|
|
|
response => dispatch(entriesLoaded(collection, response.entries.reverse(), response.pagination)),
|
2016-10-12 16:01:27 +02:00
|
|
|
error => dispatch(entriesFailed(collection, error))
|
2016-06-08 04:42:24 -03:00
|
|
|
);
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 });
|
2016-12-29 17:18:24 -02:00
|
|
|
dispatch(emptyDraftCreated(newEntry));
|
2016-08-24 21:36:44 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:30:40 -02:00
|
|
|
export function persistEntry(collection) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2017-01-13 19:30:40 -02:00
|
|
|
const entryDraft = state.entryDraft;
|
|
|
|
|
|
|
|
// Early return if draft contains validation errors
|
2017-07-21 23:40:33 -07:00
|
|
|
if (!entryDraft.get('fieldsErrors').isEmpty()) return Promise.reject();
|
2017-06-21 13:49:43 -04:00
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
const backend = currentBackend(state.config);
|
2017-01-10 22:23:22 -02:00
|
|
|
const assetProxies = entryDraft.get('mediaFiles').map(path => getAsset(state, path));
|
2016-10-13 14:30:11 +02:00
|
|
|
const entry = entryDraft.get('entry');
|
2017-06-21 13:49:43 -04:00
|
|
|
const serializeValues = (values, fields) => {
|
|
|
|
return fields.reduce((acc, field) => {
|
|
|
|
const fieldName = field.get('name');
|
|
|
|
const value = values.get(fieldName);
|
2017-06-22 17:35:47 -04:00
|
|
|
const serializer = registry.getWidgetValueSerializer(field.get('widget'));
|
2017-06-21 13:49:43 -04:00
|
|
|
if (List.isList(value)) {
|
|
|
|
return acc.set(fieldName, value.map(val => serializeValues(val, field.get('fields'))));
|
|
|
|
} else if (Map.isMap(value)) {
|
|
|
|
return acc.set(fieldName, serializeValues(value, field.get('fields')));
|
|
|
|
} else if (serializer && !isNil(value)) {
|
|
|
|
return acc.set(fieldName, serializer.serialize(value));
|
|
|
|
} else if (!isNil(value)) {
|
|
|
|
return acc.set(fieldName, value);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, Map());
|
|
|
|
};
|
|
|
|
const transformedData = serializeValues(entryDraft.getIn(['entry', 'data']), collection.get('fields'));
|
|
|
|
const transformedEntry = entry.set('data', transformedData);
|
|
|
|
const transformedEntryDraft = entryDraft.set('entry', transformedEntry);
|
|
|
|
dispatch(entryPersisting(collection, transformedEntry));
|
2017-07-21 23:40:33 -07:00
|
|
|
return backend
|
2017-06-21 13:49:43 -04:00
|
|
|
.persistEntry(state.config, collection, transformedEntryDraft, assetProxies.toJS())
|
2016-10-17 12:35:31 +02:00
|
|
|
.then(() => {
|
|
|
|
dispatch(notifSend({
|
|
|
|
message: 'Entry saved',
|
|
|
|
kind: 'success',
|
|
|
|
dismissAfter: 4000,
|
|
|
|
}));
|
2017-06-21 13:49:43 -04:00
|
|
|
return dispatch(entryPersisted(collection, transformedEntry));
|
2016-10-17 12:35:31 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
dispatch(notifSend({
|
2017-01-11 20:58:15 -02:00
|
|
|
message: `Failed to persist entry: ${ error }`,
|
2016-10-17 12:35:31 +02:00
|
|
|
kind: 'danger',
|
2017-01-11 20:58:15 -02:00
|
|
|
dismissAfter: 8000,
|
2016-10-17 12:35:31 +02:00
|
|
|
}));
|
2017-06-21 13:49:43 -04:00
|
|
|
return dispatch(entryPersistFail(collection, transformedEntry, error));
|
2016-10-17 12:35:31 +02:00
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
2017-07-21 23:40:33 -07:00
|
|
|
|
|
|
|
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 dispatch(entryDeleteFail(collection, slug, error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|