Skip to content

Commit

Permalink
Merge pull request #814 from cultuurnet/feature/III-5846
Browse files Browse the repository at this point in the history
III-5846 - use debounced value for getOrganizersByWebsiteQuery
  • Loading branch information
brampauwelyn authored Oct 18, 2023
2 parents 079bb89 + de95c5e commit ba3f4e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/hooks/useDebounce.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect, useState } from 'react';

// see https://github.com/tannerlinsley/react-query/issues/293
// see https://usehooks.com/useDebounce/
const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
};

export { useDebounce };
21 changes: 13 additions & 8 deletions src/pages/organizers/create/steps/UrlStep.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useRouter } from 'next/router';
import { FormEvent, useEffect, useMemo, useState } from 'react';
import { FormEvent, useEffect, useState } from 'react';
import { Controller, useWatch } from 'react-hook-form';
import { Trans, useTranslation } from 'react-i18next';

import { useGetOrganizersByWebsiteQuery } from '@/hooks/api/organizers';
import { useDebounce } from '@/hooks/useDebounce';
import { SupportedLanguage } from '@/i18n/index';
import { StepProps } from '@/pages/steps/Steps';
import { Organizer } from '@/types/Organizer';
Expand Down Expand Up @@ -32,16 +33,20 @@ const UrlStep = ({
const { query } = useRouter();
const { t, i18n } = useTranslation();

const [searchInput, setSearchInput] = useState('');

const [watchedUrl] = useWatch({
control,
name: ['nameAndUrl.url'],
});

const debouncedSearchInput = useDebounce(searchInput, 275);

const getOrganizersByWebsiteQuery = useGetOrganizersByWebsiteQuery(
{
website: watchedUrl,
website: debouncedSearchInput,
},
{ enabled: !!watchedUrl && isValidUrl(watchedUrl) },
{ enabled: !!debouncedSearchInput && isValidUrl(debouncedSearchInput) },
);

const existingOrganizer: Organizer | undefined =
Expand All @@ -51,17 +56,15 @@ const UrlStep = ({
const isUrlAlreadyTaken = errors.nameAndUrl?.url?.type === 'not_unique';

useEffect(() => {
if (!isValidUrl(watchedUrl)) {
if (!isValidUrl(searchInput)) {
setError('nameAndUrl.url', { type: 'matches' });
return;
}

clearErrors('nameAndUrl.url');
}, [watchedUrl, clearErrors, setError]);
}, [searchInput, clearErrors, setError]);

useEffect(() => {
if (!isValidUrl) return;

if (
existingOrganizer &&
parseOfferId(existingOrganizer['@id']) !== query.organizerId
Expand Down Expand Up @@ -94,9 +97,11 @@ const UrlStep = ({
<Input
value={field.value?.url}
onChange={(event) => {
const value = (event.target as HTMLInputElement).value;
setSearchInput(value);
field.onChange({
...field.value,
url: (event.target as HTMLInputElement).value,
url: value,
});
}}
onBlur={(event: FormEvent<HTMLInputElement>) => {
Expand Down

0 comments on commit ba3f4e2

Please sign in to comment.