Improve email parsing on sharing.

This commit is contained in:
Paulo Gustavo Veiga 2022-04-02 11:53:52 -03:00
parent 819428651e
commit 267dbfc9c1

View File

@ -56,11 +56,18 @@ const ShareDialog = ({ mapId, onClose }: SimpleDialogProps): React.ReactElement
}
);
const splitEmail = (emails: string): string[] => {
return emails.split(/,|;/)
.map(e => e.trim().replace(/\s/g, ''))
.filter(e => e.trim().length > 0);
}
const addMutation = useMutation(
(model: ShareModel) => {
const emails = model.emails.split(',');
const emails = splitEmail(model.emails);
const permissions = emails.map((email: string) => {
return { email: email.replace(/\s/g, ''), role: model.role };
return { email: email, role: model.role };
});
return client.addMapPermissions(mapId, model.message, permissions);
},
@ -114,9 +121,7 @@ const ShareDialog = ({ mapId, onClose }: SimpleDialogProps): React.ReactElement
};
// very basic email validation, just make sure the basic syntax is fine
const isValid = model.emails
.split(/,|;/)
.filter(e => e.trim().length > 0)
const isValid = splitEmail(model.emails)
.every(str => /\S+@\S+\.\S+/.test((str || '')
.trim()));