Skip to content

Commit

Permalink
OCP Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MVarshini committed Jul 16, 2024
1 parent 54473f6 commit 0e5824d
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 154 deletions.
62 changes: 49 additions & 13 deletions frontend/src/actions/commonActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as TYPES from "@/actions/types.js";

import { setCatFilters, sliceTableRows } from "./homeActions";
import { setOCPCatFilters, sliceOCPTableRows } from "./ocpActions";

import { DEFAULT_PER_PAGE } from "@/assets/constants/paginationConstants";
import { sliceTableRows } from "./homeActions";
import { cloneDeep } from "lodash";

const getSortableRowValues = (result, tableColumns) => {
const tableKeys = tableColumns.map((item) => item.value);
Expand Down Expand Up @@ -75,7 +76,8 @@ export const calculateMetrics = (results) => {
};

export const buildFilterData = (currState) => (dispatch, getState) => {
const results = [...getState()[currState].results];
const results = [...getState()[currState].filteredResults];
const categoryFilterValue = getState()[currState].categoryFilterValue;

const tableFilters = [...getState()[currState].tableFilters];

Expand All @@ -85,11 +87,19 @@ export const buildFilterData = (currState) => (dispatch, getState) => {
let obj = {
name: filter.name,
key,
value: [...new Set(results.map((item) => item[key]))],
value: [
...new Set(results.map((item) => item[key]?.toString()?.toLowerCase())),
],
};
filterData.push(obj);
}
dispatch(setFilterData(filterData, currState, tableFilters[0].name));
dispatch(
setFilterData(
filterData,
currState,
categoryFilterValue || tableFilters[0].name
)
);
};

const setFilterData = (filterData, currState, activeFilter) => (dispatch) => {
Expand All @@ -99,6 +109,12 @@ const setFilterData = (filterData, currState, activeFilter) => (dispatch) => {
payload: filterData,
});
dispatch(setOCPCatFilters(activeFilter));
} else if (currState === "cpt") {
dispatch({
type: TYPES.SET_CPT_FILTER_DATA,
payload: filterData,
});
dispatch(setCatFilters(activeFilter));
}
};

Expand All @@ -124,15 +140,35 @@ export const getFilteredData = (appliedFilters, results) => {
return filtered;
};

export const getAppliedFilters =
(currState, selectedOption) => (dispatch, getState) => {
const { categoryFilterValue, filterData } = getState()[currState];
const appliedFilters = { ...getState()[currState].appliedFilters };
export const deleteAppliedFilters =
(filterKey, filterValue, currState) => (dispatch, getState) => {
const appliedFilters = cloneDeep(getState()[currState].appliedFilters);

const index = appliedFilters[filterKey].indexOf(
filterValue?.toString()?.toLowerCase()
);
if (index >= 0) {
appliedFilters[filterKey].splice(index, 1);
if (appliedFilters[filterKey].length === 0) {
delete appliedFilters[filterKey];
}
}
return appliedFilters;
};

const category = filterData.filter(
(item) => item.name === categoryFilterValue
)[0].key;
appliedFilters[category] = selectedOption;
export const getSelectedFilter =
(selectedCategory, selectedOption, currState) => (dispatch, getState) => {
const selectedFilters = cloneDeep(getState()[currState].selectedFilters);

return appliedFilters;
const obj = selectedFilters.find((i) => i.name === selectedCategory);
selectedOption = selectedOption?.toString()?.toLowerCase();
const objValue = obj.value.map((i) => i?.toString()?.toLowerCase());

if (objValue.includes(selectedOption)) {
const arr = objValue.filter((selection) => selection !== selectedOption);
obj.value = arr;
} else {
obj.value = [...obj.value, selectedOption];
}
return selectedFilters;
};
89 changes: 27 additions & 62 deletions frontend/src/actions/homeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import {
START_PAGE,
} from "@/assets/constants/paginationConstants";
import { appendDateFilter, appendQueryString } from "@/utils/helper";
import { calculateMetrics, getFilteredData, sortTable } from "./commonActions";
import {
buildFilterData,
calculateMetrics,
deleteAppliedFilters,
getFilteredData,
getSelectedFilter,
sortTable,
} from "./commonActions";

import API from "@/utils/axiosInstance";
import { cloneDeep } from "lodash";
Expand All @@ -21,8 +28,6 @@ export const fetchOCPJobsData = () => async (dispatch, getState) => {
pretty: true,
...(start_date && { start_date }),
...(end_date && { end_date }),
// start_date: "2024-04-21",
// end_date: "2024-04-22",
},
});
if (response?.data?.results?.length > 0) {
Expand All @@ -44,10 +49,7 @@ export const fetchOCPJobsData = () => async (dispatch, getState) => {
});
dispatch(applyFilters());
dispatch(sortTable("cpt"));
dispatch(getCPTSummary());
dispatch(setPageOptions(START_PAGE, DEFAULT_PER_PAGE));
dispatch(sliceTableRows(0, DEFAULT_PER_PAGE));
// dispatch(buildFilterData());
dispatch(tableReCalcValues());
}
} catch (error) {
dispatch(showFailureToast());
Expand All @@ -74,29 +76,6 @@ export const sliceTableRows = (startIdx, endIdx) => (dispatch, getState) => {
});
};

export const buildFilterData = () => (dispatch, getState) => {
const results = [...getState().cpt.filteredResults];

const tableFilters = [...getState().cpt.tableFilters];

const filterData = [];
for (const filter of tableFilters) {
const key = filter.value;
let obj = {
name: filter.name,
key,
value: [...new Set(results.map((item) => item[key]?.toLowerCase()))],
};
filterData.push(obj);
}

dispatch({
type: TYPES.SET_CPT_FILTER_DATA,
payload: filterData,
});
dispatch(setCatFilters(tableFilters[0].name));
};

export const setCatFilters = (category) => (dispatch, getState) => {
const filterData = [...getState().cpt.filterData];
const options = filterData.filter((item) => item.name === category)[0].value;
Expand All @@ -123,19 +102,10 @@ export const setSelectedFilterFromUrl = (params) => (dispatch, getState) => {
});
};
export const setSelectedFilter =
(selectedCategory, selectedOption) => (dispatch, getState) => {
const selectedFilters = cloneDeep(getState().cpt.selectedFilters);

const obj = selectedFilters.find((i) => i.name === selectedCategory);
selectedOption = selectedOption?.toString()?.toLowerCase();
const objValue = obj.value.map((i) => i?.toString()?.toLowerCase());

if (objValue.includes(selectedOption)) {
const arr = objValue.filter((selection) => selection !== selectedOption);
obj.value = arr;
} else {
obj.value = [...obj.value, selectedOption];
}
(selectedCategory, selectedOption) => (dispatch) => {
const selectedFilters = dispatch(
getSelectedFilter(selectedCategory, selectedOption, "cpt")
);
dispatch({
type: TYPES.SET_SELECTED_FILTERS,
payload: selectedFilters,
Expand Down Expand Up @@ -170,25 +140,16 @@ export const setOtherSummaryFilter = () => (dispatch, getState) => {
type: TYPES.SET_FILTERED_DATA,
payload: data,
});
dispatch(getCPTSummary());
dispatch(setPageOptions(START_PAGE, DEFAULT_PER_PAGE));
dispatch(sliceTableRows(0, DEFAULT_PER_PAGE));
dispatch(tableReCalcValues());
};
export const removeAppliedFilters =
(filterKey, filterValue, navigate) => (dispatch, getState) => {
const appliedFilters = cloneDeep(getState().cpt.appliedFilters);
const { start_date, end_date } = getState().cpt;

const index = appliedFilters[filterKey]
?.toString()
?.toLowerCase()
.indexOf(filterValue);
if (index >= 0) {
appliedFilters[filterKey].splice(index, 1);
if (appliedFilters[filterKey].length === 0) {
delete appliedFilters[filterKey];
}
}
const appliedFilters = dispatch(
deleteAppliedFilters(filterKey, filterValue, "cpt")
);

dispatch({
type: TYPES.SET_APPLIED_FILTERS,
payload: appliedFilters,
Expand All @@ -212,12 +173,10 @@ export const applyFilters = () => (dispatch, getState) => {

dispatch({
type: TYPES.SET_FILTERED_DATA,
payload: isFilterApplied ? filtered : results,
payload: filtered,
});
dispatch(getCPTSummary());
dispatch(setPageOptions(START_PAGE, DEFAULT_PER_PAGE));
dispatch(sliceTableRows(0, DEFAULT_PER_PAGE));
dispatch(buildFilterData());
dispatch(tableReCalcValues());
dispatch(buildFilterData("cpt"));
};

export const setFilterFromURL = (searchParams) => ({
Expand Down Expand Up @@ -261,3 +220,9 @@ export const getCPTSummary = () => (dispatch, getState) => {
payload: countObj,
});
};

export const tableReCalcValues = () => (dispatch) => {
dispatch(getCPTSummary());
dispatch(setPageOptions(START_PAGE, DEFAULT_PER_PAGE));
dispatch(sliceTableRows(0, DEFAULT_PER_PAGE));
};
Loading

0 comments on commit 0e5824d

Please sign in to comment.