[questions][fix] fix expanded typeaheads (#516)
parent
c10aa15347
commit
6792e20f0f
@ -0,0 +1,22 @@
|
||||
import { trpc } from '../trpc';
|
||||
|
||||
export default function useCompanyOptions(query: string) {
|
||||
const companies = trpc.useQuery([
|
||||
'companies.list',
|
||||
{
|
||||
name: query,
|
||||
},
|
||||
]);
|
||||
|
||||
const { data, ...restQuery } = companies;
|
||||
|
||||
return {
|
||||
data:
|
||||
data?.map(({ id, name }) => ({
|
||||
id,
|
||||
label: name,
|
||||
value: id,
|
||||
})) ?? [],
|
||||
...restQuery,
|
||||
};
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
import type { Country } from '@prisma/client';
|
||||
|
||||
import { trpc } from '../trpc';
|
||||
|
||||
function stringPositionComparator(a: string, b: string, query: string): number {
|
||||
const normalizedQueryString = query.trim().toLocaleLowerCase();
|
||||
const positionA = a.toLocaleLowerCase().indexOf(normalizedQueryString);
|
||||
const positionB = b.toLocaleLowerCase().indexOf(normalizedQueryString);
|
||||
return (
|
||||
(positionA === -1 ? 9999 : positionA) -
|
||||
(positionB === -1 ? 9999 : positionB)
|
||||
);
|
||||
}
|
||||
|
||||
export function useCompareCountry(query: string) {
|
||||
return (a: Country, b: Country) => {
|
||||
const normalizedQueryString = query.trim().toLocaleLowerCase();
|
||||
if (
|
||||
a.code.toLocaleLowerCase() === normalizedQueryString ||
|
||||
b.code.toLocaleLowerCase() === normalizedQueryString
|
||||
) {
|
||||
return stringPositionComparator(a.code, b.code, normalizedQueryString);
|
||||
}
|
||||
|
||||
return stringPositionComparator(a.name, b.name, normalizedQueryString);
|
||||
};
|
||||
}
|
||||
|
||||
export default function useCountryOptions(query: string) {
|
||||
const countries = trpc.useQuery([
|
||||
'locations.countries.list',
|
||||
{
|
||||
name: query,
|
||||
},
|
||||
]);
|
||||
|
||||
const { data, ...restQuery } = countries;
|
||||
|
||||
const compareCountry = useCompareCountry(query);
|
||||
|
||||
const countryOptions = (data ?? [])
|
||||
// Client-side sorting by position of query string appearing
|
||||
// in the country name since we can't do that in Prisma.
|
||||
.sort(compareCountry)
|
||||
.map(({ id, name }) => ({
|
||||
id,
|
||||
label: name,
|
||||
value: id,
|
||||
}));
|
||||
|
||||
return {
|
||||
...restQuery,
|
||||
data: countryOptions,
|
||||
};
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { JobTitleLabels } from '~/components/shared/JobTitles';
|
||||
|
||||
const sortedJobTitleOptions = Object.entries(JobTitleLabels)
|
||||
.map(([slug, { label, ranking }]) => ({
|
||||
id: slug,
|
||||
label,
|
||||
ranking,
|
||||
value: slug,
|
||||
}))
|
||||
.sort((a, b) => b.ranking - a.ranking);
|
||||
|
||||
export default function useJobTitleOptions(query: string) {
|
||||
const jobTitles = sortedJobTitleOptions.filter(({ label }) =>
|
||||
label.toLocaleLowerCase().includes(query.trim().toLocaleLowerCase()),
|
||||
);
|
||||
|
||||
return jobTitles;
|
||||
}
|
Loading…
Reference in new issue