Skip to content

Commit

Permalink
Refactor tenant API routes for core config list eg/ fetching URL chan…
Browse files Browse the repository at this point in the history
…ge needed. Fix missing data fetch when listing tenants.
  • Loading branch information
prateek3255 committed Mar 20, 2024
1 parent ce763f3 commit 6e816c2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/api/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const useCoreConfigService = () => {
config: CoreConfigOptions;
}> => {
const response = await fetchData({
url: getApiUrl("/api/multitenancy/core-config/list"),
url: getApiUrl("/api/core/config/list"),
method: "GET",
});

Expand Down
2 changes: 1 addition & 1 deletion src/api/tenants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type TenantInfo = {
thirdParty: {
enabled: boolean;
providers: Array<ProviderConfig>;
mergedProvidersFromCoreAndStatic: Array<ProviderConfig>;
};
passwordless: {
enabled: boolean;
Expand All @@ -57,7 +58,6 @@ export type TenantInfo = {
coreConfig: Record<string, unknown>;
userCount: number;
validFirstFactors: Array<string>;
mergedProvidersFromCoreAndStatic: Array<ProviderConfig>;
};

export type UpdateTenant = {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/tenants/tenantDetail/TenantDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const TenantDetail = ({
if (
typeof tenant?.tenantId === "string" &&
tenant?.thirdParty.enabled &&
tenant?.mergedProvidersFromCoreAndStatic.length === 0
tenant?.thirdParty.mergedProvidersFromCoreAndStatic.length === 0
) {
setIsNoProviderAddedDialogVisible(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const TenantDetailContextProvider = ({
refetchTenant,
coreConfigOptions,
setTenantInfo,
resolvedProviders: tenantInfo.mergedProvidersFromCoreAndStatic,
resolvedProviders: tenantInfo.thirdParty.mergedProvidersFromCoreAndStatic,
}}>
{children}
</TenantDetailContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export const DeleteThirdPartyProviderDialog = ({
}) => {
const [isDeletingProvider, setIsDeletingProvider] = useState(false);
const { deleteThirdPartyProvider } = useThirdPartyService();
const { tenantInfo } = useTenantDetailContext();
const { tenantInfo, resolvedProviders } = useTenantDetailContext();
const { showToast } = useContext(PopupContentContext);
const isLastProvider = resolvedProviders.length === 1;

const handleDeleteProperty = async () => {
try {
Expand All @@ -62,25 +63,37 @@ export const DeleteThirdPartyProviderDialog = ({

return (
<Dialog
title="Delete Provider?"
title={isLastProvider ? "Provider cannot be deleted" : "Delete Provider?"}
onCloseDialog={onCloseDialog}>
<DialogContent>
<p className="confirm-text">
Are you sure you want to delete the provider: <span className="third-party-id">{thirdPartyId}</span>
{isLastProvider ? (
<>
This is your only added provider, and it cannot be deleted because at least one provider is
required when third party login method is enabled.
</>
) : (
<>
Are you sure you want to delete the provider:{" "}
<span className="third-party-id">{thirdPartyId}</span>
</>
)}
</p>
<DialogFooter border="border-none">
<Button
onClick={onCloseDialog}
color="gray-outline">
Cancel
</Button>
<Button
color="danger"
isLoading={isDeletingProvider}
disabled={isDeletingProvider}
onClick={handleDeleteProperty}>
Yes, Delete
</Button>
{!isLastProvider && (
<Button
color="danger"
isLoading={isDeletingProvider}
disabled={isDeletingProvider}
onClick={handleDeleteProperty}>
Yes, Delete
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
Expand Down
34 changes: 8 additions & 26 deletions src/ui/components/tenants/tenantsListTable/TenantsListTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
import { Tenant } from "../../../../api/tenants/list";
import { FIRST_FACTOR_IDS } from "../../../../constants";
import { getImageUrl, getInitializedRecipes } from "../../../../utils";
import { Tenant } from "../../../../api/tenants/login-methods";
import { getImageUrl } from "../../../../utils";
import Pagination from "../../pagination";
import { RecipePill } from "../../recipePill/RecipePill";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../../table";
Expand All @@ -34,30 +33,13 @@ type TenantsListTableProps = {

const TenantLoginMethods = ({ tenant }: { tenant: Tenant }) => {
const getEnabledLoginMethods = () => {
const tenantLoginMethods = {
emailPassword: tenant.emailPassword.enabled,
passwordless: tenant.passwordless.enabled,
thirdParty: tenant.thirdParty.enabled,
};
if (Array.isArray(tenant.firstFactors) && tenant.firstFactors.length > 0) {
const allFactors = Array.from(
new Set([...tenant.firstFactors, ...(tenant.requiredSecondaryFactors ?? [])])
);
tenantLoginMethods.emailPassword = allFactors.some((factor) =>
FIRST_FACTOR_IDS.some((f) => f.loginMethod === "emailpassword" && f.id === factor)
);
tenantLoginMethods.passwordless = allFactors.some((factor) =>
FIRST_FACTOR_IDS.some((f) => f.loginMethod === "otp-email" && f.id === factor)
);
tenantLoginMethods.thirdParty = allFactors.some((factor) =>
FIRST_FACTOR_IDS.some((f) => f.loginMethod === "thirdparty" && f.id === factor)
);
}
const initalizedRecipes = getInitializedRecipes();
return {
emailPassword: tenantLoginMethods.emailPassword && initalizedRecipes.emailPassword,
passwordless: tenantLoginMethods.passwordless && initalizedRecipes.passwordless.enabled,
thirdParty: tenantLoginMethods.thirdParty && initalizedRecipes.thirdParty,
emailPassword: tenant.emailPassword.enabled || tenant.thirdPartyEmailPasssword.enabled,
passwordless: tenant.passwordless.enabled || tenant.thirdPartyPasswordless.enabled,
thirdParty:
tenant.thirdParty.enabled ||
tenant.thirdPartyEmailPasssword.enabled ||
tenant.thirdPartyPasswordless.enabled,
};
};

Expand Down
6 changes: 3 additions & 3 deletions src/ui/pages/tenants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { useContext, useDeferredValue, useEffect, useState } from "react";
import { useLocation, useNavigate } from "react-router";
import { useGetTenantsList, type Tenant } from "../../../api/tenants/list";
import { useGetTenantsLoginMethods, type Tenant } from "../../../api/tenants/login-methods";
import { ReactComponent as PlusIcon } from "../../../assets/plus.svg";
import { getImageUrl, useQuery } from "../../../utils";
import Button from "../../components/button";
Expand All @@ -29,7 +29,7 @@ import "./index.scss";
const TENANTS_PAGINATION_LIMIT = 10;

const TenantList = ({ selectTenant }: { selectTenant: (tenantId: string) => void }) => {
const { fetchTenants } = useGetTenantsList();
const { fetchTenantsLoginMethods } = useGetTenantsLoginMethods();
const { showToast } = useContext(PopupContentContext);
const [tenants, setTenants] = useState<Array<Tenant> | undefined>(undefined);
const [searchQuery, setSearchQuery] = useState<string>("");
Expand All @@ -47,7 +47,7 @@ const TenantList = ({ selectTenant }: { selectTenant: (tenantId: string) => void
useEffect(() => {
const getTenants = async () => {
try {
const response = await fetchTenants();
const response = await fetchTenantsLoginMethods();
if (response?.status === "OK") {
setTenants(response.tenants);
} else {
Expand Down

0 comments on commit 6e816c2

Please sign in to comment.