mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
399 lines
11 KiB
399 lines
11 KiB
7 months ago
|
<script lang="ts" setup>
|
||
|
import { ProjectRoles, type RoleLabels, WorkspaceUserRoles } from 'nocodb-sdk'
|
||
|
|
||
8 months ago
|
import { extractEmail } from '~/helpers/parsers/parserHelpers'
|
||
|
|
||
8 months ago
|
const props = defineProps<{
|
||
|
modelValue: boolean
|
||
7 months ago
|
type?: 'base' | 'workspace' | 'organization'
|
||
8 months ago
|
baseId?: string
|
||
7 months ago
|
emails?: string[]
|
||
|
workspaceId?: string
|
||
8 months ago
|
}>()
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
|
const basesStore = useBases()
|
||
|
|
||
7 months ago
|
const workspaceStore = useWorkspace()
|
||
8 months ago
|
|
||
|
const { createProjectUser } = basesStore
|
||
|
|
||
7 months ago
|
const { inviteCollaborator: inviteWsCollaborator } = workspaceStore
|
||
|
|
||
|
const dialogShow = useVModel(props, 'modelValue', emit)
|
||
|
|
||
|
const orderedRoles = computed(() => {
|
||
|
return props.type === 'base' ? ProjectRoles : WorkspaceUserRoles
|
||
|
})
|
||
|
|
||
|
const inviteData = reactive({
|
||
|
email: '',
|
||
|
roles: orderedRoles.value.NO_ACCESS,
|
||
|
})
|
||
|
|
||
8 months ago
|
const divRef = ref<HTMLDivElement>()
|
||
|
|
||
|
const focusRef = ref<HTMLInputElement>()
|
||
|
const isDivFocused = ref(false)
|
||
|
|
||
|
const emailValidation = reactive({
|
||
|
isError: true,
|
||
|
message: '',
|
||
|
})
|
||
|
|
||
7 months ago
|
const singleEmailValue = ref('')
|
||
8 months ago
|
|
||
7 months ago
|
const emailBadges = ref<Array<string>>([])
|
||
|
|
||
|
const allowedRoles = ref<[]>([])
|
||
|
|
||
|
const focusOnDiv = () => {
|
||
|
focusRef.value?.focus()
|
||
|
isDivFocused.value = true
|
||
|
}
|
||
|
|
||
|
watch(dialogShow, async (newVal) => {
|
||
|
if (newVal) {
|
||
|
try {
|
||
|
// todo: enable after discussing with anbu
|
||
|
// const currentRoleIndex = Object.values(orderedRoles.value).findIndex(
|
||
|
// (role) => userRoles.value && Object.keys(userRoles.value).includes(role),
|
||
|
// )
|
||
|
// if (currentRoleIndex !== -1) {
|
||
|
allowedRoles.value = Object.values(orderedRoles.value) // .slice(currentRoleIndex + 1)
|
||
|
// }
|
||
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
8 months ago
|
}
|
||
7 months ago
|
|
||
|
if (props.emails) {
|
||
|
emailBadges.value = props.emails
|
||
|
}
|
||
|
|
||
|
setTimeout(() => {
|
||
|
focusOnDiv()
|
||
|
}, 100)
|
||
|
} else {
|
||
|
emailBadges.value = []
|
||
|
inviteData.email = ''
|
||
|
singleEmailValue.value = ''
|
||
8 months ago
|
}
|
||
|
})
|
||
|
|
||
|
const insertOrUpdateString = (str: string) => {
|
||
|
// Check if the string already exists in the array
|
||
|
const index = emailBadges.value.indexOf(str)
|
||
|
|
||
|
if (index !== -1) {
|
||
|
// If the string exists, remove it
|
||
|
emailBadges.value.splice(index, 1)
|
||
|
}
|
||
|
|
||
|
// Add the new string to the array
|
||
|
emailBadges.value.push(str)
|
||
|
}
|
||
|
|
||
8 months ago
|
const emailInputValidation = (input: string, isBulkEmailCopyPaste: boolean = false): boolean => {
|
||
8 months ago
|
if (!input.length) {
|
||
8 months ago
|
if (isBulkEmailCopyPaste) return false
|
||
|
|
||
8 months ago
|
emailValidation.isError = true
|
||
|
emailValidation.message = 'Email should not be empty'
|
||
|
return false
|
||
|
}
|
||
|
if (!validateEmail(input.trim())) {
|
||
8 months ago
|
if (isBulkEmailCopyPaste) return false
|
||
|
|
||
8 months ago
|
emailValidation.isError = true
|
||
|
emailValidation.message = 'Invalid Email'
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
7 months ago
|
const isInviteButtonDisabled = computed(() => {
|
||
8 months ago
|
if (!emailBadges.value.length && !singleEmailValue.value.length) {
|
||
|
return true
|
||
|
}
|
||
|
if (emailBadges.value.length && inviteData.email) {
|
||
|
return true
|
||
|
}
|
||
|
})
|
||
|
|
||
|
watch(inviteData, (newVal) => {
|
||
|
// when user only want to enter a single email
|
||
7 months ago
|
// we don't convert that as badge
|
||
8 months ago
|
|
||
|
const isSingleEmailValid = validateEmail(newVal.email)
|
||
|
if (isSingleEmailValid && !emailBadges.value.length) {
|
||
|
singleEmailValue.value = newVal.email
|
||
|
emailValidation.isError = false
|
||
|
return
|
||
|
}
|
||
|
singleEmailValue.value = ''
|
||
|
|
||
7 months ago
|
// when user enters multiple emails comma separated or space separated
|
||
8 months ago
|
const isNewEmail = newVal.email.charAt(newVal.email.length - 1) === ',' || newVal.email.charAt(newVal.email.length - 1) === ' '
|
||
|
if (isNewEmail && newVal.email.trim().length) {
|
||
|
const emailToAdd = newVal.email.split(',')[0].trim() || newVal.email.split(' ')[0].trim()
|
||
|
if (!validateEmail(emailToAdd)) {
|
||
|
emailValidation.isError = true
|
||
|
emailValidation.message = 'Invalid Email'
|
||
|
return
|
||
|
}
|
||
|
/**
|
||
|
if email is already enterd we delete the already
|
||
|
existing email and add new one
|
||
|
**/
|
||
|
if (emailBadges.value.includes(emailToAdd)) {
|
||
|
insertOrUpdateString(emailToAdd)
|
||
|
inviteData.email = ''
|
||
|
return
|
||
|
}
|
||
|
emailBadges.value.push(emailToAdd)
|
||
|
inviteData.email = ''
|
||
|
singleEmailValue.value = ''
|
||
|
}
|
||
|
if (!newVal.email.length && emailValidation.isError) {
|
||
|
emailValidation.isError = false
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const handleEnter = () => {
|
||
|
const isEmailIsValid = emailInputValidation(inviteData.email)
|
||
|
if (!isEmailIsValid) return
|
||
|
|
||
|
inviteData.email += ' '
|
||
|
emailValidation.isError = false
|
||
|
emailValidation.message = ''
|
||
|
}
|
||
|
// remove one email per backspace
|
||
|
onKeyStroke('Backspace', () => {
|
||
|
if (isDivFocused.value && inviteData.email.length < 1) {
|
||
|
emailBadges.value.pop()
|
||
|
}
|
||
|
})
|
||
|
|
||
|
watch(dialogShow, (newVal) => {
|
||
|
if (newVal) {
|
||
|
setTimeout(() => {
|
||
|
focusOnDiv()
|
||
|
}, 100)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// when bulk email is pasted
|
||
|
const onPaste = (e: ClipboardEvent) => {
|
||
8 months ago
|
emailValidation.isError = false
|
||
|
|
||
8 months ago
|
const pastedText = e.clipboardData?.getData('text')
|
||
|
|
||
|
const inputArray = pastedText?.split(',') || pastedText?.split(' ')
|
||
|
|
||
|
// if data is pasted to a already existing text in input
|
||
|
// we add existingInput + pasted data
|
||
|
if (inputArray?.length === 1 && inviteData.email.length) {
|
||
|
inputArray[0] = inviteData.email += inputArray[0]
|
||
|
}
|
||
|
|
||
|
inputArray?.forEach((el) => {
|
||
8 months ago
|
el = extractEmail(el) || el
|
||
|
|
||
|
const isEmailIsValid = emailInputValidation(el, inputArray.length > 1)
|
||
8 months ago
|
|
||
|
if (!isEmailIsValid) return
|
||
|
|
||
|
/**
|
||
|
if email is already enterd we delete the already
|
||
|
existing email and add new one
|
||
|
**/
|
||
|
if (emailBadges.value.includes(el)) {
|
||
|
insertOrUpdateString(el)
|
||
|
return
|
||
|
}
|
||
|
emailBadges.value.push(el)
|
||
|
|
||
|
inviteData.email = ''
|
||
|
})
|
||
|
inviteData.email = ''
|
||
|
}
|
||
|
|
||
7 months ago
|
const workSpaces = ref<NcWorkspace[]>([])
|
||
|
|
||
|
const inviteCollaborator = async () => {
|
||
8 months ago
|
try {
|
||
|
const payloadData = singleEmailValue.value || emailBadges.value.join(',')
|
||
|
if (!payloadData.includes(',')) {
|
||
|
const validationStatus = validateEmail(payloadData)
|
||
|
if (!validationStatus) {
|
||
|
emailValidation.isError = true
|
||
|
emailValidation.message = 'invalid email'
|
||
|
}
|
||
|
}
|
||
7 months ago
|
if (props.type === 'base' && props.baseId) {
|
||
|
await createProjectUser(props.baseId!, {
|
||
|
email: payloadData,
|
||
|
roles: inviteData.roles,
|
||
|
} as unknown as User)
|
||
|
} else if (props.type === 'workspace' && props.workspaceId) {
|
||
|
await inviteWsCollaborator(payloadData, inviteData.roles, props.workspaceId)
|
||
|
} else if (props.type === 'organization') {
|
||
|
// TODO: Add support for Bulk Workspace Invite
|
||
|
for (const workspace of workSpaces.value) {
|
||
|
await inviteWsCollaborator(payloadData, inviteData.roles, workspace.id)
|
||
|
}
|
||
|
}
|
||
8 months ago
|
|
||
|
message.success('Invitation sent successfully')
|
||
|
inviteData.email = ''
|
||
|
emailBadges.value = []
|
||
|
dialogShow.value = false
|
||
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
|
} finally {
|
||
|
singleEmailValue.value = ''
|
||
|
}
|
||
|
}
|
||
8 months ago
|
|
||
7 months ago
|
const organizationStore = useOrganization()
|
||
|
|
||
|
const { listWorkspaces } = organizationStore
|
||
|
|
||
|
const { workspaces } = storeToRefs(organizationStore)
|
||
|
|
||
|
const workSpaceSelectList = computed(() => {
|
||
|
return workspaces.value.filter((w) => !workSpaces.value.find((ws) => ws.id === w.id))
|
||
|
})
|
||
|
|
||
|
const addToList = (workspaceId: string) => {
|
||
|
workSpaces.value.push(workspaces.value.find((w) => w.id === workspaceId)!)
|
||
|
}
|
||
|
const removeWorkspace = (workspaceId: string) => {
|
||
|
workSpaces.value = workSpaces.value.filter((w) => w.id !== workspaceId)
|
||
|
}
|
||
|
|
||
|
onMounted(async () => {
|
||
|
if (props.type === 'organization') {
|
||
|
await listWorkspaces()
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const onRoleChange = (role: keyof typeof RoleLabels) => (inviteData.roles = role as ProjectRoles | WorkspaceUserRoles)
|
||
8 months ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<NcModal
|
||
|
v-model:visible="dialogShow"
|
||
|
:header="$t('activity.createTable')"
|
||
7 months ago
|
:show-separator="false"
|
||
8 months ago
|
size="medium"
|
||
7 months ago
|
class="nc-invite-dlg"
|
||
8 months ago
|
@keydown.esc="dialogShow = false"
|
||
|
>
|
||
|
<template #header>
|
||
|
<div class="flex flex-row items-center gap-x-2">
|
||
7 months ago
|
{{
|
||
|
type === 'organization'
|
||
|
? $t('labels.addMembersToOrganization')
|
||
|
: type === 'base'
|
||
|
? $t('activity.addMember')
|
||
|
: $t('activity.inviteToWorkspace')
|
||
|
}}
|
||
8 months ago
|
</div>
|
||
|
</template>
|
||
|
<div class="flex items-center justify-between gap-3 mt-2">
|
||
7 months ago
|
<div class="flex w-full gap-4 flex-col">
|
||
8 months ago
|
<div class="flex justify-between gap-3 w-full">
|
||
|
<div
|
||
|
ref="divRef"
|
||
|
:class="{
|
||
|
'border-primary/100': isDivFocused,
|
||
|
'p-1': emailBadges?.length > 1,
|
||
|
}"
|
||
7 months ago
|
class="flex items-center border-1 gap-1 w-full overflow-x-scroll nc-scrollbar-x-md items-center h-10 rounded-lg !min-w-96"
|
||
|
tabindex="0"
|
||
8 months ago
|
@blur="isDivFocused = false"
|
||
7 months ago
|
@click="focusOnDiv"
|
||
8 months ago
|
>
|
||
|
<span
|
||
|
v-for="(email, index) in emailBadges"
|
||
|
:key="email"
|
||
7 months ago
|
class="border-1 text-gray-800 first:ml-1 bg-gray-100 rounded-md flex items-center px-2 py-1"
|
||
8 months ago
|
>
|
||
|
{{ email }}
|
||
|
<component
|
||
|
:is="iconMap.close"
|
||
|
class="ml-0.5 hover:cursor-pointer mt-0.5 w-4 h-4"
|
||
|
@click="emailBadges.splice(index, 1)"
|
||
|
/>
|
||
|
</span>
|
||
|
<input
|
||
|
id="email"
|
||
|
ref="focusRef"
|
||
|
v-model="inviteData.email"
|
||
|
:placeholder="$t('activity.enterEmail')"
|
||
|
class="w-full min-w-36 outline-none px-2"
|
||
|
data-testid="email-input"
|
||
|
@blur="isDivFocused = false"
|
||
7 months ago
|
@keyup.enter="handleEnter"
|
||
8 months ago
|
@paste.prevent="onPaste"
|
||
|
/>
|
||
|
</div>
|
||
|
<RolesSelector
|
||
7 months ago
|
:description="false"
|
||
|
:on-role-change="onRoleChange"
|
||
8 months ago
|
:role="inviteData.roles"
|
||
|
:roles="allowedRoles"
|
||
7 months ago
|
class="!min-w-[152px] nc-invite-role-selector"
|
||
|
size="lg"
|
||
8 months ago
|
/>
|
||
|
</div>
|
||
|
|
||
|
<span v-if="emailValidation.isError && emailValidation.message" class="ml-2 text-red-500 text-[10px] mt-1.5">{{
|
||
|
emailValidation.message
|
||
|
}}</span>
|
||
7 months ago
|
|
||
|
<template v-if="type === 'organization'">
|
||
|
<NcSelect :placeholder="$t('labels.selectWorkspace')" size="middle" @change="addToList">
|
||
|
<a-select-option v-for="workspace in workSpaceSelectList" :key="workspace.id" :value="workspace.id">
|
||
|
{{ workspace.title }}
|
||
|
</a-select-option>
|
||
|
</NcSelect>
|
||
|
|
||
|
<div class="flex flex-wrap gap-2">
|
||
|
<NcBadge v-for="workspace in workSpaces" :key="workspace.id">
|
||
|
<div class="px-2 flex gap-2 items-center py-1">
|
||
|
<GeneralWorkspaceIcon :workspace="workspace" hide-label size="small" />
|
||
|
<span class="text-gray-600">
|
||
|
{{ workspace.title }}
|
||
|
</span>
|
||
|
<component :is="iconMap.close" class="w-3 h-3" @click="removeWorkspace(workspace.id)" />
|
||
|
</div>
|
||
|
</NcBadge>
|
||
|
</div>
|
||
|
</template>
|
||
8 months ago
|
</div>
|
||
|
</div>
|
||
|
<div class="flex mt-8 justify-end">
|
||
|
<div class="flex gap-2">
|
||
|
<NcButton type="secondary" @click="dialogShow = false"> {{ $t('labels.cancel') }} </NcButton>
|
||
|
<NcButton
|
||
7 months ago
|
:disabled="isInviteButtonDisabled || emailValidation.isError"
|
||
8 months ago
|
size="medium"
|
||
7 months ago
|
type="primary"
|
||
|
class="nc-invite-btn"
|
||
|
@click="inviteCollaborator"
|
||
8 months ago
|
>
|
||
7 months ago
|
{{ type === 'base' ? $t('activity.inviteToBase') : $t('activity.inviteToWorkspace') }}
|
||
8 months ago
|
</NcButton>
|
||
|
</div>
|
||
|
</div>
|
||
|
</NcModal>
|
||
|
</template>
|
||
7 months ago
|
|
||
|
<style lang="scss" scoped>
|
||
|
:deep(.nc-invite-role-selector .nc-role-badge) {
|
||
|
@apply w-full;
|
||
|
}
|
||
|
</style>
|