commit 803a9bc662a1ae3d94bc9519217e506f9cc206e7
parent 6bb9962f2f1ba8d59c9aa2fe4ca5abbead32491b
Author: Agastya Chandrakant <me@hanabi.in>
Date: Sun, 18 Apr 2021 18:06:39 +0530
add option to search using tags
Diffstat:
4 files changed, 94 insertions(+), 2 deletions(-)
diff --git a/generateRandom.js b/generateRandom.js
@@ -63,13 +63,14 @@ function getDetailsANDQuotes(allFiles) {
}
function createQuoteObj(author, data, quote) {
- const { attributed, date, misattributed, title, unverified } = data;
+ const { attributed, date, misattributed, tags, title, unverified } = data;
const uri = makeUri(quote);
return {
attributed,
author,
date,
misattributed,
+ tags,
title,
unverified,
uri,
diff --git a/helperFns.js b/helperFns.js
@@ -83,3 +83,56 @@ export function createQuoteObj(author, data, quote) {
uri,
};
}
+
+function extractInfoFrom(router) {
+ const { q } = router.query;
+ let [and, or] = [false, false];
+ if (q.includes(",")) and = true;
+ if (q.includes("|")) or = true;
+ if (and == or) router.replace("/400");
+ const tags = !!and ? q.split(",") : q.split("|");
+ return { and, or, tags };
+}
+
+function getAndQuotes(tags) {
+ const quotes = require("./random-quotes.js");
+ const filteredQuoted = [];
+ for (let i = 0; i < quotes.length; i++) {
+ const quote = quotes[i];
+ let reject = false;
+ for (let j = 0; j < tags.length; j++) {
+ const tag = tags[j];
+ if (!quote.tags.includes(tag)) {
+ reject = true;
+ break;
+ }
+ }
+ if (!reject) filteredQuoted.push(quote);
+ }
+ return filteredQuoted;
+}
+
+function getOrQuotes(tags) {
+ const quotes = require("./random-quotes.js");
+ const filteredQuoted = [];
+ for (let i = 0; i < quotes.length; i++) {
+ const quote = quotes[i];
+ let accept = false;
+ for (let j = 0; j < tags.length; j++) {
+ const tag = tags[j];
+ if (quote.tags.includes(tag)) {
+ accept = true;
+ break;
+ }
+ }
+ if (accept) filteredQuoted.push(quote);
+ }
+ return filteredQuoted;
+}
+
+export function chooseQuotes(router) {
+ const { and, tags } = extractInfoFrom(router);
+ const quotes = !!and ? getAndQuotes(tags) : getOrQuotes(tags);
+ if (quotes.length == 0) router.replace("/404");
+ return quotes;
+}
diff --git a/package.json b/package.json
@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
- "build": "next build && node generateRandom.js",
+ "build": "node generateRandom.js && next build",
"start": "next start",
"prestart": "node generateRandom.js"
},
diff --git a/pages/tags/choose.js b/pages/tags/choose.js
@@ -0,0 +1,38 @@
+import { useRouter } from "next/router";
+
+import CommonHead from "../../components/CommonHead.js";
+import Footer from "../../components/Footer.js";
+import Header from "../../components/Header.js";
+import HeadTitle from "../../components/HeadTitle.js";
+import UnorderedList from "../../components/UnorderedList.js";
+
+import { site } from "../../config.js";
+
+function Tags() {
+ const router = useRouter();
+ const { chooseQuotes } = require("../../helperFns.js");
+
+ if (router.isReady) {
+ const quotes = chooseQuotes(router);
+ return (
+ <div className="site">
+ <CommonHead />
+ <HeadTitle title="Filtered quotes" ogtitle="Filtered quotes" />
+ <Header pageTitle={`${site.title} (${quotes.length})`} />
+ <main id="main" className="main">
+ <div className="home-sections-container">
+ <div className="home-sections">
+ <section id="recent-posts" className="home-section">
+ <UnorderedList arr={quotes} />
+ </section>
+ </div>
+ </div>
+ </main>
+ <Footer />
+ </div>
+ );
+ }
+ return <div>Loading...</div>;
+}
+
+export default Tags;