1 | import React from "react";
|
2 | import config from "config";
|
3 | import _ from "lodash";
|
4 | import paths from "./paths";
|
5 |
|
6 | const BodyContent = (page, allPages) => ({ location }) =>
|
7 | render({
|
8 | section: getSection(page, location.pathname, allPages),
|
9 | config,
|
10 | page,
|
11 | location,
|
12 | });
|
13 |
|
14 | function getSection(page, pathname, allPages) {
|
15 | const sectionName = page.sectionName;
|
16 | const section = config.paths[sectionName || "/"] || config.paths["/"] || {};
|
17 |
|
18 |
|
19 |
|
20 | if (!section.name) {
|
21 | section.name = sectionName;
|
22 | }
|
23 |
|
24 |
|
25 | section.all = () => getAllSectionPages(allPages);
|
26 |
|
27 |
|
28 | section.pages = name =>
|
29 | getSectionPages(config, name || sectionName, allPages);
|
30 |
|
31 | return section;
|
32 | }
|
33 |
|
34 | function getAllSectionPages(allPages) {
|
35 | return _.map(config.paths, (path, name) => ({
|
36 | url: name,
|
37 | path,
|
38 | pages: getSectionPages(config, name, allPages),
|
39 | }));
|
40 | }
|
41 |
|
42 | function getSectionPages(config, name, allPages) {
|
43 | return _.filter(
|
44 | paths.getSectionPages(config, name, allPages),
|
45 | p => p.type === "page"
|
46 | );
|
47 | }
|
48 |
|
49 | function 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 |
|
59 | if (props.config.layout) {
|
60 | return React.createFactory(props.config.layout())(props, content);
|
61 | }
|
62 |
|
63 | return content;
|
64 | }
|
65 |
|
66 | export default BodyContent;
|