refactor code

This commit is contained in:
Abhinav 2024-01-31 11:39:38 +05:30
parent 826679b183
commit 22605e503c
2 changed files with 16 additions and 10 deletions

View file

@ -39,23 +39,29 @@ export default function AddParticipant({
[emailList, collection.sharees]
);
const collectionShare: AddParticipantFormProps['callback'] = async (
emails
) => {
if (emails.length === 1) {
if (emails[0] === user.email) {
const collectionShare: AddParticipantFormProps['callback'] = async ({
email,
emails,
}) => {
// if email is provided, means user has custom entered email, so, will need to validate for self sharing
// and already shared
if (email) {
if (email === user.email) {
throw new Error(t('SHARE_WITH_SELF'));
} else if (
collection?.sharees?.find((value) => value.email === emails[0])
collection?.sharees?.find((value) => value.email === email)
) {
throw new Error(t('ALREADY_SHARED', { email: emails[0] }));
throw new Error(t('ALREADY_SHARED', { email: email }));
}
// set emails to array of one email
emails = [email];
}
for (const email of emails) {
if (
email === user.email ||
collection?.sharees?.find((value) => value.email === email)
) {
// can just skip this email
continue;
}
try {

View file

@ -18,7 +18,7 @@ interface formValues {
selectedOptions: string[];
}
export interface AddParticipantFormProps {
callback: (emails: string[]) => Promise<void>;
callback: (props: { email?: string; emails?: string[] }) => Promise<void>;
fieldType: 'text' | 'email' | 'password';
placeholder: string;
buttonText: string;
@ -49,9 +49,9 @@ export default function AddParticipantForm(props: AddParticipantFormProps) {
try {
SetLoading(true);
if (values.inputValue !== '') {
await props.callback([values.inputValue]);
await props.callback({ email: values.inputValue });
} else if (values.selectedOptions.length !== 0) {
await props.callback(values.selectedOptions);
await props.callback({ emails: values.selectedOptions });
}
SetLoading(false);
props.onClose();