static-cms/src/reducers/medias.js

35 lines
966 B
JavaScript
Raw Normal View History

2016-06-10 00:16:01 -03:00
import { Map } from 'immutable';
import { resolvePath } from '../lib/pathHelper';
2016-06-10 14:01:14 -03:00
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
2016-06-10 00:16:01 -03:00
import MediaProxy from '../valueObjects/MediaProxy';
const medias = (state = Map(), action) => {
switch (action.type) {
case ADD_MEDIA:
return state.set(action.payload.public_path, action.payload);
2016-06-10 14:01:14 -03:00
case REMOVE_MEDIA:
return state.delete(action.payload);
2016-06-10 00:16:01 -03:00
default:
return state;
}
};
export default medias;
const memoizedProxies = {};
export const getMedia = (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.
return proxy;
2016-06-10 00:16:01 -03:00
}
// Create a new MediaProxy (for consistency) and return it.
proxy = memoizedProxies[path] = new MediaProxy(resolvePath(path, publicFolder), null, true);
return proxy;
2016-06-10 00:16:01 -03:00
};