Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore requireConditions functionality to useDatastore #206

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/services/useDatastore/useDatastore.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const useDatastore = (
resourceId,
rootAPIUrl,
options,
additionalParams = {}
additionalParams = {},
) => {
const keys = options.keys ? options.keys : true;
const { prepareColumns } = options;
Expand All @@ -21,6 +21,7 @@ const useDatastore = (
const [conditions, setConditions] = useState(
options.conditions ? options.conditions : undefined
);
const requireConditions = options.requireConditions;
const [sort, setSort] = useState(options.sort ? options.sort : undefined);
const [groupings, setGroupings] = useState(
options.groupings ? options.groupings : undefined
Expand All @@ -41,16 +42,23 @@ const useDatastore = (
groupings: groupings,
...additionalParams,
}

const additionalParamsString = Object.keys(params).length ? `&${qs.stringify(params)}` : '';
const additionalParamsString = Object.keys(params).length ? `${qs.stringify(params)}` : '';

let enabled = false;
if (id) {
if (!requireConditions)
enabled = true;
if (conditions && conditions.length)
enabled = true;
}

const {data, isPending, error} = useQuery({
queryKey: ["datastore" + id + additionalParamsString],
queryFn: () => {
return fetch(`${rootUrl}/datastore/query/${id}?${additionalParamsString}`)
.then(res => res.json())
},
enabled: id != ""
enabled: enabled
})

useEffect(() => {
Expand All @@ -70,7 +78,7 @@ const useDatastore = (
}, [data])

return {
loading: isPending,
loading: enabled ? isPending : false,
values,
count,
columns,
Expand Down
1 change: 0 additions & 1 deletion src/templates/Dataset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const Dataset = ({
{
...options,
limit: defaultPageSize,
manual: true,
},
additionalParams
) as ResourceType;
Expand Down
1 change: 0 additions & 1 deletion src/templates/FilteredResource/FilteredResourceBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const FilteredResourceBody = ({
{
...options,
limit: 25,
manual: true,
},
additionalParams
);
Expand Down