feat: add editing for companies and departments

This commit is contained in:
2026-05-26 00:53:22 +07:00
parent 662842019c
commit 6196f287dd
2 changed files with 218 additions and 20 deletions
+176 -20
View File
@@ -36,6 +36,68 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
setTimeout(() => setAlert({ type: '', message: '' }), 5000);
};
// Department Edit States
const [editingDeptId, setEditingDeptId] = useState(null);
const [editingDeptName, setEditingDeptName] = useState('');
// Company Edit States
const [editingCompanyId, setEditingCompanyId] = useState(null);
const [editingCompanyName, setEditingCompanyName] = useState('');
const handleEditCompanySubmit = async (id) => {
if (!editingCompanyName.trim()) return;
try {
const res = await fetch(`/api/companies/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ name: editingCompanyName.trim() })
});
const data = await res.json();
if (res.ok) {
setEditingCompanyId(null);
setEditingCompanyName('');
onRefreshData();
showAlert('success', 'Название компании успешно обновлено!');
} else {
showAlert('error', data.error || 'Ошибка при обновлении названия компании');
}
} catch (err) {
showAlert('error', 'Ошибка сети при обновлении компании');
}
};
const handleEditDeptSubmit = async (id) => {
if (!editingDeptName.trim()) return;
try {
const res = await fetch(`/api/departments/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ name: editingDeptName.trim() })
});
const data = await res.json();
if (res.ok) {
setEditingDeptId(null);
setEditingDeptName('');
onRefreshData();
showAlert('success', 'Название подразделения успешно обновлено!');
} else {
showAlert('error', data.error || 'Ошибка при обновлении подразделения');
}
} catch (err) {
showAlert('error', 'Ошибка сети при обновлении подразделения');
}
};
// ----------------------------------------------------
// Department Actions
// ----------------------------------------------------
@@ -421,16 +483,63 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
) : (
companies.map((comp) => (
<tr key={comp.id} style={{ cursor: 'default' }}>
<td style={{ fontWeight: 600 }}>{comp.name}</td>
<td style={{ textAlign: 'right' }}>
<button
className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleDeleteCompany(comp.id, comp.name)}
>
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button>
</td>
{editingCompanyId === comp.id ? (
<>
<td>
<input
type="text"
className="form-control"
value={editingCompanyName}
onChange={(e) => setEditingCompanyName(e.target.value)}
style={{ width: '100%' }}
required
/>
</td>
<td style={{ textAlign: 'right' }}>
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
<button
className="btn btn-primary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleEditCompanySubmit(comp.id)}
>
Сохранить
</button>
<button
className="btn btn-secondary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => setEditingCompanyId(null)}
>
Отмена
</button>
</div>
</td>
</>
) : (
<>
<td style={{ fontWeight: 600 }}>{comp.name}</td>
<td style={{ textAlign: 'right' }}>
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
<button
className="btn btn-secondary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => {
setEditingCompanyId(comp.id);
setEditingCompanyName(comp.name);
}}
>
<EditIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Редактировать
</button>
<button
className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleDeleteCompany(comp.id, comp.name)}
>
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button>
</div>
</td>
</>
)}
</tr>
))
)}
@@ -486,16 +595,63 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
) : (
departments.map((dept) => (
<tr key={dept.id} style={{ cursor: 'default' }}>
<td style={{ fontWeight: 600 }}>{dept.name}</td>
<td style={{ textAlign: 'right' }}>
<button
className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleDeleteDept(dept.id, dept.name)}
>
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button>
</td>
{editingDeptId === dept.id ? (
<>
<td>
<input
type="text"
className="form-control"
value={editingDeptName}
onChange={(e) => setEditingDeptName(e.target.value)}
style={{ width: '100%' }}
required
/>
</td>
<td style={{ textAlign: 'right' }}>
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
<button
className="btn btn-primary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleEditDeptSubmit(dept.id)}
>
Сохранить
</button>
<button
className="btn btn-secondary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => setEditingDeptId(null)}
>
Отмена
</button>
</div>
</td>
</>
) : (
<>
<td style={{ fontWeight: 600 }}>{dept.name}</td>
<td style={{ textAlign: 'right' }}>
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
<button
className="btn btn-secondary"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => {
setEditingDeptId(dept.id);
setEditingDeptName(dept.name);
}}
>
<EditIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Редактировать
</button>
<button
className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
onClick={() => handleDeleteDept(dept.id, dept.name)}
>
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button>
</div>
</td>
</>
)}
</tr>
))
)}