2024-06-01 15:47:53 +03:00
|
|
|
'use client';
|
|
|
|
import { useState } from "react";
|
|
|
|
|
2024-06-01 16:57:36 +03:00
|
|
|
const Form = <T,>({ initialData, submitFunction }) => {
|
2024-06-01 15:47:53 +03:00
|
|
|
const [formData, setFormData] = useState( initialData );
|
|
|
|
|
2024-06-01 16:57:36 +03:00
|
|
|
const handleSubmit = async (event) => {
|
2024-06-01 15:47:53 +03:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
//q = formData;
|
|
|
|
|
|
|
|
//addQuestion( q );
|
|
|
|
|
|
|
|
//console.log( `Adding question ${JSON.stringify(q)}` );
|
|
|
|
|
2024-06-01 16:57:36 +03:00
|
|
|
await submitFunction( formData );
|
2024-06-01 15:47:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleChange = (event) => {
|
|
|
|
const { name, value } = event.target;
|
|
|
|
setFormData( (prevData) => ({
|
|
|
|
...prevData,
|
|
|
|
[name]: value
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit} className="flex">
|
|
|
|
<div>
|
2024-06-01 16:57:36 +03:00
|
|
|
{Object.entries( initialData ).map( (elem) => {
|
2024-06-01 15:47:53 +03:00
|
|
|
return(
|
|
|
|
<input
|
2024-06-01 16:57:36 +03:00
|
|
|
type = { typeof( elem[1] ) == typeof( 0 ) ? "number" : "text" /* pretty hacky, we should change this */ }
|
|
|
|
id = {elem[0]}
|
|
|
|
name = {elem[0]}
|
|
|
|
value = {formData[elem[0]]}
|
2024-06-01 15:47:53 +03:00
|
|
|
onChange = {handleChange}
|
|
|
|
required
|
|
|
|
/>)})}
|
|
|
|
</div>
|
|
|
|
<button type="submit">Submit</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Form;
|
|
|
|
|