The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
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.
grafana/public/app/features/org/UserInviteForm.tsx

86 lines
2.8 KiB

import React, { FC } from 'react';
import { Forms, HorizontalGroup, Button, LinkButton, Input, Switch } from '@grafana/ui';
import { getConfig } from 'app/core/config';
import { OrgRole } from 'app/types';
import { getBackendSrv } from '@grafana/runtime';
import { updateLocation } from 'app/core/actions';
import { connect } from 'react-redux';
import { hot } from 'react-hot-loader';
import { appEvents } from 'app/core/core';
import { AppEvents } from '@grafana/data';
import { assureBaseUrl } from 'app/core/utils/location_util';
const roles = [
{ label: 'Viewer', value: OrgRole.Viewer },
{ label: 'Editor', value: OrgRole.Editor },
{ label: 'Admin', value: OrgRole.Admin },
];
interface FormModel {
role: OrgRole;
name: string;
loginOrEmail?: string;
sendEmail: boolean;
email: string;
}
interface Props {
updateLocation: typeof updateLocation;
}
export const UserInviteForm: FC<Props> = ({ updateLocation }) => {
const onSubmit = async (formData: FormModel) => {
try {
await getBackendSrv().post('/api/org/invites', formData);
} catch (err) {
appEvents.emit(AppEvents.alertError, ['Failed to send invite', err.message]);
}
updateLocation({ path: 'org/users/' });
};
const defaultValues: FormModel = {
name: '',
email: '',
role: OrgRole.Editor,
sendEmail: true,
};
return (
<Forms.Form defaultValues={defaultValues} onSubmit={onSubmit}>
{({ register, control, errors }) => {
return (
<>
<Forms.Field
invalid={!!errors.loginOrEmail}
error={!!errors.loginOrEmail && 'Email or Username is required'}
label="Email or Username"
>
<Input size="md" name="loginOrEmail" placeholder="email@example.com" ref={register({ required: true })} />
</Forms.Field>
<Forms.Field invalid={!!errors.name} label="Name">
<Input size="md" name="name" placeholder="(optional)" ref={register} />
</Forms.Field>
<Forms.Field invalid={!!errors.role} label="Role">
<Forms.InputControl as={Forms.RadioButtonGroup} control={control} options={roles} name="role" />
</Forms.Field>
<Forms.Field invalid={!!errors.sendEmail} label="Send invite email">
<Switch name="sendEmail" ref={register} />
</Forms.Field>
<HorizontalGroup>
<Button type="submit">Submit</Button>
<LinkButton href={assureBaseUrl(getConfig().appSubUrl + '/org/users')} variant="secondary">
Back
</LinkButton>
</HorizontalGroup>
</>
);
}}
</Forms.Form>
);
};
const mapDispatchToProps = {
updateLocation,
};
export default hot(module)(connect(null, mapDispatchToProps)(UserInviteForm));