Asset API (#204)

Asset API
This commit is contained in:
Cássio Souza
2017-01-10 22:23:22 -02:00
committed by GitHub
parent 37f690fc44
commit a4d7622ade
52 changed files with 706 additions and 687 deletions

View File

@ -2,6 +2,7 @@ import { Map, List, fromJS } from 'immutable';
import {
ENTRY_REQUEST,
ENTRY_SUCCESS,
ENTRY_FAILURE,
ENTRIES_REQUEST,
ENTRIES_SUCCESS,
} from '../actions/entries';
@ -41,6 +42,12 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
ids: (!page || page === 0) ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids),
}));
});
case ENTRY_FAILURE:
return state.withMutations((map) => {
map.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'isFetching'], false);
map.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'error'], action.payload.error.message);
});
case SEARCH_ENTRIES_SUCCESS:
loadedEntries = action.payload.entries;

View File

@ -9,8 +9,8 @@ import {
ENTRY_PERSIST_FAILURE,
} from '../actions/entries';
import {
ADD_MEDIA,
REMOVE_MEDIA,
ADD_ASSET,
REMOVE_ASSET,
} from '../actions/media';
const initialState = Map({ entry: Map(), mediaFiles: List(), fieldsMetaData: Map() });
@ -49,9 +49,9 @@ const entryDraftReducer = (state = Map(), action) => {
return state.deleteIn(['entry', 'isPersisting']);
}
case ADD_MEDIA:
case ADD_ASSET:
return state.update('mediaFiles', list => list.push(action.payload.public_path));
case REMOVE_MEDIA:
case REMOVE_ASSET:
return state.update('mediaFiles', list => list.filterNot(path => path === action.payload));
default:

View File

@ -49,5 +49,5 @@ export const selectUnpublishedEntries = (state, status) =>
export const selectIntegration = (state, collection, hook) =>
fromIntegrations.selectIntegration(state.integrations, collection, hook);
export const getMedia = (state, path) =>
fromMedias.getMedia(state.config.get('public_folder'), state.medias, path);
export const getAsset = (state, path) =>
fromMedias.getAsset(state.config.get('public_folder'), state.medias, path);

View File

@ -8,8 +8,15 @@ const integrations = (state = null, action) => {
const newState = integrations.reduce((acc, integration) => {
const { hooks, collections, provider, ...providerData } = integration;
acc.providers[provider] = { ...providerData };
collections.forEach(collection => {
hooks.forEach(hook => {
if (!collections) {
hooks.forEach((hook) => {
acc.hooks[hook] = provider;
});
return acc;
}
const integrationCollections = collections === "*" ? action.payload.collections.map(collection => collection.name) : collections;
integrationCollections.forEach((collection) => {
hooks.forEach((hook) => {
acc.hooks[collection] ? acc.hooks[collection][hook] = provider : acc.hooks[collection] = { [hook]: provider };
});
});
@ -21,9 +28,9 @@ const integrations = (state = null, action) => {
}
};
export const selectIntegration = (state, collection, hook) => {
return state.getIn(['hooks', collection, hook], false);
};
export const selectIntegration = (state, collection, hook) => (
collection? state.getIn(['hooks', collection, hook], false) : state.getIn(['hooks', hook], false)
);
export default integrations;

View File

@ -1,13 +1,13 @@
import { Map } from 'immutable';
import { resolvePath } from '../lib/pathHelper';
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
import MediaProxy from '../valueObjects/MediaProxy';
import { ADD_ASSET, REMOVE_ASSET } from '../actions/media';
import AssetProxy from '../valueObjects/AssetProxy';
const medias = (state = Map(), action) => {
switch (action.type) {
case ADD_MEDIA:
case ADD_ASSET:
return state.set(action.payload.public_path, action.payload);
case REMOVE_MEDIA:
case REMOVE_ASSET:
return state.delete(action.payload);
default:
@ -18,17 +18,17 @@ const medias = (state = Map(), action) => {
export default medias;
const memoizedProxies = {};
export const getMedia = (publicFolder, state, path) => {
export const getAsset = (publicFolder, state, path) => {
// No path provided, skip
if (!path) return null;
let proxy = state.get(path) || memoizedProxies[path];
if (proxy) {
// There is already a MediaProxy in memmory for this path. Use it.
// There is already an AssetProxy in memmory for this path. Use it.
return proxy;
}
// Create a new MediaProxy (for consistency) and return it.
proxy = memoizedProxies[path] = new MediaProxy(resolvePath(path, publicFolder), null, true);
// Create a new AssetProxy (for consistency) and return it.
proxy = memoizedProxies[path] = new AssetProxy(resolvePath(path, publicFolder), null, true);
return proxy;
};