SignupInvited.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { FC, useState } from 'react';
  2. import { useAsync } from 'react-use';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { Button, Field, Form, Input } from '@grafana/ui';
  5. import Page from 'app/core/components/Page/Page';
  6. import { getConfig } from 'app/core/config';
  7. import { contextSrv } from 'app/core/core';
  8. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  9. interface FormModel {
  10. email: string;
  11. name?: string;
  12. username: string;
  13. password?: string;
  14. }
  15. const navModel = {
  16. main: {
  17. icon: 'grafana',
  18. text: 'Invite',
  19. subTitle: 'Register your Grafana account',
  20. breadcrumbs: [{ title: 'Login', url: 'login' }],
  21. },
  22. node: {
  23. text: '',
  24. },
  25. };
  26. export interface Props extends GrafanaRouteComponentProps<{ code: string }> {}
  27. export const SignupInvitedPage: FC<Props> = ({ match }) => {
  28. const code = match.params.code;
  29. const [initFormModel, setInitFormModel] = useState<FormModel>();
  30. const [greeting, setGreeting] = useState<string>();
  31. const [invitedBy, setInvitedBy] = useState<string>();
  32. useAsync(async () => {
  33. const invite = await getBackendSrv().get(`/api/user/invite/${code}`);
  34. setInitFormModel({
  35. email: invite.email,
  36. name: invite.name,
  37. username: invite.email,
  38. });
  39. setGreeting(invite.name || invite.email || invite.username);
  40. setInvitedBy(invite.invitedBy);
  41. }, [code]);
  42. const onSubmit = async (formData: FormModel) => {
  43. await getBackendSrv().post('/api/user/invite/complete', { ...formData, inviteCode: code });
  44. window.location.href = getConfig().appSubUrl + '/';
  45. };
  46. if (!initFormModel) {
  47. return null;
  48. }
  49. return (
  50. <Page navModel={navModel}>
  51. <Page.Contents>
  52. <h3 className="page-sub-heading">Hello {greeting || 'there'}.</h3>
  53. <div className="modal-tagline p-b-2">
  54. <em>{invitedBy || 'Someone'}</em> has invited you to join Grafana and the organization{' '}
  55. <span className="highlight-word">{contextSrv.user.orgName}</span>
  56. <br />
  57. Please complete the following and choose a password to accept your invitation and continue:
  58. </div>
  59. <Form defaultValues={initFormModel} onSubmit={onSubmit}>
  60. {({ register, errors }) => (
  61. <>
  62. <Field invalid={!!errors.email} error={errors.email && errors.email.message} label="Email">
  63. <Input
  64. placeholder="email@example.com"
  65. {...register('email', {
  66. required: 'Email is required',
  67. pattern: {
  68. value: /^\S+@\S+$/,
  69. message: 'Email is invalid',
  70. },
  71. })}
  72. />
  73. </Field>
  74. <Field invalid={!!errors.name} error={errors.name && errors.name.message} label="Name">
  75. <Input placeholder="Name (optional)" {...register('name')} />
  76. </Field>
  77. <Field invalid={!!errors.username} error={errors.username && errors.username.message} label="Username">
  78. <Input {...register('username', { required: 'Username is required' })} placeholder="Username" />
  79. </Field>
  80. <Field invalid={!!errors.password} error={errors.password && errors.password.message} label="Password">
  81. <Input
  82. {...register('password', { required: 'Password is required' })}
  83. type="password"
  84. placeholder="Password"
  85. />
  86. </Field>
  87. <Button type="submit">Sign up</Button>
  88. </>
  89. )}
  90. </Form>
  91. </Page.Contents>
  92. </Page>
  93. );
  94. };
  95. export default SignupInvitedPage;