Skip to content

Commit

Permalink
Add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinkipruto committed Sep 25, 2023
1 parent 0053758 commit 4ed2d70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
17 changes: 10 additions & 7 deletions apps/codeforafrica/src/components/Articles/Articles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRouter } from "next/router";
import React, { useState, useEffect } from "react";
import React, { useEffect, useState } from "react";

import useArticles from "./useArticles";

Expand All @@ -23,16 +23,19 @@ const Articles = React.forwardRef(function Articles(props, ref) {
const [articles, setArticles] = useState(articlesList);
const [count, setCount] = useState(countProp);
const [page, setPage] = useState(pageProp);
const [q, setQ] = useState();
const [filtering, setFiltering] = useState(false);
const [tag, setTag] = useState(ALL_TAG);
const queryParams = useFilterQuery({ page, tag });
const queryParams = useFilterQuery({ page, q, tag });
const router = useRouter();

const handleChangePage = (_, value) => {
setPage(value);
};

// TODO: Kelvin Handle filtering a
const handleChangeQ = () => {};
const handleChangeQ = (_, value) => {
setQ(value || undefined);
};

const handleChangeTag = (_, value) => {
const newValue =
Expand All @@ -42,11 +45,11 @@ const Articles = React.forwardRef(function Articles(props, ref) {
};

useEffect(() => {
const isFiltering = page !== 1 || !equalsIgnoreCase(tag, ALL_TAG);
const isFiltering = page !== 1 || q || !equalsIgnoreCase(tag, ALL_TAG);
setFiltering(isFiltering);
}, [page, tag]);
}, [page, q, tag]);

const { data } = useArticles({ page, tag });
const { data } = useArticles({ page, q, tag });
useEffect(() => {
if (data) {
const { stories: results, pagination } = data;
Expand Down
26 changes: 25 additions & 1 deletion apps/codeforafrica/src/lib/data/utils/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,37 @@ export function formatStory(story) {
}

export async function getStories(api, params) {
const { page: queryPage = 1, tag, where = {}, ...other } = params;
const { page: queryPage = 1, tag, q, where = {}, ...other } = params;
const options = {
limit: 9,
page: queryPage,
where: {
...where,
...(tag && { "tags.name": { like: tag } }),
...(q && {
or: [
{
title: {
contains: q,
},
},
{
"tags.name": {
contains: q,
},
},
{
"excerpt.children.text": {
contains: q,
},
},
{
"content.richTextBlockFields.content.children.text": {
like: q,
},
},
],
}),
},
...other,
};
Expand Down

0 comments on commit 4ed2d70

Please sign in to comment.