Skip to content

Commit

Permalink
fix: recherche sur les contenus lié contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Zeinstra committed Jul 19, 2023
1 parent 5e23454 commit 0a278ce
Showing 1 changed file with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import { Result } from "./ReferenceInput";

export const contributionSearchCdtnReferencesSearch = `
query SearchCdtnReferences($query: String, $sources: [String!]) {
documents(where: {title: {_ilike: $query}, is_available: {_eq: true}, is_published: {_eq: true}, source: {_in: $sources}}, limit: 10) {
documents(where: {
_or: [
{slug: {_ilike: $query}},
{title: {_ilike: $query}}
],
is_available: {_eq: true},
is_published: {_eq: true},
source: {_in: $sources}},
limit: 10
) {
title
cdtnId: cdtn_id
source
Expand All @@ -21,10 +30,36 @@ type QueryResult = {
export const useContributionSearchCdtnReferencesQuery = (
query: string | undefined
): Result<Pick<CdtnReference, "document">> => {
const querySplit = query?.split(/[ -,]/);
const slugQuery = querySplit?.map(
(query) => `{slug: {_ilike: "%${query.normalize()}%"}}`
).join(`,
`);
const titleQuery = querySplit?.map(
(query) => `{title: {_ilike: "%${query.normalize()}%"}}`
).join(`,
`);
const [{ data, fetching, error }] = useQuery<QueryResult>({
query: contributionSearchCdtnReferencesSearch,
query: `
query SearchCdtnReferences($sources: [String!]) {
documents(where: {
_or: [
{_and: [${slugQuery}]}
{_and: [${titleQuery}]}
],
is_available: {_eq: true},
is_published: {_eq: true},
source: {_in: $sources}},
limit: 10
) {
title
cdtnId: cdtn_id
source
slug
}
}
`,
variables: {
query: `%${query}%`,
sources: [
"dossiers",
"external",
Expand Down

0 comments on commit 0a278ce

Please sign in to comment.