fullstackbench-tasks / screenshot2-add-expense-sparse /environment /app /frontend /src /components /CreateGroup.jsx
Download screenshot2-add-expense-sparse/environment/app/frontend/src/components/CreateGroup.jsx from ApolloTeam/fullstackbench-tasks: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/datasets/ApolloTeam/fullstackbench-tasks/resolve/main/screenshot2-add-expense-sparse/environment/app/frontend/src/components/CreateGroup.jsx
- Command line
-
hf download hf://datasets/ApolloTeam/fullstackbench-tasks/screenshot2-add-expense-sparse/environment/app/frontend/src/components/CreateGroup.jsx
-
curl -L -o CreateGroup.jsx https://huggingface.co/datasets/ApolloTeam/fullstackbench-tasks/resolve/main/screenshot2-add-expense-sparse/environment/app/frontend/src/components/CreateGroup.jsx
3.25 kB
| import { useState, useEffect } from 'react'; | |
| import { useNavigate, Link } from 'react-router-dom'; | |
| import { getUsers, createGroup } from '../api'; | |
| export default function CreateGroup() { | |
| const navigate = useNavigate(); | |
| const [users, setUsers] = useState([]); | |
| const [name, setName] = useState(''); | |
| const [selectedIds, setSelectedIds] = useState([]); | |
| const [error, setError] = useState(''); | |
| const [loading, setLoading] = useState(false); | |
| useEffect(() => { | |
| getUsers() | |
| .then(setUsers) | |
| .catch((e) => setError(e.message)); | |
| }, []); | |
| function toggleUser(id) { | |
| setSelectedIds((prev) => | |
| prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id] | |
| ); | |
| } | |
| async function handleSubmit(e) { | |
| e.preventDefault(); | |
| setError(''); | |
| if (!name.trim()) { | |
| setError('Group name is required'); | |
| return; | |
| } | |
| if (selectedIds.length < 2) { | |
| setError('Select at least 2 members'); | |
| return; | |
| } | |
| setLoading(true); | |
| try { | |
| await createGroup(name.trim(), selectedIds); | |
| navigate('/'); | |
| } catch (e) { | |
| setError(e.message); | |
| } finally { | |
| setLoading(false); | |
| } | |
| } | |
| return ( | |
| <div className="page-form"> | |
| <Link to="/" className="back-link"> | |
| ← Back to Dashboard | |
| </Link> | |
| <h1>Create Group</h1> | |
| <div className="card"> | |
| <div className="card-body"> | |
| {error && <div className="error-msg">{error}</div>} | |
| <form onSubmit={handleSubmit}> | |
| <div className="form-group"> | |
| <label>Group Name</label> | |
| <input | |
| className="form-input" | |
| type="text" | |
| placeholder="e.g. Trip to Hawaii" | |
| value={name} | |
| onChange={(e) => setName(e.target.value)} | |
| required | |
| /> | |
| </div> | |
| <div className="form-group"> | |
| <label>Select Members</label> | |
| {users.length === 0 ? ( | |
| <p style={{ color: 'var(--text-light)', fontSize: 14 }}> | |
| No users available. Add users from the dashboard first. | |
| </p> | |
| ) : ( | |
| <div className="checkbox-list"> | |
| {users.map((u) => ( | |
| <label | |
| key={u.id} | |
| className={`checkbox-item ${selectedIds.includes(u.id) ? 'checked' : ''}`} | |
| > | |
| <input | |
| type="checkbox" | |
| checked={selectedIds.includes(u.id)} | |
| onChange={() => toggleUser(u.id)} | |
| /> | |
| {u.name} | |
| </label> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| <div className="form-actions"> | |
| <Link to="/" className="btn btn-secondary"> | |
| Cancel | |
| </Link> | |
| <button | |
| type="submit" | |
| className="btn btn-primary" | |
| disabled={loading} | |
| > | |
| {loading ? 'Creating...' : 'Create Group'} | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| } | |