ocrcc-chatbox/src/components/ChatboxWithSettings.js

54 lines
1.0 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import Chatbox from './chatbox';
const ChatboxWithSettings = ({ settingsEndpoint, ...rest }) => {
const [settings, setSettings] = useState({});
const settingsObj = {};
const getSettings = async () => {
if (!settingsEndpoint) {
2020-10-20 23:29:12 +00:00
const props = {
...rest,
enabled: true
}
return setSettings(props);
}
const res = await fetch(settingsEndpoint);
const data = await res.json();
const { fields } = data;
Object.entries(fields).forEach(([k, v]) => {
const [scope, key] = k.split('_');
if (scope === 'web') {
settingsObj[key] = v;
}
});
2020-10-20 23:29:12 +00:00
if (!settingsObj.enabled) {
settingsObj.enabled = false;
}
return setSettings(settingsObj);
};
useEffect(() => {
getSettings();
}, []);
return (
<Chatbox {...settings} {...rest} />
);
};
ChatboxWithSettings.propTypes = {
settingsEndpoint: PropTypes.string,
};
export default ChatboxWithSettings;