From b842dbf929c15b1f9171eb45f65b24438d68ba55 Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Tue, 26 Sep 2023 11:48:42 +0300 Subject: [PATCH] Tag projects --- .../src/lib/data/blockify/ourWork.js | 3 +++ .../src/lib/data/utils/projects.js | 25 +++++++++++-------- .../src/payload/collections/Projects.js | 2 ++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/apps/codeforafrica/src/lib/data/blockify/ourWork.js b/apps/codeforafrica/src/lib/data/blockify/ourWork.js index 7bfa2ea2c..56860cdff 100644 --- a/apps/codeforafrica/src/lib/data/blockify/ourWork.js +++ b/apps/codeforafrica/src/lib/data/blockify/ourWork.js @@ -3,8 +3,11 @@ import { getProjects } from "@/codeforafrica/lib/data/utils/projects"; async function ourWork(block, api, context) { const { query } = context; const data = await getProjects(api, query); + const { docs } = await api.getCollection("tag"); + const tags = docs.map(({ name }) => name); return { + tags, ...block, ...data, slug: block.blockType, diff --git a/apps/codeforafrica/src/lib/data/utils/projects.js b/apps/codeforafrica/src/lib/data/utils/projects.js index 8ec2a36e1..9cb4f8887 100644 --- a/apps/codeforafrica/src/lib/data/utils/projects.js +++ b/apps/codeforafrica/src/lib/data/utils/projects.js @@ -1,20 +1,23 @@ -const orQueryBuilder = (fields, search) => { - if (!search) { - return []; +function getQuery(fields, query) { + const { q, tag } = query; + const whereQuery = { + or: q ? fields.map((field) => ({ [field]: { like: q } })) : [], + }; + if (tag) { + whereQuery["tag.name"] = { + like: tag, + }; } - return fields.map((field) => ({ [field]: { like: search } })); -}; + return whereQuery; +} export async function getProjects(api, params) { - const { page: queryPage = 1, q } = params; - const fields = ["name", "title", "tags", "tagLine"]; - const orQuery = orQueryBuilder(fields, q); + const { page: queryPage = 1 } = params; + const fields = ["name", "title", "tag.name", "tagLine"]; const options = { limit: 6, page: queryPage, - where: { - or: orQuery, - }, + where: getQuery(fields, params), }; const { docs: results, diff --git a/apps/codeforafrica/src/payload/collections/Projects.js b/apps/codeforafrica/src/payload/collections/Projects.js index 2eb969f65..a1ad4aa63 100644 --- a/apps/codeforafrica/src/payload/collections/Projects.js +++ b/apps/codeforafrica/src/payload/collections/Projects.js @@ -38,6 +38,8 @@ const Projects = { name: "subtitle", }), tags({ + name: "tag", + hasMany: false, admin: { position: "sidebar", },