Moved draft state for an entry (when in edit mode) to Redux

This commit is contained in:
Cássio Zen
2016-06-08 04:42:24 -03:00
parent 2d48743f37
commit 9275aaec90
11 changed files with 206 additions and 112 deletions

View File

@ -1,18 +1,31 @@
import { currentBackend } from '../backends/backend';
/*
* Contant Declarations
*/
export const ENTRY_REQUEST = 'ENTRY_REQUEST';
export const ENTRY_SUCCESS = 'ENTRY_SUCCESS';
export const ENTRY_FAILURE = 'ENTRY_FAILURE';
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 ENTRIES_REQUEST = 'ENTRIES_REQUEST';
export const ENTRIES_SUCCESS = 'ENTRIES_SUCCESS';
export const ENTRIES_FAILURE = 'ENTRIES_FAILURE';
export function entryLoading(collection, slug) {
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';
export const ENTRY_PERSIST_REQUEST = 'ENTRY_PERSIST_REQUEST';
export const ENTRY_PERSIST_SUCCESS = 'ENTRY_PERSIST_SUCCESS';
export const ENTRY_PERSIST_FAILURE = 'ENTRY_PERSIST_FAILURE';
/*
* Simple Action Creators (Internal)
*/
function entryLoading(collection, slug) {
return {
type: ENTRY_REQUEST,
payload: {
@ -22,7 +35,7 @@ export function entryLoading(collection, slug) {
};
}
export function entryLoaded(collection, entry) {
function entryLoaded(collection, entry) {
return {
type: ENTRY_SUCCESS,
payload: {
@ -32,35 +45,7 @@ export function entryLoaded(collection, entry) {
};
}
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 entriesLoading(collection) {
function entriesLoading(collection) {
return {
type: ENTRIES_REQUEST,
payload: {
@ -69,7 +54,7 @@ export function entriesLoading(collection) {
};
}
export function entriesLoaded(collection, entries, pagination) {
function entriesLoaded(collection, entries, pagination) {
return {
type: ENTRIES_SUCCESS,
payload: {
@ -80,7 +65,7 @@ export function entriesLoaded(collection, entries, pagination) {
};
}
export function entriesFailed(collection, error) {
function entriesFailed(collection, error) {
return {
type: ENTRIES_FAILURE,
error: 'Failed to load entries',
@ -89,6 +74,74 @@ export function entriesFailed(collection, error) {
};
}
function entryPersisting(collection, entry) {
return {
type: ENTRY_PERSIST_REQUEST,
payload: {
collection: collection,
entry: entry
}
};
}
function entryPersisted(collection, entry) {
return {
type: ENTRY_PERSIST_SUCCESS,
payload: {
collection: collection,
entry: entry
}
};
}
function entryPersistFail(collection, entry, error) {
return {
type: ENTRIES_FAILURE,
error: 'Failed to persist entry',
payload: error.toString()
};
}
/*
* Exported simple Action Creators
*/
export function createDraft(entry) {
return {
type: DRAFT_CREATE,
payload: entry
};
}
export function discardDraft() {
return {
type: DRAFT_DISCARD
};
}
export function changeDraft(entry) {
return {
type: DRAFT_CHANGE,
payload: entry
};
}
export function addMediaToDraft(mediaFile) {
return {
type: DRAFT_ADD_MEDIA,
payload: mediaFile
};
}
export function removeMediaFromDraft(mediaFile) {
return {
type: DRAFT_REMOVE_MEDIA,
payload: mediaFile
};
}
/*
* Exported Thunk Action Creators
*/
export function loadEntry(collection, slug) {
return (dispatch, getState) => {
const state = getState();
@ -107,8 +160,10 @@ export function loadEntries(collection) {
const backend = currentBackend(state.config);
dispatch(entriesLoading(collection));
backend.entries(collection)
.then((response) => dispatch(entriesLoaded(collection, response.entries, response.pagination)));
backend.entries(collection).then(
(response) => dispatch(entriesLoaded(collection, response.entries, response.pagination)),
(error) => dispatch(entriesFailed(collection, error))
);
};
}