static-cms/src/actions/entries.js

240 lines
6.3 KiB
JavaScript
Raw Normal View History

import { currentBackend } from '../backends/backend';
import { getIntegrationProvider } from '../integrations';
import { getMedia, selectIntegration } from '../reducers';
/*
* 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';
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 SEARCH_ENTRIES_REQUEST = 'SEARCH_ENTRIES_REQUEST';
export const SEARCH_ENTRIES_SUCCESS = 'SEARCH_ENTRIES_SUCCESS';
export const SEARCH_ENTRIES_FAILURE = 'SEARCH_ENTRIES_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'),
slug: slug
}
};
}
export function entryLoaded(collection, entry) {
return {
type: ENTRY_SUCCESS,
payload: {
collection: collection.get('name'),
entry: entry
}
};
}
export function entriesLoading(collection) {
return {
type: ENTRIES_REQUEST,
payload: {
collection: collection.get('name')
}
};
}
export function entriesLoaded(collection, entries, pagination) {
return {
type: ENTRIES_SUCCESS,
payload: {
collection: collection.get('name'),
entries: entries,
page: pagination
}
};
}
export function entriesFailed(collection, error) {
return {
type: ENTRIES_FAILURE,
error: 'Failed to load entries',
payload: error.toString(),
meta: { collection: collection.get('name') }
};
}
export function entryPersisting(collection, entry) {
return {
type: ENTRY_PERSIST_REQUEST,
payload: {
collection: collection,
entry: entry
}
};
}
export function entryPersisted(collection, entry) {
return {
type: ENTRY_PERSIST_SUCCESS,
payload: {
collection: collection,
entry: entry
}
};
}
export function entryPersistFail(collection, entry, error) {
return {
type: ENTRIES_FAILURE,
error: 'Failed to persist entry',
payload: error.toString()
};
}
export function emmptyDraftCreated(entry) {
2016-08-24 21:36:44 -03:00
return {
type: DRAFT_CREATE_EMPTY,
payload: entry
};
}
export function searchingEntries(searchTerm) {
return {
type: SEARCH_ENTRIES_REQUEST,
payload: { searchTerm }
};
}
export function SearchSuccess(searchTerm, entries, page) {
return {
type: SEARCH_ENTRIES_SUCCESS,
payload: {
searchTerm,
entries,
page
}
};
}
export function SearchFailure(searchTerm, error) {
return {
type: SEARCH_ENTRIES_FAILURE,
payload: {
searchTerm,
error
}
};
}
/*
* 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,
payload: entry
};
}
export function discardDraft() {
return {
type: DRAFT_DISCARD
};
}
export function changeDraft(entry) {
return {
type: DRAFT_CHANGE,
payload: entry
};
}
/*
* Exported Thunk Action Creators
*/
export function loadEntry(entry, collection, slug) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(entryLoading(collection, slug));
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);
}
return getPromise.then((loadedEntry) => dispatch(entryLoaded(collection, loadedEntry)));
};
}
export function loadEntries(collection, page = 0) {
return (dispatch, getState) => {
if (collection.get('isFetching')) { return; }
const state = getState();
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
const provider = integration ? getIntegrationProvider(state.integrations, integration) : currentBackend(state.config);
dispatch(entriesLoading(collection));
provider.listEntries(collection, page).then(
(response) => dispatch(entriesLoaded(collection, response.entries, response.pagination)),
(error) => dispatch(entriesFailed(collection, error))
);
};
}
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-07-19 18:56:54 -03:00
export function persistEntry(collection, entry) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
2016-07-19 18:56:54 -03:00
const MediaProxies = entry.get('mediaFiles').map(path => getMedia(state, path));
dispatch(entryPersisting(collection, entry));
backend.persistEntry(state.config, collection, entry, MediaProxies.toJS()).then(
() => {
dispatch(entryPersisted(collection, entry));
},
(error) => dispatch(entryPersistFail(collection, entry, error))
);
};
}
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');
if (!integration) console.warn('There isn\'t a search integration configured.');
const provider = integration ? getIntegrationProvider(state.integrations, integration) : currentBackend(state.config);
dispatch(searchingEntries(searchTerm));
provider.search(collections, searchTerm, page).then(
(response) => dispatch(SearchSuccess(searchTerm, response.entries, response.pagination)),
(error) => dispatch(SearchFailure(searchTerm, error))
);
};
}