2016-02-25 20:40:35 -08:00
|
|
|
import { currentBackend } from '../backends/backend';
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
export const DRAFT_CREATE = 'DRAFT_CREATE';
|
|
|
|
export const DRAFT_DISCARD = 'DRAFT_DISCARD';
|
|
|
|
export const DRAFT_CHANGE = 'DRAFT_CHANGE';
|
|
|
|
export const DRAFT_ADD_MEDIA = 'DRAFT_ADD_MEDIA';
|
|
|
|
export const DRAFT_REMOVE_MEDIA = 'DRAFT_REMOVE_MEDIA';
|
|
|
|
|
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-02-25 20:40:35 -08:00
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Simple Action Creators (Internal)
|
|
|
|
*/
|
|
|
|
function entryLoading(collection, slug) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
slug: slug
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
function entryLoaded(collection, entry) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
entry: entry
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
function entriesLoading(collection) {
|
|
|
|
return {
|
|
|
|
type: ENTRIES_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name')
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function entriesLoaded(collection, entries, pagination) {
|
|
|
|
return {
|
|
|
|
type: ENTRIES_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
entries: entries,
|
|
|
|
pages: pagination
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function entriesFailed(collection, error) {
|
|
|
|
return {
|
|
|
|
type: ENTRIES_FAILURE,
|
|
|
|
error: 'Failed to load entries',
|
|
|
|
payload: error.toString(),
|
|
|
|
meta: {collection: collection.get('name')}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-06 21:53:22 -03:00
|
|
|
collection: collection,
|
|
|
|
entry: entry
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
function entryPersisted(collection, entry) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
|
|
|
type: ENTRY_PERSIST_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection,
|
|
|
|
entry: entry
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
function entryPersistFail(collection, entry, error) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_FAILURE,
|
|
|
|
error: 'Failed to persist entry',
|
|
|
|
payload: error.toString()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Exported simple Action Creators
|
|
|
|
*/
|
|
|
|
export function createDraft(entry) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-06-08 04:42:24 -03:00
|
|
|
type: DRAFT_CREATE,
|
|
|
|
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-06-08 04:42:24 -03: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,
|
|
|
|
payload: entry
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addMediaToDraft(mediaFile) {
|
|
|
|
return {
|
|
|
|
type: DRAFT_ADD_MEDIA,
|
|
|
|
payload: mediaFile
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export function removeMediaFromDraft(mediaFile) {
|
|
|
|
return {
|
|
|
|
type: DRAFT_REMOVE_MEDIA,
|
|
|
|
payload: mediaFile
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported Thunk Action Creators
|
|
|
|
*/
|
2016-06-05 01:52:18 -07:00
|
|
|
export function loadEntry(collection, slug) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
dispatch(entryLoading(collection, slug));
|
|
|
|
backend.entry(collection, slug)
|
|
|
|
.then((entry) => dispatch(entryLoaded(collection, entry)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
export function loadEntries(collection) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
if (collection.get('isFetching')) { return; }
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
dispatch(entriesLoading(collection));
|
2016-06-08 04:42:24 -03:00
|
|
|
backend.entries(collection).then(
|
|
|
|
(response) => dispatch(entriesLoaded(collection, response.entries, response.pagination)),
|
|
|
|
(error) => dispatch(entriesFailed(collection, error))
|
|
|
|
);
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function persist(collection, entry, mediaFiles) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
dispatch(entryPersisting(collection, entry));
|
|
|
|
backend.persist(collection, entry, mediaFiles).then(
|
|
|
|
(entry) => dispatch(entryPersisted(collection, entry)),
|
|
|
|
(error) => dispatch(entryPersistFail(collection, entry, error))
|
|
|
|
);
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|