UserInviteForm.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. import { locationUtil } from '@grafana/data';
  3. import { locationService } from '@grafana/runtime';
  4. import {
  5. HorizontalGroup,
  6. Button,
  7. LinkButton,
  8. Input,
  9. Switch,
  10. RadioButtonGroup,
  11. Form,
  12. Field,
  13. InputControl,
  14. } from '@grafana/ui';
  15. import { getConfig } from 'app/core/config';
  16. import { OrgRole, useDispatch } from 'app/types';
  17. import { addInvitee } from '../invites/state/actions';
  18. const roles = [
  19. { label: 'Viewer', value: OrgRole.Viewer },
  20. { label: 'Editor', value: OrgRole.Editor },
  21. { label: 'Admin', value: OrgRole.Admin },
  22. ];
  23. export interface FormModel {
  24. role: OrgRole;
  25. name: string;
  26. loginOrEmail?: string;
  27. sendEmail: boolean;
  28. email: string;
  29. }
  30. const defaultValues: FormModel = {
  31. name: '',
  32. email: '',
  33. role: OrgRole.Editor,
  34. sendEmail: true,
  35. };
  36. export const UserInviteForm = () => {
  37. const dispatch = useDispatch();
  38. const onSubmit = async (formData: FormModel) => {
  39. await dispatch(addInvitee(formData)).unwrap();
  40. locationService.push('/org/users/');
  41. };
  42. return (
  43. <Form defaultValues={defaultValues} onSubmit={onSubmit}>
  44. {({ register, control, errors }) => {
  45. return (
  46. <>
  47. <Field
  48. invalid={!!errors.loginOrEmail}
  49. error={!!errors.loginOrEmail ? 'Email or username is required' : undefined}
  50. label="Email or username"
  51. >
  52. <Input {...register('loginOrEmail', { required: true })} placeholder="email@example.com" />
  53. </Field>
  54. <Field invalid={!!errors.name} label="Name">
  55. <Input {...register('name')} placeholder="(optional)" />
  56. </Field>
  57. <Field invalid={!!errors.role} label="Role">
  58. <InputControl
  59. render={({ field: { ref, ...field } }) => <RadioButtonGroup {...field} options={roles} />}
  60. control={control}
  61. name="role"
  62. />
  63. </Field>
  64. <Field label="Send invite email">
  65. <Switch id="send-email-switch" {...register('sendEmail')} />
  66. </Field>
  67. <HorizontalGroup>
  68. <Button type="submit">Submit</Button>
  69. <LinkButton href={locationUtil.assureBaseUrl(getConfig().appSubUrl + '/org/users')} variant="secondary">
  70. Back
  71. </LinkButton>
  72. </HorizontalGroup>
  73. </>
  74. );
  75. }}
  76. </Form>
  77. );
  78. };
  79. export default UserInviteForm;