Skip to content

Commit

Permalink
feat(clerk-js): Add useOrganization hook
Browse files Browse the repository at this point in the history
  • Loading branch information
igneel64 committed Mar 1, 2022
1 parent af010ba commit 480c422
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/resources/Organization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('Organization', () => {
object: 'organization',
id: 'test_id',
name: 'test_name',
role: 'basic_member',
created_at: 12345,
updated_at: 5678,
created_by: 'test_user_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe('OrganizationInvitation', () => {
object: 'organization_invitation',
email_address: 'test_email',
id: 'test_id',
organization_id: 'test_organization_id',
role: 'basic_member',
created_at: 12345,
updated_at: 5678,
status: 'pending',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ describe('OrganizationMembership', () => {
const organizationMemberShip = new OrganizationMembership({
object: 'organization_membership',
id: 'test_id',
user_id: 'test_user_id',
organization_id: 'test_organization_id',
created_at: 12345,
updated_at: 5678,
role: 'admin',
public_user_data: {
object: 'public_user_data',
first_name: 'test_first_name',
last_name: 'test_last_name',
profile_image_url: 'test_url',
identifier: 'test@identifier.gr',
id: 'test_user_id',
},
});

expect(organizationMemberShip).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Organization {
"name": "test_name",
"pathRoot": "",
"removeMember": [Function],
"role": "basic_member",
"updateMember": [Function],
"updatedAt": 1970-01-01T00:00:05.678Z,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ OrganizationInvitation {
"createdAt": 1970-01-01T00:00:12.345Z,
"emailAddress": "test_email",
"id": "test_id",
"organizationId": "test_organization_id",
"pathRoot": "",
"revoke": [Function],
"role": "basic_member",
"status": "pending",
"updatedAt": 1970-01-01T00:00:05.678Z,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ exports[`OrganizationMembership has the same initial properties 1`] = `
OrganizationMembership {
"createdAt": 1970-01-01T00:00:12.345Z,
"id": "test_id",
"organizationId": "test_organization_id",
"publicUserData": Object {
"firstName": "test_first_name",
"identifier": "test@identifier.gr",
"lastName": "test_last_name",
"profileImageUrl": "test_url",
"userId": undefined,
},
"role": "admin",
"updatedAt": 1970-01-01T00:00:05.678Z,
"userId": "test_user_id",
}
`;
2 changes: 2 additions & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useOrganizations';
export * from './useMagicLink';
File renamed without changes.
22 changes: 22 additions & 0 deletions packages/react/src/hooks/useOrganizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CreateOrganizationParams, OrganizationResource } from '@clerk/types';
import { useContext } from 'react';

import { useClerk } from '../contexts';
import { assertWrappedByClerkProvider } from '../contexts/assertHelpers';
import { StructureContext } from '../contexts/StructureContext';

type UseOrganizations = {
createOrganization: (
params: CreateOrganizationParams,
) => Promise<OrganizationResource>;
};

export function useOrganizations(): UseOrganizations {
const structureCtx = useContext(StructureContext);
assertWrappedByClerkProvider(structureCtx);
const clerk = useClerk();

return {
createOrganization: clerk.createOrganization,
};
}
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './contexts';
export * from './components';
export type { ClerkProp } from './types';
export { isMagicLinkError, MagicLinkErrorCode } from './errors';
export { useMagicLink } from './useMagicLink';
export * from './hooks';
13 changes: 13 additions & 0 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type {
ActiveSessionResource,
AuthenticateWithMetamaskParams,
ClientResource,
CreateOrganizationParams,
HandleMagicLinkVerificationParams,
HandleOAuthCallbackParams,
OrganizationResource,
RedirectOptions,
Resources,
SessionResource,
Expand Down Expand Up @@ -437,6 +439,17 @@ export default class IsomorphicClerk {
}
};

createOrganization = async (
params: CreateOrganizationParams,
): Promise<OrganizationResource | void> => {
const callback = () => this.clerkjs?.createOrganization(params);
if (this.clerkjs && this._loaded) {
return callback() as Promise<OrganizationResource>;
} else {
this.premountMethodCalls.set('createOrganization', callback);
}
};

signOut = async (signOutCallback?: SignOutCallback): Promise<void> => {
const callback = () => this.clerkjs?.signOut(signOutCallback);
if (this.clerkjs && this._loaded) {
Expand Down

0 comments on commit 480c422

Please sign in to comment.