Skip to content

Commit e33e79c

Browse files
committed
fix: handle invalid dates in EditEmployeeDialog to prevent crashes
- Added formatDateForInput helper function to safely format dates - Returns empty string for invalid dates instead of calling toISOString on invalid Date objects - Fixes RangeError: Invalid time value crash when editing employees with malformed date data - Resolves console error: "The specified value '+022222-12-21' does not conform to the required format, 'yyyy-MM-dd'"
1 parent e146747 commit e33e79c

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

client/src/components/dialogs/EditEmployeeDialog.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import { FormDialog } from "./FormDialog";
88
import Box from "@mui/material/Box";
99
import type { IEmployee, UpdateEmployeeInput } from "../../types/models";
1010

11+
/**
12+
* Safely format a date value for the date input field
13+
* Returns empty string if the date is invalid
14+
*/
15+
function formatDateForInput(dateValue: any): string {
16+
if (!dateValue) return "";
17+
try {
18+
const date = new Date(dateValue);
19+
// Check if date is valid
20+
if (isNaN(date.getTime())) return "";
21+
return date.toISOString().split("T")[0];
22+
} catch {
23+
return "";
24+
}
25+
}
26+
1127
interface EditEmployeeDialogProps {
1228
open: boolean;
1329
employee: IEmployee;
@@ -187,13 +203,9 @@ export function EditEmployeeDialog({
187203
<TextField
188204
label="Date of Birth"
189205
type="date"
190-
value={
206+
value={formatDateForInput(
191207
formData.additionalSpecifications?.dateOfBirth
192-
? new Date(formData.additionalSpecifications.dateOfBirth)
193-
.toISOString()
194-
.split("T")[0]
195-
: ""
196-
}
208+
)}
197209
onChange={(e) =>
198210
handleFieldChange(
199211
"additionalSpecifications.dateOfBirth",
@@ -237,13 +249,9 @@ export function EditEmployeeDialog({
237249
<TextField
238250
label="Job Start Date"
239251
type="date"
240-
value={
252+
value={formatDateForInput(
241253
formData.additionalSpecifications?.jobStartDate
242-
? new Date(formData.additionalSpecifications.jobStartDate)
243-
.toISOString()
244-
.split("T")[0]
245-
: ""
246-
}
254+
)}
247255
onChange={(e) =>
248256
handleFieldChange(
249257
"additionalSpecifications.jobStartDate",
@@ -257,13 +265,9 @@ export function EditEmployeeDialog({
257265
<TextField
258266
label="Job End Date"
259267
type="date"
260-
value={
268+
value={formatDateForInput(
261269
formData.additionalSpecifications?.jobEndDate
262-
? new Date(formData.additionalSpecifications.jobEndDate)
263-
.toISOString()
264-
.split("T")[0]
265-
: ""
266-
}
270+
)}
267271
onChange={(e) =>
268272
handleFieldChange(
269273
"additionalSpecifications.jobEndDate",

0 commit comments

Comments
 (0)