import React, { useState } from 'react'; import constants from 'utils/strings/constants'; import { Formik, FormikHelpers } from 'formik'; import * as Yup from 'yup'; import Form from 'react-bootstrap/Form'; import FormControl from 'react-bootstrap/FormControl'; import { Button, Col, Table } from 'react-bootstrap'; import { DeadCenter } from 'pages/gallery'; import { User } from 'services/userService'; import { Collection, shareCollection, unshareCollection, } from 'services/collectionService'; import { getData, LS_KEYS } from 'utils/storage/localStorage'; import SubmitButton from './SubmitButton'; import MessageDialog from './MessageDialog'; interface Props { show: boolean; onHide: () => void; collection: Collection; syncWithRemote: () => Promise; } interface formValues { email: string; } interface ShareeProps { sharee: User; collectionUnshare: (sharee: User) => void; } function CollectionShare(props: Props) { const [loading, setLoading] = useState(false); const collectionShare = async ( { email }: formValues, { resetForm, setFieldError }: FormikHelpers ) => { try { setLoading(true); const user: User = getData(LS_KEYS.USER); if (email === user.email) { setFieldError('email', constants.SHARE_WITH_SELF); } else if ( props.collection.sharees.find((value) => value.email === email) ) { setFieldError('email', constants.ALREADY_SHARED(email)); } else { await shareCollection(props.collection, email); await props.syncWithRemote(); resetForm(); } } catch (e) { let errorMessage = null; switch (e?.status) { case 400: errorMessage = constants.SHARING_BAD_REQUEST_ERROR; break; case 402: errorMessage = constants.SHARING_DISABLED_FOR_FREE_ACCOUNTS; break; case 404: errorMessage = constants.USER_DOES_NOT_EXIST; break; default: errorMessage = `${constants.UNKNOWN_ERROR} ${e.message}`; } setFieldError('email', errorMessage); } finally { setLoading(false); } }; const collectionUnshare = async (sharee) => { await unshareCollection(props.collection, sharee.email); await props.syncWithRemote(); }; const ShareeRow = ({ sharee, collectionUnshare }: ShareeProps) => ( {sharee.email} ); return (
{constants.SHARE_WITH_PEOPLE}

initialValues={{ email: '' }} validationSchema={Yup.object().shape({ email: Yup.string() .email(constants.EMAIL_ERROR) .required(constants.REQUIRED), })} validateOnChange={false} validateOnBlur={false} onSubmit={collectionShare}> {({ values, errors, touched, handleChange, handleSubmit, }) => (

{errors.email}
)}
{props.collection?.sharees.length > 0 ? ( <>

{constants.SHAREES}

{props.collection?.sharees.map((sharee) => ( ))}
) : (
{constants.ZERO_SHAREES()}
)} ); } export default CollectionShare;