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

feat: migrates webhooks to RTK #5087

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions frontend/common/providers/useWebhooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
useCreateWebhookMutation,
useDeleteWebhookMutation,
useGetWebhooksQuery,
useUpdateWebhookMutation,
} from 'common/services/useWebhook'

export const useWebhooks = (environmentId: string) => {
const { data: webhooks, isLoading } = useGetWebhooksQuery(
{ environmentId },
{ refetchOnFocus: false, skip: !environmentId },
)
const [createWebhook] = useCreateWebhookMutation()
const [updateWebhook] = useUpdateWebhookMutation()
const [deleteWebhook] = useDeleteWebhookMutation()

return {
createWebhook,
deleteWebhook,
isLoading,
updateWebhook,
webhooks,
}
}
59 changes: 59 additions & 0 deletions frontend/common/services/useWebhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { service } from 'common/service'
import { Res } from 'common/types/responses'
import { Req } from 'common/types/requests'

export const webhookService = service
.enhanceEndpoints({ addTagTypes: ['Webhooks'] })
.injectEndpoints({
endpoints: (builder) => ({
createWebhook: builder.mutation<Res['webhook'], Req['createWebhook']>({
invalidatesTags: [{ id: 'LIST', type: 'Webhooks' }],
query: ({ environmentId, ...rest }) => ({
body: {
...rest,
},
method: 'POST',
url: `environments/${environmentId}/webhooks/`,
}),
}),

deleteWebhook: builder.mutation<void, Req['deleteWebhook']>({
invalidatesTags: [{ id: 'LIST', type: 'Webhooks' }],
query: (query) => ({
method: 'DELETE',
url: `environments/${query.environmentId}/webhooks/${query.id}/`,
}),
}),

getWebhooks: builder.query<Res['webhooks'], Req['getWebhooks']>({
providesTags: [{ id: 'LIST', type: 'Webhooks' }],
query: (query) => ({
url: `environments/${query.environmentId}/webhooks/`,
}),
}),

updateWebhook: builder.mutation<Res['webhook'], Req['updateWebhook']>({
invalidatesTags: [{ id: 'LIST', type: 'Webhooks' }],
query: ({ environmentId, ...rest }) => ({
body: {
...rest,
},
method: 'PUT',
url: `environments/${environmentId}/webhooks/${rest.id}/`,
}),
}),
}),
})

export const {
useCreateWebhookMutation,
useDeleteWebhookMutation,
useGetWebhooksQuery,
useUpdateWebhookMutation,
} = webhookService

/* Usage examples:
const { data, isLoading } = useGetWebhooksQuery({ environmentId: 1 }) //get hook
const [createWebhook, { isLoading, data, isSuccess }] = useCreateWebhookMutation() //create hook
webhookService.endpoints.getWebhooks.select({id: 2})(store.getState()) //access data from any function
*/
20 changes: 20 additions & 0 deletions frontend/common/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ export type Req = {
| 'previous_billing_period'
| '90_day_period'
}
getWebhooks: {
environmentId: string
}
createWebhook: {
environmentId: string
enabled: boolean
secret: string
url: string
}
updateWebhook: {
id: number
environmentId: string
enabled: boolean
secret: string
url: string
}
deleteWebhook: {
id: number
environmentId: string
}
deleteIdentity: {
id: string
environmentId: string
Expand Down
11 changes: 11 additions & 0 deletions frontend/common/types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ export type HealthProvider = {
webhook_url: number
}

export type Webhook = {
id: number
url: string
secret: string
enabled: boolean
created_at: string
updated_at: string
}

export type Res = {
segments: PagedResponse<Segment>
segment: Segment
Expand All @@ -663,6 +672,8 @@ export type Res = {
projects: ProjectSummary[]
project: Project
environments: PagedResponse<Environment>
webhook: Webhook
webhooks: Webhook[]
organisationUsage: {
totals: {
flags: number
Expand Down
Loading