Skip to content

Commit

Permalink
Tag projects
Browse files Browse the repository at this point in the history
  • Loading branch information
koechkevin committed Sep 26, 2023
1 parent dea5c15 commit b842dbf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/lib/data/blockify/ourWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 14 additions & 11 deletions apps/codeforafrica/src/lib/data/utils/projects.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions apps/codeforafrica/src/payload/collections/Projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const Projects = {
name: "subtitle",
}),
tags({
name: "tag",
hasMany: false,
admin: {
position: "sidebar",
},
Expand Down

0 comments on commit b842dbf

Please sign in to comment.