diff --git a/public/app/features/admin/AdminEditOrgPage.tsx b/public/app/features/admin/AdminEditOrgPage.tsx index e1cab49a294..910bc855251 100644 --- a/public/app/features/admin/AdminEditOrgPage.tsx +++ b/public/app/features/admin/AdminEditOrgPage.tsx @@ -1,8 +1,9 @@ import React, { useState, useEffect } from 'react'; +import { useForm } from 'react-hook-form'; import { useAsyncFn } from 'react-use'; import { NavModelItem } from '@grafana/data'; -import { Form, Field, Input, Button, Legend, Alert } from '@grafana/ui'; +import { Field, Input, Button, Legend, Alert } from '@grafana/ui'; import { Page } from 'app/core/components/Page/Page'; import { contextSrv } from 'app/core/core'; import { GrafanaRouteComponentProps } from 'app/core/navigation/types'; @@ -27,6 +28,11 @@ const AdminEditOrgPage = ({ match }: Props) => { const [totalPages, setTotalPages] = useState(1); const [orgState, fetchOrg] = useAsyncFn(() => getOrg(orgId), []); + const { + handleSubmit, + register, + formState: { errors }, + } = useForm(); const [, fetchOrgUsers] = useAsyncFn(async (page) => { const result = await getOrgUsers(orgId, page); @@ -45,8 +51,8 @@ const AdminEditOrgPage = ({ match }: Props) => { fetchOrgUsers(page); }, [fetchOrg, fetchOrgUsers, page]); - const onUpdateOrgName = async (name: string) => { - await updateOrgName(name, orgId); + const onUpdateOrgName = async ({ orgName }: OrgNameDTO) => { + await updateOrgName(orgName, orgId); }; const renderMissingPermissionMessage = () => ( @@ -82,21 +88,18 @@ const AdminEditOrgPage = ({ match }: Props) => { <> Edit organization {orgState.value && ( -
onUpdateOrgName(values.orgName)} - > - {({ register, errors }) => ( - <> - - - - - - )} -
+
+ + + + +
)}
diff --git a/public/app/features/admin/UserCreatePage.tsx b/public/app/features/admin/UserCreatePage.tsx index da72bd30d90..34bdee39089 100644 --- a/public/app/features/admin/UserCreatePage.tsx +++ b/public/app/features/admin/UserCreatePage.tsx @@ -1,9 +1,10 @@ import React, { useCallback } from 'react'; +import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom'; import { NavModelItem } from '@grafana/data'; import { getBackendSrv } from '@grafana/runtime'; -import { Form, Button, Input, Field } from '@grafana/ui'; +import { Button, Input, Field } from '@grafana/ui'; import { Page } from 'app/core/components/Page/Page'; interface UserDTO { @@ -24,6 +25,11 @@ const pageNav: NavModelItem = { const UserCreatePage = () => { const history = useHistory(); + const { + handleSubmit, + register, + formState: { errors }, + } = useForm({ mode: 'onBlur' }); const onSubmit = useCallback( async (data: UserDTO) => { @@ -37,45 +43,34 @@ const UserCreatePage = () => { return ( -
- {({ register, errors }) => { - return ( - <> - - - + + + + - - - + + + - - - - - value.trim() !== '' && value.length >= 4, - })} - type="password" - /> - - - - ); - }} -
+ + + + + value.trim() !== '' && value.length >= 4, + })} + type="password" + /> + + +
); diff --git a/public/app/features/admin/ldap/LdapPage.tsx b/public/app/features/admin/ldap/LdapPage.tsx index d61c7711ca1..d7bf34f6b87 100644 --- a/public/app/features/admin/ldap/LdapPage.tsx +++ b/public/app/features/admin/ldap/LdapPage.tsx @@ -1,9 +1,10 @@ -import React, { PureComponent } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; +import { useForm } from 'react-hook-form'; import { connect, ConnectedProps } from 'react-redux'; import { NavModelItem } from '@grafana/data'; import { featureEnabled } from '@grafana/runtime'; -import { Alert, Button, Field, Form, Input, Stack } from '@grafana/ui'; +import { Alert, Button, Field, Input, Stack } from '@grafana/ui'; import { Page } from 'app/core/components/Page/Page'; import { contextSrv } from 'app/core/core'; import { GrafanaRouteComponentProps } from 'app/core/navigation/types'; @@ -37,10 +38,6 @@ interface OwnProps extends GrafanaRouteComponentProps<{}, { username?: string }> userError?: LdapError; } -interface State { - isLoading: boolean; -} - interface FormModel { username: string; } @@ -52,101 +49,105 @@ const pageNav: NavModelItem = { id: 'LDAP', }; -export class LdapPage extends PureComponent { - state = { - isLoading: true, - }; +export const LdapPage = ({ + clearUserMappingInfo, + queryParams, + loadLdapState, + loadLdapSyncStatus, + loadUserMapping, + clearUserError, + ldapUser, + userError, + ldapError, + ldapSyncInfo, + ldapConnectionInfo, +}: Props) => { + const [isLoading, setIsLoading] = useState(true); + const { register, handleSubmit } = useForm(); - async componentDidMount() { - const { clearUserMappingInfo, queryParams } = this.props; - await clearUserMappingInfo(); - await this.fetchLDAPStatus(); + const fetchUserMapping = useCallback( + async (username: string) => { + return loadUserMapping(username); + }, + [loadUserMapping] + ); - if (queryParams.username) { - await this.fetchUserMapping(queryParams.username); + useEffect(() => { + const fetchLDAPStatus = async () => { + return Promise.all([loadLdapState(), loadLdapSyncStatus()]); + }; + + async function init() { + await clearUserMappingInfo(); + await fetchLDAPStatus(); + + if (queryParams.username) { + await fetchUserMapping(queryParams.username); + } + + setIsLoading(false); } - this.setState({ isLoading: false }); - } + init(); + }, [clearUserMappingInfo, fetchUserMapping, loadLdapState, loadLdapSyncStatus, queryParams]); - async fetchLDAPStatus() { - const { loadLdapState, loadLdapSyncStatus } = this.props; - return Promise.all([loadLdapState(), loadLdapSyncStatus()]); - } - - async fetchUserMapping(username: string) { - const { loadUserMapping } = this.props; - return await loadUserMapping(username); - } - - search = (username: string) => { + const search = ({ username }: FormModel) => { if (username) { - this.fetchUserMapping(username); + fetchUserMapping(username); } }; - onClearUserError = () => { - this.props.clearUserError(); + const onClearUserError = () => { + clearUserError(); }; - render() { - const { ldapUser, userError, ldapError, ldapSyncInfo, ldapConnectionInfo, queryParams } = this.props; - const { isLoading } = this.state; - const canReadLDAPUser = contextSrv.hasPermission(AccessControlAction.LDAPUsersRead); + const canReadLDAPUser = contextSrv.hasPermission(AccessControlAction.LDAPUsersRead); + return ( + + + + {ldapError && ldapError.title && ( + + {ldapError.body} + + )} - return ( - - - - {ldapError && ldapError.title && ( - - {ldapError.body} - - )} + - + {featureEnabled('ldapsync') && ldapSyncInfo && } - {featureEnabled('ldapsync') && ldapSyncInfo && } - - {canReadLDAPUser && ( -
-

Test user mapping

-
this.search(data.username)}> - {({ register }) => ( - - - Run - - } - /> - - )} -
- {userError && userError.title && ( - - {userError.body} - - )} - {ldapUser && } -
- )} -
-
-
- ); - } -} + {canReadLDAPUser && ( +
+

Test user mapping

+
+ + + Run + + } + /> + +
+ {userError && userError.title && ( + + {userError.body} + + )} + {ldapUser && } +
+ )} +
+
+
+ ); +}; const mapStateToProps = (state: StoreState) => ({ ldapConnectionInfo: state.ldap.connectionInfo,