[portal][feat] improve country typeahead ranking

pull/519/head
Yangshun Tay 3 years ago
parent 30c68afed6
commit ac215dcbff

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Country" ADD COLUMN "ranking" INTEGER DEFAULT 0;

@ -110,6 +110,8 @@ model Country {
id String @id id String @id
name String @unique name String @unique
code String @unique code String @unique
// The higher the value of the ranking, the higher it appears in the search results.
ranking Int? @default(0)
states State[] states State[]
questionsQuestionEncounters QuestionsQuestionEncounter[] questionsQuestionEncounters QuestionsQuestionEncounter[]
ResumesResume ResumesResume[] ResumesResume ResumesResume[]

@ -22,6 +22,7 @@ type Props = BaseProps &
value?: TypeaheadOption | null; value?: TypeaheadOption | null;
}>; }>;
// TODO: Merge with CountriesTypeahead instead.
export default function ResumeLocationTypeahead({ export default function ResumeLocationTypeahead({
onSelect, onSelect,
selectedValues = new Set(), selectedValues = new Set(),
@ -42,18 +43,24 @@ export default function ResumeLocationTypeahead({
return []; return [];
} }
return data return (
.map(({ id, name }) => ({ data
id, // Client-side sorting by position of query string appearing
label: name, // in the country name since we can't do that in Prisma.
value: id, .sort((a, b) => a.name.indexOf(query) - b.name.indexOf(query))
})) .map(({ id, name }) => ({
.filter((option) => !selectedValues.has(option.value)); id,
}, [countries, selectedValues]); label: name,
value: id,
}))
.filter((option) => !selectedValues.has(option.value))
);
}, [countries, query, selectedValues]);
return ( return (
<Typeahead <Typeahead
label="Location" label="Location"
minQueryLength={2}
noResultsMessage="No location found" noResultsMessage="No location found"
nullable={true} nullable={true}
options={options} options={options}

@ -39,16 +39,22 @@ export default function CountriesTypeahead({
return ( return (
<Typeahead <Typeahead
label="Country" label="Country"
minQueryLength={3} minQueryLength={2}
noResultsMessage="No countries found" noResultsMessage="No countries found"
nullable={true} nullable={true}
options={ options={(data ?? [])
data?.map(({ id, name }) => ({ // Client-side sorting by position of query string appearing
// in the country name since we can't do that in Prisma.
.sort(
(a, b) =>
a.name.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()) -
b.name.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()),
)
.map(({ id, name }) => ({
id, id,
label: name, label: name,
value: id, value: id,
})) ?? [] }))}
}
value={value} value={value}
onQueryChange={setQuery} onQueryChange={setQuery}
onSelect={onSelect} onSelect={onSelect}

@ -45,14 +45,24 @@ export const locationsRouter = createRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
return await ctx.prisma.country.findMany({ return await ctx.prisma.country.findMany({
orderBy: { orderBy: {
name: 'asc', ranking: 'desc',
}, },
take: 10, take: 10,
where: { where: {
name: { OR: [
contains: input.name, {
mode: 'insensitive', name: {
}, contains: input.name,
mode: 'insensitive',
},
},
{
code: {
contains: input.name,
mode: 'insensitive',
},
},
],
}, },
}); });
}, },

Loading…
Cancel
Save