frontend/src/components/SchedulerHistory.tsx

55 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-01-05 23:46:51 +01:00
import React, { useContext, useEffect, useState } from 'react';
import styled from 'styled-components';
import { coursesContext } from '../contexts/CoursesProvider';
2021-01-12 22:59:12 +01:00
import { SchedulerEvent } from '../types';
2021-01-05 23:46:51 +01:00
import { Scheduler } from './Scheduler';
import { SchedulerHistoryNavigation } from './SchedulerHistoryNavigation';
const Wrapper = styled.div`
display: flex;
flex-direction: column;
width: 100%;
`;
2021-01-12 22:59:12 +01:00
interface SchedulerHistoryProps {
schedulerHistoryEvents: Array<SchedulerEvent>;
}
export const SchedulerHistory = ({schedulerHistoryEvents}:SchedulerHistoryProps) => {
const { timetableHistory, setHistoryBasketFromHistoryGroups } = useContext(coursesContext)!;
const [currentTimetable, setCurrentTimetable] = useState(timetableHistory.length===0 ? 0 : timetableHistory.length - 1);
let commisionDate = timetableHistory[currentTimetable]?.commisionDate;
2021-01-05 23:46:51 +01:00
const SubstractCurrentTimetable = (value: number) => {
if (currentTimetable > 0) {
setCurrentTimetable((currentTimetable) => currentTimetable + value);
}
};
const AddCurrentTimetable = (value: number) => {
if (currentTimetable < timetableHistory.length - 1) {
setCurrentTimetable((currentTimetable) => currentTimetable + value);
}
2021-01-05 23:46:51 +01:00
};
useEffect(() => {
const timetable = timetableHistory[currentTimetable];
if (timetable) {
const { groups } = timetable;
2021-01-12 22:59:12 +01:00
setHistoryBasketFromHistoryGroups(groups);
}
else{
setHistoryBasketFromHistoryGroups([]);
2021-01-05 23:46:51 +01:00
}
}, [currentTimetable,timetableHistory]);
2021-01-05 23:46:51 +01:00
return (
<Wrapper>
{timetableHistory.length > 0 && (
<SchedulerHistoryNavigation commisionDate={commisionDate} SubstractCurrentTimetable={SubstractCurrentTimetable} AddCurrentTimetable={AddCurrentTimetable} />
2021-01-05 23:46:51 +01:00
)}
2021-01-12 22:59:12 +01:00
<Scheduler schedulerEvents={schedulerHistoryEvents}/>
2021-01-05 23:46:51 +01:00
</Wrapper>
);
};