mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-14 17:35:35 +01:00
Add CustomizationContext and custom hook
As we want to share customization context between reForis & Plugins
This commit is contained in:
parent
f327428765
commit
c469d8dfde
57
src/customizationContext/CustomizationContext.js
Normal file
57
src/customizationContext/CustomizationContext.js
Normal file
|
@ -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 <Spinner fullScreen />;
|
||||||
|
}
|
||||||
|
|
||||||
|
const deviceDetails = getCustomizationResponse.data || {};
|
||||||
|
|
||||||
|
const isCustomized = !!(
|
||||||
|
deviceDetails &&
|
||||||
|
deviceDetails.customization !== undefined &&
|
||||||
|
deviceDetails.customization === "shield"
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CustomizationContext.Provider value={{ deviceDetails, isCustomized }}>
|
||||||
|
{children}
|
||||||
|
</CustomizationContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function useCustomizationContext() {
|
||||||
|
const { CustomizationContext } = window;
|
||||||
|
return useContext(CustomizationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { CustomizationContextProvider, useCustomizationContext };
|
|
@ -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.
|
* This is free software, licensed under the GNU General Public License v3.
|
||||||
* See /LICENSE for more information.
|
* See /LICENSE for more information.
|
||||||
|
@ -91,3 +91,9 @@ export {
|
||||||
|
|
||||||
// Alert context
|
// Alert context
|
||||||
export { AlertContextProvider, useAlert } from "./alertContext/AlertContext";
|
export { AlertContextProvider, useAlert } from "./alertContext/AlertContext";
|
||||||
|
|
||||||
|
// Customization context
|
||||||
|
export {
|
||||||
|
CustomizationContextProvider,
|
||||||
|
useCustomizationContext,
|
||||||
|
} from "./customizationContext/CustomizationContext";
|
||||||
|
|
Loading…
Reference in New Issue
Block a user