Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

feat: add search functionality in chapterDashboard - #2301

Merged
gikf merged 12 commits into
freeCodeCamp:mainfrom
Sboonny:feat/filter-chapter-through-name
Feb 12, 2023
Merged

gikf merged 12 commits into
freeCodeCamp:mainfrom
Sboonny:feat/filter-chapter-through-name

Conversation

@Sboonny

@Sboonny Sboonny commented Jan 27, 2023

Copy link
Copy Markdown
Member
  • I have read Chapter's contributing guidelines.
  • My pull request has a descriptive title (not a vague title like Update README.md).
  • My pull request targets the main branch of Chapter.

Because of the sort of the data in this PR #2291, I thought maybe a filter function is a due. This should be in Utils later, but for now I am testing it in chapterPages

@Sboonny Sboonny added the UI/UX This issue deals with UI/UX label Jan 27, 2023
@ghost

ghost commented Jan 27, 2023 •

Copy link
Copy Markdown
👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

Comment on lines +104 to +110
const filteredChapter = useMemo(
() =>
data.dashboardChapters.filter(({ name }) =>
name.toLowerCase().includes(search),
),
[data, search],
);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this failing because it relies on a condition? The hooks run on mount, but the data is got on load?

@Sboonny Sboonny Jan 29, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RIP performance, I guess 🤣. maybe I can move the logic into the top and create a function that run it, but idk if that is actually worth it, while this works

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it work it would need to be put before if (isLoading || error) return <DashboardLoading error={error} />;, as that condition sometimes can render before the useMemo is used it will make the hook error appear. It might not be worth to for useMemo here at all, as search will be changing at each character anyway.

@Sboonny
Sboonny marked this pull request as ready for review January 29, 2023 17:06
@Sboonny
Sboonny requested a review from a team January 29, 2023 17:06
@Sboonny Sboonny mentioned this pull request Jan 29, 2023
3 tasks done

@gikf gikf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works nicely 🚀.

There isn't any issue with complexity, but was there some specific reason for not attempting using useState for the filter string, instead of opting for useReducer?

There's couple nitpicks below.

The search field is not very visually separated from the Add new button. It makes it appear on the first look, like both of them are related.
image
Moving filter field to it own line might be the quickest way to clearly give them both their own space:
image
What do you think?

Comment on lines +104 to +110
const filteredChapter = useMemo(
() =>
data.dashboardChapters.filter(({ name }) =>
name.toLowerCase().includes(search),
),
[data, search],
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it work it would need to be put before if (isLoading || error) return <DashboardLoading error={error} />;, as that condition sometimes can render before the useMemo is used it will make the hook error appear. It might not be worth to for useMemo here at all, as search will be changing at each character anyway.

Co-authored-by: gikf <60067306+gikf@users.noreply.github.com>
@Sboonny

Sboonny commented Jan 30, 2023

Copy link
Copy Markdown
Member Author

but was there some specific reason for not attempting using useState for the filter string, instead of opting for useReducer?

I had more experience with setState and I wanted to try something else, but after this, I wanted to try to make it with useState, don't know how to apply dispatch to it, tho, maybe with flushSync https://beta.reactjs.org/reference/react-dom/flushSync 🤔.

Co-authored-by: Krzysztof G. <60067306+gikf@users.noreply.github.com>
@gikf

gikf commented Jan 30, 2023

Copy link
Copy Markdown
Member

I had more experience with setState and I wanted to try something else, but after this, I wanted to try to make it with useState, don't know how to apply dispatch to it, tho, maybe with flushSync https://beta.reactjs.org/reference/react-dom/flushSync thinking.

With useState there wouldn't be really what to dispatch. Set function would replace in onChange dispatch from reducer and that's basically it.

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel bad asking you to replace the reducer, but it's more than we need here.

Sboonny and others added 3 commits January 31, 2023 17:06
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
];

export const ChaptersPage: NextPageWithLayout = () => {
const [filterChapter, setFilterChapter] = useState('');

@Sboonny Sboonny Feb 1, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you know that the type will fail if it's {filterChapter, setFilterChapter}. I have spent a good while on it, trying to use generics, until I gave up and copied hasMore into it.

The failing type is, in hindsight, I should have noticed the array 🤣

[string, Dispatch<setStateAction>]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasMore? The code you've written seems fine as is.

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
@gikf
gikf enabled auto-merge February 12, 2023 20:33
@gikf
gikf dismissed ojeytonwilliams’s stale review February 12, 2023 20:35

Requested change was implemented

@gikf
gikf added this pull request to the merge queue Feb 12, 2023
Merged via the queue into freeCodeCamp:main with commit d65c0e6 Feb 12, 2023
@Sboonny
Sboonny deleted the feat/filter-chapter-through-name branch February 13, 2023 06:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

status: ready for review UI/UX This issue deals with UI/UX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants