Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(console): reorg organization details routes #5702

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { condArray } from '@silverhand/essentials';
import { type RouteObject } from 'react-router-dom';
import { Navigate, type RouteObject } from 'react-router-dom';

import { isDevFeaturesEnabled } from '@/consts/env';
import OrganizationDetails from '@/pages/OrganizationDetails';
import Members from '@/pages/OrganizationDetails/Members';
import Settings from '@/pages/OrganizationDetails/Settings';
import { OrganizationDetailsTabs } from '@/pages/OrganizationDetails/types';
import Organizations from '@/pages/Organizations';

export const organizations: RouteObject = {
Expand All @@ -14,6 +17,14 @@ export const organizations: RouteObject = {
path: 'template',
element: <Organizations tab="template" />,
},
{ path: ':id/*', element: <OrganizationDetails /> }
{
path: ':id/*',
element: <OrganizationDetails />,
children: [
{ index: true, element: <Navigate replace to={OrganizationDetailsTabs.Settings} /> },
{ path: OrganizationDetailsTabs.Settings, element: <Settings /> },
{ path: OrganizationDetailsTabs.Members, element: <Members /> },
],
}
),
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type UserWithOrganizationRoles, type Organization } from '@logto/schemas';
import { type UserWithOrganizationRoles } from '@logto/schemas';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useOutletContext } from 'react-router-dom';
import useSWR from 'swr';

import Plus from '@/assets/icons/plus.svg';
Expand All @@ -19,17 +20,16 @@ import useActionTranslation from '@/hooks/use-action-translation';
import useApi, { type RequestError } from '@/hooks/use-api';
import { buildUrl } from '@/utils/url';

import { type OrganizationDetailsOutletContext } from '../types';

import AddMembersToOrganization from './AddMembersToOrganization';
import EditOrganizationRolesModal from './EditOrganizationRolesModal';
import * as styles from './index.module.scss';

const pageSize = defaultPageSize;

type Props = {
organization: Organization;
};

function Members({ organization }: Props) {
function Members() {
const { data: organization } = useOutletContext<OrganizationDetailsOutletContext>();
const api = useApi();
const [keyword, setKeyword] = useState('');
const [page, setPage] = useState(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Organization } from '@logto/schemas';
import { useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { useOutletContext } from 'react-router-dom';

import DetailsForm from '@/components/DetailsForm';
import FormCard from '@/components/FormCard';
Expand All @@ -11,17 +12,10 @@ import TextInput from '@/ds-components/TextInput';
import useApi from '@/hooks/use-api';
import { trySubmitSafe } from '@/utils/form';

type Props = {
/**
* Whether the organization is being deleted, this is used to disable the unsaved
* changes alert modal.
*/
isDeleting: boolean;
data: Organization;
onUpdated: (data: Organization) => void;
};
import { type OrganizationDetailsOutletContext } from '../types';

function Settings({ isDeleting, data, onUpdated }: Props) {
function Settings() {
const { isDeleting, data, onUpdated } = useOutletContext<OrganizationDetailsOutletContext>();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const {
register,
Expand Down
38 changes: 13 additions & 25 deletions packages/console/src/pages/OrganizationDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { type Organization } from '@logto/schemas';
import { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
// FIXME: @gao
// eslint-disable-next-line no-restricted-imports
import { Navigate, Route, Routes, useParams } from 'react-router-dom';
import { Outlet, useParams } from 'react-router-dom';
import useSWR from 'swr';

import Delete from '@/assets/icons/delete.svg';
Expand All @@ -23,15 +21,10 @@ import useTenantPathname from '@/hooks/use-tenant-pathname';

import Introduction from '../Organizations/Guide/Introduction';

import Members from './Members';
import Settings from './Settings';
import * as styles from './index.module.scss';
import { OrganizationDetailsTabs, type OrganizationDetailsOutletContext } from './types';

const pathname = '/organizations';
const tabs = Object.freeze({
settings: 'settings',
members: 'members',
});

function OrganizationDetails() {
const { id } = useParams();
Expand Down Expand Up @@ -111,27 +104,22 @@ function OrganizationDetails() {
{t('organization_details.delete_confirmation')}
</DeleteConfirmModal>
<TabNav>
<TabNavItem href={`${pathname}/${data.id}/${tabs.settings}`}>
<TabNavItem href={`${pathname}/${data.id}/${OrganizationDetailsTabs.Settings}`}>
{t('general.settings_nav')}
</TabNavItem>
<TabNavItem href={`${pathname}/${data.id}/${tabs.members}`}>
<TabNavItem href={`${pathname}/${data.id}/${OrganizationDetailsTabs.Members}`}>
{t('organizations.members')}
</TabNavItem>
</TabNav>
<Routes>
<Route index element={<Navigate replace to={tabs.settings} />} />
<Route
path={tabs.settings}
element={
<Settings
isDeleting={isDeleting}
data={data}
onUpdated={async (data) => mutate(data)}
/>
}
/>
<Route path={tabs.members} element={<Members organization={data} />} />
</Routes>
<Outlet
context={
{
data,
isDeleting,
onUpdated: async (data) => mutate(data),
} satisfies OrganizationDetailsOutletContext
}
/>
</>
)}
</DetailsPage>
Expand Down
16 changes: 16 additions & 0 deletions packages/console/src/pages/OrganizationDetails/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type Organization } from '@logto/schemas';

export type OrganizationDetailsOutletContext = {
data: Organization;
/**
* Whether the organization is being deleted, this is used to disable the unsaved
* changes alert modal.
*/
isDeleting: boolean;
onUpdated: (data: Organization) => void;
};

export enum OrganizationDetailsTabs {
Settings = 'settings',
Members = 'members',
}
Loading