Files
Automata/packages/editor-ui/src/components/InviteUsersModal.vue
Milorad FIlipović bae3098e4e refactor(editor): Finish pinia migration, remove all vuex dependancies (#4533)
*  Added pinia support. Migrated community nodes module.
*  Added ui pinia store, moved some data from root store to it, updated modals to work with pinia stores
*  Added ui pinia store and migrated a part of the root store
*  Migrated `settings` store to pinia
*  Removing vuex store refs from router
*  Migrated `users` module to pinia store
*  Fixing errors after sync with master
*  One more error after merge
*  Created `workflows` pinia store. Moved large part of root store to it. Started updating references.
*  Finished migrating workflows store to pinia
*  Renaming some getters and actions to make more sense
*  Finished migrating the root store to pinia
*  Migrated ndv store to pinia
*  Renaming main panel dimensions getter so it doesn't clash with data prop name
* ✔️ Fixing lint errors
*  Migrated `templates` store to pinia
*  Migrated the `nodeTypes`store
*  Removed unused pieces of code and oold vuex modules
*  Adding vuex calls to pinia store, fixing wrong references
* 💄 Removing leftover $store refs
*  Added legacy getters and mutations to store to support webhooks
*  Added missing front-end hooks, updated vuex state subscriptions to pinia
* ✔️ Fixing linting errors
*  Removing vue composition api plugin
*  Fixing main sidebar state when loading node view
* 🐛 Fixing an error when activating workflows
* 🐛 Fixing isses with workflow settings and executions auto-refresh
* 🐛 Removing duplicate listeners which cause import error
* 🐛 Fixing route authentication
*  Updating freshly pulled $store refs
*  Adding deleted const
*  Updating store references in ee features. Reseting NodeView credentials update flag when resetting workspace
*  Adding return type to email submission modal
*  Making NodeView only react to paste event when active
* 🐛 Fixing signup view errors
*  Started migrating the `credentials` module to pinia
* 👌 Addressing PR review comments
*  Migrated permissions module to pinia
*  Migrated `nodeCreator`, `tags` and `versions` modules to pinia
*  Implemented webhooks pinia store
*  Removing all leftover vuex files and references
*  Removing final vuex refs
*  Updating expected credentialId type
*  Removing node credentials subscription code, reducing node click debounce timeout
* 🐛 Fixing pushing nodes downstream when inserting new node
* ✔️ Fixing a lint error in new type guard
*  Updating helper reference
* ✔️ Removing unnecessary awaits
*  fix(editor): remove unnecessary imports from NDV
*  Merging mapStores blocks in NodeView
*  fix(editor): make sure JS Plumb not loaded earlier than needed
*  Updating type guard nad credentials subscriptions
*  Updating type guard so it doesn't use `any` type

Co-authored-by: Csaba Tuncsik <csaba@n8n.io>
2022-11-09 10:01:50 +01:00

208 lines
5.1 KiB
Vue

<template>
<Modal
:name="INVITE_USER_MODAL_KEY"
@enter="onSubmit"
:title="$locale.baseText('settings.users.inviteNewUsers')"
:center="true"
width="460px"
:eventBus="modalBus"
>
<template slot="content">
<n8n-form-inputs
:inputs="config"
:eventBus="formBus"
:columnView="true"
@input="onInput"
@submit="onSubmit"
/>
</template>
<template slot="footer">
<n8n-button :loading="loading" :disabled="!enabledButton" :label="buttonLabel" @click="onSubmitClick" float="right" />
</template>
</Modal>
</template>
<script lang="ts">
import mixins from "vue-typed-mixins";
import { showMessage } from "@/components/mixins/showMessage";
import Modal from "./Modal.vue";
import Vue from "vue";
import { IFormInputs, IInviteResponse } from "@/Interface";
import { VALID_EMAIL_REGEX, INVITE_USER_MODAL_KEY } from "@/constants";
import { ROLE } from "@/stores/userHelpers";
import { mapStores } from "pinia";
import { useUsersStore } from "@/stores/users";
const NAME_EMAIL_FORMAT_REGEX = /^.* <(.*)>$/;
function getEmail(email: string): string {
let parsed = email.trim();
if (NAME_EMAIL_FORMAT_REGEX.test(parsed)) {
const matches = parsed.match(NAME_EMAIL_FORMAT_REGEX);
if (matches && matches.length === 2) {
parsed = matches[1];
}
}
return parsed;
}
export default mixins(showMessage).extend({
components: { Modal },
name: "InviteUsersModal",
props: {
modalName: {
type: String,
},
},
data() {
return {
config: null as IFormInputs | null,
formBus: new Vue(),
modalBus: new Vue(),
emails: '',
loading: false,
INVITE_USER_MODAL_KEY,
};
},
mounted() {
this.config = [
{
name: 'emails',
properties: {
label: this.$locale.baseText('settings.users.newEmailsToInvite'),
required: true,
validationRules: [{name: 'VALID_EMAILS'}],
validators: {
VALID_EMAILS: {
validate: this.validateEmails,
},
},
placeholder: 'name1@email.com, name2@email.com, ...',
capitalize: true,
focusInitially: true,
},
},
{
name: 'role',
initialValue: 'member',
properties: {
label: this.$locale.baseText('auth.role'),
required: true,
type: 'select',
options: [
{
value: ROLE.Member,
label: this.$locale.baseText('auth.roles.member'),
},
],
capitalize: true,
},
},
];
},
computed: {
...mapStores(useUsersStore),
emailsCount(): number {
return this.emails.split(',').filter((email: string) => !!email.trim()).length;
},
buttonLabel(): string {
if (this.emailsCount > 1) {
return this.$locale.baseText(
'settings.users.inviteXUser',
{ interpolate: { count: this.emailsCount.toString() }},
);
}
return this.$locale.baseText('settings.users.inviteUser');
},
enabledButton(): boolean {
return this.emailsCount >= 1;
},
},
methods: {
validateEmails(value: string | number | boolean | null | undefined) {
if (typeof value !== 'string') {
return false;
}
const emails = value.split(',');
for (let i = 0; i < emails.length; i++) {
const email = emails[i];
const parsed = getEmail(email);
if (!!parsed.trim() && !VALID_EMAIL_REGEX.test(String(parsed).trim().toLowerCase())) {
return {
messageKey: 'settings.users.invalidEmailError',
options: { interpolate: { email: parsed }},
};
}
}
return false;
},
onInput(e: {name: string, value: string}) {
if (e.name === 'emails') {
this.emails = e.value;
}
},
async onSubmit() {
try {
this.loading = true;
const emails = this.emails.split(',')
.map((email) => ({email: getEmail(email)}))
.filter((invite) => !!invite.email);
if (emails.length === 0) {
throw new Error(this.$locale.baseText('settings.users.noUsersToInvite'));
}
const invited: IInviteResponse[] = await this.usersStore.inviteUsers(emails);
const invitedEmails = invited.reduce((accu, {user, error}) => {
if (error) {
accu.error.push(user.email);
}
else {
accu.success.push(user.email);
}
return accu;
}, {
success: [] as string[],
error: [] as string[],
});
if (invitedEmails.success.length) {
this.$showMessage({
type: 'success',
title: this.$locale.baseText(invitedEmails.success.length > 1 ? 'settings.users.usersInvited' : 'settings.users.userInvited'),
message: this.$locale.baseText('settings.users.emailInvitesSent', { interpolate: { emails: invitedEmails.success.join(', ') }}),
});
}
if (invitedEmails.error.length) {
setTimeout(() => {
this.$showMessage({
type: 'error',
title: this.$locale.baseText('settings.users.usersEmailedError'),
message: this.$locale.baseText('settings.users.emailInvitesSentError', { interpolate: { emails: invitedEmails.error.join(', ') }}),
});
}, 0); // notifications stack on top of each other otherwise
}
this.modalBus.$emit('close');
} catch (error) {
this.$showError(error, this.$locale.baseText('settings.users.usersInvitedError'));
}
this.loading = false;
},
onSubmitClick() {
this.formBus.$emit('submit');
},
},
});
</script>