Skip to content

Commit

Permalink
Add support for datetime fields in generic-editor for contests
Browse files Browse the repository at this point in the history
  • Loading branch information
aahnik committed Jan 11, 2025
1 parent ccdb9c7 commit e070295
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/[orgId]/contests/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const fields: Field[] = [
placeholder: "Unique contest ID",
},
{ name: "description", label: "Description", type: "textarea" },
{ name: "startTime", label: "Start Time", type: "date" },
{ name: "endTime", label: "End Time", type: "date" },
{ name: "startTime", label: "Start Time", type: "datetime" },
{ name: "endTime", label: "End Time", type: "datetime" },
{
name: "problems",
label: "Problems",
Expand Down
47 changes: 36 additions & 11 deletions mint/generic-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { z } from "zod";
export interface Field {
name: string;
label: string;
type: "text" | "number" | "date" | "select" | "textarea";
type: "text" | "number" | "date" | "datetime" | "select" | "textarea";
options?: { value: string; label: string }[];
placeholder?: string;
}
Expand Down Expand Up @@ -62,24 +62,37 @@ export function GenericEditor<T>({

useEffect(() => {
if (data) {
reset(data);
// Convert datetime strings to local datetime-local format for input
const formattedData = { ...data };
fields.forEach((field) => {
if (field.type === "datetime" && formattedData[field.name as keyof typeof formattedData]) {
const date = new Date(formattedData[field.name as keyof typeof formattedData] as string);
formattedData[field.name as keyof typeof formattedData] = date
.toISOString()
.slice(0, 16) as any;
}
});
reset(formattedData);
} else {
reset({} as T);
}
}, [data, reset]);
}, [data, reset, fields]);

const onSubmit = (formData: T) => {
// If this is a new entry, add an id
console.log("onSubmit in GenericEditor");
console.log("formData", formData);
// Convert datetime-local values to ISO strings
const processedData = { ...formData };
fields.forEach((field) => {
if (field.type === "datetime" && processedData[field.name as keyof typeof processedData]) {
const date = new Date(processedData[field.name as keyof typeof processedData] as string);
processedData[field.name as keyof typeof processedData] = date.toISOString() as any;
}
});

// If this is a new entry, add an id
if (!data) {
formData = {
...formData,
id: Date.now(),
} as T;
processedData.id = Date.now() as any;
}
onSave(formData);
onSave(processedData as T);
onClose();
};

Expand Down Expand Up @@ -138,6 +151,18 @@ export function GenericEditor<T>({
: ""
}
/>
) : field.type === "datetime" ? (
<Input
id={field.name}
type="datetime-local"
placeholder={field.placeholder || ""}
{...register(field.name as any)}
className={
errors[field.name as keyof typeof errors]
? "border-red-500"
: ""
}
/>
) : (
<Input
id={field.name}
Expand Down

0 comments on commit e070295

Please sign in to comment.