Skip to content

Commit

Permalink
Merge pull request #663 from cultuurnet/bugfix/III-5432
Browse files Browse the repository at this point in the history
  • Loading branch information
Anahkiasen authored Mar 20, 2023
2 parents aed71b7 + 34c7867 commit 0a431eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/hooks/api/places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type GetPlacesByQueryArguments = {
name: string;
terms: Array<Values<typeof EventTypes>>;
zip?: string;
addressLocality?: string;
addressCountry?: Country;
};

Expand All @@ -125,16 +126,18 @@ const getPlacesByQuery = async ({
name,
terms,
zip,
addressLocality,
addressCountry,
}: Headers & GetPlacesByQueryArguments) => {
const termsString = terms.reduce(
(acc, currentTerm) => `${acc}terms.id:${currentTerm}`,
'',
);
const postalCodeString = zip ? `address.\\*postalCode:${zip}` : '';
const queryArguments = [termsString, postalCodeString].filter(
(argument) => !!argument,
);
const queryArguments = [
termsString,
zip ? `address.\\*postalCode:${zip}` : '',
addressLocality ? `address.\\*.addressLocality:${addressLocality}` : '',
].filter((argument) => !!argument);

const res = await fetchFromApi({
path: '/places/',
Expand Down Expand Up @@ -164,7 +167,13 @@ const getPlacesByQuery = async ({
};

const useGetPlacesByQuery = (
{ name, terms, zip, addressCountry }: GetPlacesByQueryArguments,
{
name,
terms,
zip,
addressLocality,
addressCountry,
}: GetPlacesByQueryArguments,
configuration = {},
) =>
useAuthenticatedQuery<Place[]>({
Expand All @@ -175,6 +184,7 @@ const useGetPlacesByQuery = (
terms,
zip,
addressCountry,
addressLocality,
},
enabled: !!name || terms.length,
...configuration,
Expand Down
1 change: 1 addition & 0 deletions src/pages/steps/PlaceStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const PlaceStep = ({
name: searchInput,
terms,
zip: municipality?.zip,
addressLocality: municipality?.name,
addressCountry: country,
},
{ enabled: !!searchInput },
Expand Down
7 changes: 6 additions & 1 deletion src/utils/fetchFromApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ const fetchFromApi = async ({

try {
url = new URL(`${publicRuntimeConfig.apiUrl}${path}`);
url.search = new URLSearchParams(omit(searchParams, 'queryKey')).toString();
searchParams = omit(searchParams, 'queryKey');
if (typeof searchParams?.q !== 'undefined' && !searchParams?.q) {
searchParams = omit(searchParams, 'q');
}

url.search = new URLSearchParams(searchParams).toString();
} catch (e) {
if (!silentError) {
throw new FetchError(400, e?.message ?? 'Unknown error');
Expand Down

0 comments on commit 0a431eb

Please sign in to comment.