2020-10-20 22:38:38 +00:00
|
|
|
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);
|
2020-10-20 22:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-10-20 22:38:38 +00:00
|
|
|
|
|
|
|
return setSettings(settingsObj);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getSettings();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Chatbox {...settings} {...rest} />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ChatboxWithSettings.propTypes = {
|
|
|
|
settingsEndpoint: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatboxWithSettings;
|