2016-10-17 12:35:31 +02:00
|
|
|
import { actions as notifActions } from 'redux-notifications';
|
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';
|
|
|
|
import { getMedia, selectIntegration } from '../reducers';
|
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-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';
|
|
|
|
|
2016-10-10 15:34:21 -03:00
|
|
|
export const SEARCH_ENTRIES_REQUEST = 'SEARCH_ENTRIES_REQUEST';
|
|
|
|
export const SEARCH_ENTRIES_SUCCESS = 'SEARCH_ENTRIES_SUCCESS';
|
|
|
|
export const SEARCH_ENTRIES_FAILURE = 'SEARCH_ENTRIES_FAILURE';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:00:03 +02:00
|
|
|
export function emmptyDraftCreated(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-10-10 15:34:21 -03:00
|
|
|
export function searchingEntries(searchTerm) {
|
|
|
|
return {
|
|
|
|
type: SEARCH_ENTRIES_REQUEST,
|
2016-10-12 16:01:27 +02:00
|
|
|
payload: { searchTerm },
|
2016-10-10 15:34:21 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:01:27 +02:00
|
|
|
export function searchSuccess(searchTerm, entries, page) {
|
2016-10-10 15:34:21 -03:00
|
|
|
return {
|
|
|
|
type: SEARCH_ENTRIES_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
searchTerm,
|
|
|
|
entries,
|
2016-10-12 16:01:27 +02:00
|
|
|
page,
|
|
|
|
},
|
2016-10-10 15:34:21 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:01:27 +02:00
|
|
|
export function searchFailure(searchTerm, error) {
|
2016-10-10 15:34:21 -03:00
|
|
|
return {
|
|
|
|
type: SEARCH_ENTRIES_FAILURE,
|
|
|
|
payload: {
|
|
|
|
searchTerm,
|
2016-10-12 16:01:27 +02:00
|
|
|
error,
|
|
|
|
},
|
2016-10-10 15:34:21 -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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported Thunk Action Creators
|
|
|
|
*/
|
2016-10-10 15:34:21 -03:00
|
|
|
|
|
|
|
export function loadEntry(entry, 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-10 15:34:21 -03:00
|
|
|
let getPromise;
|
|
|
|
if (entry && entry.get('path')) {
|
|
|
|
getPromise = backend.getEntry(entry.get('collection'), entry.get('slug'), entry.get('path'));
|
|
|
|
} else {
|
|
|
|
getPromise = backend.lookupEntry(collection, slug);
|
|
|
|
}
|
2016-10-12 16:01:27 +02:00
|
|
|
return getPromise.then(loadedEntry => dispatch(entryLoaded(collection, loadedEntry)));
|
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();
|
2016-10-10 15:34:21 -03:00
|
|
|
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
|
|
|
|
const provider = integration ? getIntegrationProvider(state.integrations, integration) : currentBackend(state.config);
|
2016-02-25 20:40:35 -08:00
|
|
|
dispatch(entriesLoading(collection));
|
2016-10-10 15:34:21 -03:00
|
|
|
provider.listEntries(collection, page).then(
|
2016-10-12 16:01:27 +02:00
|
|
|
response => dispatch(entriesLoaded(collection, response.entries, response.pagination)),
|
|
|
|
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) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
const newEntry = backend.newEntry(collection);
|
|
|
|
dispatch(emmptyDraftCreated(newEntry));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:30:11 +02:00
|
|
|
export function persistEntry(collection, entryDraft) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
2016-10-13 14:30:11 +02:00
|
|
|
const mediaProxies = entryDraft.get('mediaFiles').map(path => getMedia(state, path));
|
|
|
|
const entry = entryDraft.get('entry');
|
2016-06-06 21:53:22 -03:00
|
|
|
dispatch(entryPersisting(collection, entry));
|
2016-10-17 12:35:31 +02:00
|
|
|
backend
|
|
|
|
.persistEntry(state.config, collection, entryDraft, mediaProxies.toJS())
|
|
|
|
.then(() => {
|
|
|
|
dispatch(notifSend({
|
|
|
|
message: 'Entry saved',
|
|
|
|
kind: 'success',
|
|
|
|
dismissAfter: 4000,
|
|
|
|
}));
|
|
|
|
dispatch(entryPersisted(collection, entry));
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
dispatch(notifSend({
|
|
|
|
message: 'Failed to persist entry',
|
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 4000,
|
|
|
|
}));
|
|
|
|
dispatch(entryPersistFail(collection, entry, error));
|
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
2016-10-10 15:34:21 -03:00
|
|
|
|
|
|
|
export function searchEntries(searchTerm, page = 0) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
let collections = state.collections.keySeq().toArray();
|
|
|
|
collections = collections.filter(collection => selectIntegration(state, collection, 'search'));
|
|
|
|
const integration = selectIntegration(state, collections[0], 'search');
|
2016-10-12 16:01:27 +02:00
|
|
|
if (!integration) {
|
|
|
|
dispatch(searchFailure(searchTerm, 'Search integration is not configured.'));
|
|
|
|
}
|
|
|
|
const provider = integration ?
|
|
|
|
getIntegrationProvider(state.integrations, integration)
|
|
|
|
: currentBackend(state.config);
|
2016-10-10 15:34:21 -03:00
|
|
|
dispatch(searchingEntries(searchTerm));
|
|
|
|
provider.search(collections, searchTerm, page).then(
|
2016-10-12 16:01:27 +02:00
|
|
|
response => dispatch(searchSuccess(searchTerm, response.entries, response.pagination)),
|
|
|
|
error => dispatch(searchFailure(searchTerm, error))
|
2016-10-10 15:34:21 -03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|