grafana/public/app/features/serviceaccounts/ServiceAccountCreatePage.tsx
Eric Leijonmarck 78bdcede70
Service accounts: polish UI and refactoring (#47269)
* no other tab has title

* made Add service button not visable and upper right

* renaming and added space between token number

* aligned button (:css:)

* refactor: out component listitem

* unused import
2022-04-04 17:09:51 +01:00

62 lines
1.9 KiB
TypeScript

import React, { useCallback } from 'react';
import { connect } from 'react-redux';
import { Form, Button, Input, Field } from '@grafana/ui';
import { NavModel } from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime';
import { StoreState } from '../../types';
import { getNavModel } from '../../core/selectors/navModel';
import Page from 'app/core/components/Page/Page';
import { useHistory } from 'react-router-dom';
interface ServiceAccountCreatePageProps {
navModel: NavModel;
}
interface ServiceAccountDTO {
name: string;
}
const createServiceAccount = async (sa: ServiceAccountDTO) => getBackendSrv().post('/api/serviceaccounts/', sa);
const ServiceAccountCreatePage: React.FC<ServiceAccountCreatePageProps> = ({ navModel }) => {
const history = useHistory();
const onSubmit = useCallback(
async (data: ServiceAccountDTO) => {
await createServiceAccount(data);
history.push('/org/serviceaccounts/');
},
[history]
);
return (
<Page navModel={navModel}>
<Page.Contents>
<h1>Create service account</h1>
<Form onSubmit={onSubmit} validateOn="onBlur">
{({ register, errors }) => {
return (
<>
<Field
label="Display name"
required
invalid={!!errors.name}
error={errors.name ? 'Display name is required' : undefined}
>
<Input id="display-name-input" {...register('name', { required: true })} />
</Field>
<Button type="submit">Create</Button>
</>
);
}}
</Form>
</Page.Contents>
</Page>
);
};
const mapStateToProps = (state: StoreState) => ({
navModel: getNavModel(state.navIndex, 'serviceaccounts'),
});
export default connect(mapStateToProps)(ServiceAccountCreatePage);