From 7ee1c3e8579278bf98efb465ac70f7feb2d30f8d Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 5 Jul 2023 21:08:52 -0700 Subject: [PATCH] Send locale to api with search query --- utils/api.tsx | 3 ++- utils/getLocale.tsx | 14 ++++++++++++++ utils/mentionSuggestions.tsx | 5 ++++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 utils/getLocale.tsx diff --git a/utils/api.tsx b/utils/api.tsx index 7ef3f082..bd55cd0b 100644 --- a/utils/api.tsx +++ b/utils/api.tsx @@ -70,11 +70,12 @@ class Api { }) } - searchAll(query: string) { + searchAll(query: string, locale: string) { const resourceUrl = `${this.url}/search` return axios.post(`${resourceUrl}`, { search: { query: query, + locale: locale } }) } diff --git a/utils/getLocale.tsx b/utils/getLocale.tsx new file mode 100644 index 00000000..00558f2d --- /dev/null +++ b/utils/getLocale.tsx @@ -0,0 +1,14 @@ +import { useEffect, useState } from 'react' +import { useRouter } from 'next/router' + +export default function useLocale() { + const router = useRouter() + + const [locale, setLocale] = useState(router.locale || 'en') + + useEffect(() => { + setLocale(router.locale || 'en') + }, [router]) + + return locale +} diff --git a/utils/mentionSuggestions.tsx b/utils/mentionSuggestions.tsx index 5bcf4672..d9211b51 100644 --- a/utils/mentionSuggestions.tsx +++ b/utils/mentionSuggestions.tsx @@ -2,6 +2,7 @@ import { ReactRenderer } from '@tiptap/react' import { MentionOptions } from '@tiptap/extension-mention' import { SuggestionKeyDownProps, SuggestionProps } from '@tiptap/suggestion' import tippy, { Instance as TippyInstance } from 'tippy.js' +import { getCookie } from 'cookies-next' import { MentionList, @@ -10,6 +11,7 @@ import { } from '~components/MentionList' import api from '~utils/api' import { numberToElement } from '~utils/elements' +import { get } from 'http' interface RawSearchResponse { searchable_type: string @@ -45,7 +47,8 @@ function transform(object: RawSearchResponse) { export const mentionSuggestionOptions: MentionOptions['suggestion'] = { items: async ({ query }): Promise => { - const response = await api.searchAll(query) + const locale = getCookie('NEXT_LOCALE') ?? 'en' + const response = await api.searchAll(query, locale) const results = response.data.results return results