From c469d8dfde0adc059df4fe13cd74a895868adf47 Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Thu, 10 Feb 2022 11:17:15 +0100 Subject: [PATCH] Add CustomizationContext and custom hook As we want to share customization context between reForis & Plugins --- .../CustomizationContext.js | 57 +++++++++++++++++++ src/index.js | 8 ++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/customizationContext/CustomizationContext.js diff --git a/src/customizationContext/CustomizationContext.js b/src/customizationContext/CustomizationContext.js new file mode 100644 index 0000000..e763421 --- /dev/null +++ b/src/customizationContext/CustomizationContext.js @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/) + * + * This is free software, licensed under the GNU General Public License v3. + * See /LICENSE for more information. + */ + +import React, { useContext, useEffect } from "react"; +import PropTypes from "prop-types"; + +import { useAPIGet } from "../api/hooks"; +import { ForisURLs } from "../utils/forisUrls"; + +import { Spinner } from "../bootstrap/Spinner"; + +CustomizationContextProvider.propTypes = { + children: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.node), + PropTypes.node, + ]), +}; + +function CustomizationContextProvider({ children }) { + const { CustomizationContext } = window; + const [getCustomizationResponse, getCustomization] = useAPIGet( + ForisURLs.about + ); + + useEffect(() => { + getCustomization(); + }, [getCustomization]); + + if (getCustomizationResponse.state !== "success") { + return ; + } + + const deviceDetails = getCustomizationResponse.data || {}; + + const isCustomized = !!( + deviceDetails && + deviceDetails.customization !== undefined && + deviceDetails.customization === "shield" + ); + + return ( + + {children} + + ); +} + +function useCustomizationContext() { + const { CustomizationContext } = window; + return useContext(CustomizationContext); +} + +export { CustomizationContextProvider, useCustomizationContext }; diff --git a/src/index.js b/src/index.js index c52f1fa..09ec807 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2021 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. @@ -91,3 +91,9 @@ export { // Alert context export { AlertContextProvider, useAlert } from "./alertContext/AlertContext"; + +// Customization context +export { + CustomizationContextProvider, + useCustomizationContext, +} from "./customizationContext/CustomizationContext";