Skip to content

Commit

Permalink
Merge pull request #269 from chrishiguto/feature/add-search-depends-on
Browse files Browse the repository at this point in the history
feat: add searchDependsOn to validate when the customSearch can be di…
  • Loading branch information
andreneto97 authored Dec 4, 2024
2 parents 30bdfc7 + 6c3580a commit 3b8dc10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Props = {
orderableListCacheKey?: string;
cacheApiPath?: string;
complementaryActions?: ReactNode;
searchDependsOn?: string[];
};

const FilterSubmodule = (props: Props) => {
Expand All @@ -64,9 +65,16 @@ const FilterSubmodule = (props: Props) => {
customSearch,
} = useCrudRoot();

const canMakeRequest =
!props.searchDependsOn?.length ||
props.searchDependsOn?.every(
(dependency) =>
!filters?.find((filter) => filter.id === dependency)?.isLoading,
);

const customSearchData = useMemo(
() => customSearch?.(filterValues),
[filterValues],
[filterValues, canMakeRequest],
);

const externalSearch = useMemo(
Expand Down Expand Up @@ -105,7 +113,7 @@ const FilterSubmodule = (props: Props) => {
}, {});

useEffect(() => {
if (!hasExternalSearch) {
if (!hasExternalSearch && canMakeRequest) {
updateSearch(null);
const filterObj = {
...reduceFilters(filterValues, 'simpleFilter'),
Expand All @@ -115,7 +123,7 @@ const FilterSubmodule = (props: Props) => {
updateSimpleFilter(filterObj, true);
}

if (hasExternalSearch) {
if (hasExternalSearch && canMakeRequest) {
const filterObj = {
...reduceFilters(filterValues, 'search'),
...customFilter?.(filterValues),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface TableSubmoduleProps {
addButtonContent?: ReactNode;
additionalFilterRowContent?: ReactNode;
additionalTableContent?: ReactNode;
searchDependsOn?: string[];
}

const TableSubmodule = (props: TableSubmoduleProps) => {
Expand Down Expand Up @@ -279,6 +280,7 @@ const TableSubmodule = (props: TableSubmoduleProps) => {
<FilterSubmodule
orderableListCacheKey={props.filterCacheKey}
cacheApiPath={props.cacheApiPath}
searchDependsOn={props.searchDependsOn}
complementaryActions={
<Box sx={{ display: 'flex' }}>
{props.reordable !== false && (
Expand Down
2 changes: 2 additions & 0 deletions packages/react-material-ui/src/modules/crud/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface ModuleProps {
addButtonEndIcon?: ReactNode;
addButtonContent?: ReactNode;
additionalFilterRowContent?: ReactNode;
searchDependsOn?: string[];
}

const CrudModule = (props: ModuleProps) => {
Expand Down Expand Up @@ -320,6 +321,7 @@ const CrudModule = (props: ModuleProps) => {
addButtonEndIcon={props.addButtonEndIcon}
addButtonContent={props.addButtonContent}
additionalFilterRowContent={props.additionalFilterRowContent}
searchDependsOn={props.searchDependsOn}
{...useTableReturn}
{...tableSubmoduleProps}
/>
Expand Down

0 comments on commit 3b8dc10

Please sign in to comment.