From fb02d5204832eb0d580ebb2ca95f193f1fb21ecb Mon Sep 17 00:00:00 2001 From: Matheus Kuster Date: Tue, 10 Nov 2020 10:58:52 -0300 Subject: [PATCH] Refactor Filter & Sort options to a generic component --- pages/search.tsx | 188 ++++++++++++++++++++++++++++++----------------- 1 file changed, 120 insertions(+), 68 deletions(-) diff --git a/pages/search.tsx b/pages/search.tsx index 4caa82da5..7d14f21d4 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -1,4 +1,4 @@ -import {useState} from 'react' +import { FC, useState } from 'react' import cn from 'classnames' import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' import Link from 'next/link' @@ -21,6 +21,18 @@ import { useSearchMeta, } from '@lib/search' +interface OptionLinkProps { + active: boolean + name: string + pathname: string + query: string +} + +interface FilterOptionsProps { + pathname: string + title: string +} + export async function getStaticProps({ preview, locale, @@ -70,6 +82,43 @@ export default function Search({ sort: typeof sort === 'string' ? sort : '', }) + const OptionLink: FC = ({ + active, + name, + pathname, + query + }) => ( +
  • + + {name} + +
  • + ) + + const FilterOptions: FC = ({ + pathname, + title, + children, + }) => ( + + ) + const Sort = () => (
    • Sort
    • @@ -83,70 +132,13 @@ export default function Search({ {SORT.map(([key, text]) => ( -
    • - - {text} - -
    • - ))} -
    - ) - - const Categories = () => ( - - ) - - const Designers = () => ( -
      -
    • - - All Designers - -
    • - {brands.flatMap(({ node }) => ( -
    • - - {node.name} - -
    • + pathname={pathname} + active={sort === key} + name={text} + query={filterQuery({ q, sort: key })} + /> ))}
    ) @@ -155,8 +147,38 @@ export default function Search({
    - - + + <> + {categories.map((cat) => ( + + ))} + + + + <> + {brands.flatMap(({ node }) => ( + + ))} + +
    {(q || activeCategory || activeBrand) && ( @@ -214,8 +236,38 @@ export default function Search({ {showMobileMenu && (
    - - + + <> + {categories.map((cat) => ( + + ))} + + + + <> + {brands.flatMap(({ node }) => ( + + ))} + +