UNPKG

1.65 kBJavaScriptView Raw
1import React from "react";
2import config from "config"; // Aliased through webpack
3import _ from "lodash";
4import paths from "./paths";
5
6const BodyContent = (page, allPages) => ({ location }) =>
7 render({
8 section: getSection(page, location.pathname, allPages),
9 config,
10 page,
11 location,
12 });
13
14function getSection(page, pathname, allPages) {
15 const sectionName = page.sectionName;
16 const section = config.paths[sectionName || "/"] || config.paths["/"] || {};
17
18 // Extra check as read-only properties cannot be set. Better to wrap
19 // in a function instead?
20 if (!section.name) {
21 section.name = sectionName;
22 }
23
24 // Get all pages of all sections
25 section.all = () => getAllSectionPages(allPages);
26
27 // Get pages of the current section or the named one
28 section.pages = name =>
29 getSectionPages(config, name || sectionName, allPages);
30
31 return section;
32}
33
34function getAllSectionPages(allPages) {
35 return _.map(config.paths, (path, name) => ({
36 url: name,
37 path,
38 pages: getSectionPages(config, name, allPages),
39 }));
40}
41
42function getSectionPages(config, name, allPages) {
43 return _.filter(
44 paths.getSectionPages(config, name, allPages),
45 p => p.type === "page"
46 );
47}
48
49function render(props) {
50 let content;
51
52 if (props.page.layout) {
53 content = React.createFactory(props.page.layout)(props);
54 } else {
55 console.error("Trying to render a page with an unknown type", props);
56 }
57
58 // XXX: Refactor config level layout out of the system?
59 if (props.config.layout) {
60 return React.createFactory(props.config.layout())(props, content);
61 }
62
63 return content;
64}
65
66export default BodyContent;