Skip to content

Instantly share code, notes, and snippets.

@akkikumar72
Last active May 29, 2020 10:08
Show Gist options
  • Save akkikumar72/53a86fa5e320f11adf26241ccda88c7a to your computer and use it in GitHub Desktop.
Save akkikumar72/53a86fa5e320f11adf26241ccda88c7a to your computer and use it in GitHub Desktop.
import { useCallback, useState } from 'react';
import { useSelector } from 'react-redux';
import { ApplicationStateType } from '../../reducers';
import { filterTestJobsByProject, getTestJobsForSelectedProject } from '../../utils/testJobUtils';
export const useTreeMenuClicked = () => {
const testJobsLibrary = useSelector((state: ApplicationStateType) => state.testJobsLibrary);
const { testProjects, testJobs } = testJobsLibrary;
const testJobsAll = JSON.parse(JSON.stringify(testJobs));
const [testJobFiltered, setTestJobFiltered] = useState(false);
const handleTreeMenuClicked: (filterBy: string) => void = useCallback(
(filterBy) => {
if (filterBy === 'all') {
testJobsLibrary.testJobs = testJobsAll;
setTestJobFiltered(true);
} else if (filterBy === 'orphaned') {
const { orphanedTestJobs } = filterTestJobsByProject(testProjects, testJobs);
testJobsLibrary.testJobs = orphanedTestJobs;
setTestJobFiltered(true);
} else if (filterBy === 'projects') {
const { testJobsWithParent } = filterTestJobsByProject(testProjects, testJobs);
testJobsLibrary.testJobs = testJobsWithParent;
setTestJobFiltered(true);
} else {
testJobsLibrary.testJobs = getTestJobsForSelectedProject(testProjects, filterBy, testJobs);
setTestJobFiltered(true);
}
},
[testJobsLibrary]
);
return {
handleTreeMenuClicked,
setTestJobFiltered,
testJobFiltered
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment