feat: add editing for companies and departments
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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,16 +483,63 @@ 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' }}>
|
||||||
<td style={{ fontWeight: 600 }}>{comp.name}</td>
|
{editingCompanyId === comp.id ? (
|
||||||
<td style={{ textAlign: 'right' }}>
|
<>
|
||||||
<button
|
<td>
|
||||||
className="btn btn-danger"
|
<input
|
||||||
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
|
type="text"
|
||||||
onClick={() => handleDeleteCompany(comp.id, comp.name)}
|
className="form-control"
|
||||||
>
|
value={editingCompanyName}
|
||||||
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
|
onChange={(e) => setEditingCompanyName(e.target.value)}
|
||||||
</button>
|
style={{ width: '100%' }}
|
||||||
</td>
|
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>
|
</tr>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
@@ -486,16 +595,63 @@ 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' }}>
|
||||||
<td style={{ fontWeight: 600 }}>{dept.name}</td>
|
{editingDeptId === dept.id ? (
|
||||||
<td style={{ textAlign: 'right' }}>
|
<>
|
||||||
<button
|
<td>
|
||||||
className="btn btn-danger"
|
<input
|
||||||
style={{ padding: '0.35rem 0.65rem', fontSize: '0.8rem' }}
|
type="text"
|
||||||
onClick={() => handleDeleteDept(dept.id, dept.name)}
|
className="form-control"
|
||||||
>
|
value={editingDeptName}
|
||||||
<DeleteIcon style={{ width: '0.9rem', height: '0.9rem' }} /> Удалить
|
onChange={(e) => setEditingDeptName(e.target.value)}
|
||||||
</button>
|
style={{ width: '100%' }}
|
||||||
</td>
|
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>
|
</tr>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user