From ce0cb74c4c367e01f3075d5aa87ac1e1b7ffe69a Mon Sep 17 00:00:00 2001 From: Nebulis Date: Wed, 19 May 2021 00:59:22 +0800 Subject: [PATCH] feat: add filters in article page --- src/components/core/select.tsx | 5 ++- src/pages/articles.tsx | 73 +++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/src/components/core/select.tsx b/src/components/core/select.tsx index 187b30f6..63122bbb 100644 --- a/src/components/core/select.tsx +++ b/src/components/core/select.tsx @@ -8,7 +8,8 @@ export const Select: React.FunctionComponent<{ options: OptionTypeBase[] onChange: (value: string | undefined) => void placeholder: string -}> = ({ options, onChange, placeholder }) => { + value?: OptionTypeBase +}> = ({ options, onChange, placeholder, value }) => { return ( ({ @@ -58,6 +60,7 @@ export const Select: React.FunctionComponent<{ placeholder={placeholder} onChange={(element) => (element ? onChange(element.value) : onChange(undefined))} options={options} + se /> ) } diff --git a/src/pages/articles.tsx b/src/pages/articles.tsx index 519dd8ea..f8a7a491 100644 --- a/src/pages/articles.tsx +++ b/src/pages/articles.tsx @@ -1,9 +1,9 @@ -import React, { useContext, useState } from "react" +import React, { useContext } from "react" import SEO from "../components/layout/seo" import queryString from "query-string" -import { PageProps } from "gatsby" +import { navigate, PageProps } from "gatsby" import { BlogLayoutWithDrawer } from "../components/layout/main-layout" -import { getArticles } from "../components/core/links/links.utils" +import { getArticles, sortByLabel } from "../components/core/links/links.utils" import { ApplicationContext } from "../components/application" import { ArticlesContainer } from "../components/layout/layout" import { MainTitleSection } from "../components/core/section" @@ -13,31 +13,84 @@ import i18n from "i18next" import translationFr from "../locales/fr/articles.json" import translationEn from "../locales/en/articles.json" import { isKind, Kind } from "../components/core/links/links.types" +import { Select } from "../components/core/select" const namespace = "articles" i18n.addResourceBundle("fr", namespace, translationFr) i18n.addResourceBundle("en", namespace, translationEn) const IndexPage: React.FunctionComponent = ({ location }) => { const query = queryString.parse(location.search) - const [search] = useState<{ continent: string; country: string; kind: Kind | "none" }>({ - continent: typeof query.continent === "string" ? query.continent : "", - country: typeof query.country === "string" ? query.country : "", - kind: isKind(query.kind) ? query.kind : "none", - }) + const search: { continent?: string; country?: string; kind?: Kind } = { + continent: typeof query.continent === "string" ? query.continent : undefined, + country: typeof query.country === "string" ? query.country : undefined, + kind: isKind(query.kind) ? query.kind : undefined, + } const { development } = useContext(ApplicationContext) - const { t } = useCustomTranslation([namespace, "common"]) + const { i18n, t } = useCustomTranslation([namespace, "common"]) const title = t("title") + const tags: string[] = [search.country, search.continent].filter(Boolean) as string[] const articles = getArticles({ development, - tags: [search.country, search.continent].filter(Boolean), kind: search.kind, + filter: (cachedLink) => (tags.length > 0 ? tags.every((t) => cachedLink.tags.includes(t)) : true), + }) + const countries = getArticles({ + kind: "country", + card: false, + development, + }) + .sort(sortByLabel(i18n.languageCode)) + .map((country) => { + return { value: country.id, label: t(`common:country.${country.id}.title`) } + }) + const continents = getArticles({ + kind: "continent", + card: false, + development, }) + .sort(sortByLabel(i18n.languageCode)) + .map((continent) => { + return { value: continent.id, label: t(`common:continent.${continent.id}`) } + }) + const kinds = [ + { value: "highlight", label: "Tous nos récits de voyage" }, + { value: "other", label: "Tous nos conseils / partages" }, + ] + const navigateTo = (data: any) => { + const newSearch = { ...search, ...data } + console.log({ newSearch }) + navigate(`${location.pathname}?${queryString.stringify(newSearch)}`) + } return ( <> {title} + { + navigateTo({ continent: value }) + }} + value={continents.find((continent) => continent.value === search.continent)} + options={continents} + /> +