diff --git a/example.env b/example.env deleted file mode 100644 index 5de4bec9..00000000 --- a/example.env +++ /dev/null @@ -1,2 +0,0 @@ -GATEWAY_URL= -SCHEDULA_URL= \ No newline at end of file diff --git a/package.json b/package.json index 35bb7649..a428a340 100755 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "react-hook-form": "^7.41.5", "react-icons": "^4.8.0", "react-input-mask": "^2.0.4", + "react-pdf": "^7.0.3", "react-render-if-visible": "^2.1.1", "react-router-dom": "^6.6.1", "react-toastify": "^9.1.2", diff --git a/src/components/order-service-edit-form/index.tsx b/src/components/order-service-edit-form/index.tsx new file mode 100644 index 00000000..19d006a1 --- /dev/null +++ b/src/components/order-service-edit-form/index.tsx @@ -0,0 +1,378 @@ +/* eslint-disable prettier/prettier */ +/* eslint-disable @typescript-eslint/no-use-before-define */ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { useForm } from 'react-hook-form'; +import { AxiosResponse } from 'axios'; +import { ChangeEvent, useEffect, useState } from 'react'; +import { + Box, + Button, + Flex, + Grid, + GridItem, +} from '@chakra-ui/react'; +import { SingleValue } from 'chakra-react-select'; +import { format } from 'date-fns'; +import { Input } from '../form-fields/input'; +import { toast } from '@/utils/toast'; +import { api } from '@/config/lib/axios'; +import { EquipmentData } from '@/pages/equipments/EquipmentsControl'; +import { TextArea } from '../form-fields/text-area'; +import { NewControlledSelect } from '../form-fields/new-controlled-select'; +import { OSSTATUS } from '@/constants/orderservice'; +import { User } from '@/constants/user'; + +type EditOrderServiceFormValues = { + equipment: EquipmentData; + equipmentId: string; + seiProcess: string; + id: string; + status: string; + description: string; + finishDate: string; + withdrawalName?: string; + withdrawalDocument?: string; + senderName?: string; + senderDocument?: string; + technicianName?: string; +}; + +interface ISelectOption { + label: string; + value: number | string; +} + +interface EditOrderServiceFormProps { + onClose: () => void; + orderService: EditOrderServiceFormValues; + refreshRequest: boolean; + setRefreshRequest: React.Dispatch>; +} + +export default function OrderServiceEditForm({ + onClose, + orderService, + refreshRequest, + setRefreshRequest, +}: EditOrderServiceFormProps) { + const take = 5; + const [equipments, setEquipments] = useState([]); + const [buttonText, setButtonText] = useState('Salvar'); + + const fetchEquipments = async (str: string) => { + try { + const { data }: AxiosResponse = await api.get( + `equipment/find?searchTipping=${str}&take=${take}` + ); + setEquipments(data); + } catch (error) { + console.error('Nenhum Equipamento encontrado'); + } + }; + + const handleChange = (event: SingleValue) => { + const selectedOption = equipments.find( + (equipment) => equipment.tippingNumber === event?.value + ); + setSelectedEquipment(selectedOption as EquipmentData); + }; + const { + control, + register, + handleSubmit, + watch, + resetField, + formState: { errors }, + setValue, + } = useForm({ + defaultValues: orderService, + }); + + const watchStatus = watch('status'); + + useEffect(() => { + resetField('withdrawalName'); + resetField('withdrawalDocument'); + resetField('technicianName'); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [resetField, watchStatus]); + + const [selectedEquipment, setSelectedEquipment] = useState( + orderService.equipment + ); + console.log(orderService); + const debounce = void>(fn: T, ms = 400) => { + let timeoutId: ReturnType; + return function (this: any, ...args: Parameters) { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => fn.apply(this, args), ms); + }; + }; + const formattedOptions = ( + data: T[], + label: K, + value: K + ): ISelectOption[] => { + return data?.map((item: T) => { + const optionLable = String(item[label]); + const optionValue: number | string = String(item[value]); + return { label: optionLable, value: optionValue }; + }); + }; + const handleSearch = debounce(async (str) => { + if (str !== '') { + fetchEquipments(str); + } + }, 500); + const onSubmit = handleSubmit(async (formData) => { + try { + const { + seiProcess, + description, + status, + withdrawalName, + withdrawalDocument, + technicianName + } = formData; + const currentDate = new Date(); + const formattedDate = format(currentDate, 'yyyy-MM-dd'); + const payload = { + equipmentId: selectedEquipment.id, + seiProcess, + description, + withdrawalName, + withdrawalDocument, + status, + finishDate: formattedDate, + id: orderService.id, + technicianName + }; + + const response = await api.put( + 'equipment/updateOrderService', + payload + ); + + if (response.status === 200) { + toast.success('Ordem de serviço editada com sucesso', 'Sucesso'); + setRefreshRequest(!refreshRequest); + if (onClose) { + onClose(); + } + return; + } + toast.error('Erro ao tentar editar a Ordem de serviço', 'Erro'); + } catch { + toast.error('Erro ao tentar editar a Ordem de serviço', 'Erro'); + } + }); + + useEffect(() => { + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + watchStatus === 'CONCLUDED' ? setButtonText('Finalizar') : setButtonText('Salvar'); + }, [watchStatus]); + + useEffect(() => { }, []); + return ( +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Ordem de Serviço: + + + + + + + + + + + +