forked from Github/ocrcc-chatbox
check schedule if using admin settings
This commit is contained in:
parent
c8c42e9086
commit
dcd7718f8f
@ -5,6 +5,8 @@ import Chatbox from './chatbox';
|
|||||||
|
|
||||||
const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => {
|
const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => {
|
||||||
const [settings, setSettings] = useState({});
|
const [settings, setSettings] = useState({});
|
||||||
|
const [shifts, setShifts] = useState();
|
||||||
|
const [isAvailable, setAvailability] = useState(false);
|
||||||
const settingsObj = {};
|
const settingsObj = {};
|
||||||
|
|
||||||
const getSettings = async () => {
|
const getSettings = async () => {
|
||||||
@ -12,6 +14,7 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) =>
|
|||||||
const props = {
|
const props = {
|
||||||
...rest,
|
...rest,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
isAvailable: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
return setSettings(props);
|
return setSettings(props);
|
||||||
@ -20,7 +23,9 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) =>
|
|||||||
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
|
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const { fields } = data;
|
const { fields, schedule = [] } = data;
|
||||||
|
|
||||||
|
setShifts(schedule);
|
||||||
|
|
||||||
Object.entries(fields).forEach(([k, v]) => {
|
Object.entries(fields).forEach(([k, v]) => {
|
||||||
const [scope, key] = k.split('_');
|
const [scope, key] = k.split('_');
|
||||||
@ -37,12 +42,45 @@ const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) =>
|
|||||||
return setSettings(settingsObj);
|
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(() => {
|
useEffect(() => {
|
||||||
getSettings();
|
getSettings();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (shifts) {
|
||||||
|
checkSchedule();
|
||||||
|
}
|
||||||
|
}, [shifts]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Chatbox matrixServerUrl={matrixServerUrl} {...settings} {...rest} />
|
/* eslint-disable react/jsx-props-no-spreading */
|
||||||
|
<Chatbox matrixServerUrl={matrixServerUrl} isAvailable={isAvailable} {...settings} {...rest} />
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ class ChatBox extends React.Component {
|
|||||||
isMobile: true,
|
isMobile: true,
|
||||||
isSlowConnection: true,
|
isSlowConnection: true,
|
||||||
decryptionErrors: {},
|
decryptionErrors: {},
|
||||||
messagesInFlight: [],
|
messagesInFlight: []
|
||||||
}
|
}
|
||||||
this.state = this.initialState
|
this.state = this.initialState
|
||||||
this.chatboxInput = React.createRef();
|
this.chatboxInput = React.createRef();
|
||||||
@ -587,14 +587,14 @@ class ChatBox extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
document.addEventListener("keydown", this.handleKeyDown, false);
|
document.addEventListener("keydown", this.handleKeyDown, false)
|
||||||
window.addEventListener('beforeunload', this.exitChat)
|
window.addEventListener('beforeunload', this.exitChat)
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
document.removeEventListener("keydown", this.handleKeyDown, false);
|
document.removeEventListener("keydown", this.handleKeyDown, false)
|
||||||
window.removeEventListener('beforeunload', this.exitChat)
|
window.removeEventListener('beforeunload', this.exitChat)
|
||||||
this.exitChat();
|
this.exitChat()
|
||||||
}
|
}
|
||||||
|
|
||||||
handleInputChange = e => {
|
handleInputChange = e => {
|
||||||
@ -655,7 +655,7 @@ class ChatBox extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (!this.props.enabled) {
|
if (!this.props.enabled || !this.props.isAvailable) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -788,7 +788,7 @@ ChatBox.propTypes = {
|
|||||||
maxWaitTime: PropTypes.number,
|
maxWaitTime: PropTypes.number,
|
||||||
waitInterval: PropTypes.number,
|
waitInterval: PropTypes.number,
|
||||||
dockLabel: PropTypes.string,
|
dockLabel: PropTypes.string,
|
||||||
enabled: PropTypes.bool,
|
enabled: PropTypes.bool
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatBox.defaultProps = {
|
ChatBox.defaultProps = {
|
||||||
|
Loading…
Reference in New Issue
Block a user