|
| 1 | +import { CellContext } from "components/contexts/CellContext"; |
| 2 | +import { ActionTypes } from "helpers/Constants"; |
| 3 | +import React, { useContext, useState } from "react"; |
| 4 | +import { DateTime } from "luxon"; |
| 5 | +import DatePicker from "react-datepicker"; |
| 6 | +import NoteInfo from "services/NoteInfo"; |
| 7 | +import { Portal } from "@material-ui/core"; |
| 8 | +import { CalendarProps } from "cdm/DatabaseModel"; |
| 9 | + |
| 10 | +const CalendarTimePortal = (calendarProps: CalendarProps) => { |
| 11 | + const { column, cellProperties } = calendarProps; |
| 12 | + const dataDispatch = (cellProperties as any).dataDispatch; |
| 13 | + // Selector reference state |
| 14 | + |
| 15 | + // Selector popper state |
| 16 | + /** state of cell value */ |
| 17 | + const { contextValue, setContextValue } = useContext(CellContext); |
| 18 | + |
| 19 | + /** Note info of current Cell */ |
| 20 | + const note: NoteInfo = (cellProperties.row.original as any).note; |
| 21 | + const [calendarState, setCalendarState] = useState( |
| 22 | + DateTime.isDateTime(contextValue.value) |
| 23 | + ? contextValue.value.toJSDate() |
| 24 | + : null |
| 25 | + ); |
| 26 | + |
| 27 | + function handleCalendarChange(date: Date) { |
| 28 | + const newValue = DateTime.fromJSDate(date); |
| 29 | + // save on disk |
| 30 | + dataDispatch({ |
| 31 | + type: ActionTypes.UPDATE_CELL, |
| 32 | + file: note.getFile(), |
| 33 | + key: column.key, |
| 34 | + value: DateTime.fromJSDate(date).toFormat("yyyy-MM-dd"), |
| 35 | + row: cellProperties.row, |
| 36 | + columnId: column.id, |
| 37 | + }); |
| 38 | + |
| 39 | + setCalendarState(date); |
| 40 | + setContextValue({ |
| 41 | + value: newValue, |
| 42 | + update: true, |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + const CalendarContainer = (containerProps: any) => { |
| 47 | + const el = document.getElementById("popper-container"); |
| 48 | + return <Portal container={el}>{containerProps.children}</Portal>; |
| 49 | + }; |
| 50 | + |
| 51 | + return column.isMetadata ? ( |
| 52 | + <span className="data-input calendar-time"> |
| 53 | + {(contextValue.value as DateTime).toFormat("yyyy-MM-dd h:mm a")} |
| 54 | + </span> |
| 55 | + ) : ( |
| 56 | + <div className="data-input calendar-time"> |
| 57 | + <DatePicker |
| 58 | + dateFormat="yyyy-MM-dd h:mm aa" |
| 59 | + selected={calendarState} |
| 60 | + onChange={handleCalendarChange} |
| 61 | + popperContainer={CalendarContainer} |
| 62 | + timeInputLabel="Time:" |
| 63 | + showTimeInput |
| 64 | + placeholderText="Pick a moment..." |
| 65 | + /> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +export default CalendarTimePortal; |
0 commit comments