Skip to content

Commit

Permalink
Fix header bar in events page
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiash98 committed Apr 15, 2024
1 parent b610f54 commit 95018d2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
61 changes: 33 additions & 28 deletions apps/blifrivillig/components/TranslatedField.js
Original file line number Diff line number Diff line change
@@ -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 };
export { getTranslationsData, TranslationContext };
20 changes: 19 additions & 1 deletion apps/kvarteret/pages/events/index.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -209,4 +213,18 @@ const EventSearch = () => {
);
};

/**
* Need to have getStaticProps in each page to fetch translations and layout data
*/
export const getStaticProps: GetStaticProps<any> = async (context) => {
const layout = await fetchLayoutData(context.locale);

return {
props: {
translations: await getTranslationsData(context.locale, []),
layout: layout,
},
};
};

export default EventSearch;
8 changes: 5 additions & 3 deletions packages/dak-components/lib/components/TranslatedField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down

0 comments on commit 95018d2

Please sign in to comment.