From 8889f3d865d1066d05a1c39bea98d83f7ca1c942 Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Tue, 3 Oct 2023 12:01:38 +0300 Subject: [PATCH 1/2] Fix ecosystem filters. - Search by location not working. - On Empty list, blank page shown --- .../components/EcosystemList/EcosystemList.js | 63 ++++++++++--------- .../data/common/processPageContributors.js | 27 ++++---- .../data/common/processPageOrganisations.js | 25 ++++---- .../src/lib/data/common/processPageTools.js | 44 ++++++++----- 4 files changed, 90 insertions(+), 69 deletions(-) diff --git a/apps/charterafrica/src/components/EcosystemList/EcosystemList.js b/apps/charterafrica/src/components/EcosystemList/EcosystemList.js index 84c0e7f9e..5b09f6fc7 100644 --- a/apps/charterafrica/src/components/EcosystemList/EcosystemList.js +++ b/apps/charterafrica/src/components/EcosystemList/EcosystemList.js @@ -84,9 +84,7 @@ const EcosystemList = React.forwardRef(function EcosystemList(props, ref) { if (loading && listRef.current) { listRef.current.scrollIntoView({ behavior: "smooth" }); } - if (!results.length && !values.search) { - return null; - } + return (
- - {title} - - {loading ? : null} - - {results.map((item) => ( - - + {results.length ? ( + <> + + {title} + + {loading ? : null} + + {results.map((item) => ( + + + + ))} - ))} - - onPageChange(p)} - page={page} - sx={{ - backgroundColor: "common.white", - mt: 5, - pb: 8, - }} - /> + onPageChange(p)} + page={page} + sx={{ + backgroundColor: "common.white", + mt: 5, + pb: 8, + }} + /> + + ) : null}
diff --git a/apps/charterafrica/src/lib/data/common/processPageContributors.js b/apps/charterafrica/src/lib/data/common/processPageContributors.js index 696495864..3eda7a1c5 100644 --- a/apps/charterafrica/src/lib/data/common/processPageContributors.js +++ b/apps/charterafrica/src/lib/data/common/processPageContributors.js @@ -7,22 +7,25 @@ import queryString from "@/charterafrica/utils/ecosystem/queryString"; import formatDateTime from "@/charterafrica/utils/formatDate"; import labelsPerLocale from "@/charterafrica/utils/translationConstants"; -const orQueryBuilder = (fields, search) => { - return fields.map((field) => ({ [field]: { like: search } })); +const queryBuilder = (query) => { + const { search, location } = query; + const fields = ["description", "fullName", "location", "externalId"]; + const where = {}; + if (search) { + where.or = fields.map((field) => ({ [field]: { like: search } })); + } + if (location) { + where.location = { equals: location }; + } + return where; }; export async function getContributors(page, api, context) { const { locale, - query: { page: pageNumber = 1, limit = 12, search, sort = "fullName" } = {}, + query: { page: pageNumber = 1, limit = 12, sort = "fullName" } = {}, } = context; - - const fields = ["description", "fullName", "location", "externalId"]; - const toolQueries = orQueryBuilder(fields, search); - const query = { - or: toolQueries, - }; - + const where = queryBuilder(context.query); const { docs, ...pagination } = await api.getCollection( CONTRIBUTORS_COLLECTION, { @@ -30,9 +33,7 @@ export async function getContributors(page, api, context) { page: parseInt(pageNumber, 10) || 1, limit, sort, - where: { - ...query, - }, + where, }, ); const results = docs.map((person) => { diff --git a/apps/charterafrica/src/lib/data/common/processPageOrganisations.js b/apps/charterafrica/src/lib/data/common/processPageOrganisations.js index 2cc87c3bb..bc6cdec7c 100644 --- a/apps/charterafrica/src/lib/data/common/processPageOrganisations.js +++ b/apps/charterafrica/src/lib/data/common/processPageOrganisations.js @@ -4,8 +4,17 @@ import queryString from "@/charterafrica/utils/ecosystem/queryString"; import formatDateTime from "@/charterafrica/utils/formatDate"; import labelsPerLocale from "@/charterafrica/utils/translationConstants"; -const orQueryBuilder = (fields, search) => { - return fields.map((field) => ({ [field]: { like: search } })); +const queryBuilder = (query) => { + const { search, location } = query; + const fields = ["description", "location", "name", "externalId", "slug"]; + const where = {}; + if (search) { + where.or = fields.map((field) => ({ [field]: { like: search } })); + } + if (location) { + where.location = { equals: location }; + } + return where; }; async function processPageSingleOrganisation(page, api, context) { @@ -65,13 +74,9 @@ async function processPageSingleOrganisation(page, api, context) { export async function getOrganisations(page, api, context) { const { locale, - query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {}, + query: { page: pageNumber = 1, limit = 12, sort = "name" } = {}, } = context; - const fields = ["description", "location", "name", "externalId", "slug"]; - const toolQueries = orQueryBuilder(fields, search); - const query = { - or: toolQueries, - }; + const where = queryBuilder(context.query); const filterLabels = labelsPerLocale[locale]; const { docs, ...pagination } = await api.getCollection( ORGANIZATION_COLLECTION, @@ -80,9 +85,7 @@ export async function getOrganisations(page, api, context) { page: pageNumber, limit, sort, - where: { - ...query, - }, + where, }, ); const results = docs.map((tool) => { diff --git a/apps/charterafrica/src/lib/data/common/processPageTools.js b/apps/charterafrica/src/lib/data/common/processPageTools.js index 5be5680e0..0704b3aa6 100644 --- a/apps/charterafrica/src/lib/data/common/processPageTools.js +++ b/apps/charterafrica/src/lib/data/common/processPageTools.js @@ -7,8 +7,30 @@ import queryString from "@/charterafrica/utils/ecosystem/queryString"; import formatDateTime from "@/charterafrica/utils/formatDate"; import labelsPerLocale from "@/charterafrica/utils/translationConstants"; -const orQueryBuilder = (fields, search) => { - return fields.map((field) => ({ [field]: { like: search } })); +const queryBuilder = (query) => { + const { search, theme, homeCountry } = query; + const where = {}; + if (search) { + const fields = [ + "description", + "theme", + "operatingCountries", + "name", + "id", + "slug", + "homeCountry", + ]; + where.or = fields.map((field) => ({ [field]: { like: search } })); + } + if (homeCountry) { + where.homeCountry = { + equals: homeCountry, + }; + } + if (theme) { + where.theme = { equals: theme }; + } + return where; }; const getRepoLink = (tool) => { @@ -99,27 +121,15 @@ async function processPageSingleTool(page, api, context) { export async function getTools(page, api, context) { const { locale, - query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {}, + query: { page: pageNumber = 1, limit = 12, sort = "name" } = {}, } = context; - const fields = [ - "description", - "theme", - "operatingCountries", - "name", - "id", - "slug", - ]; - const toolQueries = orQueryBuilder(fields, search); - const query = { - or: toolQueries, - }; - + const where = queryBuilder(context.query); const { docs, ...pagination } = await api.getCollection(TOOL_COLLECTION, { locale, page: pageNumber, limit, sort, - where: query, + where, }); const results = docs.map((tool) => { From 11d027fd819885dbec2ff57a7a0981d95acbf952 Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Tue, 3 Oct 2023 12:52:02 +0300 Subject: [PATCH 2/2] Bump charterafrica version 0.1.8 -> 0.1.9 --- apps/charterafrica/contrib/dokku/Dockerfile | 2 +- apps/charterafrica/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/charterafrica/contrib/dokku/Dockerfile b/apps/charterafrica/contrib/dokku/Dockerfile index c6f33abc3..96ea957bc 100644 --- a/apps/charterafrica/contrib/dokku/Dockerfile +++ b/apps/charterafrica/contrib/dokku/Dockerfile @@ -1 +1 @@ -FROM codeforafrica/charterafrica-ui:0.1.8 +FROM codeforafrica/charterafrica-ui:0.1.9 diff --git a/apps/charterafrica/package.json b/apps/charterafrica/package.json index 93b94bdd0..a18844b8c 100644 --- a/apps/charterafrica/package.json +++ b/apps/charterafrica/package.json @@ -1,6 +1,6 @@ { "name": "charterafrica", - "version": "0.1.8", + "version": "0.1.9", "private": true, "author": "Code for Africa ", "description": "This is the official code for https://charter.africa site",