-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julien Bouquillon
committed
Oct 3, 2018
1 parent
94acf60
commit 8eb9bcd
Showing
7 changed files
with
277 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from "react"; | ||
import { withRouter } from "next/router"; | ||
import Head from "next/head"; | ||
import fetch from "isomorphic-unfetch"; | ||
import { Container, Alert } from "@socialgouv/code-du-travail-ui"; | ||
|
||
import Search from "../src/search/Search"; | ||
import Answer from "../src/search/Answer"; | ||
import api from "../conf/api.js"; | ||
|
||
const BigError = ({ children }) => ( | ||
<Container style={{ fontSize: "2em", textAlign: "center", margin: "20%" }}> | ||
<Alert warning>{children}</Alert> | ||
</Container> | ||
); | ||
|
||
class Question extends React.Component { | ||
static async getInitialProps({ res, query }) { | ||
console.log("getInitialProps", query); | ||
return await fetch(`${api.BASE_URL}/items/faq/${query.slug}`) | ||
.then(r => r.json()) | ||
.then(data => ({ | ||
data | ||
})) | ||
.catch(e => { | ||
console.log("e", e); | ||
res.statusCode = 404; | ||
throw e; | ||
}); | ||
} | ||
|
||
render() { | ||
const { data } = this.props; | ||
return ( | ||
<React.Fragment> | ||
<Head> | ||
<title>{data._source.title}</title> | ||
</Head> | ||
<Search /> | ||
{!data && <BigError>Cette question n'a pas été trouvée</BigError>} | ||
{data && ( | ||
<Answer | ||
title={data._source.title} | ||
html={data._source.text} | ||
footer="FAQ" | ||
/> | ||
)} | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default withRouter(Question); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React from "react"; | ||
import { withRouter } from "next/router"; | ||
import Head from "next/head"; | ||
import { BreadCrumbs, Container, Alert } from "@socialgouv/code-du-travail-ui"; | ||
|
||
import find from "unist-util-find"; | ||
import parents from "unist-util-parents"; | ||
|
||
import { Link } from "../routes"; | ||
import Search from "../src/search/Search"; | ||
import Categories from "../src/Categories"; | ||
import themes from "../src/data/themes2"; | ||
|
||
const BigError = ({ children }) => ( | ||
<div | ||
style={{ | ||
fontSize: "1.3em", | ||
textAlign: "center", | ||
margin: "40px auto", | ||
background: "var(--color-light-background)" | ||
}} | ||
> | ||
<Alert warning>{children}</Alert> | ||
<br /> | ||
<br /> | ||
</div> | ||
); | ||
|
||
// check if node definition match given slug | ||
const slugMatch = (node, slug) => | ||
Array.isArray(node.slug) ? node.slug.join("/") === slug : node.slug === slug; | ||
|
||
// build list of parents data for the breadcrumbs | ||
const getParents = node => { | ||
const p = []; | ||
if (node.type !== "root") { | ||
p.push(node); | ||
} | ||
let cur = node && node.parent; | ||
while (cur) { | ||
p.push(cur); | ||
cur = cur.parent; | ||
} | ||
return p.reverse(); | ||
}; | ||
|
||
// return breadcrumbs components | ||
const getBreadcrumbs = parents => | ||
(parents && | ||
parents.map( | ||
(parent, i) => | ||
i === 0 ? ( | ||
<Link key={parent.title} route="themes"> | ||
<a title={parent.title}>{parent.title}</a> | ||
</Link> | ||
) : i === parents.length - 1 ? ( | ||
<span title={parent.title}>{parent.title}</span> | ||
) : ( | ||
<Link | ||
key={parent.title} | ||
route="theme" | ||
params={{ slug: parent.slug || ["/"] }} | ||
> | ||
<a title={parent.title}>{parent.title}</a> | ||
</Link> | ||
) | ||
)) || | ||
[]; | ||
|
||
// Theme page | ||
class Theme extends React.Component { | ||
static async getInitialProps({ res, query }) { | ||
// build a unist tree from themes.json | ||
const themeTree = parents({ | ||
type: "root", | ||
title: "Thèmes", | ||
children: themes | ||
}); | ||
// get current theme | ||
const theme = | ||
find(themeTree, n => slugMatch(n, query.slug || "/")) || themeTree; | ||
|
||
// get theme parents for breadcrumbs | ||
const themeParents = getParents(theme); | ||
|
||
if (!theme && res) { | ||
res.statusCode = 404; | ||
} | ||
return { theme, parents: themeParents }; | ||
} | ||
|
||
render() { | ||
const { theme, parents } = this.props; | ||
const breadCrumbs = getBreadcrumbs(parents); | ||
return ( | ||
<React.Fragment> | ||
<Head> | ||
<title>{theme && theme.title}</title> | ||
</Head> | ||
<Search /> | ||
<Container> | ||
{!theme && <BigError>Ce thème n'a pas été trouvé</BigError>} | ||
{(breadCrumbs && | ||
breadCrumbs.length && ( | ||
<h2 style={{ textAlign: "center", margin: 20 }}> | ||
<BreadCrumbs entries={breadCrumbs} /> | ||
</h2> | ||
)) || ( | ||
<h2 style={{ textAlign: "center", margin: 20 }}> | ||
Choisissez un thème : | ||
</h2> | ||
)} | ||
{(theme && | ||
theme.children && | ||
theme.children.length && ( | ||
<Categories title={null} themes={theme.children} /> | ||
)) || ( | ||
<BigError> | ||
Aucun contenu actuellement disponible sur ce thème :/ | ||
</BigError> | ||
)} | ||
</Container> | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default withRouter(Theme); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from "react"; | ||
|
||
import { Section } from "@socialgouv/code-du-travail-ui"; | ||
|
||
const Answer = ({ title, html, footer }) => ( | ||
<Section light> | ||
<header> | ||
<h2>{title}</h2> | ||
</header> | ||
<div dangerouslySetInnerHTML={{ __html: html }} /> | ||
<footer> | ||
<p>{footer}</p> | ||
</footer> | ||
</Section> | ||
); | ||
|
||
export default Answer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.