From 0a278ce27723239f45dda4393076cc7a3f1130d7 Mon Sep 17 00:00:00 2001 From: Victor Zeinstra Date: Wed, 19 Jul 2023 11:49:44 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20recherche=20sur=20les=20contenus=20li?= =?UTF-8?q?=C3=A9=20contributions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../references/cdtnReferencesSearch.query.ts | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/targets/frontend/src/components/contributions/answers/references/cdtnReferencesSearch.query.ts b/targets/frontend/src/components/contributions/answers/references/cdtnReferencesSearch.query.ts index acacf341c..9e2f58ced 100644 --- a/targets/frontend/src/components/contributions/answers/references/cdtnReferencesSearch.query.ts +++ b/targets/frontend/src/components/contributions/answers/references/cdtnReferencesSearch.query.ts @@ -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 @@ -21,10 +30,36 @@ type QueryResult = { export const useContributionSearchCdtnReferencesQuery = ( query: string | undefined ): Result> => { + 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({ - 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",