Created
July 24, 2019 07:14
-
-
Save iamclaytonray/c98378977226d4d89da0ed707963cf36 to your computer and use it in GitHub Desktop.
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, { useState, createContext } from "react" | |
import { useTranslation } from "react-i18next" | |
export const AppProvider = createContext() | |
export const Provider = ({ children }) => { | |
const { i18n } = useTranslation() | |
const [cookie, setCookie] = useState(false) | |
const [language, setLanguage] = useState("ENGLISH") | |
const [langMenu, setLangMenu] = useState(false) | |
const [jobsVisibility, setJobsVisibility] = React.useState(false) | |
const [aboutUsVisibility, setAboutUsVisibility] = React.useState(false) | |
const [formVisibility, setFormVisibility] = React.useState(false) | |
const [tab, setTab] = React.useState('about'); | |
// Cookies | |
const checkCookieInLS = () => { | |
if (localStorage.getItem("Cookies")) { | |
setCookie(true) | |
} else { | |
setCookie(false) | |
} | |
} | |
const addCookieToLS = () => { | |
if (!cookie) { | |
localStorage.setItem("Cookies", "true") | |
setCookie(true) | |
} | |
} | |
// Language changer | |
const languages = { | |
en: "English", | |
cn: "繁體中文" | |
} | |
const changeLanguage = lang => { | |
setLanguage(languages[lang]) | |
i18n.changeLanguage(lang) | |
} | |
const toggleLangMenu = () => setLangMenu(!langMenu) | |
const languageRef = React.useRef(null) | |
const langMenuRef = React.useRef(null) | |
// Nav - JOBS & ABOUT US | |
const toggleTab = tab => { | |
setTab(tab) // type Tab = 'jobs' | 'about'; | |
} | |
const jobsRef = React.useRef(null) | |
const aboutUsRef = React.useRef(null) | |
// Consultation form | |
const toggleFormVisibility = () => { | |
window.scrollTo({ | |
top: 0, | |
behavior: "smooth" | |
}) | |
setFormVisibility(!formVisibility) | |
} | |
const closeForm = e => { | |
e.preventDefault() | |
setFormVisibility(false) | |
} | |
const formRef = React.useRef(null) | |
// Global state object | |
const value = { | |
cookie, | |
language, | |
langMenu, | |
jobsVisibility, | |
aboutUsVisibility, | |
formVisibility, | |
setFormVisibility, | |
checkCookieInLS, | |
addCookieToLS, | |
setLanguage, | |
setLangMenu, | |
changeLanguage, | |
toggleLangMenu, | |
languageRef, | |
langMenuRef, | |
setJobsVisibility, | |
setAboutUsVisibility, | |
toggleVisibility, | |
closeModule, | |
jobsRef, | |
aboutUsRef, | |
toggleFormVisibility, | |
closeForm, | |
formRef | |
} | |
return <AppProvider.Provider value={value}>{children}</AppProvider.Provider> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment