Skip to content

Commit

Permalink
fix: change what debounce is calling
Browse files Browse the repository at this point in the history
  • Loading branch information
gosiexon-zen committed Sep 3, 2024
1 parent 20c808d commit b3e99f8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 94 deletions.
85 changes: 44 additions & 41 deletions assets/new-request-form-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 52 additions & 53 deletions src/modules/new-request-form/fields/LookupField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Option,
Message,
} from "@zendeskgarden/react-dropdowns.next";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import type { Field, FieldOption } from "../data-types";
import { Span } from "@zendeskgarden/react-typography";
import SearchIcon from "@zendeskgarden/svg-icons/src/16/search-stroke.svg";
Expand Down Expand Up @@ -91,8 +91,54 @@ export function LookupField({
[customObjectKey]
);

const fetchOptions = async (inputValue: string) => {
const searchParams = new URLSearchParams();
searchParams.set("name", inputValue.toLocaleLowerCase());
searchParams.set("source", "zen:ticket");
searchParams.set("field_id", fieldId.toString());
searchParams.set("requester_id", userId.toString());
if (organizationId !== null)
searchParams.set("organization_id", organizationId);
setIsLoadingOptions(true);
try {
const response = await fetch(
`/api/v2/custom_objects/${customObjectKey}/records/autocomplete?${searchParams.toString()}`
);

const data = await response.json();
if (data !== undefined) {
setOptions(
data.custom_object_records.map(
({ name, id }: { name: string; id: string }) => ({
name,
value: id,
})
)
);
} else {
setOptions([]);
}
} catch (error) {
return error;
} finally {
setIsLoadingOptions(false);
}
return;
};

const debouncedFetchOptions = useCallback(debounce(fetchOptions, 300), [

Check warning on line 129 in src/modules/new-request-form/fields/LookupField.tsx

View workflow job for this annotation

GitHub Actions / Lint JS files

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
customObjectKey,
userId,
organizationId,
fieldId,
]);

useEffect(() => {
return () => debouncedFetchOptions.cancel();
}, [debouncedFetchOptions]);

const handleChange = useCallback<NonNullable<IComboboxProps["onChange"]>>(
async ({ inputValue, selectionValue }) => {
({ inputValue, selectionValue }) => {
if (selectionValue !== undefined) {
if (selectionValue == "") {
setSelectedOption(EMPTY_OPTION);
Expand All @@ -106,61 +152,21 @@ export function LookupField({
if (selectedOption) {
setInputValue(selectedOption.name);
setSelectedOption(selectedOption);
onChange(selectedOption.value);
setOptions([selectedOption]);
onChange(selectedOption.value);
}
}
return;
}

if (inputValue !== undefined) {
setInputValue(inputValue);
const searchParams = new URLSearchParams();
searchParams.set("name", inputValue.toLocaleLowerCase());
searchParams.set("source", "zen:ticket");
searchParams.set("field_id", fieldId.toString());
searchParams.set("requester_id", userId.toString());
if (organizationId !== null)
searchParams.set("organization_id", organizationId);
setIsLoadingOptions(true);
try {
const response = await fetch(
`/api/v2/custom_objects/${customObjectKey}/records/autocomplete?${searchParams.toString()}`
);

const data = await response.json();
if (data !== undefined) {
setOptions(
data.custom_object_records.map(
({ name, id }: { name: string; id: string }) => ({
name,
value: id,
})
)
);
} else {
setOptions([]);
}
} catch (error) {
return error;
} finally {
setIsLoadingOptions(false);
}
debouncedFetchOptions(inputValue);
}
return;
},
[customObjectKey, userId, organizationId, fieldId, onChange, options]
);

const debounceHandleChange = useMemo(
() => debounce(handleChange, 300),
[handleChange]
[debouncedFetchOptions, onChange, options]
);

useEffect(() => {
return () => debounceHandleChange.cancel();
}, [debounceHandleChange]);

useEffect(() => {
if (value) {
fetchSelectedOption(value as string);
Expand All @@ -172,12 +178,6 @@ export function LookupField({
selectedOption && setOptions([selectedOption]);
};

const handleKeyDown = useCallback((event) => {
if (event.key === "Enter") {
event.preventDefault();
}
}, []);

return (
<GardenField>
<Label>
Expand All @@ -199,11 +199,10 @@ export function LookupField({
`Search ${label.toLowerCase()}`
)}
onFocus={onFocus}
onChange={debounceHandleChange}
onChange={handleChange}
renderValue={() =>
selectedOption ? selectedOption?.name : EMPTY_OPTION.name
}
onKeyDown={handleKeyDown}
>
{selectedOption?.name !== EMPTY_OPTION.name && (
<Option value="" label="-">
Expand Down

0 comments on commit b3e99f8

Please sign in to comment.