Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes pruning saved emails when there's a duplicate entry. #2398

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/components/MultiEmailInput/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { isPresent } from "neetocist";
import { pluck } from "ramda";

import { EMAIL_REGEX } from "./constants";

const getEmailsMap = (options = [], inputEmails = []) =>
new Map([...inputEmails, ...options].map(option => [option.value, option]));
const getEmailsMap = (inputEmails = [], options = []) => {
const emails = [...inputEmails, ...options];
const emailsMap = new Map();

const processEmailOptions = (options = [], inputEmails = []) => {
const emailsMap = getEmailsMap(options, inputEmails);
emails.forEach(option => {
const hasPersistedEntry = isPresent(emailsMap.get(option.value)?.id);
if (!hasPersistedEntry) emailsMap.set(option.value, option);
});

return emailsMap;
};

const processEmailOptions = (inputEmails = [], options = []) => {
const emailsMap = getEmailsMap(inputEmails, options);

return email => {
const emailDetails = emailsMap.get(email) || { value: email };
Expand All @@ -23,7 +33,7 @@ export const formatEmailInputOption = ({ label, value, ...otherDetails }) => ({
});

export const pruneDuplicates = (inputValues, options) => {
const emailProcessor = processEmailOptions(options, inputValues);
const emailProcessor = processEmailOptions(inputValues, options);
const emails = pluck("value", inputValues);
const uniqueValuesSet = new Set();
const duplicates = [];
Expand Down
Loading