From 95018d24b8b1e4660b64fc4d9070be5483bae656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Haugsb=C3=B8?= Date: Mon, 15 Apr 2024 18:58:15 +0200 Subject: [PATCH] Fix header bar in events page --- .../components/TranslatedField.js | 61 ++++++++++--------- apps/kvarteret/pages/events/index.tsx | 20 +++++- .../lib/components/TranslatedField.js | 8 ++- 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/apps/blifrivillig/components/TranslatedField.js b/apps/blifrivillig/components/TranslatedField.js index f80a5d4..89868ae 100644 --- a/apps/blifrivillig/components/TranslatedField.js +++ b/apps/blifrivillig/components/TranslatedField.js @@ -1,35 +1,40 @@ -import React, {useContext} from 'react' -import { gql } from '@apollo/client'; +import React, { useContext } from "react"; +import { gql } from "@apollo/client"; const TranslationContext = React.createContext([]); const getTranslationsData = async (client, lang) => { - const { data } = await client.query({ - variables: {lang}, - query: gql` - query TranslationQuery($lang: String) { - texts { - text_id - translations(filter: {languages_code: {url_code: {_eq: $lang}}}) { - text - } - } - } - ` - }); - - return data.texts.reduce((acc, it) => { - acc[it.text_id] = it.translations[0].text - return acc - }, {}) -} + const { data } = await client.query({ + variables: { lang }, + query: gql` + query TranslationQuery($lang: String) { + texts { + text_id + translations( + filter: { languages_code: { url_code: { _eq: $lang } } } + ) { + text + } + } + } + `, + }); -const TranslatedField = ({tKey}) => { - const translations = useContext(TranslationContext); - const item = translations[tKey]; - if(!item) throw "Missing translation with key " + tKey; - return item; -} + return data.texts.reduce((acc, it) => { + acc[it.text_id] = it.translations[0].text; + return acc; + }, {}); +}; + +const TranslatedField = ({ tKey }) => { + const translations = useContext(TranslationContext); + const item = translations[tKey]; + + if (!item) { + console.error("Missing translation for key " + tKey); + } + return item; +}; export default TranslatedField; -export { getTranslationsData, TranslationContext }; \ No newline at end of file +export { getTranslationsData, TranslationContext }; diff --git a/apps/kvarteret/pages/events/index.tsx b/apps/kvarteret/pages/events/index.tsx index ba563f8..6d6b51e 100644 --- a/apps/kvarteret/pages/events/index.tsx +++ b/apps/kvarteret/pages/events/index.tsx @@ -1,7 +1,11 @@ import { useCallback, useEffect, useState } from "react"; import axios from "axios"; import { BlurImage } from "dak-components"; -import TranslatedField from "dak-components/lib/components/TranslatedField"; +import TranslatedField, { + getTranslationsData, +} from "dak-components/lib/components/TranslatedField"; +import fetchLayoutData from "dak-components/lib/cms/layout"; +import { GetStaticProps } from "next"; type Event = { slug: string; @@ -209,4 +213,18 @@ const EventSearch = () => { ); }; +/** + * Need to have getStaticProps in each page to fetch translations and layout data + */ +export const getStaticProps: GetStaticProps = async (context) => { + const layout = await fetchLayoutData(context.locale); + + return { + props: { + translations: await getTranslationsData(context.locale, []), + layout: layout, + }, + }; +}; + export default EventSearch; diff --git a/packages/dak-components/lib/components/TranslatedField.js b/packages/dak-components/lib/components/TranslatedField.js index f69e7e4..881d6df 100644 --- a/packages/dak-components/lib/components/TranslatedField.js +++ b/packages/dak-components/lib/components/TranslatedField.js @@ -46,11 +46,13 @@ const useTranslation = (key) => { key + " useContext(TranslationContext) is undefined" ); - return key; // default to render key if translations are not loaded + return key ?? null; // default to render key if translations are not loaded } const item = translations[key]; - if (!item) throw "Missing translation with key " + key; - return item; + if (!item) { + console.error("Missing translation for key " + key); + } + return item ?? null; }; const TranslatedField = ({ tKey }) => {