Skip to content

Commit

Permalink
Replace getUserStoreList() method with fetching from provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DonOmalVindula committed Feb 28, 2025
1 parent e517ed6 commit b3da7dd
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import useUserStores from "@wso2is/admin.userstores.v1/hooks/use-user-stores";
import { UserStoreListItem } from "@wso2is/admin.userstores.v1/models/user-stores";
import { TestableComponentInterface } from "@wso2is/core/models";
import { Field, FormValue, Forms } from "@wso2is/forms";
import React, { FunctionComponent, ReactElement, useEffect, useState } from "react";
import React, { FunctionComponent, ReactElement, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import { Divider, Grid } from "semantic-ui-react";
Expand Down Expand Up @@ -63,7 +63,6 @@ export const MappedAttributes: FunctionComponent<MappedAttributesPropsInterface>
[ "data-testid" ]: testId
} = props;

const [ userStore, setUserStore ] = useState<UserStoreListItem[]>([]);
const hiddenUserStores: string[] = useSelector((state: AppState) => state.config.ui.hiddenUserStores ?? []);
const primaryUserStoreDomainName: string = useSelector((state: AppState) =>
state?.config?.ui?.primaryUserStoreDomainName ?? userstoresConfig.primaryUserstoreName);
Expand All @@ -75,7 +74,7 @@ export const MappedAttributes: FunctionComponent<MappedAttributesPropsInterface>
userStoresList
} = useUserStores();

useEffect(() => {
const userStore: UserStoreListItem[] = useMemo(() => {
const userstore: UserStoreListItem[] = [];

if (attributeConfig.localAttributes.createWizard.showPrimaryUserStore) {
Expand All @@ -96,7 +95,7 @@ export const MappedAttributes: FunctionComponent<MappedAttributesPropsInterface>
});
}

setUserStore(userstore);
return userstore;
}, [ userStoresList, isUserStoreListFetchRequestLoading ]);

return (
Expand Down
77 changes: 38 additions & 39 deletions features/admin.claims.v1/pages/local-claims-edit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2020-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -17,16 +17,15 @@
*/

import { AppConstants } from "@wso2is/admin.core.v1/constants/app-constants";
import { AppState } from "@wso2is/admin.core.v1/store";
import { history } from "@wso2is/admin.core.v1/helpers/history";
import { AppState } from "@wso2is/admin.core.v1/store";
import { attributeConfig, userstoresConfig } from "@wso2is/admin.extensions.v1";
import { getUserStoreList } from "@wso2is/admin.userstores.v1/api";
import useUserStores from "@wso2is/admin.userstores.v1/hooks/use-user-stores";
import { UserStoreBasicData } from "@wso2is/admin.userstores.v1/models";
import { AlertLevels, Claim, TestableComponentInterface } from "@wso2is/core/models";
import { addAlert } from "@wso2is/core/store";
import { AnimatedAvatar, ResourceTab, ResourceTabPaneInterface, TabPageLayout } from "@wso2is/react-components";
import { AxiosResponse } from "axios";
import React, { FunctionComponent, ReactElement, useEffect, useState } from "react";
import React, { FunctionComponent, ReactElement, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
import { RouteComponentProps } from "react-router";
Expand Down Expand Up @@ -68,19 +67,50 @@ const LocalClaimsEditPage: FunctionComponent<LocalClaimsEditPageInterface> = (
[ "data-testid" ]: testId
} = props;

const dispatch: Dispatch = useDispatch();

const { t } = useTranslation();
const {
isLoading: isUserStoreListFetchRequestLoading,
mutateUserStoreList,
userStoresList
} = useUserStores();

const claimID: string = match.params.id;
const hiddenUserStores: string[] = useSelector((state: AppState) => state?.config?.ui?.hiddenUserStores);
const primaryUserStoreDomainName: string = useSelector((state: AppState) =>
state?.config?.ui?.primaryUserStoreDomainName);

const [ claim, setClaim ] = useState<Claim>(null);
const [ isLocalClaimDetailsRequestLoading, setIsLocalClaimDetailsRequestLoading ] = useState<boolean>(false);
const [ userStores, setUserStores ] = useState<UserStoreBasicData[]>([]);
const defaultActiveIndex: string = ClaimTabIDs.GENERAL;

const dispatch: Dispatch = useDispatch();
useEffect(() => {
getClaim();
mutateUserStoreList();
}, []);

const { t } = useTranslation();
const userStores: UserStoreBasicData[] = useMemo(() => {
const userStores: UserStoreBasicData[] = [];

if (userstoresConfig?.primaryUserstoreName === primaryUserStoreDomainName) {
userStores.push({
id: primaryUserStoreDomainName,
name: primaryUserStoreDomainName
});
}

if (!isUserStoreListFetchRequestLoading && userStoresList?.length > 0) {
const filteredUserStores: UserStoreBasicData[] = hiddenUserStores?.length > 0
? userStoresList.filter((store: UserStoreBasicData) =>
!hiddenUserStores?.includes(store?.name))
: userStoresList;

userStores.push(...filteredUserStores);
}

return userStores;
}, [ isUserStoreListFetchRequestLoading, userStoresList ]);

/**
* Fetches the local claim.
Expand All @@ -107,37 +137,6 @@ const LocalClaimsEditPage: FunctionComponent<LocalClaimsEditPageInterface> = (
});
};

useEffect(() => {
getClaim();
}, []);

useEffect(() => {
const userStores: UserStoreBasicData[] = [];

if (userstoresConfig?.primaryUserstoreName === primaryUserStoreDomainName) {
userStores.push({
id: primaryUserStoreDomainName,
name: primaryUserStoreDomainName
});
}

getUserStoreList()
.then((response: AxiosResponse) => {
const userStoresData: UserStoreBasicData[] = response?.data || [];

const filteredUserStores: UserStoreBasicData[] = hiddenUserStores?.length > 0
? userStoresData.filter((store: UserStoreBasicData) =>
!hiddenUserStores?.includes(store?.name))
: userStoresData;

userStores.push(...filteredUserStores);
setUserStores(userStores);
})
.catch(() => {
setUserStores(userStores);
});
}, []);

/**
* Contains the data of the panes.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2023-2024, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2023-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -17,16 +17,17 @@
*/

import { Show } from "@wso2is/access-control";
import { SimpleUserStoreListItemInterface } from "@wso2is/admin.applications.v1/models/application";
import { AppState } from "@wso2is/admin.core.v1/store";
import { FeatureConfigInterface } from "@wso2is/admin.core.v1/models/config";
import { identityProviderConfig } from "@wso2is/admin.extensions.v1";
import { AppState } from "@wso2is/admin.core.v1/store";
import { identityProviderConfig, userstoresConfig } from "@wso2is/admin.extensions.v1";
import { useGetCurrentOrganizationType } from "@wso2is/admin.organizations.v1/hooks/use-get-organization-type";
import useUserStores from "@wso2is/admin.userstores.v1/hooks/use-user-stores";
import { UserStoreListItem } from "@wso2is/admin.userstores.v1/models";
import { TestableComponentInterface } from "@wso2is/core/models";
import { Field, FormValue, Forms } from "@wso2is/forms";
import { Code, DocumentationLink, Hint, Message, useDocumentation } from "@wso2is/react-components";
import classNames from "classnames";
import React, { Fragment, FunctionComponent, ReactElement, useEffect, useState } from "react";
import React, { Fragment, FunctionComponent, ReactElement, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import { Button, DropdownItemProps, Grid } from "semantic-ui-react";
Expand All @@ -43,7 +44,6 @@ interface JITProvisioningConfigurationFormPropsInterface extends TestableCompone
idpId: string;
onSubmit: (values: ConnectionInterface) => void;
initialValues: JITProvisioningResponseInterface;
useStoreList: SimpleUserStoreListItemInterface[];
isReadOnly?: boolean;
/**
* Specifies if the form is submitting.
Expand Down Expand Up @@ -72,7 +72,6 @@ export const JITProvisioningConfigurationsForm: FunctionComponent<JITProvisionin
const {
initialValues,
onSubmit,
useStoreList,
isReadOnly,
isSubmitting,
[ "data-testid" ]: testId
Expand All @@ -81,10 +80,58 @@ export const JITProvisioningConfigurationsForm: FunctionComponent<JITProvisionin
const { t } = useTranslation();
const { getLink } = useDocumentation();
const { isSubOrganization } = useGetCurrentOrganizationType();
const {
isLoading: isUserStoreListFetchRequestLoading,
isUserStoreReadOnly,
mutateUserStoreList,
userStoresList
} = useUserStores();

const featureConfig : FeatureConfigInterface = useSelector((state: AppState) => state.config.ui.features);
const primaryUserStoreDomainName: string = useSelector((state: AppState) =>
state?.config?.ui?.primaryUserStoreDomainName ?? userstoresConfig.primaryUserstoreName);

const [ isJITProvisioningEnabled, setIsJITProvisioningEnabled ] = useState<boolean>(false);

const userStoreOptions: DropdownItemProps[] = useMemo(() => {
const storeOptions: DropdownItemProps[] = [
{
key: -1,
text: primaryUserStoreDomainName,
value: primaryUserStoreDomainName
}
];

if (!isUserStoreListFetchRequestLoading && userStoresList?.length > 0) {
userStoresList.map((store: UserStoreListItem, index: number) => {
const isReadOnly: boolean = isUserStoreReadOnly(store.name);
const isEnabled: boolean = store.enabled;

if (store.name.toUpperCase() !== userstoresConfig.primaryUserstoreName && !isReadOnly && isEnabled) {
const storeOption: DropdownItemProps = {
key: index,
text: store.name,
value: store.name
};

storeOptions.push(storeOption);
}
});
}

return storeOptions;
}, [ isUserStoreListFetchRequestLoading, userStoresList ]);

useEffect(() => {
mutateUserStoreList();
}, []);

useEffect(() => {
if (initialValues?.isEnabled) {
setIsJITProvisioningEnabled(initialValues?.isEnabled);
}
}, [ initialValues ]);

/**
* Prepare form values for submitting.
*
Expand All @@ -110,31 +157,6 @@ export const JITProvisioningConfigurationsForm: FunctionComponent<JITProvisionin
} as JITProvisioningResponseInterface;
};

/**
* Create user store options.
*/
const getUserStoreOption = () => {
const allowedOptions: DropdownItemProps[] = [];

if (useStoreList) {
useStoreList?.map((userStore: SimpleUserStoreListItemInterface) => {
allowedOptions.push({
key: useStoreList.indexOf(userStore),
text: userStore?.name,
value: userStore?.name
});
});
}

return allowedOptions;
};

useEffect(() => {
if (initialValues?.isEnabled) {
setIsJITProvisioningEnabled(initialValues?.isEnabled);
}
}, [ initialValues ]);

const supportedProvisioningSchemes: {
label: string;
value: SupportedJITProvisioningSchemes;
Expand Down Expand Up @@ -281,9 +303,9 @@ export const JITProvisioningConfigurationsForm: FunctionComponent<JITProvisionin
required={ false }
requiredErrorMessage=""
type="dropdown"
default={ useStoreList && useStoreList.length > 0 && useStoreList[ 0 ].name }
default={ userStoreOptions[0]?.name }
value={ initialValues?.userstore }
children={ getUserStoreOption() }
children={ userStoreOptions }
disabled={ !isJITProvisioningEnabled }
data-testid={ `${ testId }-user-store-domain` }
readOnly={ isReadOnly }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
* under the License.
*/

import { SimpleUserStoreListItemInterface } from "@wso2is/admin.applications.v1/models/application";
import { AppState } from "@wso2is/admin.core.v1/store";
import { getUserStoreList } from "@wso2is/admin.userstores.v1/api";
import { AlertLevels, TestableComponentInterface } from "@wso2is/core/models";
import { addAlert } from "@wso2is/core/store";
import { EmphasizedSegment } from "@wso2is/react-components";
import { AxiosResponse } from "axios";
import React, { FunctionComponent, ReactElement, useEffect, useState } from "react";
import React, { FunctionComponent, ReactElement } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
import { useDispatch } from "react-redux";
import { Dispatch } from "redux";
import { updateJITProvisioningConfigs } from "../../../api/connections";
import { JITProvisioningResponseInterface } from "../../../models/connection";
Expand Down Expand Up @@ -80,14 +76,9 @@ export const JITProvisioningSettings: FunctionComponent<JITProvisioningSettingsI
[ "data-testid" ]: testId
} = props;

const primaryUserStoreDomainName: string = useSelector((state: AppState) =>
state?.config?.ui?.primaryUserStoreDomainName);

const dispatch: Dispatch = useDispatch();
const { t } = useTranslation();

const [ userStore, setUserStore ] = useState<SimpleUserStoreListItemInterface[]>([]);

/**
* Handles the advanced config form submit action.
*
Expand Down Expand Up @@ -117,22 +108,6 @@ export const JITProvisioningSettings: FunctionComponent<JITProvisioningSettingsI
});
};

useEffect(() => {
const userstore: SimpleUserStoreListItemInterface[] = [];

userstore.push({
id: primaryUserStoreDomainName,
name: primaryUserStoreDomainName
});

getUserStoreList().then((response: AxiosResponse) => {
userstore.push(...response.data);
setUserStore(userstore);
}).catch(() => {
setUserStore(userstore);
});
}, []);

return (
!isLoading
? (
Expand All @@ -141,7 +116,6 @@ export const JITProvisioningSettings: FunctionComponent<JITProvisioningSettingsI
idpId={ idpId }
initialValues={ jitProvisioningConfigurations }
onSubmit={ handleJITProvisioningConfigFormSubmit }
useStoreList={ userStore }
data-testid={ testId }
isReadOnly={ isReadOnly }
/>
Expand Down
Loading

0 comments on commit b3da7dd

Please sign in to comment.