Skip to content

Commit

Permalink
chore(clerk-js): Eliminate the usage of memberhipList (#1744)
Browse files Browse the repository at this point in the history
* chore(shared): Update jsdocs for useOrganizations

* chore(clerk-js): Eliminate the usage of memberhipList

* chore(shared,clerk-js): Add changeset

* chore(shared): Update jsdocs with code formatting
  • Loading branch information
panteliselef authored Sep 19, 2023
1 parent 01b024c commit 906d1d2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .changeset/sharp-ligers-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import type { MembershipRole, OrganizationMembershipResource } from '@clerk/type

import { useCoreOrganization, useCoreUser } from '../../contexts';
import { Badge, localizationKeys, Td, Text } from '../../customizables';
import { ThreeDotsMenu, useCardState, usePagination, UserPreview } from '../../elements';
import { ThreeDotsMenu, useCardState, UserPreview } from '../../elements';
import { handleError, roleLocalizationKey } from '../../utils';
import { DataTable, RoleSelect, RowContainer } from './MemberListTable';

const ITEMS_PER_PAGE = 10;

export const ActiveMembersList = () => {
const card = useCardState();
const { page, changePage } = usePagination({ defaultPage: 1 });
const {
organization,
membershipList,
membership: currentUserMembership,
memberships: adminMembers,
memberships,
...rest
} = useCoreOrganization({
membershipList: { offset: (page - 1) * ITEMS_PER_PAGE, limit: ITEMS_PER_PAGE },
memberships: true,
});

const { memberships: adminMembers } = useCoreOrganization({
memberships: {
role: ['admin'],
},
});

const isAdmin = currentUserMembership?.role === 'admin';

const mutateSwrState = () => {
Expand Down Expand Up @@ -64,19 +64,19 @@ export const ActiveMembersList = () => {

return (
<DataTable
page={page}
onPageChange={changePage}
itemCount={organization.membersCount}
itemsPerPage={ITEMS_PER_PAGE}
isLoading={!membershipList}
page={memberships?.page || 1}
onPageChange={n => memberships?.fetchPage?.(n)}
itemCount={memberships?.count || 0}
pageCount={memberships?.pageCount || 0}
isLoading={memberships?.isLoading}
emptyStateLocalizationKey={localizationKeys('organizationProfile.membersPage.detailsTitle__emptyRow')}
headers={[
localizationKeys('organizationProfile.membersPage.activeMembersTab.tableHeader__user'),
localizationKeys('organizationProfile.membersPage.activeMembersTab.tableHeader__joined'),
localizationKeys('organizationProfile.membersPage.activeMembersTab.tableHeader__role'),
localizationKeys('organizationProfile.membersPage.activeMembersTab.tableHeader__actions'),
]}
rows={(membershipList || []).map(m => (
rows={(memberships?.data || []).map(m => (
<MemberRow
key={m.id}
membership={m}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,32 @@ type MembersListTableProps = {
page: number;
onPageChange: (page: number) => void;
itemCount: number;
itemsPerPage: number;
emptyStateLocalizationKey: LocalizationKey;
};
} & (
| {
itemsPerPage?: never;
pageCount: number;
}
| {
itemsPerPage: number;
pageCount?: never;
}
);

export const DataTable = (props: MembersListTableProps) => {
const { headers, page, onPageChange, rows, isLoading, itemCount, itemsPerPage, emptyStateLocalizationKey } = props;
const {
headers,
page,
onPageChange,
rows,
isLoading,
itemCount,
itemsPerPage,
pageCount: pageCountProp,
emptyStateLocalizationKey,
} = props;

const pageCount = rows.length !== 0 ? Math.ceil(itemCount / itemsPerPage) : 1;
const pageCount = rows.length !== 0 ? pageCountProp ?? Math.ceil(itemCount / itemsPerPage) : 1;
const startRowIndex = (page - 1) * rows.length;
const endRowIndex = Math.min(page * rows.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ const OrganizationDangerSection = () => {
const {
organization,
membership,
membershipList: adminMembers,
memberships: adminMembers,
} = useCoreOrganization({
membershipList: { role: ['admin'] },
memberships: {
role: ['admin'],
},
});
const { navigate } = useRouter();

Expand All @@ -106,7 +108,7 @@ const OrganizationDangerSection = () => {
}

const adminDeleteEnabled = organization.adminDeleteEnabled;
const hasMoreThanOneAdmin = (adminMembers?.length || 0) > 1;
const hasMoreThanOneAdmin = (adminMembers?.count || 0) > 1;
const isAdmin = membership.role === 'admin';

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,17 @@ describe('OrganizationMembers', () => {

fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(
Promise.resolve({
data: [],
data: membersList,
total_count: 2,
}),
);

fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(Promise.resolve(membersList));
fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(
Promise.resolve({
data: [],
total_count: 2,
}),
);

const { queryByText, queryAllByRole } = render(<OrganizationMembers />, { wrapper });

Expand Down Expand Up @@ -183,15 +188,17 @@ describe('OrganizationMembers', () => {
});
});

fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(
Promise.resolve({ data: membersList, total_count: 0 }),
);

fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(
Promise.resolve({
data: [],
total_count: 0,
}),
);

fixtures.clerk.organization?.getMemberships.mockReturnValueOnce(Promise.resolve(membersList));

const { queryByRole } = render(<OrganizationMembers />, { wrapper });

await waitFor(() => {
Expand Down Expand Up @@ -327,7 +334,12 @@ describe('OrganizationMembers', () => {
});
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve(membersList));
fixtures.clerk.organization?.getMemberships.mockReturnValue(
Promise.resolve({
data: membersList,
total_count: 2,
}),
);
const { findByText } = render(<OrganizationMembers />, { wrapper });
await waitFor(() => expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled());
expect(await findByText('You')).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('OrganizationSettings', () => {
f.withUser({ email_addresses: ['[email protected]'], organization_memberships: [{ name: 'Org1', role: 'admin' }] });
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve(adminsList));
fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve({ data: adminsList, total_count: 1 }));
fixtures.clerk.organization?.getDomains.mockReturnValue(
Promise.resolve({
data: domainList,
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('OrganizationSettings', () => {
f.withUser({ email_addresses: ['[email protected]'], organization_memberships: [{ name: 'Org1', role: 'admin' }] });
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve(adminsList));
fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve({ data: adminsList, total_count: 2 }));
fixtures.clerk.organization?.getDomains.mockReturnValue(
Promise.resolve({
data: domainList,
Expand Down Expand Up @@ -129,7 +129,9 @@ describe('OrganizationSettings', () => {
});
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve(adminsList));
fixtures.clerk.organization?.getMemberships.mockReturnValue(
Promise.resolve({ data: adminsList, total_count: 2 }),
);
const { getByText } = render(<OrganizationSettings />, { wrapper });
await waitFor(() => {
expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
Expand Down
8 changes: 4 additions & 4 deletions packages/shared/src/hooks/useOrganization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';
type UseOrganizationParams = {
invitationList?: GetPendingInvitationsParams;
/**
* @deprecated Use members instead
* @deprecated Use `memberships` instead
*/
membershipList?: GetMembershipsParams;
domains?:
Expand Down Expand Up @@ -52,7 +52,7 @@ type UseOrganizationReturn =
organization: undefined;
invitationList: undefined;
/**
* @deprecated Use members instead
* @deprecated Use `memberships` instead
*/
membershipList: undefined;
membership: undefined;
Expand All @@ -65,7 +65,7 @@ type UseOrganizationReturn =
organization: OrganizationResource;
invitationList: undefined;
/**
* @deprecated Use members instead
* @deprecated Use `memberships` instead
*/
membershipList: undefined;
membership: undefined;
Expand All @@ -78,7 +78,7 @@ type UseOrganizationReturn =
organization: OrganizationResource | null;
invitationList: OrganizationInvitationResource[] | null | undefined;
/**
* @deprecated Use members instead
* @deprecated Use `memberships` instead
*/
membershipList: OrganizationMembershipResource[] | null | undefined;
membership: OrganizationMembershipResource | null | undefined;
Expand Down
16 changes: 8 additions & 8 deletions packages/shared/src/hooks/useOrganizations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ type UseOrganizationsReturn =

/**
* @deprecated Use `createOrganization` from `useOrganizationList`
* Example const {createOrganization} = useOrganizationList()
* Example: `const {createOrganization} = useOrganizationList()`
*/
createOrganization: undefined;

/**
* @deprecated Use `membershipList` from `useOrganization`
* Example const {membershipList} = useOrganization()
* @deprecated Use `memberships` from `useOrganization`
* Example: `const {memberships} = useOrganization()`
*/
getOrganizationMemberships: undefined;

/**
* @deprecated Use `getOrganization` from `useClerk`
* Example const {getOrganization} = useClerk()
* Example: `const {getOrganization} = useClerk()`
*/
getOrganization: undefined;
}
| {
isLoaded: true;
/**
* @deprecated Use `createOrganization` from `useOrganizationList`
* Example const {createOrganization} = useOrganizationList()
* Example: `const {createOrganization} = useOrganizationList()`
*/
createOrganization: (params: CreateOrganizationParams) => Promise<OrganizationResource>;

/**
* @deprecated Use `membershipList` from `useOrganization`
* Example const {membershipList} = useOrganization()
* @deprecated Use `memberships` from `useOrganization`
* Example: `const {memberships} = useOrganization()`
*/
getOrganizationMemberships: () => Promise<OrganizationMembershipResource[]>;

/**
* @deprecated Use `getOrganization` from `useClerk`
* Example const {getOrganization} = useClerk()
* Example: `const {getOrganization} = useClerk()`
*/
getOrganization: (organizationId: string) => Promise<OrganizationResource | undefined>;
};
Expand Down

0 comments on commit 906d1d2

Please sign in to comment.