| 1 |
import React, { useEffect, useState } from "react"; |
| 2 |
import BoxContent from "./BoxContent"; |
| 3 |
import ContentNotFoundError from "../../inc/err/ContentNotFoundError"; |
| 4 |
|
| 5 |
const getCData = contentId => { |
| 6 |
return tablebergAdminMenuData?.[contentId]; |
| 7 |
}; |
| 8 |
/** |
| 9 |
* Box content provider component. |
| 10 |
* Any BoxContent component property can be passed to this component. All will be reflected to generated one. |
| 11 |
* Passing content related property will make this absolutely useless since it will be overwritten by fetched data. |
| 12 |
* |
| 13 |
* This component will fetch given data id from store and generate BoxContent component based on it. |
| 14 |
* |
| 15 |
* @param {Object} props component properties |
| 16 |
* @param {string} props.contentId content id |
| 17 |
* @param {string} [props.size=BoxContentSize.NORMAL] box content size |
| 18 |
*/ |
| 19 |
function BoxContentProvider(props) { |
| 20 |
const [contentTitle, setTitle] = useState(null); |
| 21 |
const [mainContent, setContent] = useState(null); |
| 22 |
const [rest, setRest] = useState({}); |
| 23 |
|
| 24 |
const { contentId, ...dataRest } = props; |
| 25 |
const cData = getCData(contentId); |
| 26 |
|
| 27 |
/** |
| 28 |
* useEffect hook. |
| 29 |
*/ |
| 30 |
useEffect(() => { |
| 31 |
if (cData) { |
| 32 |
const { title, content } = cData; |
| 33 |
setTitle(title); |
| 34 |
setContent(content); |
| 35 |
setRest(dataRest); |
| 36 |
} else { |
| 37 |
throw new ContentNotFoundError(contentId); |
| 38 |
} |
| 39 |
}, []); |
| 40 |
|
| 41 |
return ( |
| 42 |
<BoxContent {...rest} title={contentTitle} content={mainContent}> |
| 43 |
{props.children} |
| 44 |
</BoxContent> |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @module BoxContentProvider |
| 50 |
*/ |
| 51 |
export default BoxContentProvider; |
| 52 |
|