Skip to content

Commit

Permalink
791: improved handling of all checkbox between searches
Browse files Browse the repository at this point in the history
  • Loading branch information
myapos committed Mar 15, 2024
1 parent 112130f commit 6909c25
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const AddProjectModal = ({ popUp, handlePopUpToggle, handlePopUpClose }:
<FormControl label="Projects" isError={Boolean(error)} errorText={error?.message}>
<ProjectsTable
projects={[...filteredProjects]}
checkedProjects={field.value}
preservedCheckedProjects={field.value}
setCheckedProjects={(newValue) => field.onChange(newValue)}
searchValue={searchValue}
setSearchValue={setSearchValue}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEventHandler, FC } from "react";
import { ChangeEventHandler, FC, useMemo } from "react";
import { faProjectDiagram } from "@fortawesome/free-solid-svg-icons";

import {
Expand All @@ -25,11 +25,20 @@ import SearchProjects from "./SearchProjects";

const ProjectsTable: FC<ProjectsTableProps> = ({
projects,
checkedProjects,
preservedCheckedProjects,
setCheckedProjects,
searchValue,
setSearchValue
}) => {
const checkedProjects = useMemo(() => {
const newState: CheckedProjectsMap = {};
projects.forEach((project) => {
newState[project.id] = preservedCheckedProjects[project.id];
});
newState[CheckboxKeys.ALL] = preservedCheckedProjects[CheckboxKeys.ALL];
return newState;
}, [preservedCheckedProjects]);

const onSearch: ChangeEventHandler<HTMLInputElement> = (e) => {
const { value } = e.target;

Expand Down Expand Up @@ -107,7 +116,7 @@ const ProjectsTable: FC<ProjectsTableProps> = ({
</Table>
{projects?.length === 0 && <EmptyState title="No projects found" icon={faProjectDiagram} />}
</TableContainer>
<NumOfCheckedProjects checkedProjects={checkedProjects} />
<NumOfCheckedProjects checkedProjects={preservedCheckedProjects} />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CheckboxKeys, CheckedProjectsMap } from "../types";

const monitorCheckAll = ({ state }: { state: CheckedProjectsMap }): CheckedProjectsMap => {
const newState = { ...state };

// check if all projects are enabled to check all checkbox programmatically
const { [CheckboxKeys.ALL]: all, ...projects } = newState;
const numOfProjects = Object.keys(projects).length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type CheckedProjectsMap = Record<string, boolean>;

export type ProjectsTableProps = {
projects: ProjectProps[];
checkedProjects: CheckedProjectsMap;
preservedCheckedProjects: CheckedProjectsMap;
setCheckedProjects: (value: CheckedProjectsMap) => void;
searchValue: string;
setSearchValue: (value: string) => void;
Expand Down

0 comments on commit 6909c25

Please sign in to comment.