Skip to content

Commit

Permalink
fix(insights): Fix ApiRequest.subscription() (#25762)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Twixes and github-actions[bot] authored Oct 23, 2024
1 parent a42c2ae commit 1b57c13
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ class ApiRequest {
}

// Recordings
public recording(recordingId: SessionRecordingType['id'], teamId?: TeamType['id']): ApiRequest {
return this.environmentsDetail(teamId).addPathComponent('session_recordings').addPathComponent(recordingId)
}
public recordings(teamId?: TeamType['id']): ApiRequest {
return this.environmentsDetail(teamId).addPathComponent('session_recordings')
}
public recording(recordingId: SessionRecordingType['id'], teamId?: TeamType['id']): ApiRequest {
return this.recordings(teamId).addPathComponent(recordingId)
}
public recordingMatchingEvents(teamId?: TeamType['id']): ApiRequest {
return this.environmentsDetail(teamId)
.addPathComponent('session_recordings')
Expand Down Expand Up @@ -748,7 +748,7 @@ class ApiRequest {
}

public subscription(id: SubscriptionType['id'], teamId?: TeamType['id']): ApiRequest {
return this.environmentsDetail(teamId).addPathComponent(id)
return this.subscriptions(teamId).addPathComponent(id)
}

// # Integrations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export const fixtureSubscriptionResponse = (id: number, args: Partial<Subscripti
describe('subscriptionLogic', () => {
let newLogic: ReturnType<typeof subscriptionLogic.build>
let existingLogic: ReturnType<typeof subscriptionLogic.build>
let subscriptions: SubscriptionType[] = []
beforeEach(async () => {
subscriptions = [fixtureSubscriptionResponse(1), fixtureSubscriptionResponse(2)]
useMocks({
get: {
'/api/projects/:team/subscriptions/1': fixtureSubscriptionResponse(1),
'/api/environments/:team/subscriptions': { count: 1, results: [fixtureSubscriptionResponse(1)] },
'/api/environments/:team/subscriptions/1': fixtureSubscriptionResponse(1),
'/api/projects/:team/integrations': { count: 0, results: [] },
},
})
Expand All @@ -42,12 +41,28 @@ describe('subscriptionLogic', () => {
})
existingLogic = subscriptionLogic({
insightShortId: Insight1,
id: subscriptions[0].id,
id: 1,
})
newLogic.mount()
existingLogic.mount()
})

it('loads existing subscription', async () => {
router.actions.push('/insights/123/subscriptions/1')
await expectLogic(existingLogic).toFinishListeners().toDispatchActions(['loadSubscriptionSuccess'])
expect(existingLogic.values.subscription).toMatchObject({
id: 1,
title: 'My example subscription',
target_type: 'email',
target_value: '[email protected],[email protected]',
frequency: 'monthly',
interval: 2,
start_date: '2022-01-01T00:09:00',
byweekday: ['wednesday'],
bysetpos: 1,
})
})

it('updates values depending on frequency', async () => {
router.actions.push('/insights/123/subscriptions/new')
await expectLogic(newLogic).toFinishListeners()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connect, kea, key, listeners, path, props } from 'kea'
import { kea, key, listeners, path, props } from 'kea'
import { forms } from 'kea-forms'
import { loaders } from 'kea-loaders'
import { beforeUnload, router, urlToAction } from 'kea-router'
Expand Down Expand Up @@ -30,9 +30,6 @@ export const subscriptionLogic = kea<subscriptionLogicType>([
path(['lib', 'components', 'Subscriptions', 'subscriptionLogic']),
props({} as SubscriptionsLogicProps),
key(({ id, insightShortId, dashboardId }) => `${insightShortId || dashboardId}-${id ?? 'new'}`),
connect(({ insightShortId, dashboardId }: SubscriptionsLogicProps) => ({
actions: [subscriptionsLogic({ insightShortId, dashboardId }), ['loadSubscriptions']],
})),

loaders(({ props }) => ({
subscription: {
Expand Down Expand Up @@ -97,7 +94,9 @@ export const subscriptionLogic = kea<subscriptionLogicType>([
router.actions.replace(urlForSubscription(updatedSub.id, props))
}

actions.loadSubscriptions()
// If a subscriptionsLogic for this insight/dashboard is mounted already, let's make sure
// this change is propagated to `subscriptions` there
subscriptionsLogic.findMounted(props)?.actions.loadSubscriptions()
actions.loadSubscriptionSuccess(updatedSub)
lemonToast.success(`Subscription saved.`)

Expand Down

0 comments on commit 1b57c13

Please sign in to comment.