|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import type {Workspace, WorkspaceRequest} from '@/types'; |
| 3 | + |
| 4 | +interface WorkspaceFormProps { |
| 5 | + workspace?: Workspace | null; |
| 6 | + onSubmit: (data: WorkspaceRequest) => Promise<void>; |
| 7 | + onCancel: () => void; |
| 8 | + loading?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +const WorkspaceForm: React.FC<WorkspaceFormProps> = ({workspace, onSubmit, onCancel, loading = false}) => { |
| 12 | + const [name, setName] = useState(''); |
| 13 | + const [description, setDescription] = useState(''); |
| 14 | + const [error, setError] = useState(''); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + if (workspace) { |
| 18 | + setName(workspace.name); |
| 19 | + setDescription(workspace.description || ''); |
| 20 | + } else { |
| 21 | + setName(''); |
| 22 | + setDescription(''); |
| 23 | + } |
| 24 | + }, [workspace]); |
| 25 | + |
| 26 | + const handleSubmit = async (e: React.SubmitEvent) => { |
| 27 | + e.preventDefault(); |
| 28 | + |
| 29 | + setError(''); |
| 30 | + |
| 31 | + if (!name.trim()) { |
| 32 | + setError('Name is required'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + await onSubmit({ |
| 38 | + name: name.trim(), |
| 39 | + description: description.trim() || undefined |
| 40 | + }); |
| 41 | + } catch (err: unknown) { |
| 42 | + if (err && typeof err === 'object' && 'response' in err) { |
| 43 | + const axiosError = err as { response?: { data?: { message?: string } } }; |
| 44 | + setError(axiosError.response?.data?.message || 'An error occurred'); |
| 45 | + } else { |
| 46 | + setError('An error occurred'); |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 53 | + {error && ( |
| 54 | + <div className="bg-error-50 text-error-700 px-4 py-3 rounded-lg text-md"> |
| 55 | + {error} |
| 56 | + </div> |
| 57 | + )} |
| 58 | + |
| 59 | + <div> |
| 60 | + <label htmlFor="name" className="block text-md font-medium text-neutral-700 mb-2"> |
| 61 | + Name |
| 62 | + </label> |
| 63 | + <input |
| 64 | + id="name" |
| 65 | + type="text" |
| 66 | + value={name} |
| 67 | + onChange={(e) => setName(e.target.value)} |
| 68 | + className="w-full px-4 py-2 border border-neutral-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors" |
| 69 | + placeholder="My API Project" |
| 70 | + required |
| 71 | + autoFocus |
| 72 | + /> |
| 73 | + </div> |
| 74 | + |
| 75 | + <div> |
| 76 | + <label htmlFor="description" className="block text-md font-medium text-neutral-700 mb-2"> |
| 77 | + Description |
| 78 | + </label> |
| 79 | + <textarea |
| 80 | + id="description" |
| 81 | + value={description} |
| 82 | + onChange={(e) => setDescription(e.target.value)} |
| 83 | + rows={3} |
| 84 | + className="w-full px-4 py-2 border border-neutral-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors resize-none" |
| 85 | + placeholder="Optional description for this workspace..." |
| 86 | + /> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="flex justify-end gap-3 pt-4"> |
| 90 | + <button |
| 91 | + type="button" |
| 92 | + onClick={onCancel} |
| 93 | + disabled={loading} |
| 94 | + className="px-4 py-2 text-md font-medium text-neutral-700 hover:bg-neutral-100 rounded-lg transition-colors disabled:opacity-50 cursor-pointer" |
| 95 | + > |
| 96 | + Cancel |
| 97 | + </button> |
| 98 | + <button |
| 99 | + type="submit" |
| 100 | + disabled={loading} |
| 101 | + className="px-4 py-2 text-md font-medium text-white bg-primary-600 hover:bg-primary-700 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer" |
| 102 | + > |
| 103 | + {loading ? 'Saving...' : workspace ? 'Update' : 'Create'} |
| 104 | + </button> |
| 105 | + </div> |
| 106 | + </form> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +export default WorkspaceForm; |
0 commit comments