grafana/public/app/features/admin/AdminEditOrgPage.tsx
Alex Khomenko 3b515e650c
Grafana-UI: Update React Hook Form to v7 (#33328)
* Update hook form

* Update Form component

* Update ChangePassword.tsx

* Update custom types

* Update SaveDashboardForm

* Update form story

* Update FieldArray.story.tsx

* Bump hook form version

* Update typescript to v4.2.4

* Update ForgottenPassword.tsx

* Update LoginForm.tsx

* Update SignupPage.tsx

* Update VerifyEmail.tsx

* Update AdminEditOrgPage.tsx

* Update UserCreatePage.tsx

* Update BasicSettings.tsx

* Update NotificationChannelForm.tsx

* Update NotificationChannelOptions.tsx

* Update NotificationSettings.tsx

* Update OptionElement.tsx

* Update AlertRuleForm.tsx

* Update AlertTypeStep.tsx

* Update AnnotationsField.tsx

* Update ConditionField.tsx

* Update ConditionsStep.tsx

* Update GroupAndNamespaceFields.tsx

* Update LabelsField.tsx

* Update QueryStep.tsx

* Update RowOptionsForm.tsx

* Update SaveDashboardAsForm.tsx

* Update NewDashboardsFolder.tsx

* Update ImportDashboardForm.tsx

* Update DashboardImportPage.tsx

* Update NewOrgPage.tsx

* Update OrgProfile.tsx

* Update UserInviteForm.tsx

* Update PlaylistForm.tsx

* Update ChangePasswordForm.tsx

* Update UserProfileEditForm.tsx

* Update TeamSettings.tsx

* Update SignupInvited.tsx

* Expose setValue from the Form

* Update typescript to v4.2.4

* Remove ref from field props

* Fix tests

* Revert TS update

* Use exact version

* Update latest batch of changes

* Reduce the number of strict TS errors

* Fix defaults

* more type error fixes

* Update CreateTeam

* fix folder picker in rule form

* fixes for hook form 7

* Update docs

Co-authored-by: Domas <domasx2@gmail.com>
2021-04-29 16:54:38 +03:00

113 lines
3.7 KiB
TypeScript

import React, { FC, useState, useEffect } from 'react';
import Page from 'app/core/components/Page/Page';
import { useSelector } from 'react-redux';
import { StoreState, OrgUser } from 'app/types';
import { getNavModel } from 'app/core/selectors/navModel';
import UsersTable from '../users/UsersTable';
import { useAsyncFn } from 'react-use';
import { getBackendSrv } from '@grafana/runtime';
import { UrlQueryValue } from '@grafana/data';
import { Form, Field, Input, Button, Legend } from '@grafana/ui';
import { css } from '@emotion/css';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
interface OrgNameDTO {
orgName: string;
}
const getOrg = async (orgId: UrlQueryValue) => {
return await getBackendSrv().get('/api/orgs/' + orgId);
};
const getOrgUsers = async (orgId: UrlQueryValue) => {
return await getBackendSrv().get('/api/orgs/' + orgId + '/users');
};
const updateOrgUserRole = async (orgUser: OrgUser, orgId: UrlQueryValue) => {
await getBackendSrv().patch('/api/orgs/' + orgId + '/users/' + orgUser.userId, orgUser);
};
const removeOrgUser = async (orgUser: OrgUser, orgId: UrlQueryValue) => {
return await getBackendSrv().delete('/api/orgs/' + orgId + '/users/' + orgUser.userId);
};
interface Props extends GrafanaRouteComponentProps<{ id: string }> {}
export const AdminEditOrgPage: FC<Props> = ({ match }) => {
const navIndex = useSelector((state: StoreState) => state.navIndex);
const navModel = getNavModel(navIndex, 'global-orgs');
const orgId = parseInt(match.params.id, 10);
const [users, setUsers] = useState<OrgUser[]>([]);
const [orgState, fetchOrg] = useAsyncFn(() => getOrg(orgId), []);
const [, fetchOrgUsers] = useAsyncFn(() => getOrgUsers(orgId), []);
useEffect(() => {
fetchOrg();
fetchOrgUsers().then((res) => setUsers(res));
}, [fetchOrg, fetchOrgUsers]);
const updateOrgName = async (name: string) => {
return await getBackendSrv().put('/api/orgs/' + orgId, { ...orgState.value, name });
};
return (
<Page navModel={navModel}>
<Page.Contents>
<>
<Legend>Edit organization</Legend>
{orgState.value && (
<Form
defaultValues={{ orgName: orgState.value.name }}
onSubmit={async (values: OrgNameDTO) => await updateOrgName(values.orgName)}
>
{({ register, errors }) => (
<>
<Field label="Name" invalid={!!errors.orgName} error="Name is required">
<Input {...register('orgName', { required: true })} />
</Field>
<Button>Update</Button>
</>
)}
</Form>
)}
<div
className={css`
margin-top: 20px;
`}
>
<Legend>Organization users</Legend>
{!!users.length && (
<UsersTable
users={users}
onRoleChange={(role, orgUser) => {
updateOrgUserRole({ ...orgUser, role }, orgId);
setUsers(
users.map((user) => {
if (orgUser.userId === user.userId) {
return { ...orgUser, role };
}
return user;
})
);
fetchOrgUsers();
}}
onRemoveUser={(orgUser) => {
removeOrgUser(orgUser, orgId);
setUsers(users.filter((user) => orgUser.userId !== user.userId));
fetchOrgUsers();
}}
/>
)}
</div>
</>
</Page.Contents>
</Page>
);
};
export default AdminEditOrgPage;