Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate FAQ page into the Typesense backend #401

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9ef8171
init faq typesense
ekamuktia Jul 25, 2021
67b7b95
Merge remote-tracking branch 'origin/main' into feature/typesense-faq
ekamuktia Jul 25, 2021
94b56ec
Merge branch 'main' into feature/typesense-faq
ekamuktia Jul 25, 2021
0f12a6a
change FaqData to Faq
ekamuktia Jul 25, 2021
a817d3d
Merge branch 'feature/typesense-faq' of https://github.com/ekamuktia/…
ekamuktia Jul 25, 2021
a81eacc
filter to modal
ekamuktia Jul 25, 2021
5894fbf
change router to window
ekamuktia Jul 25, 2021
9283a2e
filter params type
ekamuktia Jul 25, 2021
b32bd9c
remove modal
ekamuktia Jul 25, 2021
cb2a1da
Merge branch 'main' into feature/typesense-faq
ekamuktia Jul 25, 2021
81034e4
change to usetouter
ekamuktia Jul 25, 2021
c7c61b2
Merge branch 'feature/typesense-faq' of https://github.com/ekamuktia/…
ekamuktia Jul 25, 2021
fb896f7
usefiltermodal as config, console log for debug
ekamuktia Jul 25, 2021
25a7800
usefiltermodal as optional
ekamuktia Jul 25, 2021
7dda93a
usefiltermodal false
ekamuktia Jul 25, 2021
f5a0a0f
Merge remote-tracking branch 'origin/main' into feature/typesense-faq
ekamuktia Jul 26, 2021
ebf6efb
Merge branch 'main' of https://github.com/ekamuktia/wargabantuwarga.c…
ekamuktia Jul 27, 2021
aaac2f1
fix: refinement list select option value
ekamuktia Jul 27, 2021
6f35e72
fix: remove console log
ekamuktia Jul 27, 2021
de04d15
Merge branch 'main' of https://github.com/ekamuktia/wargabantuwarga.c…
ekamuktia Jul 28, 2021
96cb318
Merge branch 'main' into feature/typesense-faq
ekamuktia Jul 28, 2021
2ab64b7
Merge branch 'main' into feature/typesense-faq
ekamuktia Jul 28, 2021
69aebe7
Merge branch 'main' of https://github.com/ekamuktia/wargabantuwarga.c…
ekamuktia Aug 8, 2021
6a761e2
Merge branch 'feature/typesense-faq' of https://github.com/ekamuktia/…
ekamuktia Aug 8, 2021
c719fb6
Merge branch 'main' into feature/typesense-faq
mazipan Aug 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions @types/typesense-instantsearch-adapter/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
declare module "typesense-instantsearch-adapter" {
type SearchClient = object;

export interface TypesenseNode {
host: string;
port: string;
protocol: string;
}

export interface TypesenseSearchParameters {
queryBy: string;
sortBy?: string;
highlightFullFields?: string;
}

export interface TypesenseServer {
apiKey: string;
nodes: TypesenseNode[];
}

export interface TypesenseInstantsearchAdapterOptions {
server?: TypesenseServer;
additionalSearchParameters: TypesenseSearchParameters;
}

export default class TypesenseInstantsearchAdapter {
readonly searchClient: SearchClient;

constructor(options: TypesenseInstantsearchAdapterOptions);
}
}
30 changes: 10 additions & 20 deletions __tests__/pages/faq.test.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import React from "react";

import { faqBuilder } from "~/lib/__mocks__/builders/faq";
import faqSheets from "~/lib/faqs";
// import { faqBuilder } from "~/lib/__mocks__/builders/faq";
// import faqSheets from "~/lib/faqs";
import FaqPage from "../../pages/faq";

import FaqPage, { getStaticProps } from "../../pages/faq";

import { perBuild } from "@jackfranklin/test-data-bot";
// import { perBuild } from "@jackfranklin/test-data-bot";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code should not be pushed.

import { render, screen } from "@testing-library/react";

describe("FaqPage", () => {
const faq = faqBuilder();
const faqs = [faq];

it("renders the title correctly", () => {
render(<FaqPage faqSheets={faqs} />);
render(<FaqPage />);

expect(screen.getByText(/pertanyaan yang sering ditanyakan/i))
.toMatchInlineSnapshot(`
Expand All @@ -25,15 +21,16 @@ describe("FaqPage", () => {
`);
});

/*
it("renders the questions and answers correctly", () => {
render(<FaqPage faqSheets={faqs} />);
render(<FaqPage />);

expect(screen.getByText(faq.pertanyaan)).toBeVisible();
expect(screen.getByText(faq.jawaban)).toBeVisible();
});

it("renders the links correctly", () => {
render(<FaqPage faqSheets={faqs} />);
render(<FaqPage />);

const link = screen.getByText(faq.sumber as string);

Expand All @@ -49,18 +46,11 @@ describe("FaqPage", () => {
},
});

render(<FaqPage faqSheets={[faqWithoutSourceLink]} />);
render(<FaqPage />);

expect(
screen.getByText(`Sumber: ${faqWithoutSourceLink.sumber}`),
).toBeVisible();
});
});

describe("getStaticProps", () => {
it("returns the props from the faq-sheets correctly", () => {
expect(getStaticProps({})).toEqual({
props: { faqSheets },
});
});
*/
});
82 changes: 82 additions & 0 deletions components/faq-list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Faq } from "~/lib/faqs";

import htmr from "htmr";

type FaqListProps = {
data: Faq[];
};

function groupBy<T, U>(data: T[], key: U) {
return data.reduce((acc: any, currentValue: any) => {
const groupKey = currentValue[key];
if (!acc[groupKey]) {
acc[groupKey] = [];
}
acc[groupKey].push(currentValue);
return acc;
}, {});
}

export function FAQList(props: FaqListProps) {
const listFaqs = groupBy(props.data, "kategori_pertanyaan");
return (
<div className="space-y-4">
{Object.keys(listFaqs as Record<string, unknown>).map(
(category: string) => (
<div
key={category}
className="p-4 bg-white shadow overflow-hidden rounded-md"
>
<div className="relative">
<div
aria-hidden="true"
className="absolute inset-0 flex items-center"
>
<div className="w-full border-t border-gray-300" />
</div>
<div className="relative flex flex-row items-center justify-start">
<span className="pr-3 bg-white text-lg font-medium text-gray-900">
{category}
</span>
</div>
</div>
<dl className="divide-y divide-gray-200">
{listFaqs[category].map((question: Faq) => (
<div
key={question.pertanyaan}
className="pt-6 pb-8 md:grid md:grid-cols-12 md:gap-8"
>
<dt className="text-base font-semibold text-gray-900 md:col-span-5">
{question.pertanyaan}
</dt>
<dd className="mt-2 md:mt-0 md:col-span-7">
<p className="text-base text-gray-500">
{htmr(
question.jawaban.replace(/(?:\r\n|\r|\n)/g, "<br>"),
)}
</p>
<small>
Sumber:{" "}
{question.link ? (
<a
className="underline text-blue-800"
href={question.link}
rel="noreferrer"
target="_blank"
>
{question.sumber}
</a>
) : (
question.sumber
)}
</small>
</dd>
</div>
))}
</dl>
</div>
),
)}
</div>
);
}
19 changes: 19 additions & 0 deletions components/search/custom-hits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FAQList } from "~/components/faq-list";
import { Faq } from "~/lib/faqs";

import { StateResultsProvided } from "react-instantsearch-core";
import { connectStateResults } from "react-instantsearch-dom";

function Hits(stateResults: StateResultsProvided) {
const { searchResults } = stateResults;
let results: Faq[];

try {
results = searchResults.hits as unknown as Faq[];
} catch (e) {
results = [];
}
return <FAQList data={results} />;
}

export default connectStateResults(Hits);
Loading