cbillowes/curious-programmer-oxygen

View on GitHub
src/layouts/index.jsx

Summary

Maintainability
A
2 hrs
Test Coverage
import React from "react"
import Helmet from "react-helmet"
import Footer from "../components/Footer/Component"
import config from "../../data/SiteConfig"
import "./index.css"
require("../prism/prism-okaidia.css")

export default class MainLayout extends React.Component {
  getLocalTitle() {
    function capitalize(string) {
      return string.charAt(0).toUpperCase() + string.slice(1)
    }
    const pathPrefix = config.pathPrefix ? config.pathPrefix : "/"
    const currentPath = this.props.location.pathname
      .replace(pathPrefix, "")
      .replace("/", "")
    let title = ""
    if (currentPath === "") {
      title = "Home"
    } else if (currentPath === "tags/") {
      title = "Tags"
    } else if (currentPath === "categories/") {
      title = "Categories"
    } else if (currentPath === "about/") {
      title = "About"
    } else if (currentPath.indexOf("posts")) {
      title = "Article"
    } else if (currentPath.indexOf("tags/")) {
      const tag = currentPath
        .replace("tags/", "")
        .replace("/", "")
        .replace("-", " ")
      title = `Tagged in ${capitalize(tag)}`
    } else if (currentPath.indexOf("categories/")) {
      const category = currentPath
        .replace("categories/", "")
        .replace("/", "")
        .replace("-", " ")
      title = `${capitalize(category)}`
    }
    return title
  }
  render() {
    const { children } = this.props
    return (
      <div>
        <Helmet>
          <title>{`${config.siteTitle} |  ${this.getLocalTitle()}`}</title>
          <meta name="description" content={config.siteDescription} />
        </Helmet>
        {children()}
        <Footer config={config} />
      </div>
    )
  }
}