add default settings endpoint'

This commit is contained in:
Sharon Kennedy 2020-12-15 10:24:20 -05:00
parent 819d9b8ebb
commit c45182a574
3 changed files with 11 additions and 10 deletions

View File

@ -13,10 +13,8 @@
<script src="./chatbox.js"></script> <script src="./chatbox.js"></script>
<script> <script>
var config = { var config = {
settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings',
matrixServerUrl: 'https://matrix.safesupport.chat' matrixServerUrl: 'https://matrix.safesupport.chat'
} };
EmbeddableChatbox.mount(config); EmbeddableChatbox.mount(config);
</script> </script>

View File

@ -1,15 +1,21 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Chatbox from './chatbox'; import Chatbox from './chatbox';
import { DEFAULT_SETTINGS_ENDPOINT } from '../utils/constants';
const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest }) => {
const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => {
const [settings, setSettings] = useState({}); const [settings, setSettings] = useState({});
const [shifts, setShifts] = useState(); const [shifts, setShifts] = useState();
const [isAvailable, setAvailability] = useState(false); const [isAvailable, setAvailability] = useState(false);
const getSettings = async () => { const getSettings = async () => {
if (!settingsEndpoint) { const endpoint = settingsEndpoint || DEFAULT_SETTINGS_ENDPOINT;
const url = `${endpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
const res = await fetch(url);
const data = await res.json();
const { fields, schedule = [] } = data;
if (!fields) {
const props = { const props = {
...rest, ...rest,
enabled: true, enabled: true,
@ -19,10 +25,6 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) =>
return setSettings(props); return setSettings(props);
} }
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
const res = await fetch(url);
const data = await res.json();
const { fields, schedule = [] } = data;
const settingsObj = {}; const settingsObj = {};
setShifts(schedule); setShifts(schedule);

1
src/utils/constants.js Normal file
View File

@ -0,0 +1 @@
export const DEFAULT_SETTINGS_ENDPOINT = 'https://safesupport-admin.herokuapp.com/api/get-settings';