frontend/src/components/Calendar/index.tsx

88 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-06-05 14:31:48 +02:00
import * as React from "react";
2020-06-09 16:36:28 +02:00
import { ViewState } from "@devexpress/dx-react-scheduler";
2020-06-05 14:31:48 +02:00
import { AppointmentModel } from "@devexpress/dx-react-scheduler";
2020-06-10 21:04:22 +02:00
import { Scheduler, WeekView, Appointments, AppointmentTooltip } from "@devexpress/dx-react-scheduler-material-ui";
2020-06-05 14:31:48 +02:00
import moment from "moment";
import "moment/locale/pl";
import "./index.scss";
2020-06-10 21:04:22 +02:00
import { makeStyles, createStyles } from "@material-ui/core/styles";
2020-06-05 14:31:48 +02:00
2020-06-07 15:57:51 +02:00
interface CalendarProps {
2020-06-10 21:04:22 +02:00
data: Array<AppointmentModel>;
2020-06-05 14:31:48 +02:00
}
2020-06-08 20:10:20 +02:00
interface CalendarState {
2020-06-10 21:04:22 +02:00
currentDate: Date;
2020-06-08 20:10:20 +02:00
}
2020-06-07 15:57:51 +02:00
2020-06-10 21:04:22 +02:00
const formatDayScaleDate = (date: moment.MomentInput, nextOptions: Intl.DateTimeFormatOptions): string => {
const momentDate = moment(date).locale("pl");
return momentDate.format(nextOptions.weekday ? "dddd" : " ").toUpperCase();
2020-06-05 14:31:48 +02:00
};
2020-06-10 21:04:22 +02:00
const useStyles = makeStyles(() =>
createStyles({
dayScaleCell: {
paddingTop: 10,
paddingBottom: 10,
},
timeTableLayout: {
border: "1px solid rgba(224, 224, 224, 1);",
borderCollapse: "separate",
},
appointmentLayer: {
borderRadius: 15,
marginLeft: 5,
textAlign: "center",
},
})
2020-06-05 14:31:48 +02:00
);
2020-06-10 21:04:22 +02:00
const DayScaleCell = ({ formatDate, ...restProps }: WeekView.DayScaleCellProps) => {
const classes = useStyles();
return (
<WeekView.DayScaleCell {...restProps} formatDate={formatDayScaleDate} today={false} className={classes.dayScaleCell} />
);
2020-06-06 13:17:56 +02:00
};
2020-06-05 14:58:52 +02:00
2020-06-10 21:04:22 +02:00
const TimeTableLayout = ({ ...restProps }: WeekView.TimeTableLayoutProps) => {
const classes = useStyles();
return <WeekView.TimeTableLayout {...restProps} className={classes.timeTableLayout} />;
2020-06-06 13:17:56 +02:00
};
2020-06-05 14:58:52 +02:00
2020-06-10 21:04:22 +02:00
const Appointment = ({ ...restProps }: Appointments.AppointmentProps) => {
const classes = useStyles();
return <Appointments.Appointment {...restProps} className={classes.appointmentLayer} />;
2020-06-06 13:17:56 +02:00
};
2020-06-05 14:31:48 +02:00
2020-06-10 21:04:22 +02:00
export default class Calendar extends React.PureComponent<CalendarProps, CalendarState> {
constructor(props: CalendarProps) {
super(props);
2020-06-07 16:31:35 +02:00
2020-06-10 21:04:22 +02:00
this.state = {
currentDate: new Date("2020-06-01"),
};
}
2020-06-07 16:31:35 +02:00
2020-06-10 21:04:22 +02:00
render() {
const { data } = this.props;
const { currentDate } = this.state;
2020-06-05 14:31:48 +02:00
2020-06-10 21:04:22 +02:00
return (
<Scheduler data={data} locale={"PL-PL"} firstDayOfWeek={1}>
<ViewState defaultCurrentDate={currentDate} />
<WeekView
startDayHour={8}
endDayHour={20}
excludedDays={[0, 6]}
cellDuration={60}
dayScaleCellComponent={DayScaleCell}
timeTableLayoutComponent={TimeTableLayout}
/>
<Appointments appointmentComponent={Appointment} />
<AppointmentTooltip />
</Scheduler>
);
}
2020-06-05 14:31:48 +02:00
}