import React, { useEffect, useState } from 'react' import TextInput from '@/Components/TextInput' import { validateEmail } from '@/utils' import { toast } from 'react-toastify' import { Inertia } from '@inertiajs/inertia' export default function ModalShare(props) { const { isOpen, toggle, modalState } = props const [shareTo, setShareTo] = useState('') const [shares, setShares] = useState([]) const handleKeyEnter = (e) => { if(e.code === 'Enter') { if(validateEmail(shareTo)){ setShares(shares.concat({'share_to': shareTo})) setShareTo('') } else { toast.error('not valid email') } } } const removeItem = (index) => { setShares(shares.filter((_, i) => i !== index)) } const handleClickShare = () => { if (shareTo != '') { toast.info('please press enter on form email') return } Inertia.post(route('docs.share', modalState.data), { shares: shares }, { onSuccess: () => toggle() }) } useEffect(() => { if(modalState.data != null) { setShares(modalState.data.shares) //[{ user_id:'', 'share_to': ''}] } }, [modalState.data]) return (
setShareTo(e.target.value)} onKeyDown={handleKeyEnter} />
{shares.map((share, index) => (

{share.share_to}

removeItem(index)}>x
))}
Bagikan
Batal
) }