2020-10-20 22:38:38 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Chatbox from './chatbox';
|
|
|
|
|
2020-12-15 15:24:20 +00:00
|
|
|
const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest }) => {
|
2020-10-20 22:38:38 +00:00
|
|
|
const [settings, setSettings] = useState({});
|
2020-11-28 20:02:55 +00:00
|
|
|
const [shifts, setShifts] = useState();
|
2022-01-17 20:44:05 +00:00
|
|
|
const [isAvailable, setAvailability] = useState(true);
|
2020-10-20 22:38:38 +00:00
|
|
|
|
|
|
|
const getSettings = async () => {
|
2022-01-17 20:44:05 +00:00
|
|
|
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
|
2020-12-15 15:24:20 +00:00
|
|
|
const res = await fetch(url);
|
|
|
|
const data = await res.json();
|
|
|
|
const { fields, schedule = [] } = data;
|
|
|
|
|
|
|
|
if (!fields) {
|
2020-10-20 23:29:12 +00:00
|
|
|
const props = {
|
|
|
|
...rest,
|
2020-11-13 03:00:06 +00:00
|
|
|
enabled: true,
|
2020-11-28 20:02:55 +00:00
|
|
|
isAvailable: true,
|
2020-11-13 03:00:06 +00:00
|
|
|
};
|
2020-10-20 23:29:12 +00:00
|
|
|
|
|
|
|
return setSettings(props);
|
2020-10-20 22:38:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 22:37:26 +00:00
|
|
|
const settingsObj = {};
|
2020-11-28 20:02:55 +00:00
|
|
|
|
|
|
|
setShifts(schedule);
|
2020-10-20 22:38:38 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2020-11-28 20:02:55 +00:00
|
|
|
const checkSchedule = () => {
|
|
|
|
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const weekday = weekdays[now.getDay()];
|
|
|
|
const hours = now.getHours();
|
|
|
|
const minutes = now.getMinutes();
|
|
|
|
|
|
|
|
shifts.map(async (shift) => {
|
|
|
|
const [startHours, startMinutes] = shift.start.split(':').map((t) => Number(t));
|
|
|
|
const [endHours, endMinutes] = shift.end.split(':').map((t) => Number(t));
|
|
|
|
if (
|
|
|
|
shift.service === 'web'
|
|
|
|
&& shift.weekday === weekday
|
|
|
|
&& ((startHours < hours) || (startHours === hours && startMinutes <= minutes))
|
|
|
|
&& ((endHours > hours) || (endHours === hours && endMinutes > minutes))
|
|
|
|
) {
|
|
|
|
setAvailability(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 22:38:38 +00:00
|
|
|
useEffect(() => {
|
2022-01-17 20:44:05 +00:00
|
|
|
if (settingsEndpoint) {
|
|
|
|
getSettings();
|
|
|
|
}
|
|
|
|
}, [settingsEndpoint]);
|
2020-10-20 22:38:38 +00:00
|
|
|
|
2020-11-28 20:02:55 +00:00
|
|
|
useEffect(() => {
|
2022-01-17 20:44:05 +00:00
|
|
|
if (shifts && shifts.length > 0) {
|
2020-11-28 20:02:55 +00:00
|
|
|
checkSchedule();
|
|
|
|
}
|
|
|
|
}, [shifts]);
|
|
|
|
|
2020-10-20 22:38:38 +00:00
|
|
|
return (
|
2020-11-28 20:02:55 +00:00
|
|
|
/* eslint-disable react/jsx-props-no-spreading */
|
|
|
|
<Chatbox matrixServerUrl={matrixServerUrl} isAvailable={isAvailable} {...settings} {...rest} />
|
2020-10-20 22:38:38 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ChatboxWithSettings.propTypes = {
|
2020-11-13 03:00:06 +00:00
|
|
|
matrixServerUrl: PropTypes.string.isRequired,
|
2020-10-20 22:38:38 +00:00
|
|
|
settingsEndpoint: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2020-11-13 03:00:06 +00:00
|
|
|
ChatboxWithSettings.defaultProps = {
|
|
|
|
settingsEndpoint: null,
|
|
|
|
};
|
|
|
|
|
2020-10-20 22:38:38 +00:00
|
|
|
export default ChatboxWithSettings;
|