Created
July 19, 2020 13:40
-
-
Save javierguzman/35c214a771c7e39f8eb00f601504b6f7 to your computer and use it in GitHub Desktop.
Example of terms and conditions in different languages using markdown and dynamic import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from 'react'; | |
import i18next from 'i18next'; | |
import { getCurrentLanguage } from '@Utils/language'; | |
import ReactMarkdown from 'react-markdown'; | |
import Loader from '@Loader'; | |
type MarkdownContent = string | undefined; | |
const TermsAndConditions = (): JSX.Element => { | |
const currentLanguage = getCurrentLanguage(i18next.languages); // it returns i18next.languages[0] -> 'es', 'fr', 'en' etc. | |
const [getTermsAndConditions, setTermsAndConditions] = useState<MarkdownContent>(undefined); | |
const [isLoading, setIsLoading] = useState(true); | |
useEffect(() => { | |
const grabTermsAndConditions = async (): Promise<void> => { | |
const termsAndConditionsFile = await import( | |
`./markdown/TermsAndConditions.${currentLanguage}.md` | |
); | |
const termsAndConditionsContent = await fetch(termsAndConditionsFile.default); | |
const text = await termsAndConditionsContent.text(); | |
setTermsAndConditions(text); | |
setIsLoading(false); | |
}; | |
grabTermsAndConditions(); | |
}, [currentLanguage]); | |
return ( | |
<div className="container"> | |
<Loader isLoading={isLoading} width={200} height={200}> | |
<ReactMarkdown source={getTermsAndConditions} /> | |
</Loader> | |
</div> | |
); | |
}; | |
export { TermsAndConditions }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment