Last active
May 16, 2025 15:14
-
-
Save lucsh/2380d91eb8405a9ba7f026149d9379fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
const initialUsers = [ | |
{ id: 1, name: 'Alice', email: '[email protected]', status: 1, lastEdited: '2025-04-01' }, | |
{ id: 2, name: 'Bob', email: '[email protected]', status: 0, lastEdited: '2025-04-02' }, | |
{ id: 3, name: 'Charlie', email: '[email protected]', status: 1, lastEdited: '2025-04-03' }, | |
{ id: 4, name: 'David', email: '[email protected]', 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 | |
// Mock fetching data (simulate async API call) | |
useEffect(() => { | |
// Implement this | |
}, []); | |
// 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