diff --git a/app/(pages)/(main)/volunteering/logs/workLogInput.js b/app/(pages)/(main)/volunteering/logs/workLogInput.js index 84abb73..f048786 100644 --- a/app/(pages)/(main)/volunteering/logs/workLogInput.js +++ b/app/(pages)/(main)/volunteering/logs/workLogInput.js @@ -1,5 +1,5 @@ -import { Box, Button, Skeleton, Stack, TextField, Typography } from "@mui/material"; +import { Button, Fab, Skeleton, Stack, TextField, Tooltip, Typography } from "@mui/material"; import { useState } from "react"; import { DateTimePicker, LocalizationProvider } from "@mui/x-date-pickers"; import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFnsV3"; @@ -7,6 +7,7 @@ import CustomAutoComplete from "@/app/components/input/CustomAutocomplete"; import CustomNumberInput from "@/app/components/input/CustomNumberInput"; import prismaRequest from "@/app/middleware/prisma/prismaRequest"; import locale from "date-fns/locale/en-GB"; +import { CalendarToday, PunchClock } from "@mui/icons-material"; export default function workLogInput( session, @@ -17,8 +18,11 @@ export default function workLogInput( const [registeredFor, setRegisteredFor] = useState(null); const [selectedGroup, setSelectedGroup] = useState(null); const [selectedDateTime, setSelectedDateTime] = useState(new Date()); + const [endDateTime, setEndDateTime] = useState(new Date()); const [hours, setHours] = useState(0); const [description, setDescription] = useState(""); + const [shouldInputHours, setShouldInputHours] = useState(false); + const [endInputMethodTooltip, setEndInputMethodTooltip] = useState("Change to 'End of work'"); const [registeredForError, setRegisteredForError] = useState(false); const [selectedGroupError, setSelectedGroupError] = useState(false); @@ -89,6 +93,21 @@ export default function workLogInput( }, 5000); }; + const switchEndInputMethod = () => { + if (!shouldInputHours) { + setShouldInputHours(true); + document.querySelector(".endDateTime").setAttribute("style", "display: inline"); + document.querySelector(".hours").setAttribute("style", "display: none"); + setEndInputMethodTooltip("Change to 'Hours worked'"); + + } else { + setShouldInputHours(false); + document.querySelector(".endDateTime").setAttribute("style", "display: none"); + document.querySelector(".hours").setAttribute("style", "display: inline"); + setEndInputMethodTooltip("Change to 'End of work'"); + } + }; + return ( @@ -116,15 +135,54 @@ export default function workLogInput( ampm={false} disableOpenPicker onChange={(e) => setSelectedDateTime(e)} - /> + /> - data.match(/[^0-9.]/) || data.match(/[.]{2,}/g)} - error={hoursError} - /> + + + { + if (e < selectedDateTime) return; // Prevent endDateTime from being before startDateTime + setEndDateTime(e); + setHours(Math.round(((e - selectedDateTime) / 3600000) * 10) / 10) // Update hours + }} + /> + + {setHours(value);}} + check={(data) => data.match(/[^0-9.]/) || data.match(/[.]{2,}/g)} + error={hoursError} + /> + + {switchEndInputMethod();}} + > + { shouldInputHours ? : } + + + 24, + hoursError: Number.isNaN(hours) || hours <= 0 || hours > 24, descriptionError: description.length == 0, }; diff --git a/app/components/input/CustomNumberInput.js b/app/components/input/CustomNumberInput.js index d087566..2d745ff 100644 --- a/app/components/input/CustomNumberInput.js +++ b/app/components/input/CustomNumberInput.js @@ -4,16 +4,18 @@ import { Component } from "react"; export default class CustomNumberInput extends Component { render() { - const { label, value, setValue, check, error } = this.props; + const { label, value, setValue, check, error, className } = this.props; return ( e.target.select()} + fullWidth onBlur={(e) => { let value = check(e.target.value) ? 0 : e.target.value; if (value === "") value = 0;