'use client'; import BaseButton from '@/components/ui-kit/BaseButton'; import BaseIconButton from '@/components/ui-kit/BaseIconButton'; import { party_requests } from '@/data/party/party.requests'; import { box_requests } from '@/data/box/box.requests'; import { real_box_requests } from '@/data/real-box/real-box.requests'; import { pageLinks } from '@/helpers/constants'; import { notifyUnknownError } from '@/services/notification'; import { Box, FormHelperText, Grid, Stack, Typography, styled } from '@mui/material'; import { AddCircleRounded, Close } from '@mui/icons-material'; import { useSearchParams } from 'next/navigation'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import { Controller, useFieldArray, useForm } from 'react-hook-form'; import AsyncSelect from 'react-select/async'; import { selectDefaultStyles } from '@/components/ui-kit/BaseReactSelect'; import { useAuthContext } from '@/context/auth-context'; import { useMyNavigation } from '@/hooks/useMyNavigation'; import { useMyTranslation } from '@/hooks/useMyTranslation'; import { FormValues, RealCreateBoxBodyType, UpdateRealBoxBodyType } from '@/data/real-box/real-box.model'; import get from 'lodash.get'; import useRequest from '@/hooks/useRequest'; const StyledCreateBox = styled(Box)` .item-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; } & > * { flex: 1 1 1; } `; interface Props { partiesData?: { value: number; label: string }[]; initialValues?: { id?: number; boxId?: string; partyId?: number; partyName?: string; paketIds?: Array<{ id: number; packetName: string }>; }; } const DashboardCreateRealBoxPage = ({ initialValues, partiesData }: Props) => { const { user, isAdmin: isAdminUser } = useAuthContext(); const editMode = !!initialValues?.id; const t = useMyTranslation(); const params = useSearchParams(); const { push } = useMyNavigation(); const [partyId, setPartyId] = useState(''); const [loading, setLoading] = useState(false); const helperRef = useRef<{ finished: boolean; partyFinished: boolean; clientFinished: boolean; settedDefaultParty: any; }>({ settedDefaultParty: null, partyFinished: false, clientFinished: false, finished: false, }); const { register, control, handleSubmit, setValue, reset, formState: { errors }, } = useForm({ defaultValues: { partyName: initialValues?.partyName || '', paketIds: initialValues?.paketIds ? initialValues.paketIds.map((paket) => ({ id: paket.id })) : params.get('party_id') ? [{ id: +params.get('party_id')! }] : [{ id: '' }], id: initialValues?.id, boxId: initialValues?.boxId, partyId: initialValues?.partyId, }, }); // Reset form when initialValues change (for edit mode) useEffect(() => { if (initialValues) { reset({ partyName: initialValues.partyName || '', paketIds: initialValues.paketIds ? initialValues.paketIds.map((paket) => ({ id: paket.id })) : [{ id: '' }], id: initialValues.id, boxId: initialValues.boxId, partyId: initialValues.partyId, }); if (initialValues.partyId) { setPartyId(initialValues.partyId); } } }, [initialValues, reset]); const { fields, append, remove } = useFieldArray({ control, name: 'paketIds', keyName: 'key', }); const requiredText = t('required'); const getBoxesQuery = useRequest( () => box_requests.getAll({ partyId: partyId, }), { selectData: (data) => data?.data?.data ?? [], enabled: !!partyId, }, ); const list = useMemo(() => { return getBoxesQuery.data?.data.filter((box: any) => box.status === 'READY_TO_INVOICE') ?? []; }, [getBoxesQuery.data]); useEffect(() => { if (partyId) { getBoxesQuery.refetch(); } }, [partyId, getBoxesQuery.refetch]); const { data: defaultParties } = useRequest( () => party_requests.getAll({ status: 'COLLECTING' }), { enabled: true, selectData: (data) => data.data.data.data.map((p: any) => ({ value: p.id, label: p.name })), onSuccess: (data) => { if (!editMode && data?.data?.data?.data?.[0]) { const defaultParty = data.data.data.data[0]; helperRef.current.settedDefaultParty = defaultParty; setValue('partyName', defaultParty.name); setValue('paketIds.0.id', defaultParty.id); // Use dot notation setPartyId(defaultParty.id); } helperRef.current.partyFinished = true; if (helperRef.current.clientFinished) { helperRef.current.finished = true; } }, placeholderData: [], }, ); const onSubmit = handleSubmit(async (values) => { try { setLoading(true); const packetDtos = values.paketIds.map((paket) => Number(paket.id)).filter((id) => id); if (editMode) { const updateBody: UpdateRealBoxBodyType = { boxId: initialValues!.boxId!, partyName: values.partyName, packetDtos, }; await real_box_requests.update(updateBody); } else { const createBody: RealCreateBoxBodyType = { partyName: values.partyName, packetDtos, }; await real_box_requests.create(createBody); } push(pageLinks.dashboard.real_boxes.index); } catch (error) { notifyUnknownError(error); } finally { setLoading(false); } }); const partyOptions = async (inputValue: string) => { try { const res = await party_requests.getAll({ status: 'COLLECTING', partyName: inputValue, }); return res.data.data.data.map((p: any) => ({ label: p.name, value: p.id })); } catch (error) { notifyUnknownError(error); return []; } }; const appendPaket = () => { append({ id: '' }); }; const removePaket = (index: number) => { remove(index); }; return ( {editMode ? t('update_box') : t('create_box')} {t('party_name')} ( { field.onChange(newValue?.label ?? ''); setPartyId(newValue?.value ?? ''); }} defaultValue={ editMode ? { value: initialValues?.partyId, label: initialValues?.partyName, } : partiesData?.length ? { value: partiesData[0].value, label: partiesData[0].label, } : null } styles={selectDefaultStyles} noOptionsMessage={() => t('not_found')} loadingMessage={() => t('loading')} onBlur={field.onBlur} name={field.name} defaultOptions={defaultParties ?? []} loadOptions={partyOptions} placeholder={t('enter_party_name_to_find')} /> )} /> {!!errors.partyName && ( {requiredText} )} {t('packet')} {fields.map((field, index) => ( ( { paketField.onChange(newValue?.value ?? ''); }} defaultValue={ editMode && initialValues?.paketIds?.[index] ? { value: initialValues.paketIds[index].id, label: initialValues.paketIds[index].packetName || `Box ${initialValues.paketIds[index].id}`, } : null } styles={selectDefaultStyles} noOptionsMessage={() => t('not_found')} loadingMessage={() => t('loading')} onBlur={paketField.onBlur} name={paketField.name} defaultOptions={list.map((box: any) => ({ value: box.id, label: box.box_name || box.name || `Box ${box.id}`, }))} loadOptions={async (inputValue: string) => { if (!partyId) return []; try { const res = await box_requests.getAll({ partyId, }); return res.data.data.data.map((box: any) => ({ label: box.box_name || box.name || `Box ${box.id}`, value: box.id, })); } catch (error) { notifyUnknownError(error); return []; } }} placeholder={t('enter_box_name_to_find')} /> )} /> {!!get(errors, `paketIds.${index}.id`) && ( {requiredText} )} {fields.length > 1 && ( removePaket(index)} > )} ))} } onClick={appendPaket} > {t('add_more')} {editMode ? t('update') : t('create')} ); }; export default DashboardCreateRealBoxPage;