-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types,clerk-js): Introduce OrganizationSuggestion resource and t…
…ypes We introduce a new OrganizationSuggestion resource and the corresponding types, in order to preview user with suggestions of organizations they can join. The available methods for the time being are retrieve() which return a list of organization suggestions of the user and accept() which allows a user to request to join the organization. Also make available the user's suggestions from the useOrganizationList hook
- Loading branch information
1 parent
8d1e7d7
commit a412a50
Showing
11 changed files
with
247 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
Introduce a new resource called OrganizationSuggestion along with retrieve() & accept() methods | ||
Also make available the user's suggestions from the useOrganizationList hook |
22 changes: 22 additions & 0 deletions
22
packages/clerk-js/src/core/resources/OrganizationSuggestion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { OrganizationSuggestion } from './internal'; | ||
|
||
describe('OrganizationSuggestion', () => { | ||
it('has the same initial properties', () => { | ||
const organizationSuggestion = new OrganizationSuggestion({ | ||
object: 'organization_suggestion', | ||
id: 'test_id', | ||
public_organization_data: { | ||
id: 'test_org_id', | ||
name: 'Test org', | ||
slug: 'test-org', | ||
image_url: 'test_image_url', | ||
has_image: true, | ||
}, | ||
status: 'pending', | ||
created_at: 12345, | ||
updated_at: 5678, | ||
}); | ||
|
||
expect(organizationSuggestion).toMatchSnapshot(); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
packages/clerk-js/src/core/resources/OrganizationSuggestion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { | ||
ClerkPaginatedResponse, | ||
GetUserOrganizationSuggestionsParams, | ||
OrganizationSuggestionJSON, | ||
OrganizationSuggestionResource, | ||
OrganizationSuggestionStatus, | ||
UserOrganizationInvitationResource, | ||
} from '@clerk/types'; | ||
|
||
import { unixEpochToDate } from '../../utils/date'; | ||
import { convertPageToOffset } from '../../utils/pagesToOffset'; | ||
import { BaseResource } from './Base'; | ||
|
||
export class OrganizationSuggestion extends BaseResource implements OrganizationSuggestionResource { | ||
id!: string; | ||
publicOrganizationData!: UserOrganizationInvitationResource['publicOrganizationData']; | ||
status!: OrganizationSuggestionStatus; | ||
createdAt!: Date; | ||
updatedAt!: Date; | ||
|
||
constructor(data: OrganizationSuggestionJSON) { | ||
super(); | ||
this.fromJSON(data); | ||
} | ||
|
||
static async retrieve( | ||
params?: GetUserOrganizationSuggestionsParams, | ||
): Promise<ClerkPaginatedResponse<OrganizationSuggestion>> { | ||
return await BaseResource._fetch({ | ||
path: '/me/organization_suggestions', | ||
method: 'GET', | ||
search: convertPageToOffset(params) as any, | ||
}) | ||
.then(res => { | ||
const { data: suggestions, total_count } = | ||
res?.response as unknown as ClerkPaginatedResponse<OrganizationSuggestionJSON>; | ||
|
||
return { | ||
total_count, | ||
data: suggestions.map(suggestion => new OrganizationSuggestion(suggestion)), | ||
}; | ||
}) | ||
.catch(() => ({ | ||
total_count: 0, | ||
data: [], | ||
})); | ||
} | ||
|
||
accept = async (): Promise<OrganizationSuggestionResource> => { | ||
return await this._basePost({ | ||
path: `/me/organization_suggestions/${this.id}/accept`, | ||
}); | ||
}; | ||
|
||
protected fromJSON(data: OrganizationSuggestionJSON | null): this { | ||
if (data) { | ||
this.id = data.id; | ||
this.status = data.status; | ||
this.publicOrganizationData = { | ||
hasImage: data.public_organization_data.has_image, | ||
imageUrl: data.public_organization_data.image_url, | ||
name: data.public_organization_data.name, | ||
id: data.public_organization_data.id, | ||
slug: data.public_organization_data.slug, | ||
}; | ||
this.createdAt = unixEpochToDate(data.created_at); | ||
this.updatedAt = unixEpochToDate(data.updated_at); | ||
} | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/clerk-js/src/core/resources/__snapshots__/OrganizationSuggestion.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`OrganizationSuggestion has the same initial properties 1`] = ` | ||
OrganizationSuggestion { | ||
"accept": [Function], | ||
"createdAt": 1970-01-01T00:00:12.345Z, | ||
"id": "test_id", | ||
"pathRoot": "", | ||
"publicOrganizationData": { | ||
"hasImage": true, | ||
"id": "test_org_id", | ||
"imageUrl": "test_image_url", | ||
"name": "Test org", | ||
"slug": "test-org", | ||
}, | ||
"status": "pending", | ||
"updatedAt": 1970-01-01T00:00:05.678Z, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.