Lidarr/frontend/src/Calendar/CalendarConnector.js

175 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-09-04 02:20:56 +00:00
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
2020-09-07 01:33:10 +00:00
import * as calendarActions from 'Store/Actions/calendarActions';
import { clearQueueDetails, fetchQueueDetails } from 'Store/Actions/queueActions';
import { clearTrackFiles, fetchTrackFiles } from 'Store/Actions/trackFileActions';
2017-09-04 02:20:56 +00:00
import hasDifferentItems from 'Utilities/Object/hasDifferentItems';
import selectUniqueIds from 'Utilities/Object/selectUniqueIds';
2020-09-07 01:33:10 +00:00
import { registerPagePopulator, unregisterPagePopulator } from 'Utilities/pagePopulator';
2017-09-04 02:20:56 +00:00
import Calendar from './Calendar';
const UPDATE_DELAY = 3600000; // 1 hour
function createMapStateToProps() {
return createSelector(
(state) => state.calendar,
(calendar) => {
return calendar;
}
);
}
const mapDispatchToProps = {
...calendarActions,
2017-09-25 02:58:13 +00:00
fetchTrackFiles,
clearTrackFiles,
2017-09-04 02:20:56 +00:00
fetchQueueDetails,
clearQueueDetails
};
class CalendarConnector extends Component {
//
// Lifecycle
constructor(props, context) {
super(props, context);
this.updateTimeoutId = null;
}
componentDidMount() {
const {
useCurrentPage,
fetchCalendar,
gotoCalendarToday
} = this.props;
2017-10-07 06:47:28 +00:00
registerPagePopulator(this.repopulate);
if (useCurrentPage) {
fetchCalendar();
} else {
gotoCalendarToday();
}
2017-09-04 02:20:56 +00:00
this.scheduleUpdate();
}
componentDidUpdate(prevProps) {
const {
items,
time
} = this.props;
if (hasDifferentItems(prevProps.items, items)) {
const albumIds = selectUniqueIds(items, 'id');
2017-09-25 02:58:13 +00:00
// const trackFileIds = selectUniqueIds(items, 'trackFileId');
2017-09-04 02:20:56 +00:00
if (items.length) {
this.props.fetchQueueDetails({ albumIds });
}
2017-09-04 02:20:56 +00:00
2017-09-25 02:58:13 +00:00
// if (trackFileIds.length) {
// this.props.fetchTrackFiles({ trackFileIds });
// }
2017-09-04 02:20:56 +00:00
}
if (prevProps.time !== time) {
this.scheduleUpdate();
}
}
componentWillUnmount() {
2017-10-07 06:47:28 +00:00
unregisterPagePopulator(this.repopulate);
2017-09-04 02:20:56 +00:00
this.props.clearCalendar();
this.props.clearQueueDetails();
2017-09-25 02:58:13 +00:00
this.props.clearTrackFiles();
2017-09-04 02:20:56 +00:00
this.clearUpdateTimeout();
}
//
// Control
2017-10-07 06:47:28 +00:00
repopulate = () => {
const {
time,
view
} = this.props;
this.props.fetchQueueDetails({ time, view });
this.props.fetchCalendar({ time, view });
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
scheduleUpdate = () => {
this.clearUpdateTimeout();
2017-10-07 06:47:28 +00:00
this.updateTimeoutId = setTimeout(this.updateCalendar, UPDATE_DELAY);
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
clearUpdateTimeout = () => {
if (this.updateTimeoutId) {
clearTimeout(this.updateTimeoutId);
}
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
updateCalendar = () => {
this.props.gotoCalendarToday();
this.scheduleUpdate();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
//
// Listeners
onCalendarViewChange = (view) => {
this.props.setCalendarView({ view });
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onTodayPress = () => {
this.props.gotoCalendarToday();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onPreviousPress = () => {
this.props.gotoCalendarPreviousRange();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
onNextPress = () => {
this.props.gotoCalendarNextRange();
2021-12-24 18:18:14 +00:00
};
2017-09-04 02:20:56 +00:00
//
// Render
render() {
return (
<Calendar
{...this.props}
onCalendarViewChange={this.onCalendarViewChange}
onTodayPress={this.onTodayPress}
onPreviousPress={this.onPreviousPress}
onNextPress={this.onNextPress}
/>
);
}
}
CalendarConnector.propTypes = {
2019-02-28 04:33:26 +00:00
useCurrentPage: PropTypes.bool.isRequired,
2017-09-04 02:20:56 +00:00
time: PropTypes.string,
2017-10-07 06:47:28 +00:00
view: PropTypes.string.isRequired,
2017-09-04 02:20:56 +00:00
items: PropTypes.arrayOf(PropTypes.object).isRequired,
setCalendarView: PropTypes.func.isRequired,
gotoCalendarToday: PropTypes.func.isRequired,
gotoCalendarPreviousRange: PropTypes.func.isRequired,
gotoCalendarNextRange: PropTypes.func.isRequired,
clearCalendar: PropTypes.func.isRequired,
2017-10-07 06:47:28 +00:00
fetchCalendar: PropTypes.func.isRequired,
2017-09-25 02:58:13 +00:00
fetchTrackFiles: PropTypes.func.isRequired,
clearTrackFiles: PropTypes.func.isRequired,
2017-09-04 02:20:56 +00:00
fetchQueueDetails: PropTypes.func.isRequired,
clearQueueDetails: PropTypes.func.isRequired
};
export default connect(createMapStateToProps, mapDispatchToProps)(CalendarConnector);