Display events on the same day in the right order, prevent refresh spam

This commit is contained in:
keplyx 2019-09-15 16:33:24 +02:00
parent ddf58959c9
commit 74a587cac7

View file

@ -44,6 +44,9 @@ export default class PlanningScreen extends React.Component<Props, State> {
modalRef: { current: null | Modalize }; modalRef: { current: null | Modalize };
webDataManager: WebDataManager; webDataManager: WebDataManager;
lastRefresh: Date;
minTimeBetweenRefresh = 60;
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.modalRef = React.createRef(); this.modalRef = React.createRef();
@ -198,6 +201,13 @@ export default class PlanningScreen extends React.Component<Props, State> {
* @private * @private
*/ */
_onRefresh = () => { _onRefresh = () => {
let canRefresh;
if (this.lastRefresh !== undefined)
canRefresh = (new Date().getTime() - this.lastRefresh.getTime())/1000 > this.minTimeBetweenRefresh;
else
canRefresh = true;
if (canRefresh) {
this.setState({refreshing: true}); this.setState({refreshing: true});
this.webDataManager.readData() this.webDataManager.readData()
.then((fetchedData) => { .then((fetchedData) => {
@ -205,6 +215,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
refreshing: false, refreshing: false,
}); });
this.generateEventAgenda(fetchedData); this.generateEventAgenda(fetchedData);
this.lastRefresh = new Date();
}) })
.catch((err) => { .catch((err) => {
this.setState({ this.setState({
@ -212,17 +223,45 @@ export default class PlanningScreen extends React.Component<Props, State> {
}); });
console.log(err); console.log(err);
}); });
}
}; };
generateEventAgenda(eventList: Array<Object>) { generateEventAgenda(eventList: Array<Object>) {
let agendaItems = this.generateEmptyCalendar(); let agendaItems = this.generateEmptyCalendar();
for (let i = 0; i < eventList.length; i++) { for (let i = 0; i < eventList.length; i++) {
if (agendaItems[this.getEventStartDate(eventList[i])] !== undefined) if (agendaItems[this.getEventStartDate(eventList[i])] !== undefined) {
agendaItems[this.getEventStartDate(eventList[i])].push(eventList[i]); this.pushEventInOrder(agendaItems, eventList[i], this.getEventStartDate(eventList[i]));
}
} }
this.setState({agendaItems: agendaItems}) this.setState({agendaItems: agendaItems})
} }
pushEventInOrder(agendaItems: Object, event: Object, startDate: string) {
if (agendaItems[startDate].length === 0)
agendaItems[startDate].push(event);
else {
for (let i = 0; i < agendaItems[startDate].length; i++) {
if (this.isEventBefore(event, agendaItems[startDate][i])) {
agendaItems[startDate].splice(i, 0, event);
break;
} else if (i === agendaItems[startDate].length - 1) {
agendaItems[startDate].push(event);
break;
}
}
}
}
isEventBefore(event1: Object, event2: Object) {
let date1 = new Date();
let date2 = new Date();
let timeArray = this.getEventStartTime(event1).split(":");
date1.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
timeArray = this.getEventStartTime(event2).split(":");
date2.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
return date1 < date2;
}
getEventStartDate(event: Object) { getEventStartDate(event: Object) {
return event.date_begin.split(" ")[0]; return event.date_begin.split(" ")[0];
} }