Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1896 fetching other collections from preview #6523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/decap-cms-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ declare module 'decap-cms-core' {
export type PreviewTemplateComponentProps = {
entry: Map<string, any>;
collection: Map<string, any>;
getCollection: (collectionName: string, slug?: string) => Promise<Map<string, any>[]>;
widgetFor: (name: any, fields?: any, values?: any, fieldsMetaData?: any) => JSX.Element | null;
widgetsFor: (name: any) => any;
getAsset: GetAssetFunction;
Expand Down
2 changes: 1 addition & 1 deletion packages/decap-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function entriesFailed(collection: Collection, error: Error) {
};
}

async function getAllEntries(state: State, collection: Collection) {
export async function getAllEntries(state: State, collection: Collection) {
const backend = currentBackend(state.config);
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
const provider: Backend = integration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getPreviewStyles,
getRemarkPlugins,
} from '../../../lib/registry';
import { getAllEntries, tryLoadEntry } from '../../../actions/entries';
import { ErrorBoundary } from '../../UI';
import {
selectTemplateName,
Expand Down Expand Up @@ -196,6 +197,23 @@ export class PreviewPane extends React.Component {
});
};

/**
* This function exists entirely to expose collections from outside of this entry
*
*/
getCollection = async (collectionName, slug) => {
const { state } = this.props;
const selectedCollection = state.collections.get(collectionName);

if (typeof slug === 'undefined') {
const entries = await getAllEntries(state, selectedCollection);
return entries.map(entry => Map().set('data', entry.data));
}

const entry = await tryLoadEntry(state, selectedCollection, slug);
return Map().set('data', entry.data);
};

render() {
const { entry, collection, config } = this.props;

Expand All @@ -212,6 +230,7 @@ export class PreviewPane extends React.Component {
...this.props,
widgetFor: this.widgetFor,
widgetsFor: this.widgetsFor,
getCollection: this.getCollection,
};

const styleEls = getPreviewStyles().map((style, i) => {
Expand Down Expand Up @@ -261,7 +280,7 @@ PreviewPane.propTypes = {

function mapStateToProps(state) {
const isLoadingAsset = selectIsLoadingAsset(state.medias);
return { isLoadingAsset, config: state.config };
return { isLoadingAsset, config: state.config, state };
demshy marked this conversation as resolved.
Show resolved Hide resolved
}

function mapDispatchToProps(dispatch) {
Expand Down
Loading