Skip to content

Instantly share code, notes, and snippets.

@OldrichKruchna
Forked from lucsh/ex.tsx
Last active May 16, 2025 09:10
Show Gist options
  • Save OldrichKruchna/8c6d610eb7f691011bc6e795abd89670 to your computer and use it in GitHub Desktop.
Save OldrichKruchna/8c6d610eb7f691011bc6e795abd89670 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react';
const initialUsers = [
{ id: 1, name: 'Alice', email: 'alice@example.com', status: 1, lastEdited: '2025-04-01' },
{ id: 2, name: 'Bob', email: 'bob@example.com', status: 0, lastEdited: '2025-04-02' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com', status: 1, lastEdited: '2025-04-03' },
{ id: 4, name: 'David', email: 'david@example.com', status: 0, lastEdited: '2025-04-01' },
];
const UserList = () => {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [filter, setFilter] = useState('all'); // Filter by status (active/inactive)
const [undoHistory, setUndoHistory] = useState([]); // Store history of edits
// Mocked fetching data (simulated async API call)
useEffect(() => {
const timeoutId = setTimeout(() => {
setUsers(initialUsers);
setLoading(false);
}, 1000);
return () => clearTimeout(timeoutId);
}, []);
// Handle inline editing
const handleEdit = (id, newName) => {
// Implement this
};
// Undo last edit
const handleUndo = () => {
// Implement this
};
// Filter users by status
// Implement this
const filteredUsers = []
// Sort users by last edited date (descending)
// Implement this
const sortedUsers = []
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error loading users: {error}</div>;
}
return (
<div>
<div>
<button onClick={() => setFilter('all')}>All Users</button>
<button onClick={() => setFilter('active')}>Active Users</button>
<button onClick={() => setFilter('inactive')}>Inactive Users</button>
</div>
<div>
<button onClick={handleUndo} disabled={undoHistory.length === 0}>Undo Last Edit</button>
</div>
<ul>
{sortedUsers.map(user => (
<li key={user.id}>
<span>{user.name}</span>
<input
type="text"
value={user.name}
onChange={(e) => handleEdit(user.id, e.target.value)}
/>
<span>({user.status === 1 ? 'Active' : 'Inactive'})</span>
</li>
))}
</ul>
</div>
);
};
export default UserList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment