Use public_folder to display images persisted with no path reference

This commit is contained in:
Cássio Zen 2016-11-17 11:12:14 -02:00
parent d81d0d416f
commit 8d9f894928
4 changed files with 23 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import styles from './EntryListing.css';
export default class EntryListing extends React.Component { export default class EntryListing extends React.Component {
static propTypes = { static propTypes = {
children: PropTypes.node.isRequired, children: PropTypes.node.isRequired,
publicFolder: PropTypes.string.isRequired,
collections: PropTypes.oneOfType([ collections: PropTypes.oneOfType([
ImmutablePropTypes.map, ImmutablePropTypes.map,
ImmutablePropTypes.iterable, ImmutablePropTypes.iterable,
@ -23,7 +24,7 @@ export default class EntryListing extends React.Component {
this.props.onPaginate(this.props.page + 1); this.props.onPaginate(this.props.page + 1);
}; };
inferFields(collection) { inferFields(collection) { //eslint-disable-line
const titleField = selectInferedField(collection, 'title'); const titleField = selectInferedField(collection, 'title');
const descriptionField = selectInferedField(collection, 'description'); const descriptionField = selectInferedField(collection, 'description');
const imageField = selectInferedField(collection, 'image'); const imageField = selectInferedField(collection, 'image');
@ -33,11 +34,14 @@ export default class EntryListing extends React.Component {
return { titleField, descriptionField, imageField, remainingFields }; return { titleField, descriptionField, imageField, remainingFields };
} }
renderCard(collection, entry, inferedFields) { renderCard(collection, entry, inferedFields, publicFolder) {
const path = `/collections/${ collection.get('name') }/entries/${ entry.get('slug') }`; const path = `/collections/${ collection.get('name') }/entries/${ entry.get('slug') }`;
const label = entry.get('label'); const label = entry.get('label');
const title = label || entry.getIn(['data', inferedFields.titleField]); const title = label || entry.getIn(['data', inferedFields.titleField]);
const image = entry.getIn(['data', inferedFields.imageField]); let image = entry.getIn(['data', inferedFields.imageField]);
if (image && image.indexOf('/') === -1) {
image = `/${ publicFolder }/${ image }`;
}
return ( return (
<Card <Card
key={entry.get('slug')} key={entry.get('slug')}
@ -60,11 +64,11 @@ export default class EntryListing extends React.Component {
); );
} }
renderCards = () => { renderCards = () => {
const { collections, entries } = this.props; const { collections, entries, publicFolder } = this.props;
if (Map.isMap(collections)) { if (Map.isMap(collections)) {
const inferedFields = this.inferFields(collections); const inferedFields = this.inferFields(collections);
return entries.map(entry => this.renderCard(collections, entry, inferedFields)); return entries.map(entry => this.renderCard(collections, entry, inferedFields, publicFolder));
} }
return entries.map((entry) => { return entries.map((entry) => {
const collection = collections const collection = collections

View File

@ -12,6 +12,7 @@ class CollectionPage extends React.Component {
static propTypes = { static propTypes = {
collection: ImmutablePropTypes.map.isRequired, collection: ImmutablePropTypes.map.isRequired,
collections: ImmutablePropTypes.orderedMap.isRequired, collections: ImmutablePropTypes.orderedMap.isRequired,
publicFolder: PropTypes.string.isRequired,
dispatch: PropTypes.func.isRequired, dispatch: PropTypes.func.isRequired,
page: PropTypes.number, page: PropTypes.number,
entries: ImmutablePropTypes.list, entries: ImmutablePropTypes.list,
@ -37,7 +38,7 @@ class CollectionPage extends React.Component {
}; };
render() { render() {
const { collections, collection, page, entries } = this.props; const { collections, collection, publicFolder, page, entries } = this.props;
if (collections == null) { if (collections == null) {
return <h1>No collections defined in your config.yml</h1>; return <h1>No collections defined in your config.yml</h1>;
} }
@ -46,6 +47,7 @@ class CollectionPage extends React.Component {
<EntryListing <EntryListing
collections={collection} collections={collection}
entries={entries} entries={entries}
publicFolder={publicFolder}
page={page} page={page}
onPaginate={this.handleLoadMore} onPaginate={this.handleLoadMore}
> >
@ -60,14 +62,15 @@ class CollectionPage extends React.Component {
function mapStateToProps(state, ownProps) { function mapStateToProps(state, ownProps) {
const { collections } = state; const { collections, config } = state;
const { name, slug } = ownProps.params; const { name, slug } = ownProps.params;
const publicFolder = config.get('public_folder');
const collection = name ? collections.get(name) : collections.first(); const collection = name ? collections.get(name) : collections.first();
const page = state.entries.getIn(['pages', collection.get('name'), 'page']); const page = state.entries.getIn(['pages', collection.get('name'), 'page']);
const entries = selectEntries(state, collection.get('name')); const entries = selectEntries(state, collection.get('name'));
return { slug, collection, collections, page, entries }; return { slug, publicFolder, collection, collections, page, entries };
} }
export default connect(mapStateToProps)(CollectionPage); export default connect(mapStateToProps)(CollectionPage);

View File

@ -46,4 +46,4 @@ export const selectIntegration = (state, collection, hook) =>
fromIntegrations.selectIntegration(state.integrations, collection, hook); fromIntegrations.selectIntegration(state.integrations, collection, hook);
export const getMedia = (state, path) => export const getMedia = (state, path) =>
fromMedias.getMedia(state.medias, path); fromMedias.getMedia(state.config.get('public_folder'), state.medias, path);

View File

@ -16,10 +16,14 @@ const medias = (state = Map(), action) => {
export default medias; export default medias;
export const getMedia = (state, path) => { export const getMedia = (publicFolder, state, path) => {
if (state.has(path)) { if (state.has(path)) {
return state.get(path); return state.get(path);
} else {
return new MediaProxy(path, null, true);
} }
let localPath = path;
if (path && path.indexOf('/') === -1) {
localPath = `/${ publicFolder }/${ localPath }`;
}
return new MediaProxy(localPath, null, true);
}; };