mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Починка добавления сотрудника3
This commit is contained in:
@@ -1,19 +1,18 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted } from 'vue';
|
import { ref, onMounted } from 'vue';
|
||||||
import { departments } from '@/utils/constants';
|
import { departments } from '@/utils/constants';
|
||||||
import type { Employee } from '@/types/employee';
|
import type { Employee, EmployeeFormData } from '@/types/employee';
|
||||||
import EmployeeForm from './EmployeeForm.vue'; // Добавляем импорт
|
import EmployeeForm from './EmployeeForm.vue';
|
||||||
|
|
||||||
const employees = ref<Employee[]>([]);
|
const employees = ref<Employee[]>([]);
|
||||||
const showAddForm = ref(false);
|
const showAddForm = ref(false);
|
||||||
const editingEmployee = ref<Employee | null>(null);
|
const editingEmployee = ref<Employee | null>(null);
|
||||||
|
|
||||||
|
|
||||||
function getDepartmentLabel(value: string) {
|
function getDepartmentLabel(value: string) {
|
||||||
return departments.find(d => d.value === value)?.label || value;
|
return departments.find(d => d.value === value)?.label || value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function editEmployee(employee: any) {
|
function editEmployee(employee: Employee) {
|
||||||
editingEmployee.value = employee;
|
editingEmployee.value = employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,15 +21,20 @@ function closeForm() {
|
|||||||
editingEmployee.value = null;
|
editingEmployee.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSubmit(data: any) {
|
async function handleSubmit(data: EmployeeFormData) {
|
||||||
try {
|
try {
|
||||||
if (editingEmployee.value) {
|
if (editingEmployee.value) {
|
||||||
// Update existing employee
|
// Update existing employee
|
||||||
await fetch(`/api/employees/${editingEmployee.value.id}`, {
|
const response = await fetch(`/api/employees/${editingEmployee.value.id}`, {
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
throw new Error(error.detail || 'Failed to update employee');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Create new employee
|
// Create new employee
|
||||||
const response = await fetch('/api/employees/', {
|
const response = await fetch('/api/employees/', {
|
||||||
@@ -48,6 +52,7 @@ async function handleSubmit(data: any) {
|
|||||||
closeForm();
|
closeForm();
|
||||||
alert(editingEmployee.value ? 'Сотрудник обновлен' : 'Сотрудник добавлен');
|
alert(editingEmployee.value ? 'Сотрудник обновлен' : 'Сотрудник добавлен');
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.error('Error:', error);
|
||||||
alert(`Ошибка: ${error.message}`);
|
alert(`Ошибка: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,7 +120,6 @@ onMounted(fetchEmployees);
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Employee form modal -->
|
|
||||||
<EmployeeForm
|
<EmployeeForm
|
||||||
v-if="showAddForm || editingEmployee"
|
v-if="showAddForm || editingEmployee"
|
||||||
:employee="editingEmployee"
|
:employee="editingEmployee"
|
||||||
|
Reference in New Issue
Block a user