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
+42
View File
@@ -104,6 +104,27 @@ app.delete('/api/departments/:id', authenticateToken, async (req, res) => {
} }
}); });
app.put('/api/departments/:id', authenticateToken, async (req, res) => {
const { id } = req.params;
const { name } = req.body;
if (!name || name.trim() === '') {
return res.status(400).json({ error: 'Department name is required' });
}
try {
const result = await db.run('UPDATE departments SET name = ? WHERE id = ?', [name.trim(), id]);
if (result.changes === 0) {
return res.status(404).json({ error: 'Department not found' });
}
res.json({ id: parseInt(id, 10), name: name.trim() });
} catch (error) {
if (error.message.includes('UNIQUE')) {
return res.status(400).json({ error: 'Department with this name already exists' });
}
res.status(500).json({ error: error.message });
}
});
// 3. Companies Endpoints (New dictionary CRUD) // 3. Companies Endpoints (New dictionary CRUD)
app.get('/api/companies', async (req, res) => { app.get('/api/companies', async (req, res) => {
try { try {
@@ -142,6 +163,27 @@ app.delete('/api/companies/:id', authenticateToken, async (req, res) => {
} }
}); });
app.put('/api/companies/:id', authenticateToken, async (req, res) => {
const { id } = req.params;
const { name } = req.body;
if (!name || name.trim() === '') {
return res.status(400).json({ error: 'Company name is required' });
}
try {
const result = await db.run('UPDATE companies SET name = ? WHERE id = ?', [name.trim(), id]);
if (result.changes === 0) {
return res.status(404).json({ error: 'Company not found' });
}
res.json({ id: parseInt(id, 10), name: name.trim() });
} catch (error) {
if (error.message.includes('UNIQUE')) {
return res.status(400).json({ error: 'Company with this name already exists' });
}
res.status(500).json({ error: error.message });
}
});
// 4. Employees Endpoints (Search & List) // 4. Employees Endpoints (Search & List)
app.get('/api/employees', async (req, res) => { app.get('/api/employees', async (req, res) => {
const { search, departmentId } = req.query; const { search, departmentId } = req.query;
+156
View File
@@ -36,6 +36,68 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
setTimeout(() => setAlert({ type: '', message: '' }), 5000); 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 // Department Actions
// ---------------------------------------------------- // ----------------------------------------------------
@@ -421,8 +483,52 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
) : ( ) : (
companies.map((comp) => ( companies.map((comp) => (
<tr key={comp.id} style={{ cursor: 'default' }}> <tr key={comp.id} style={{ cursor: 'default' }}>
{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={{ fontWeight: 600 }}>{comp.name}</td>
<td style={{ textAlign: 'right' }}> <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 <button
className="btn btn-danger" className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }} style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
@@ -430,7 +536,10 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
> >
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить <DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button> </button>
</div>
</td> </td>
</>
)}
</tr> </tr>
)) ))
)} )}
@@ -486,8 +595,52 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
) : ( ) : (
departments.map((dept) => ( departments.map((dept) => (
<tr key={dept.id} style={{ cursor: 'default' }}> <tr key={dept.id} style={{ cursor: 'default' }}>
{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={{ fontWeight: 600 }}>{dept.name}</td>
<td style={{ textAlign: 'right' }}> <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 <button
className="btn btn-danger" className="btn btn-danger"
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }} style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
@@ -495,7 +648,10 @@ const AdminPanel = ({ token, employees, departments, companies, onRefreshData })
> >
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить <DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
</button> </button>
</div>
</td> </td>
</>
)}
</tr> </tr>
)) ))
)} )}