Spaces:
Sleeping
Sleeping
File size: 6,771 Bytes
1c8e50c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import React, { useEffect, useState } from 'react';
import { submissionAPI, taskAPI, userAPI } from '../../services/api';
import Loader from '../common/Loader';
import EmptyState from '../common/EmptyState';
import { formatDateTime, safeArray } from '../../utils/formatters';
const AdminPanel = () => {
const [users, setUsers] = useState([]);
const [submissions, setSubmissions] = useState([]);
const [newTask, setNewTask] = useState({ title: '', description: '', dueDate: '' });
const [loading, setLoading] = useState(true);
const [dashboardError, setDashboardError] = useState('');
const [taskError, setTaskError] = useState('');
const [creatingTask, setCreatingTask] = useState(false);
useEffect(() => {
const loadDashboard = async () => {
try {
setLoading(true);
const [usersResponse, submissionsResponse] = await Promise.all([
userAPI.getAllUsers(),
submissionAPI.getAllSubmissions(),
]);
setUsers(safeArray(usersResponse?.data));
setSubmissions(safeArray(submissionsResponse?.data));
} catch (err) {
const message = err?.response?.data?.message ?? 'We were unable to load admin data.';
setDashboardError(message);
} finally {
setLoading(false);
}
};
loadDashboard();
}, []);
const handleTaskChange = (event) => {
const { name, value } = event.target;
setNewTask((prev) => ({ ...prev, [name]: value }));
};
const handleCreateTask = async (event) => {
event.preventDefault();
setCreatingTask(true);
setTaskError('');
try {
await taskAPI.createTask(newTask);
setNewTask({ title: '', description: '', dueDate: '' });
} catch (err) {
const message = err?.response?.data?.message ?? 'The task could not be created.';
setTaskError(message);
} finally {
setCreatingTask(false);
}
};
if (loading) {
return <Loader label="Loading admin overview" />;
}
return (
<div className="grid" style={{ gap: '2rem' }}>
{dashboardError && (
<div className="alert alert--error" role="alert">
{dashboardError}
</div>
)}
<form className="card form" onSubmit={handleCreateTask} noValidate>
<header>
<h3 className="section-heading" style={{ marginBottom: '0.25rem' }}>
Quick task creation
</h3>
<p className="section-subheading" style={{ margin: 0 }}>
Create work items directly from the admin panel.
</p>
</header>
<div className="form-field">
<label className="form-label" htmlFor="admin-task-title">
Title
</label>
<input
id="admin-task-title"
className="form-input"
name="title"
value={newTask.title}
onChange={handleTaskChange}
required
/>
</div>
<div className="form-field">
<label className="form-label" htmlFor="admin-task-description">
Description
</label>
<textarea
id="admin-task-description"
className="form-textarea"
name="description"
value={newTask.description}
onChange={handleTaskChange}
required
minLength={10}
/>
</div>
<div className="form-field">
<label className="form-label" htmlFor="admin-task-due-date">
Due date
</label>
<input
id="admin-task-due-date"
className="form-input"
type="datetime-local"
name="dueDate"
value={newTask.dueDate}
onChange={handleTaskChange}
/>
</div>
{taskError && (
<div className="alert alert--error" role="alert">
{taskError}
</div>
)}
<div>
<button className="button button--primary" type="submit" disabled={creatingTask}>
{creatingTask ? 'Creating…' : 'Create task'}
</button>
</div>
</form>
<section className="card" aria-labelledby="admin-users-heading">
<header style={{ marginBottom: '1.25rem' }}>
<h3 id="admin-users-heading" className="section-heading" style={{ margin: 0 }}>
Team members
</h3>
</header>
{users.length ? (
<div className="table-list" role="list">
{users.map((user) => (
<article key={user?.id ?? user?.email} role="listitem" className="card" style={{ boxShadow: 'none' }}>
<div style={{ fontWeight: 600 }}>{user?.fullName ?? user?.email}</div>
<div style={{ color: 'var(--color-muted)', fontSize: '0.9rem' }}>{user?.email}</div>
<span className="status-badge status-badge--muted" style={{ marginTop: '0.5rem' }}>
{user?.role ?? 'User'}
</span>
</article>
))}
</div>
) : (
<EmptyState title="No users" description="Invite colleagues to collaborate on tasks." />
)}
</section>
<section className="card" aria-labelledby="admin-submissions-heading">
<header style={{ marginBottom: '1.25rem' }}>
<h3 id="admin-submissions-heading" className="section-heading" style={{ margin: 0 }}>
Recent submissions
</h3>
</header>
{submissions.length ? (
<div className="table-list" role="list">
{submissions.slice(0, 8).map((submission) => (
<article key={submission?.id} role="listitem" className="card" style={{ boxShadow: 'none' }}>
<div style={{ display: 'grid', gap: '0.35rem' }}>
<div style={{ fontWeight: 600 }}>Task #{submission?.taskId}</div>
<p style={{ margin: 0, color: 'var(--color-muted)' }}>{submission?.content}</p>
</div>
<div className="submission-card__meta" style={{ marginTop: '0.85rem' }}>
<span
className={`status-badge ${
submission?.status === 'submitted' ? 'status-badge--success' : 'status-badge--warning'
}`}
>
{submission?.status ?? 'Pending'}
</span>
{submission?.createdAt && <span>{formatDateTime(submission?.createdAt)}</span>}
</div>
</article>
))}
</div>
) : (
<EmptyState title="No submissions" description="Submissions will appear here once team members upload work." />
)}
</section>
</div>
);
};
export default AdminPanel;
|