import BaseButton from '@/components/ui-kit/BaseButton'; import BaseModal from '@/components/ui-kit/BaseModal'; import BaseReactSelect from '@/components/ui-kit/BaseReactSelect'; import { box_requests } from '@/data/box/box.requests'; import { customer_requests } from '@/data/customers/customer.requests'; import { invoice_requests } from '@/data/invoice/invoice.requests'; import { useMyTranslation } from '@/hooks/useMyTranslation'; import useRequest from '@/hooks/useRequest'; import { notifyUnknownError } from '@/services/notification'; import { Box, Stack, Typography, styled } from '@mui/material'; import React, { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; const StyledBox = styled(Box)` .title { color: #000; font-size: 20px; font-style: normal; font-weight: 600; line-height: 24px; margin-bottom: 28px; } .label { color: #5d5850; font-size: 16px; font-style: normal; font-weight: 500; line-height: 24px; margin-bottom: 8px; } `; type Props = { onClose: () => void; open: boolean; onSuccess: () => void; defaultValues?: { clientId?: number; packetId?: number; }; }; const AttachInvoiceModal = ({ onClose, open, onSuccess, defaultValues }: Props) => { const t = useMyTranslation(); const [loading, setLoading] = useState(false); const getClientsQuery = useRequest(() => customer_requests.getAll(), { selectData(data) { return data.data.data.data.map(i => ({ label: i.fullName, value: i.id })); }, }); const getBoxesQuery = useRequest(() => box_requests.getAll(), { selectData(data) { return data.data.data.data.map(i => ({ value: i.id, label: i.name })); }, }); const clientsList = getClientsQuery.data || []; const boxesList = getBoxesQuery.data || []; const { register, control, handleSubmit, formState: { errors }, } = useForm<{ clientId: number; packetId: number; }>({ defaultValues: { ...defaultValues, }, }); const onSubmit = handleSubmit(async values => { try { setLoading(true); await invoice_requests.create({ ...values }); onSuccess(); } catch (error) { notifyUnknownError(error); } finally { setLoading(false); } }); return ( {t('create_invoice')} {t('role')} { return ( p.value === field.value)} onChange={(newValue: any) => { field.onChange(newValue.value); }} onBlur={field.onBlur} name={field.name} options={clientsList} /> ); }} /> {t('role')} { return ( p.value === field.value)} onChange={(newValue: any) => { field.onChange(newValue.value); }} onBlur={field.onBlur} name={field.name} options={boxesList} /> ); }} /> {t('create')} {t('cancel')} ); }; export default AttachInvoiceModal;