import React, { useRef } from 'react' import { AwesomeModal } from '..' import { getGlobalStyle } from '../../../utils' import { Button, Checkbox, Column, Icon, Row, Tag } from '../../atoms' import { AmountInput, InputHooks, QuantityButton } from '../../molecules' import clsx from 'clsx' import styles from './styles.module.css' import { MODAL_SIZES } from '../AwesomeModal/constanst' interface ICreateExtra { LineItems?: { Lines: Array<{ exPid?: string | number | null extraName?: string extraPrice?: number | string forEdit?: boolean exState?: boolean }> } loading?: boolean modal?: boolean inputRefs?: any pId?: any selected?: { loading: boolean exPid: any } CleanLines?: () => void handleAdd?: () => void handleEdit?: (i: number, item: any) => void handleFocusChange?: (i: number) => void handleSelect?: (item: any, index: number) => void handleLineChange?: (i: number, extraName: string, value: string | number) => void handleRemove?: (i: number, exPid: any) => void onSubmitUpdate?: ({ pId }: { pId: any }) => void setModal?: () => void } export const CreateExtra: React.FC = ({ LineItems = { Lines: [] }, loading = false, modal = false, inputRefs, pId = null, selected = { loading: false, exPid: null }, CleanLines = () => { }, handleAdd = () => { }, handleEdit = (i, item) => { return { i, item } }, handleFocusChange = (i) => { return i }, handleSelect = (item, index) => { return { item, index } }, handleLineChange = (i, extraName, value) => { return { i, extraName, value } }, handleRemove = (i, exPid) => { return { i, exPid } }, onSubmitUpdate = ({ pId }) => { return pId }, setModal = () => { } }) => { const disabled = false const endOfListRef = useRef({ scrollIntoView: (args: any) => { return args }, current: null }) const disabledButtonSave = loading || !LineItems?.Lines?.some((item) => item?.exPid === undefined || item?.exPid === null) || LineItems?.Lines?.length === 0 return ( { setModal() }} onHide={() => { setModal() }} padding={0} question={false} show={modal} size={MODAL_SIZES.large} title='AƱade Complementos' zIndex={getGlobalStyle('--z-index-99999')} >
{LineItems?.Lines?.length > 0 ? LineItems?.Lines?.map((extra, i) => { const price = extra?.extraPrice const exPid = extra?.exPid ?? null const forEdit = (Boolean((extra?.forEdit))) || false const isSelect = selected.exPid === exPid const isLoading = selected.loading || loading return (
{ const value = e.target.value return handleLineChange(i, 'extraName', `${value}`) }} required={true} disabled={forEdit && !isSelect} title='Nombre' onFocus={() => { if (selected?.exPid === exPid) return null return handleFocusChange(i) }} max={180} reference={inputRefs?.current[i]} value={extra?.extraName} /> { return handleLineChange(i, 'extraPrice', value) }} onFocus={() => { if (selected?.exPid === exPid) return null return handleFocusChange(i) }} placeholder='Precio' defaultValue={price} /> { return handleLineChange(i, 'exState', value) }} /> {forEdit ? <> : <> }
) }) : null}
{ if (endOfListRef?.current !== null) { endOfListRef.current.scrollIntoView({ behavior: 'smooth' }) } handleAdd() }} quantity={typeof LineItems?.Lines?.length === 'number' && !isNaN(LineItems.Lines.length) ? LineItems.Lines.length : 0} disabled={false} validationOne={true} /> ) }