Adding filter capacities to api client and server

This commit is contained in:
2025-01-20 16:08:42 +01:00
parent 35448385c5
commit 0a4bec62c1
7 changed files with 32 additions and 36 deletions

View File

@@ -10,6 +10,6 @@ export default function CrudTextWidget<T = any, S extends StrictRJSFSchema = RJS
if (props.schema.hasOwnProperty("foreign_key")) {
return (<ForeignKeyWidget {...props} />);
}else {
return (<CrudTextWidget {...props} />);
return (<TextWidget {...props} />);
}
}
}

View File

@@ -11,38 +11,24 @@ import {useList} from "@refinedev/core";
export const ForeignKeyWidget = (props: WidgetProps) => {
const [inputValue, setInputValue] = useState("");
const [selectedValue, setSelectedValue] = useState<string | null>(null);
const [debouncedInputValue, setDebouncedInputValue] = useState(inputValue);
const resource = props.schema.foreign_key.reference.resource
useEffect(() => {
const handler = setTimeout(() => setDebouncedInputValue(inputValue), 300); // Adjust debounce delay as needed
return () => clearTimeout(handler);
}, [inputValue]);
const { data, isLoading } = useList({
resource: resource,
pagination: { current: 1, pageSize: 10 },
filters: [{ field: "name", operator: "contains", value: debouncedInputValue }],
sorters: [{ field: "name", order: "asc" }],
filters: [{ field: "name", operator: "contains", value: "input" }],
});
const options = data?.data || [];
// const fetchOptions = async (input: string) => {
// try {
//
// } catch (error) {
// console.error("Error fetching options:", error);
// }
// };
// // Debounced version of the fetch function
// const debouncedFetch = useCallback(debounce(fetchOptions, 300), []);
// // Trigger fetch whenever the inputValue changes
// useEffect(() => {
// if (inputValue) {
// debouncedFetch(inputValue);
// } else {
// setOptions([]); // Clear options when input is empty
// }
// }, [inputValue, debouncedFetch]);
return (
<Autocomplete
value={selectedValue}

View File

@@ -55,9 +55,8 @@ export const dataProvider: DataProvider = {
if (filters && filters.length > 0) {
filters.forEach((filter) => {
if ("field" in filter && filter.operator === "eq") {
// Our fake API supports "eq" operator by simply appending the field name and value to the query string.
params.append(filter.field, filter.value);
if ("field" in filter && filter.value.length > 0 && filter.operator === "contains") {
params.append(filter.field + "__like", "%" + filter.value + "%");
}
});
}