Skip to content

Commit

Permalink
feat(shared): Introduce userMemberships in useOrganizationList
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Aug 16, 2023
1 parent c78d028 commit 723ec14
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { useCoreOrganizationList } from '../../contexts';

export const organizationListParams = {
userMemberships: {
infinite: true,
},
userInvitations: {
infinite: true,
},
Expand Down
68 changes: 67 additions & 1 deletion packages/shared/src/hooks/useOrganizationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
ClerkPaginatedResponse,
CreateOrganizationParams,
GetUserOrganizationInvitationsParams,
GetUserOrganizationMembershipParams,
GetUserOrganizationSuggestionsParams,
OrganizationMembershipResource,
OrganizationResource,
Expand All @@ -15,6 +16,12 @@ import type { PaginatedResources, PaginatedResourcesWithDefault } from './types'
import { usePagesOrInfinite, useWithSafeValues } from './usePagesOrInfinite';

type UseOrganizationListParams = {
userMemberships?:
| true
| (GetUserOrganizationMembershipParams & {
infinite?: boolean;
keepPreviousData?: boolean;
});
userInvitations?:
| true
| (GetUserOrganizationInvitationsParams & {
Expand All @@ -34,25 +41,40 @@ type OrganizationList = ReturnType<typeof createOrganizationList>;
type UseOrganizationListReturn =
| {
isLoaded: false;
/**
* @deprecated Use userMemberships instead
*/
organizationList: undefined;
createOrganization: undefined;
setActive: undefined;
userMemberships: PaginatedResourcesWithDefault<OrganizationMembershipResource>;
userInvitations: PaginatedResourcesWithDefault<UserOrganizationInvitationResource>;
userSuggestions: PaginatedResourcesWithDefault<OrganizationSuggestionResource>;
}
| {
isLoaded: boolean;
/**
* @deprecated Use userMemberships instead
*/
organizationList: OrganizationList;
createOrganization: (params: CreateOrganizationParams) => Promise<OrganizationResource>;
setActive: SetActive;
userMemberships: PaginatedResources<OrganizationMembershipResource>;
userInvitations: PaginatedResources<UserOrganizationInvitationResource>;
userSuggestions: PaginatedResources<OrganizationSuggestionResource>;
};

type UseOrganizationList = (params?: UseOrganizationListParams) => UseOrganizationListReturn;

export const useOrganizationList: UseOrganizationList = params => {
const { userInvitations, userSuggestions } = params || {};
const { userMemberships, userInvitations, userSuggestions } = params || {};

const userMembershipsSafeValues = useWithSafeValues(userMemberships, {
initialPage: 1,
pageSize: 10,
keepPreviousData: false,
infinite: false,
});

const userInvitationsSafeValues = useWithSafeValues(userInvitations, {
initialPage: 1,
Expand All @@ -73,6 +95,14 @@ export const useOrganizationList: UseOrganizationList = params => {
const clerk = useClerkInstanceContext();
const user = useUserContext();

const userMembershipsParams =
typeof userMemberships === 'undefined'
? undefined
: {
initialPage: userMembershipsSafeValues.initialPage,
pageSize: userMembershipsSafeValues.pageSize,
};

const userInvitationsParams =
typeof userInvitations === 'undefined'
? undefined
Expand All @@ -93,6 +123,26 @@ export const useOrganizationList: UseOrganizationList = params => {

const isClerkLoaded = !!(clerk.loaded && user);

const memberships = usePagesOrInfinite<
GetUserOrganizationMembershipParams,
ClerkPaginatedResponse<OrganizationMembershipResource>
>(
{
...userMembershipsParams,
paginated: true,
} as any,
user?.getOrganizationMemberships as unknown as any,
{
keepPreviousData: userMembershipsSafeValues.keepPreviousData,
infinite: userMembershipsSafeValues.infinite,
enabled: !!userMembershipsParams,
},
{
type: 'userMemberships',
userId: user?.id,
},
);

const invitations = usePagesOrInfinite<
GetUserOrganizationInvitationsParams,
ClerkPaginatedResponse<UserOrganizationInvitationResource>
Expand Down Expand Up @@ -138,6 +188,21 @@ export const useOrganizationList: UseOrganizationList = params => {
organizationList: undefined,
createOrganization: undefined,
setActive: undefined,
userMemberships: {
data: undefined,
count: undefined,
isLoading: false,
isFetching: false,
isError: false,
page: undefined,
pageCount: undefined,
fetchPage: undefined,
fetchNext: undefined,
fetchPrevious: undefined,
hasNextPage: false,
hasPreviousPage: false,
unstable__mutate: undefined,
},
userInvitations: {
data: undefined,
count: undefined,
Expand Down Expand Up @@ -176,6 +241,7 @@ export const useOrganizationList: UseOrganizationList = params => {
organizationList: createOrganizationList(user.organizationMemberships),
setActive: clerk.setActive,
createOrganization: clerk.createOrganization,
userMemberships: memberships,
userInvitations: invitations,
userSuggestions: suggestions,
};
Expand Down

0 comments on commit 723ec14

Please sign in to comment.