2020-03-19 08:22:53 -05:00
|
|
|
import React, { FC } from 'react';
|
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
import Page from 'app/core/components/Page/Page';
|
2020-04-14 11:52:56 -05:00
|
|
|
import { Button, Input, Field, Form } from '@grafana/ui';
|
2020-03-19 08:22:53 -05:00
|
|
|
import { getConfig } from 'app/core/config';
|
|
|
|
import { StoreState } from 'app/types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { NavModel } from '@grafana/data';
|
|
|
|
import { getNavModel } from '../../core/selectors/navModel';
|
|
|
|
|
|
|
|
const createOrg = async (newOrg: { name: string }) => {
|
|
|
|
const result = await getBackendSrv().post('/api/orgs/', newOrg);
|
|
|
|
|
|
|
|
await getBackendSrv().post('/api/user/using/' + result.orgId);
|
|
|
|
window.location.href = getConfig().appSubUrl + '/org';
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateOrg = async (orgName: string) => {
|
|
|
|
try {
|
|
|
|
await getBackendSrv().get(`api/orgs/name/${encodeURI(orgName)}`);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.status === 404) {
|
|
|
|
error.isHandled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return 'Something went wrong';
|
|
|
|
}
|
|
|
|
return 'Organization already exists';
|
|
|
|
};
|
|
|
|
|
|
|
|
interface PropsWithState {
|
|
|
|
navModel: NavModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CreateOrgFormDTO {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NewOrgPage: FC<PropsWithState> = ({ navModel }) => {
|
|
|
|
return (
|
|
|
|
<Page navModel={navModel}>
|
|
|
|
<Page.Contents>
|
2021-03-30 13:54:31 -05:00
|
|
|
<h3 className="page-sub-heading">New organization</h3>
|
2020-03-19 08:22:53 -05:00
|
|
|
|
|
|
|
<p className="playlist-description">
|
2021-03-30 13:54:31 -05:00
|
|
|
Each organization contains their own dashboards, data sources, and configuration, which cannot be shared
|
|
|
|
shared between organizations. While users might belong to more than one organization, multiple organizations
|
|
|
|
are most frequently used in multi-tenant deployments.{' '}
|
2020-03-19 08:22:53 -05:00
|
|
|
</p>
|
|
|
|
|
2020-04-14 11:52:56 -05:00
|
|
|
<Form<CreateOrgFormDTO> onSubmit={createOrg}>
|
2020-03-19 08:22:53 -05:00
|
|
|
{({ register, errors }) => {
|
|
|
|
return (
|
|
|
|
<>
|
2020-04-14 11:52:56 -05:00
|
|
|
<Field label="Organization name" invalid={!!errors.name} error={errors.name && errors.name.message}>
|
2020-04-03 03:04:19 -05:00
|
|
|
<Input
|
2021-03-30 13:54:31 -05:00
|
|
|
placeholder="Org name"
|
2021-04-29 08:54:38 -05:00
|
|
|
{...register('name', {
|
2020-03-19 08:22:53 -05:00
|
|
|
required: 'Organization name is required',
|
2021-01-20 00:59:48 -06:00
|
|
|
validate: async (orgName) => await validateOrg(orgName),
|
2020-03-19 08:22:53 -05:00
|
|
|
})}
|
|
|
|
/>
|
2020-04-14 11:52:56 -05:00
|
|
|
</Field>
|
2020-03-26 05:50:27 -05:00
|
|
|
<Button type="submit">Create</Button>
|
2020-03-19 08:22:53 -05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
2020-04-14 11:52:56 -05:00
|
|
|
</Form>
|
2020-03-19 08:22:53 -05:00
|
|
|
</Page.Contents>
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = (state: StoreState) => {
|
|
|
|
return { navModel: getNavModel(state.navIndex, 'global-orgs') };
|
|
|
|
};
|
|
|
|
|
2021-08-31 05:55:05 -05:00
|
|
|
export default connect(mapStateToProps)(NewOrgPage);
|