quotes-nextjs

My favourite quotes. (nextjs)
git clone http://git.hanabi.in/repos/quotes-nextjs.git
Log | Files | Refs | LICENSE

index.js (2670B)


      1 import CommonHead from "../../../components/CommonHead.js";
      2 import Footer from "../../../components/Footer.js";
      3 import Header from "../../../components/Header.js";
      4 import HeadTitle from "../../../components/HeadTitle.js";
      5 import UnorderedList from "../../../components/UnorderedList.js";
      6 
      7 function Author({ author, cover, quotes }) {
      8   const quoteCount = quotes.length;
      9   return (
     10     <div className="site">
     11       <CommonHead cover={cover} />
     12       <HeadTitle title={author} ogtitle={author} />
     13       <Header
     14         pageTitle={`Author: ${author} (${quoteCount})`}
     15         titleLink="/authors"
     16       />
     17       <main id="main" className="main">
     18         <UnorderedList arr={quotes} />
     19       </main>
     20       <Footer />
     21     </div>
     22   );
     23 }
     24 
     25 export async function getStaticPaths() {
     26   const { readdir } = require("fs/promises");
     27   const { join } = require("path");
     28 
     29   const {
     30     errHandler,
     31     getDetailsANDQuotes,
     32     readFrontMatter,
     33   } = require("../../../helperFns.js");
     34 
     35   try {
     36     const paths = [];
     37     const allAuthors = await readdir("quotes");
     38 
     39     for (const author of allAuthors) {
     40       const allFiles = await readdir(join("quotes", author));
     41       const { quotes } = getDetailsANDQuotes(allFiles);
     42       if (quotes.length < 1) throw new Error(`Not enough quotes`);
     43 
     44       for (const quote of quotes) {
     45         const { data } = await readFrontMatter(author, quote);
     46 
     47         const { draft } = data;
     48         if (draft) continue;
     49         paths.push({ params: { author } });
     50         break;
     51       }
     52     }
     53     return {
     54       paths,
     55       fallback: false,
     56     };
     57   } catch (err) {
     58     errHandler(err);
     59   }
     60 }
     61 
     62 export async function getStaticProps({ params: { author } }) {
     63   const { readdir } = require("fs/promises");
     64   const { join } = require("path");
     65 
     66   const {
     67     createQuoteObj,
     68     errHandler,
     69     getCover,
     70     getDetailsANDQuotes,
     71     readFrontMatter,
     72   } = require("../../../helperFns.js");
     73 
     74   try {
     75     const allFiles = await readdir(join("quotes", author));
     76     const { details, quotes } = getDetailsANDQuotes(allFiles);
     77 
     78     if (!details) throw new Error(`Error finding details`);
     79     if (quotes.length < 1) throw new Error(`Not enough quotes`);
     80 
     81     const quotesArr = [];
     82     for (const quote of quotes) {
     83       const { data } = await readFrontMatter(author, quote);
     84       const { draft } = data;
     85       if (draft) continue;
     86 
     87       const quoteObj = createQuoteObj(author, data, quote);
     88       quotesArr.push(quoteObj);
     89     }
     90 
     91     const cover = await getCover(author);
     92 
     93     quotesArr.sort((a, b) => b.date.localeCompare(a.date));
     94     return { props: { author, cover, quotes: quotesArr } };
     95   } catch (err) {
     96     errHandler(err);
     97   }
     98 }
     99 
    100 export default Author;