import { inject, observer } from 'mobx-react';
import ModalAction from 'containers/Action/ModalAction';
import globalInstancesUsersStore from 'stores/trove/instances-user';
import { InstancesDatabasesStore } from 'stores/trove/instances-database';
import { getPasswordOtherRule } from 'utils/validate';
export class UserCreate extends ModalAction {
async init() {
this.store = globalInstancesUsersStore;
this.databaseStore = new InstancesDatabasesStore();
await this.fetchDatabase();
}
static id = 'create-user';
static title = t('Create User');
get name() {
return t('Create User');
}
static policy = 'instance:extension:user:create';
static allowed() {
return Promise.resolve(true);
}
fetchDatabase() {
const { id } = this.item;
this.databaseStore.fetchList({ id });
}
get database() {
return (this.databaseStore.list.data || []).map((it) => ({
label: it.name,
value: it.name,
key: it.name,
}));
}
get formItems() {
return [
{
name: 'userName',
label: t('Name'),
type: 'input-name',
required: true,
isDatabaseUserName: true,
maxLength: 16,
},
{
name: 'database',
label: t('Database'),
type: 'select',
options: this.database,
mode: 'multiple',
required: true,
},
{
name: 'password',
label: t('Password'),
type: 'input-password',
required: true,
otherRule: getPasswordOtherRule('password'),
},
{
name: 'confirmPassword',
label: t('Confirm Password'),
type: 'input-password',
required: true,
dependencies: ['password'],
otherRule: getPasswordOtherRule('confirmPassword'),
},
];
}
onSubmit = (values) => {
const { id } = this.item;
return this.store.create(id, {
users: [
{
databases: values.database.map((it) => ({ name: it })),
name: values.userName,
password: values.password,
},
],
});
};
}
export default inject('rootStore')(observer(UserCreate));