From dcd7718f8f8dc6367e3008f1bb0212e2507495a0 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Sat, 28 Nov 2020 15:02:55 -0500 Subject: [PATCH] check schedule if using admin settings --- src/components/ChatboxWithSettings.js | 42 +++++++++++++++++++++++++-- src/components/chatbox.jsx | 12 ++++---- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/components/ChatboxWithSettings.js b/src/components/ChatboxWithSettings.js index fbebc64..5cb6549 100644 --- a/src/components/ChatboxWithSettings.js +++ b/src/components/ChatboxWithSettings.js @@ -5,6 +5,8 @@ import Chatbox from './chatbox'; const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => { const [settings, setSettings] = useState({}); + const [shifts, setShifts] = useState(); + const [isAvailable, setAvailability] = useState(false); const settingsObj = {}; const getSettings = async () => { @@ -12,6 +14,7 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => const props = { ...rest, enabled: true, + isAvailable: true, }; return setSettings(props); @@ -20,7 +23,9 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`; const res = await fetch(url); const data = await res.json(); - const { fields } = data; + const { fields, schedule = [] } = data; + + setShifts(schedule); Object.entries(fields).forEach(([k, v]) => { const [scope, key] = k.split('_'); @@ -37,12 +42,45 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => return setSettings(settingsObj); }; + const checkSchedule = () => { + if (shifts.length === 0) { + setAvailability(true); + } + + 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); + } + }); + }; + useEffect(() => { getSettings(); }, []); + useEffect(() => { + if (shifts) { + checkSchedule(); + } + }, [shifts]); + return ( - + /* eslint-disable react/jsx-props-no-spreading */ + ); }; diff --git a/src/components/chatbox.jsx b/src/components/chatbox.jsx index c9bfbc4..403a6b0 100644 --- a/src/components/chatbox.jsx +++ b/src/components/chatbox.jsx @@ -69,7 +69,7 @@ class ChatBox extends React.Component { isMobile: true, isSlowConnection: true, decryptionErrors: {}, - messagesInFlight: [], + messagesInFlight: [] } this.state = this.initialState this.chatboxInput = React.createRef(); @@ -587,14 +587,14 @@ class ChatBox extends React.Component { } componentDidMount() { - document.addEventListener("keydown", this.handleKeyDown, false); + document.addEventListener("keydown", this.handleKeyDown, false) window.addEventListener('beforeunload', this.exitChat) } componentWillUnmount() { - document.removeEventListener("keydown", this.handleKeyDown, false); + document.removeEventListener("keydown", this.handleKeyDown, false) window.removeEventListener('beforeunload', this.exitChat) - this.exitChat(); + this.exitChat() } handleInputChange = e => { @@ -655,7 +655,7 @@ class ChatBox extends React.Component { } render() { - if (!this.props.enabled) { + if (!this.props.enabled || !this.props.isAvailable) { return null } @@ -788,7 +788,7 @@ ChatBox.propTypes = { maxWaitTime: PropTypes.number, waitInterval: PropTypes.number, dockLabel: PropTypes.string, - enabled: PropTypes.bool, + enabled: PropTypes.bool } ChatBox.defaultProps = {