Communiquest/communiquest/components/QuestCreationForm.tsx
2024-09-15 01:18:39 +03:00

100 lines
3.3 KiB
TypeScript

import React, { useState } from 'react';
interface QuestFormData {
title: string;
description: string;
reward: string;
difficulty: 'Easy' | 'Medium' | 'Hard';
}
interface QuestCreationFormProps {
onSubmit: (data: QuestFormData) => void;
isSubmitting: boolean;
}
export const QuestCreationForm: React.FC<QuestCreationFormProps> = ({ onSubmit, isSubmitting }) => {
const [formData, setFormData] = useState<QuestFormData>({
title: '',
description: '',
reward: '',
difficulty: 'Medium',
});
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => {
const { name, value } = e.target;
setFormData(prev => ({ ...prev, [name]: value }));
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
// Validate reward to be a number
if (isNaN(Number(formData.reward))) {
alert('Reward must be a valid number');
return;
}
onSubmit(formData);
};
return (
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="title" className="block text-sm font-medium text-gray-700">Title</label>
<input
type="text"
id="title"
name="title"
value={formData.title}
onChange={handleChange}
required
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
/>
</div>
<div>
<label htmlFor="description" className="block text-sm font-medium text-gray-700">Description</label>
<textarea
id="description"
name="description"
value={formData.description}
onChange={handleChange}
required
rows={4}
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
></textarea>
</div>
<div>
<label htmlFor="reward" className="block text-sm font-medium text-gray-700">Reward</label>
<input
type="text"
id="reward"
name="reward"
value={formData.reward}
onChange={handleChange}
required
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
/>
</div>
<div>
<label htmlFor="difficulty" className="block text-sm font-medium text-gray-700">Difficulty</label>
<select
id="difficulty"
name="difficulty"
value={formData.difficulty}
onChange={handleChange}
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
>
<option value="Easy">Easy</option>
<option value="Medium">Medium</option>
<option value="Hard">Hard</option>
</select>
</div>
<button
type="submit"
disabled={isSubmitting}
className="w-full bg-blue-500 text-white py-2 px-4 rounded hover:bg-blue-600 transition-colors disabled:opacity-50"
>
{isSubmitting ? 'Creating...' : 'Create Quest'}
</button>
</form>
);
};