From 1f447e3a85030824f6c47324fe190e9868db6586 Mon Sep 17 00:00:00 2001 From: sreehari jayaraj Date: Fri, 22 Sep 2023 15:26:13 +0000 Subject: [PATCH] feat: email validation --- .../components/workspace/InviteSection.vue | 101 ++++++++++++------ 1 file changed, 66 insertions(+), 35 deletions(-) diff --git a/packages/nc-gui/components/workspace/InviteSection.vue b/packages/nc-gui/components/workspace/InviteSection.vue index 1cd5b74e88..4da7b2247b 100644 --- a/packages/nc-gui/components/workspace/InviteSection.vue +++ b/packages/nc-gui/components/workspace/InviteSection.vue @@ -11,6 +11,15 @@ const focusRef = ref() const isDivFocused = ref(false) const divRef = ref() +const emailValidation = reactive({ + isError: false, + message: '', +}) + +const validateEmail = (email: string): boolean => { + const regEx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ + return regEx.test(email) +} const workspaceStore = useWorkspace() const { inviteCollaborator: _inviteCollaborator } = workspaceStore @@ -24,25 +33,44 @@ watch(inviteData, (newVal) => { emailBadges.value.push(newVal.email.split(',')[0]) inviteData.email = '' } + if (newVal.email.length < 1 && emailValidation.isError) { + emailValidation.isError = false + } }) const scrollToEnd = () => { if (divRef.value) { - const maxScrollLeft = divRef.value.scrollWidth - divRef.value.clientWidth - divRef.value.scrollLeft = maxScrollLeft + divRef.value.scrollLeft = divRef.value.scrollWidth } } const handleEnter = () => { + if (inviteData.email.length < 1) { + emailValidation.isError = true + emailValidation.message = 'email should not be empty' + return + } + if (!validateEmail(inviteData.email)) { + emailValidation.isError = true + emailValidation.message = 'invalid emamil' + return + } inviteData.email += ',' + emailValidation.isError = false + emailValidation.message = '' scrollToEnd() } const inviteCollaborator = async () => { try { let inviteEmails = '' - emailBadges.value.forEach((el) => { - inviteEmails += `${el},` + emailBadges.value.forEach((el, index) => { + // prevent the last email from getting the "," + if (index === emailBadges.value.length - 1) { + inviteEmails += el + } else { + inviteEmails += `${el},` + } }) await _inviteCollaborator(inviteEmails, inviteData.roles) @@ -74,37 +102,40 @@ onMounted(async () => {
Invite
-
- +
- {{ email }} - - - + + {{ email }} + + + +
+ {{ emailValidation.message }}