Skip to content

Commit

Permalink
Simplify import of change_password_async component by providing...
Browse files Browse the repository at this point in the history
... `notifications` and `userAPIClient` props in the security plugin
  • Loading branch information
yakhinvadim committed Jun 8, 2021
1 parent 4f8f832 commit 9f18ea9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ import React, { useState, useEffect, useMemo } from 'react';
import { useValues } from 'kea';

import type { AuthenticatedUser } from '../../../../../../security/public';
import { HttpLogic } from '../../../shared/http/http_logic';
import { KibanaLogic } from '../../../shared/kibana/kibana_logic';

export const AccountSettings: React.FC = () => {
const { security, notifications } = useValues(KibanaLogic);
const { http } = useValues(HttpLogic);
const { security } = useValues(KibanaLogic);

const [currentUser, setCurrentUser] = useState<AuthenticatedUser | null>(null);

useEffect(() => {
security!.authc!.getCurrentUser().then(setCurrentUser);
}, [security.authc]);

const UserAPIClient = security!.uiApi!.UserAPIClient;
const PersonalInfo = useMemo(() => security!.uiApi!.components.getPersonalInfo, [security.uiApi]);
const ChangePassword = useMemo(() => security!.uiApi!.components.getChangePassword, [
security.uiApi,
Expand All @@ -36,11 +33,7 @@ export const AccountSettings: React.FC = () => {
return (
<>
<PersonalInfo user={currentUser} />
<ChangePassword
user={currentUser}
userAPIClient={new UserAPIClient(http)}
notifications={notifications}
/>
<ChangePassword user={currentUser} />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@

import React from 'react';

import type { CoreStart } from 'src/core/public';

import { UserAPIClient } from '../../management/users';
import type { ChangePasswordProps } from './change_password';

export const getChangePasswordComponent = async (): Promise<React.FC<ChangePasswordProps>> => {
export const getChangePasswordComponent = async (
core: CoreStart
): Promise<React.FC<Pick<ChangePasswordProps, 'user'>>> => {
const { ChangePassword } = await import('./change_password');
return (props: ChangePasswordProps) => {
return <ChangePassword {...props} />;

return (props: Pick<ChangePasswordProps, 'user'>) => {
return (
<ChangePassword
notifications={core.notifications}
userAPIClient={new UserAPIClient(core.http)}
{...props}
/>
);
};
};
2 changes: 1 addition & 1 deletion x-pack/plugins/security/public/ui_api/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export const getComponents = ({ core }: GetComponentsOptions) => {

return {
getPersonalInfo: wrapLazy(getPersonalInfoComponent),
getChangePassword: wrapLazy(getChangePasswordComponent),
getChangePassword: wrapLazy(() => getChangePasswordComponent(core)),
};
};
2 changes: 1 addition & 1 deletion x-pack/plugins/security/public/ui_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type LazyComponentFn<T> = (props: T) => ReactElement;
export interface UiApi {
components: {
getPersonalInfo: LazyComponentFn<PersonalInfoProps>;
getChangePassword: LazyComponentFn<ChangePasswordProps>;
getChangePassword: LazyComponentFn<Pick<ChangePasswordProps, 'user'>>;
};
UserAPIClient: typeof UserAPIClient;
}
Expand Down

0 comments on commit 9f18ea9

Please sign in to comment.