-
Notifications
You must be signed in to change notification settings - Fork 1
/
Detail_post_modal.tsx
139 lines (122 loc) · 4.01 KB
/
Detail_post_modal.tsx
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
import React, { useState } from "react";
import { Button, Modal, Form, Input, DatePicker, Select } from "antd";
import dayjs from 'dayjs';
import postDetail from '@/services/client/fetching/hooks/postDetail'
import type {
Client,
Service,
ExtendedEmployee,
ServiceConsumption_type,
} from "@/models/types";
const { Option } = Select;
interface ServiceConsumptionModalProps {
clients: Client[];
employees: ExtendedEmployee[];
services: Service[];
details: ServiceConsumption_type[];
}
const ServiceConsumptionModal: React.FC<ServiceConsumptionModalProps> = ({
clients,
employees,
details,
services,
}) => {
const [isModalOpen, setIsModalOpen] = useState(false as boolean);
const [selectedService, setSelectedService] = useState({} as Service);
const [selectedEmployee, setSelectedEmployee] = useState({} as ExtendedEmployee);
const [form] = Form.useForm();
const showModal = () => {
setIsModalOpen(true);
};
const handleOk = () => {
setIsModalOpen(false);
};
const handleCancel = () => {
setIsModalOpen(false);
};
const handleServiceChange = (value: number) => {
setSelectedService(services.find((service) => service.id === value) as Service);
};
const handleEmployeeChange = (value: number) => {
setSelectedEmployee(employees.find((employee) => employee.id === value) as ExtendedEmployee);
};
const onFinish = (values: any) => {
console.log('Received values of form: ', values);
handleOk();
const body = values;
body.mode = 'Silent';
postDetail(values);
};
const disabledDates = details
.filter((detail) => detail.state === "Pending" && detail.employee_id === selectedEmployee.id)
.map((detail) => dayjs(detail.reserved_at).format("YYYY-MM-DD HH:mm"));
const isDateDisabled = (currentDate: dayjs.Dayjs) => {
return disabledDates.includes(currentDate.format("YYYY-MM-DD HH:mm"));
};
const filteredEmployees = employees.filter((employee) => {
return selectedService
// @ts-ignore
? (employee.student?.disciplines.includes(selectedService.discipline)
// @ts-ignore
|| employee.teacher?.disciplines.includes(selectedService.discipline))
: true;
});
return (
<>
<Button type="primary" onClick={showModal}>
Crear consumo de servicio
</Button>
<Modal
title="Consumo de servicio"
visible={isModalOpen}
onOk={onFinish}
onCancel={handleCancel}
footer={null}
>
<Form form={form} onFinish={onFinish}>
<Form.Item label="Servicio" name="service_id" required={true}>
<Select placeholder="Selecciona un servicio" onChange={handleServiceChange}>
{services.map((service) => (
<Option key={service.id} value={service.id}>
{service.name}
</Option>
))}
</Select>
</Form.Item>
<Form.Item label="Empleado" name="employee_id" required={true}>
<Select placeholder="Selecciona un empleado" onChange={handleEmployeeChange}>
{filteredEmployees.map((employee) => (
<Option key={employee.id} value={employee.id}>
{employee.teacher
? employee.teacher.name + " " + employee.teacher.surname
: employee.student
? employee.student.name + " " + employee.student.surname
: ''}
</Option>
))}
</Select>
</Form.Item>
<Form.Item label="Cliente" name="client_id" required={true}>
<Select placeholder="Selecciona un cliente">
{clients.map((client) => (
<Option key={client.id} value={client.id}>{client.name}</Option>
))}
</Select>
</Form.Item>
<Form.Item label="Fecha y hora reservada" name="reserved_at" required={true}>
<DatePicker showTime format="YYYY-MM-DD HH:mm" disabledDate={isDateDisabled} />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Enviar
</Button>
<Button type="default" htmlType="button" onClick={handleCancel}>
Cancelar
</Button>
</Form.Item>
</Form>
</Modal>
</>
);
};
export default ServiceConsumptionModal;