Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
feat(hn): adds mapper to map Drupal content types to components
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Wensink committed Aug 14, 2017
1 parent 5acda1e commit 69a22db
Show file tree
Hide file tree
Showing 3 changed files with 1,130 additions and 367 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"author": "Bart Langelaan",
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.25.0"
"babel-runtime": "^6.25.0",
"get-nested": "^4.0.0",
"prop-types": "^15.5.10"
},
"nyc": {
"extension": [
Expand All @@ -36,13 +38,14 @@
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"hn": "^0.1.1",
"next": "beta",
"hn": "^0.2.0",
"next": "^2.4.9",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"semantic-release": "^6.3.6"
},
"scripts": {
"start": "npm run build -- -w",
"build": "babel src -d lib",
"test:next": "cd tests/functional/next && npm run build && npm start",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
Expand Down
51 changes: 49 additions & 2 deletions src/DrupalPage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component } from 'react';
import React, { Component } from 'react';
import { parse } from 'url';
import PropTypes from 'prop-types';
import getNested from 'get-nested';
import site from './site';

export default class extends Component {
static async getInitialProps ({ asPath }) {
static async getInitialProps({ asPath }) {
const location = parse(asPath, true);
const page = await site.getPage(asPath, true);

Expand All @@ -12,4 +14,49 @@ export default class extends Component {
page
};
}

static propTypes = {
layout: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]),
mapper: PropTypes.oneOfType([
PropTypes.shape(),
PropTypes.func,
]).isRequired,
};

static defaultProps = {
layout: 'div',
};

render() {
const mapper = this.props.mapper;

const Layout = this.props.layout;

const page = site.getData(this.props.page);

if(typeof getNested(() => page.type.target_id) !== 'string') {
return null;
}

const ContentType = (typeof mapper === 'object') ? mapper[page.type.target_id] : mapper(page.type.target_id);

if(!ContentType) {
console.error('Component for content type', page.type.target_id, 'not found.');
return null;
}


return (
<Layout
page={page}
location={this.props.location}
history={this.props.history}
>
<ContentType page={page} />
</Layout>
);
}
}
Loading

0 comments on commit 69a22db

Please sign in to comment.