Skip to content

Commit

Permalink
1896 fetching other collections from preview (#6523)
Browse files Browse the repository at this point in the history
* feat(preview): successfully getting collection by name and slug

* feat(blog): cleanup EditorPreviewPane.js

* feat(preview): new function added to allow previews to fetch data from other collections

* feat(preview): running format and test

* feat(preview): add getCollection to types

Signed-off-by: Wigger Boelens <me@wigger.email>

* feat(preview): fix lint issues

Signed-off-by: Wigger Boelens <me@wigger.email>

---------

Signed-off-by: Wigger Boelens <me@wigger.email>
Co-authored-by: cdennington <charlie@design-id.tech>
Co-authored-by: Wigger Boelens <me@wigger.email>
Co-authored-by: Martin Jagodic <jagodicmartin1@gmail.com>
  • Loading branch information
4 people authored and yuri-g-taboola committed May 22, 2024
1 parent 6266ccc commit 9ae558f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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 };
}

function mapDispatchToProps(dispatch) {
Expand Down

0 comments on commit 9ae558f

Please sign in to comment.